strain.t (1931B) - raw
1 #!/usr/bin/env perl 2 use Test2::V0; 3 4 use FindBin qw<$Bin>; 5 use lib $Bin, "$Bin/local/lib/perl5"; 6 7 use Strain qw<keep discard>; 8 9 imported_ok qw<keep discard> or bail_out; 10 11 my ( $input, $expected, $function ); 12 13 $input = []; 14 $expected = []; 15 $function = sub { my $x = shift; $x % 2 == 0 }; 16 is( keep( $input, $function ), $expected, "empty list" ); 17 18 $input = [ 2, 4, 6, 8, 10 ]; 19 $expected = []; 20 $function = sub { my $x = shift; $x % 2 == 1 }; 21 is( keep( $input, $function ), 22 $expected, "keep odd numbers. empty result " ); 23 24 $input = [ 2, 4, 6, 8, 10 ]; 25 $expected = []; 26 $function = sub { my $x = shift; $x % 2 == 0 }; 27 is( discard( $input, $function ), 28 $expected, "discard even numbers. empty result" ); 29 30 $input = [ 2, 4, 6, 8, 10 ]; 31 $expected = [ 2, 4, 6, 8, 10 ]; 32 $function = sub { my $x = shift; $x % 2 == 0 }; 33 is( keep( $input, $function ), 34 $expected, "keep even numbers. result == input" ); 35 36 $input = [qw(dough cash plough though through enough)]; 37 $expected = ['cash']; 38 $function = sub { my $x = shift; $x =~ m/ough$/ }; 39 is( discard( $input, $function ), 40 $expected, "discard input endswith 'ough'" ); 41 42 $input = [qw(zebra arizona apple google mozilla)]; 43 $expected = [qw(zebra arizona mozilla)]; 44 $function = sub { my $x = shift; $x =~ /z/ }; 45 is( keep( $input, $function ), $expected, "keep input with 'z'" ); 46 47 $input = [ '1,2,3', 'one', 'almost!', 'love' ]; 48 $expected = []; 49 $function = sub { my $x = shift; $x =~ /\p{IsAlpha}/ }; 50 is( discard( keep( $input, $function ) // [], $function ), 51 $expected, "keep then discard" ); 52 53 $input = [ '1,2,3', 'one', 'almost!', 'love' ]; 54 $expected = [ '1,2,3', 'one', 'almost!', 'love' ]; 55 $function = sub { my $x = shift; $x =~ /\p{Alpha}/ }; 56 my $combined = [ 57 @{ keep( $input, $function ) // [] }, 58 @{ discard( $input, $function ) // [] } 59 ]; 60 is( 61 [ sort @$combined ], 62 [ sort @$expected ], 63 "combine keep and discard results. keep + discard" 64 ); 65 66 done_testing;