SHARE
TWEET

Test of parse_replacement version 2

hakonhagland Mar 15th, 2015 257 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use feature qw(say);
  2. use strict;
  3. use warnings;
  4.  
  5. use Carp;
  6. use Data::Dump qw(dd dump);
  7. use Scalar::Util 'reftype';
  8.  
  9. # Test cases:
  10. # Format:
  11. #   string,       regex,    replacement, expected
  12. my @test_cases = (
  13.     ['aba',      'a(.*?)a', '$1',      'b'],
  14.     ['ababaxxa', 'a(.*?)a', '$1',      'bbxx'],
  15.     ['acccb',    'a(.*?)b', '$1\$',    'ccc$'],
  16.     ['abxybaxy', '(x)(y)',  '${2}3$1', 'aby3xbay3x']
  17. );
  18.  
  19. for (0..$#test_cases) {
  20.     say "Case " . ($_ + 1);
  21.     say "--------";
  22.     my ($str, $regex, $replacement, $expected_result) = @{$test_cases[$_]};
  23.     say "String: '$str'";
  24.     say "Regex: " . dump($regex);
  25.     say "Replacement: '$replacement'";
  26.     say "Expected: '$expected_result'";
  27.     pos( $str ) = 0;
  28.     while (1) {
  29.         my @captures = $str =~ /\G.*?$regex/;
  30.         last if @captures == 0;
  31.         pos($str) = $+[0];
  32.         say "Captures: " . dump(@captures);
  33.         my $func = parse_replacement( $replacement );
  34.         my $result = $func->(\@captures);
  35.         say "Result1: '$result'";
  36.         $str =~ s/$regex/$result/;
  37.     }
  38.     say "Result2: '$str'";
  39.     say "Test result: " . (($expected_result eq $str) ? "passed" : "failed");
  40.     say "";
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top