Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------
- Perl code for running test of the parse_replacement sub routine
- ---------------------------------------------------------------
- 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', 'bxx'],
- ['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'";
- my @captures = $str =~ /$regex/g;
- say "Captures: " . dump(@captures);
- my $func = parse_replacement( $replacement );
- my $result = $func->(\@captures);
- say "Result1: '$result'";
- $str =~ s/$regex/$result/g;
- say "Result2: '$str'";
- say "Test result: " . (($expected_result eq $str) ? "passed" : "failed");
- say "";
- }
- ************************
- Output:
- ************************
- Case 1
- --------
- String: 'aba'
- Regex: "a(.*?)a"
- Replacement: '$1'
- Expected: 'b'
- Captures: "b"
- Result1: 'b'
- Result2: 'b'
- Test result: passed
- Case 2
- --------
- String: 'ababaxxa'
- Regex: "a(.*?)a"
- Replacement: '$1'
- Expected: 'bxx'
- Captures: ("b", "xx")
- Result1: 'b'
- Result2: 'bbb'
- Test result: failed
- Case 3
- --------
- String: 'acccb'
- Regex: "a(.*?)b"
- Replacement: '$1\$'
- Expected: 'ccc$'
- Captures: "ccc"
- Result1: 'ccc$'
- Result2: 'ccc$'
- Test result: passed
- Case 4
- --------
- String: 'abxybaxy'
- Regex: "(x)(y)"
- Replacement: '${2}3$1'
- Expected: 'aby3xbay3x'
- Captures: ("x", "y", "x", "y")
- Result1: 'y3x'
- Result2: 'aby3xbay3x'
- Test result: passed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement