Last active
October 10, 2019 23:13
-
-
Save siracusa/4e139a38b9eff151b5c8a7f3071a0bd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Test::More; | |
# Increase this number as your sanity allows | |
use constant PAREN_DEPTH => 5; | |
my $nested_string_escape_regex = '\\\\\(' . '(?:[^()]|\(' x PAREN_DEPTH . '[^()]*' . '\))*' x PAREN_DEPTH . '\)'; | |
my @test_cases = ( | |
'(one)' => [], | |
'\(one)' => [ '\(one)' ], | |
'(one \\(two))' => [ '\\(two)' ], | |
'(one \\(two (three (four))))' => [ '\\(two (three (four)))' ], | |
'(one (two \(three (four))))' => [ '\\(three (four))' ], | |
'(one (two \(three (four)))) five \(six (seven (eight (nine))) ten)' => [ '\\(three (four))', '\\(six (seven (eight (nine))) ten)' ], | |
); | |
plan tests => @test_cases / 2; | |
for (my $i = 0; $i < @test_cases; $i += 2) { | |
my $string = $test_cases[$i]; | |
my $expect = $test_cases[$i + 1]; | |
my @match = ($string =~ /.*?($nested_string_escape_regex)/g); | |
is_deeply(\@match, $expect, $string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment