1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 '''
31 Tests for the lepl.matchers.support module (duplicate, including Python 3
32 only syntax)
33 '''
34
35
36 from unittest import TestCase
37
38 from lepl.matchers.support import function_matcher_factory, function_matcher, \
39 sequence_matcher_factory, sequence_matcher
40 from lepl.stream.core import DUMMY_HELPER, s_next
41
42
43 @function_matcher
44 -def char(support, stream):
47
48 @function_matcher_factory()
49 -def char_in(chars):
50 def match(support, stream):
51 (char, next_stream) = s_next(stream)
52 if char in chars:
53 return ([char], next_stream)
54 return match
55
56 @sequence_matcher
57 -def any_char(support, stream):
61
69 return match
70
73
75
76 matcher = char()
77 matcher.config.no_full_first_match()
78 result = list(matcher.match_sequence('ab'))
79 assert result == [(['a'], (1, DUMMY_HELPER))], result
80 matcher = char()[2:,...]
81 matcher.config.no_full_first_match()
82 result = list(matcher.match_sequence('abcd'))
83 assert result == [(['abcd'], (4, DUMMY_HELPER)),
84 (['abc'], (3, DUMMY_HELPER)),
85 (['ab'], (2, DUMMY_HELPER))], result
86 assert char()[:,...].parse('ab') == ['ab']
87
89
90 matcher = char_in('abc')
91 matcher.config.no_full_first_match()
92 result = list(matcher.match_sequence('ab'))
93 assert result == [(['a'], (1, DUMMY_HELPER))], result
94 result = list(matcher.match_sequence('pqr'))
95 assert result == [], result
96 matcher = char_in('abc')[2:,...]
97 matcher.config.no_full_first_match()
98 result = list(matcher.match_sequence('abcd'))
99 assert result == [(['abc'], (3, DUMMY_HELPER)),
100 (['ab'], (2, DUMMY_HELPER))], result
101
103
104 matcher = any_char()
105
106 matcher.config.no_full_first_match()
107 result = list(matcher.match_sequence('ab'))
108 assert result == [(['a'], (1, DUMMY_HELPER)),
109 (['b'], (2, DUMMY_HELPER))], result
110 matcher = any_char()[2:,...]
111 matcher.config.no_full_first_match()
112 result = list(matcher.match_sequence('abcd'))
113 assert result == [(['abcd'], (4, DUMMY_HELPER)),
114 (['abc'], (3, DUMMY_HELPER)),
115 (['abd'], (4, DUMMY_HELPER)),
116 (['ab'], (2, DUMMY_HELPER)),
117 (['acd'], (4, DUMMY_HELPER)),
118 (['ac'], (3, DUMMY_HELPER)),
119 (['ad'], (4, DUMMY_HELPER)),
120 (['bcd'], (4, DUMMY_HELPER)),
121 (['bc'], (3, DUMMY_HELPER)),
122 (['bd'], (4, DUMMY_HELPER)),
123 (['cd'], (4, DUMMY_HELPER))], result
124
126 matcher = any_char_in('abc')
127 matcher.config.no_full_first_match()
128 result = list(matcher.match_sequence('ab'))
129 assert result == [(['a'], (1, DUMMY_HELPER)), (['b'], ( 2, DUMMY_HELPER))], result
130 result = list(matcher.match_sequence('pqr'))
131 assert result == [], result
132 matcher = any_char_in('abc')[2:,...]
133 matcher.config.no_full_first_match()
134 result = list(matcher.match_sequence('abcd'))
135 assert result == [(['abc'], (3, DUMMY_HELPER)),
136 (['ab'], (2, DUMMY_HELPER)),
137 (['ac'], (3, DUMMY_HELPER)),
138 (['bc'], (3, DUMMY_HELPER))], result
139
141
142 try:
143 char(foo='abc')
144 assert False, 'expected error'
145 except TypeError:
146 pass
147 try:
148 char('abc')
149 assert False, 'expected error'
150 except TypeError:
151 pass
152 try:
153 char_in()
154 assert False, 'expected error'
155 except TypeError:
156 pass
157 try:
158 @function_matcher
159 def foo(a): return
160 assert False, 'expected error'
161 except TypeError:
162 pass
163 try:
164 @function_matcher_factory()
165 def foo(a, *, b=None): return
166 assert False, 'expected error'
167 except TypeError:
168 pass
169
172
174
175 from string import ascii_uppercase
176 @function_matcher
177 def capital(support, stream):
178 (char, next_stream) = s_next(stream)
179 if char in ascii_uppercase:
180 return ([char], next_stream)
181 parser = capital()[3]
182 assert parser.parse_string('ABC')
183