← Back to index

lib/BDD/Behave/SpecRegistry.rakumod

144 / 148 lines covered (97.3%)

1unit module BDD::Behave::SpecRegistry;23need BDD::Behave::SpecTree;45sub suite-type() { ::('BDD::Behave::SpecTree::Suite') }6sub example-group-type() { ::('BDD::Behave::SpecTree::ExampleGroup') }7sub example-type() { ::('BDD::Behave::SpecTree::Example') }89class Entry {10  has $.suite;11  has @.stack;12  has %!occurrences;13  has %!first-nodes;14  has Bool $.loaded is rw = False;1516  # Groups and examples share the count, because a location selects an example17  # through any group above it as well as directly.18  method next-occurrence(Int $line --> Int) {19    ++%!occurrences{$line};20  }2122  method record-node($node) {23    if $node.occurrence == 1 {24      %!first-nodes{$node.line} = $node;25    } elsif !$!loaded {26      %!first-nodes{$node.line}.shares-line = True;27    }28  }29}3031our class ExampleQueryResult {32  has Str      $.description         is required;33  has Str      $.full-description    is required;34  has IO::Path $.file                is required;35  has Int      $.line                is required;36  has IO::Path $.suite-file;37  has Str      $.suite-description;38  has Str      @.group-descriptions;39  has Str      @.tags;40  has %.metadata;41  has Bool     $.pending             = False;42  has Bool     $.focused             = False;43  has Bool     $.skipped             = False;4445  method location(--> Str) { "{$!file}:{$!line}" }4647  method to-hash(--> Hash) {48    %(49      description       => $!description,50      full-description  => $!full-description,51      file              => $!file.Str,52      line              => $!line,53      suite-file        => ($!suite-file.defined ?? $!suite-file.Str !! Str),54      suite-description => ($!suite-description // Str),55      group-descriptions => @!group-descriptions.List,56      tags              => @!tags.List,57      metadata          => %!metadata.Hash,58      pending           => $!pending,59      focused           => $!focused,60      skipped           => $!skipped,61    );62  }63}6465sub matches-pattern(Str $description, Str $pattern --> Bool) {66  if $pattern.chars > 267     && $pattern.starts-with('/')68     && $pattern.ends-with('/') {69    my $body = $pattern.substr(1, $pattern.chars - 2);70    my $rx = / <{ $body }> /;71    return so $description.match($rx);72  }73  $description.contains($pattern);74}7576sub path-matches(IO::Path $node-path, Str $pattern-path --> Bool) {77  return True if $node-path.Str eq $pattern-path;78  return True if $node-path.absolute eq $pattern-path.IO.absolute;79  return True if $node-path.Str.ends-with('/' ~ $pattern-path);80  return True if $node-path.basename eq $pattern-path;81  False;82}8384sub meta-value-equals($actual, $expected --> Bool) {85  return False unless $actual.defined;86  if $expected ~~ Bool {87    return $expected ?? ?$actual !! !$actual;88  }89  if $expected ~~ Positional {90    return False unless $actual ~~ Positional;91    my @a = $actual.list;92    my @e = $expected.list;93    return False unless @a.elems == @e.elems;94    for @e.kv -> $i, $v {95      return False unless @a[$i] eq $v;96    }97    return True;98  }99  if $actual ~~ Positional {100    return $actual.list.first(* eq $expected).defined;101  }102  $actual eq $expected;103}104105class SpecRegistry {106  has %.entries;107  has $.current-entry is rw;108109  method entry-for(IO::Path $file --> Entry) {110    my IO::Path $path = $file.absolute.IO;111    my $key = $path.absolute.Str;112    %.entries{$key} //= Entry.new(113      suite => suite-type().create(:description($path.basename), :file($path)),114      stack => [],115    );116    %.entries{$key};117  }118119  # An id goes out in events once its file has loaded, so a node an example120  # registers while running must not change the id of a node loaded before it.121  method finish-loading(IO::Path $file) {122    self.entry-for($file).loaded = True;123  }124125  method suite-for(IO::Path $file) {126    self.entry-for($file).suite;127  }128129  method register-group(:$description!, :&block!, :$file!, :$line!, :@tags = [], :%extra-meta = %(), :$skipped, :$focused) {130    my IO::Path $path = ($file ~~ IO::Path ?? $file !! $file.IO).absolute.IO;131    my $entry = self.entry-for($path);132    $!current-entry = $entry;133    my $parent = $entry.stack.elems ?? $entry.stack[*-1] !! $entry.suite;134    my $group = example-group-type().new(135      :$description, :file($path), :$line,136      :occurrence($entry.next-occurrence($line.Int)),137    );138    $group.set-metadata(:tags(@tags.list)) if @tags.elems;139    $group.set-metadata(:skipped(True))    if $skipped;140    $group.set-metadata(:focused(True))    if $focused;141    for %extra-meta.kv -> $key, $value {142      $group.set-metadata(|%($key => $value));143    }144    $entry.record-node($group);145    $parent.add-group($group);146    $entry.stack.push($group);147    LEAVE {148      $entry.stack.pop;149      $!current-entry = $entry.stack.elems ?? $entry !! Nil;150    }151    block();152    $group;153  }154155  method register-example(:$description!, :&block!, :$file!, :$line!, :@tags = [], :%extra-meta = %(), :$skipped, :$focused) {156    my IO::Path $path = ($file ~~ IO::Path ?? $file !! $file.IO).absolute.IO;157    my $entry = self.entry-for($path);158    my $parent = $entry.stack.elems ?? $entry.stack[*-1] !! $entry.suite;159    my $example = example-type().new(160      :$description, :file($path), :$line, :block(&block),161      :occurrence($entry.next-occurrence($line.Int)),162    );163164    my @lets;165    @lets.append: $entry.suite.let-definitions.list;166    for $entry.stack.grep(example-group-type()) -> $group {167      @lets.append: $group.let-definitions.list;168    }169    $example.set-metadata(:lets(@lets));170    $example.set-metadata(:tags(@tags.list)) if @tags.elems;171    $example.set-metadata(:skipped(True))    if $skipped;172    $example.set-metadata(:focused(True))    if $focused;173    for %extra-meta.kv -> $key, $value {174      $example.set-metadata(|%($key => $value));175    }176    $entry.record-node($example);177    $parent.add-example($example);178    $example;179  }180181  method current-group-for(IO::Path $file) {182    my IO::Path $path = ($file ~~ IO::Path ?? $file !! $file.IO).absolute.IO;183    my $entry = self.entry-for($path);184    $entry.stack.elems ?? $entry.stack[*-1] !! Nil;185  }186187  method suites() {188    %.entries.values.map(*.suite).List;189  }190191  method suite-for-file(IO::Path $file) {192    my $key = $file.absolute.Str;193    %.entries{$key}:exists ?? %.entries{$key}.suite !! Nil;194  }195196  method clear {197    %.entries = ();198  }199200  method all-examples(--> List) {201    my @results;202    for self.suites.list -> $suite {203      self!collect-examples($suite, $suite, [], @results);204    }205    @results.List;206  }207208  method !collect-examples($node, $suite, @group-descriptions, @results) {209    given $node {210      when example-type() {211        @results.push: self!to-query-result($node, $suite, @group-descriptions);212      }213      when example-group-type() {214        my @next-groups = (|@group-descriptions, $node.description);215        for $node.children.list -> $child {216          self!collect-examples($child, $suite, @next-groups, @results);217        }218      }219      when suite-type() {220        for $node.children.list -> $child {221          self!collect-examples($child, $suite, @group-descriptions, @results);222        }223      }224    }225  }226227  method !to-query-result($example, $suite, @group-descriptions --> ExampleQueryResult) {228    my %metadata = $example.metadata.clone;229    %metadata<lets>:delete;230    my $full-description = (|@group-descriptions, $example.description).join(' ');231232    ExampleQueryResult.new(233      description       => $example.description,234      :$full-description,235      file              => $example.file,236      line              => $example.line,237      suite-file        => $suite.file,238      suite-description => $suite.description,239      group-descriptions => @group-descriptions.List,240      tags              => $example.effective-tags.List,241      :%metadata,242      pending           => $example.pending.so,243      focused           => $example.effective-focused.so,244      skipped           => $example.effective-skipped.so,245    );246  }247248  method query-examples(249    :$description-pattern,250    :$file,251    :$line,252    :@include-tags    = [],253    :@exclude-tags    = [],254    :%metadata        = %(),255    :%metadata-exclude = %(),256    :$pending,257    :$focused,258    :$skipped,259    --> List260  ) {261    my @all = self.all-examples;262    my @kept;263    for @all -> $r {264      if $description-pattern.defined && $description-pattern.chars {265        next unless matches-pattern($r.full-description, $description-pattern);266      }267268      if $file.defined {269        my $pattern-path = $file ~~ IO::Path ?? $file.Str !! $file.Str;270        next unless path-matches($r.file, $pattern-path);271      }272273      if $line.defined {274        next unless $r.line == $line;275      }276277      if @include-tags.elems {278        next unless @include-tags.first({ $r.tags.first(* eq $_).defined }).defined;279      }280281      if @exclude-tags.elems {282        next if @exclude-tags.first({ $r.tags.first(* eq $_).defined }).defined;283      }284285      my $matches-meta = True;286      for %metadata.kv -> $k, $v {287        unless meta-value-equals($r.metadata{$k}, $v) {288          $matches-meta = False;289          last;290        }291      }292      next unless $matches-meta;293294      my $excluded = False;295      for %metadata-exclude.kv -> $k, $v {296        if meta-value-equals($r.metadata{$k}, $v) {297          $excluded = True;298          last;299        }300      }301      next if $excluded;302303      if $pending.defined {304        next unless ?$r.pending == ?$pending;305      }306307      if $focused.defined {308        next unless ?$r.focused == ?$focused;309      }310311      if $skipped.defined {312        next unless ?$r.skipped == ?$skipped;313      }314315      @kept.push: $r;316    }317    @kept.List;318  }319320  method count-examples(|args --> Int) {321    self.query-examples(|args).elems;322  }323}324325my SpecRegistry $REGISTRY .= new;326327our sub registry() is export(:DEFAULT) {328  $REGISTRY;329}330331our sub suites() is export(:DEFAULT) {332  $REGISTRY.suites;333}