|
1
|
|
|
from coalib.parsing.StringProcessing import ( |
|
2
|
|
|
InBetweenMatch, nested_search_in_between) |
|
3
|
|
|
from tests.parsing.StringProcessing.StringProcessingTestBase import ( |
|
4
|
|
|
StringProcessingTestBase) |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class NestedSearchInBetweenTest(StringProcessingTestBase): |
|
8
|
|
|
bs = StringProcessingTestBase.bs |
|
9
|
|
|
|
|
10
|
|
|
test_basic_expected_results = [ |
|
11
|
|
|
[("(", 0, "", 1, ")", 1), |
|
12
|
|
|
("(", 6, "This is a word", 7, ")", 21), |
|
13
|
|
|
("(", 25, "(in a word) another ", 26, ")", 46)], |
|
14
|
|
|
[("(", 4, "((((((((((((((((((1)2)3))))))))))))))))", 5, ")", 44)], |
|
15
|
|
|
[("(", 6, "do (it ) more ", 7, ")", 21), |
|
16
|
|
|
("(", 41, "", 42, ")", 42), |
|
17
|
|
|
("(", 44, "hello.", 45, ")", 51)], |
|
18
|
|
|
[("(", 0, "", 1, ")", 1), |
|
19
|
|
|
("(", 8, r"This\ is a word" + bs, 9, ")", 25), |
|
20
|
|
|
("(", 29, r"(in a\\\ word\\\\\) another " + bs, 30, ")", 59)], |
|
21
|
|
|
[("(", 5, |
|
22
|
|
|
r"\(\((((((\\\(((((((((((1)2)3))\\\\\)))))))))))))\)" + bs, 6, |
|
23
|
|
|
")", 57)], |
|
24
|
|
|
[("(", 7, "do (it ) more ", 8, ")", 22), |
|
25
|
|
|
("(", 45, "", 46, ")", 46), |
|
26
|
|
|
("(", 48, "hello.", 49, ")", 55)]] |
|
27
|
|
|
|
|
28
|
|
|
# Test the basic functionality of nested_search_in_between(). |
|
29
|
|
|
def test_basic(self): |
|
30
|
|
|
self.assertResultsEqual( |
|
31
|
|
|
nested_search_in_between, |
|
32
|
|
|
{(self.search_in_between_begin_pattern, |
|
33
|
|
|
self.search_in_between_end_pattern, |
|
34
|
|
|
test_string, |
|
35
|
|
|
0, |
|
36
|
|
|
False, |
|
37
|
|
|
False): [InBetweenMatch.from_values(*args) |
|
38
|
|
|
for args in result] |
|
39
|
|
|
for test_string, result in zip( |
|
40
|
|
|
self.search_in_between_test_strings, |
|
41
|
|
|
self.test_basic_expected_results)}, |
|
42
|
|
|
list) |
|
43
|
|
|
|
|
44
|
|
|
# Test nested_search_in_between() when feeding it with the same begin- and |
|
45
|
|
|
# end-sequences. |
|
46
|
|
|
def test_same_pattern(self): |
|
47
|
|
|
self.assertResultsEqual( |
|
48
|
|
|
nested_search_in_between, |
|
49
|
|
|
{(pattern, pattern, test_string, 0, False, False): [] |
|
50
|
|
|
for test_string in self.search_in_between_test_strings |
|
51
|
|
|
for pattern in [self.search_in_between_begin_pattern, |
|
52
|
|
|
self.search_in_between_end_pattern]}, |
|
53
|
|
|
list) |
|
54
|
|
|
|
|
55
|
|
|
# Test nested_search_in_between() for its max_match parameter. |
|
56
|
|
|
def test_max_match(self): |
|
57
|
|
|
self.assertResultsEqual( |
|
58
|
|
|
nested_search_in_between, |
|
59
|
|
|
{(self.search_in_between_begin_pattern, |
|
60
|
|
|
self.search_in_between_end_pattern, |
|
61
|
|
|
test_string, |
|
62
|
|
|
max_match, |
|
63
|
|
|
False, |
|
64
|
|
|
False): [InBetweenMatch.from_values(*args) |
|
65
|
|
|
for args in result] |
|
66
|
|
|
for max_match in [1, 2, 5, 22] |
|
67
|
|
|
for test_string, result in zip( |
|
68
|
|
|
self.search_in_between_test_strings, |
|
69
|
|
|
[elem[0:max_match] |
|
70
|
|
|
for elem in self.test_basic_expected_results])}, |
|
71
|
|
|
list) |
|
72
|
|
|
|
|
73
|
|
|
# Test nested_search_in_between() with a regex pattern. |
|
74
|
|
|
def test_regex_pattern(self): |
|
75
|
|
|
self.assertResultsEqual( |
|
76
|
|
|
nested_search_in_between, |
|
77
|
|
|
{(r"(?:)\(", r"\)(?:)", test_string, 0, False, True): |
|
78
|
|
|
[InBetweenMatch.from_values(*args) for args in result] |
|
79
|
|
|
for test_string, result in zip( |
|
80
|
|
|
self.search_in_between_test_strings, |
|
81
|
|
|
self.test_basic_expected_results)}, |
|
82
|
|
|
list) |
|
83
|
|
|
|
|
84
|
|
|
# Test nested_search_in_between() for its auto_trim feature. |
|
85
|
|
|
def test_auto_trim(self): |
|
86
|
|
|
expected_results = [ |
|
87
|
|
|
[("(", 6, "This is a word", 7, ")", 21), |
|
88
|
|
|
("(", 25, "(in a word) another ", 26, ")", 46)], |
|
89
|
|
|
[("(", 4, "((((((((((((((((((1)2)3))))))))))))))))", 5, ")", 44)], |
|
90
|
|
|
[("(", 6, "do (it ) more ", 7, ")", 21), |
|
91
|
|
|
("(", 44, "hello.", 45, ")", 51)], |
|
92
|
|
|
[("(", 8, r"This\ is a word" + self.bs, 9, ")", 25), |
|
93
|
|
|
("(", 29, |
|
94
|
|
|
r"(in a\\\ word\\\\\) another " + self.bs, 30, |
|
95
|
|
|
")", 59)], |
|
96
|
|
|
[("(", |
|
97
|
|
|
5, |
|
98
|
|
|
r"\(\((((((\\\(((((((((((1)2)3))\\\\\)))))))))))))\)" + self.bs, |
|
99
|
|
|
6, |
|
100
|
|
|
")", |
|
101
|
|
|
57)], |
|
102
|
|
|
[("(", 7, "do (it ) more ", 8, ")", 22), |
|
103
|
|
|
("(", 48, "hello.", 49, ")", 55)]] |
|
104
|
|
|
|
|
105
|
|
|
self.assertResultsEqual( |
|
106
|
|
|
nested_search_in_between, |
|
107
|
|
|
{(begin_pattern, |
|
108
|
|
|
end_pattern, |
|
109
|
|
|
test_string, |
|
110
|
|
|
0, |
|
111
|
|
|
True, |
|
112
|
|
|
use_regex): [InBetweenMatch.from_values(*args) |
|
113
|
|
|
for args in result] |
|
114
|
|
|
for test_string, result in zip( |
|
115
|
|
|
self.search_in_between_test_strings, |
|
116
|
|
|
expected_results) |
|
117
|
|
|
for use_regex, begin_pattern, end_pattern in [ |
|
118
|
|
|
(True, r"\(", r"\)"), |
|
119
|
|
|
(False, |
|
120
|
|
|
self.search_in_between_begin_pattern, |
|
121
|
|
|
self.search_in_between_end_pattern)]}, |
|
122
|
|
|
list) |
|
123
|
|
|
|
|
124
|
|
|
# Test for special cases that exposed bugs earlier. |
|
125
|
|
|
def test_special(self): |
|
126
|
|
|
self.assertResultsEqual( |
|
127
|
|
|
nested_search_in_between, |
|
128
|
|
|
{("(", ")", "a)b(c", 0, True, False): []}, |
|
129
|
|
|
list) |
|
130
|
|
|
|