33 / 35 lines covered (94.3%)
1unit module BDD::Behave::Mock::ArgMatcher;23use BDD::Behave::Callsite;45our sub user-callframe() is export {6 my $i = 1;7 loop {8 my $cf = callframe($i++);9 last without $cf;10 last if $i > 32;11 my $file = ~($cf.file // '');12 next if $file eq ''13 || $file.contains('Metamodel')14 || $file.contains('NQP::')15 || $file.contains('/nqp')16 || is-behave-source($file);17 return $cf;18 }19 Nil;20}2122class Call is export {23 has Str $.method is required;24 has @.args;25 has %.named;26 has IO::Path $.file;27 has Int $.line;28}2930role ArgMatcher is export {31 method matches(Mu \value --> Bool) { ... }32 method describe(--> Str) { ... }33}3435class AnyArg does ArgMatcher is export {36 method matches(Mu \value --> Bool) { True }37 method describe(--> Str) { 'anything' }38}3940class InstanceOf does ArgMatcher is export {41 has Mu $.type;42 submethod BUILD(Mu :$type is raw) { $!type := $type }43 method matches(Mu \value --> Bool) { value ~~ $!type }44 method describe(--> Str) { "instance-of({$!type.^name})" }45}4647class HashIncluding does ArgMatcher is export {48 has %.expected;49 submethod BUILD(:%expected) { %!expected = %expected }50 method matches(Mu \value --> Bool) {51 return False unless value ~~ Associative;52 for %!expected.kv -> $k, $exp {53 return False unless value{$k}:exists;54 my $actual = value{$k};55 if $exp ~~ ArgMatcher {56 return False unless $exp.matches($actual);57 } else {58 return False unless $actual ~~ $exp;59 }60 }61 True;62 }63 method describe(--> Str) { "hash-including({%!expected.raku})" }64}6566class ArrayIncluding does ArgMatcher is export {67 has @.items;68 submethod BUILD(:@items) { @!items = @items }69 method matches(Mu \value --> Bool) {70 return False unless value ~~ Positional;71 my @actual = value.list;72 for @!items -> $exp {73 my $found;74 if $exp ~~ ArgMatcher {75 $found = @actual.first({ $exp.matches($_) }).defined;76 } else {77 $found = @actual.first({ $_ ~~ $exp }).defined;78 }79 return False unless $found;80 }81 True;82 }83 method describe(--> Str) { "array-including({@!items.raku})" }84}8586our sub anything() is export { AnyArg.new }87our sub instance-of(Mu \type) is export { InstanceOf.new(:type(type)) }88our sub hash-including(*%pairs) is export { HashIncluding.new(:expected(%pairs)) }89our sub array-including(*@items) is export { ArrayIncluding.new(:items(@items)) }