← Back to index

lib/BDD/Behave/SpecTree/Core.rakumod

84 / 88 lines covered (95.5%)

1unit module BDD::Behave::SpecTree::Core;23subset HookPhase of Str where { $_ eq any(<before-all after-all before-each after-each around-all around-each>) };45# Re-parenting any node invalidates every cached ancestry at once. The tree is6# built once and never changes afterwards, so the counter stops moving before7# the first example runs and the caches stay warm for the whole run.8my Int $tree-generation = 0;910our class SpecNode {11  has Str $.description is required;12  has IO::Path $.file is required;13  has Int $.line is required;14  has Int $.occurrence = 1;15  has Bool $.shares-line is rw = False;16  has     $!parent = Nil;17  has %.metadata = Hash.new;18  has List $!ancestry;19  has Int  $!ancestry-generation = -1;2021  submethod BUILD(:$!description, :$file!, :$line!, :$parent, :%metadata = {}, Int :$occurrence = 1, Bool :$shares-line = False) {22    $!file = $file ~~ IO::Path ?? $file !! $file.IO;23    $!line = $line;24    $!occurrence = $occurrence;25    $!shares-line = $shares-line;26    $!parent = $parent if $parent.defined;27    %!metadata = %metadata.clone;28  }2930  method parent() is rw {31    Proxy.new(32      FETCH => -> $         { $!parent },33      STORE => -> $, $value { $!parent = $value; $tree-generation++ },34    );35  }3637  method location { "$!file:$!line" }3839  # Nodes registered in a loop share a line, so each of them carries its40  # occurrence as a suffix. A bare line still selects every node on it.41  method line-key(--> Str) {42    $!occurrence > 1 || $!shares-line ?? self!occurrence-line-key !! $!line.Str;43  }4445  method !occurrence-line-key(--> Str) { $!line ~ "[$!occurrence]" }4647  method node-id(--> Str) { $!file.absolute ~ ':' ~ self.line-key }4849  method matches-line(Str $line-key --> Bool) {50    $line-key eq $!line.Str || $line-key eq self!occurrence-line-key;51  }5253  method root { self.ancestry[0] }5455  # Every effective-* lookup, `depth`, and `root` walks this, and the runner56  # calls four of them per example, so the walk is done once per tree.57  method ancestry(--> List) {58    # `.List` decontainerizes: returning the attribute itself hands back a59    # Scalar holding a List, which `for` then iterates as a single item.60    return $!ancestry.List61      if $!ancestry.defined && $!ancestry-generation == $tree-generation;6263    my @nodes;64    my $node = self;65    while $node.defined {66      @nodes.push($node);67      $node = $node.parent;68    }6970    $!ancestry-generation = $tree-generation;71    $!ancestry = @nodes.reverse.List;7273    $!ancestry.List;74  }7576  method set-metadata(*%pairs) {77    for %pairs.kv -> $key, $value {78      %!metadata{$key} = $value;79    }80    self;81  }8283  method get-metadata(Str:D $key, :$default) {84    %!metadata{$key} // $default;85  }8687  method tags(--> List) {88    my $stored = %!metadata<tags>;89    return ().List unless $stored.defined;90    $stored ~~ Positional ?? $stored.list.List !! ($stored,).List;91  }9293  method has-tag(Str:D $tag --> Bool) {94    self.tags.first(* eq $tag).defined;95  }9697  method effective-tags(--> List) {98    my @collected;99    for self.ancestry -> $node {100      @collected.append: $node.tags;101    }102    @collected.unique.List;103  }104105  method has-effective-tag(Str:D $tag --> Bool) {106    self.effective-tags.first(* eq $tag).defined;107  }108109  method effective-metadata-value(Str:D $key) {110    for self.ancestry.reverse -> $node {111      my %meta := $node.metadata;112      next unless %meta{$key}:exists;113      my $value = %meta{$key};114      return $value if $value.defined;115    }116    Nil;117  }118119  method skipped(--> Bool) { %!metadata<skipped> ?? True !! False }120121  method focused(--> Bool) { %!metadata<focused> ?? True !! False }122123  method effective-skipped(--> Bool) {124    self.ancestry.first(*.skipped).defined;125  }126127  method effective-focused(--> Bool) {128    self.ancestry.first(*.focused).defined;129  }130131  method depth { self.ancestry.elems - 1 }132133  method is-root { !self.parent.defined }134}135136our role Container {137  has SpecNode @.children;138139  method add-child(SpecNode $child --> SpecNode) {140    $child.parent = self;141    @!children.push($child);142    $child;143  }144}145146sub meta-value-matches($actual, $expected --> Bool) {147  return False unless $actual.defined;148  if $expected ~~ Positional {149    my @actual-list = $actual ~~ Positional ?? $actual.list !! ($actual,);150    for $expected.list -> $needle {151      return False unless @actual-list.first(* eq $needle).defined;152    }153    return True;154  }155  if $actual ~~ Positional {156    return $actual.list.first(* eq $expected).defined;157  }158  $actual eq $expected;159}160161our class Hook {162  has Callable $.callback is required;163  has @.include-tags;164  has @.exclude-tags;165  has %.meta;166167  method has-filter(--> Bool) {168    @!include-tags.elems > 0 || @!exclude-tags.elems > 0 || %!meta.elems > 0;169  }170171  method matches(SpecNode:D $node --> Bool) {172    return True unless self.has-filter;173174    my @effective-tags = $node.effective-tags;175176    for @!include-tags -> $tag {177      return False unless @effective-tags.first(* eq $tag).defined;178    }179180    for @!exclude-tags -> $tag {181      return False if @effective-tags.first(* eq $tag).defined;182    }183184    for %!meta.kv -> $key, $expected {185      my $actual = $node.effective-metadata-value($key);186      return False unless meta-value-matches($actual, $expected);187    }188189    True;190  }191}192193sub base-exports() {194  %(195    HookPhase => HookPhase,196    SpecNode => SpecNode,197    Container => Container,198    Hook => Hook,199  );200}201202sub EXPORT(:$ALL?) {203  my %exports = base-exports();204  %(205    DEFAULT => %exports,206    ALL => %exports,207  );208}