← Back to index

lib/BDD/Behave/SpecTree/Example.rakumod

31 / 32 lines covered (96.9%)

1unit module BDD::Behave::SpecTree::Example;23need BDD::Behave::SpecTree::Core;4need BDD::Behave::LetRuntime;56constant SpecNode = BDD::Behave::SpecTree::Core::SpecNode;7constant LetRuntime = BDD::Behave::LetRuntime::LetRuntime;89class LetContext {10  method FALLBACK(Str $name, |c) {11    my $runtime = $*LET-RUNTIME;1213    if c.elems == 0 && $runtime.defined && $runtime.has-name($name) {14      return $runtime.value($name);15    }1617    my $helpers = $*BEHAVE-HELPERS;1819    if $helpers.defined {20      for $helpers.values -> $helper {21        return $helper."$name"(|c) if $helper.^can($name);22      }23    }2425    if c.elems == 0 {26      return $runtime.value($name);27    }2829    die "Let values are read-only";30  }31}3233our class Example is SpecNode {34  has Callable $.block is required;35  has Bool $.pending is rw = False;36  has Real $.duration is rw;37  has Instant $.started-at is rw;38  has Instant $.finished-at is rw;39  has @.benchmarks;40  has Bool $!takes-context;4142  method execute(*%context) {43    my $*BEHAVE-CURRENT-EXAMPLE = self;44    my $*BEHAVE-BENCHMARK-COUNTER = 0;4546    my $existing;47    try { $existing = $*LET-RUNTIME if $*LET-RUNTIME.defined }4849    if $existing.defined {50      self!run-block(|%context);51    } else {52      my @lets = self.get-metadata('lets', :default([])).flat.List;53      my $runtime = LetRuntime.new(:definitions(@lets));54      my $*LET-RUNTIME = $runtime;55      self!run-block(|%context);56    }57  }5859  # The answer cannot change for a given block, and the block runs again for60  # every `--retry` attempt and every benchmark iteration.61  method !takes-context(--> Bool) {62    return $!takes-context if $!takes-context.defined;6364    my $params = $!block.signature.params;6566    $!takes-context = $params.elems > 0 && !$params[0].named;67  }6869  method !run-block(*%context) {70    if self!takes-context {71      $!block(LetContext.new, |%context);72    } else {73      $!block(|%context);74    }75  }7677  method mark-pending(:$reason) {78    $!pending = True;79    self.set-metadata(:pending-reason($reason // 'pending'));80    self;81  }82}