Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use feature qw(say);
- use strict;
- use warnings;
- use Carp;
- use Data::Dump qw(dd dump);
- use Scalar::Util 'reftype';
- # Test cases:
- # Format:
- # string, regex, replacement, expected
- my @test_cases = (
- ['aba', 'a(.*?)a', '$1', 'b'],
- ['ababaxxa', 'a(.*?)a', '$1', 'bbxx'],
- ['acccb', 'a(.*?)b', '$1\$', 'ccc$'],
- ['abxybaxy', '(x)(y)', '${2}3$1', 'aby3xbay3x']
- );
- for (0..$#test_cases) {
- say "Case " . ($_ + 1);
- say "--------";
- my ($str, $regex, $replacement, $expected_result) = @{$test_cases[$_]};
- say "String: '$str'";
- say "Regex: " . dump($regex);
- say "Replacement: '$replacement'";
- say "Expected: '$expected_result'";
- pos( $str ) = 0;
- while (1) {
- my @captures = $str =~ /\G.*?$regex/;
- last if @captures == 0;
- pos($str) = $+[0];
- say "Captures: " . dump(@captures);
- my $func = parse_replacement( $replacement );
- my $result = $func->(\@captures);
- say "Result1: '$result'";
- $str =~ s/$regex/$result/;
- }
- say "Result2: '$str'";
- say "Test result: " . (($expected_result eq $str) ? "passed" : "failed");
- say "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement