1 from lepl.stream.core import DUMMY_HELPER, s_next
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 '''
32 Tests for the lepl.matchers.support module
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
41
42 @function_matcher
43 -def char(support, stream):
46
47 @function_matcher_factory()
48 -def char_in(chars):
49 def match(support, stream):
50 (char, next_stream) = s_next(stream)
51 if char in chars:
52 return ([char], next_stream)
53 return match
54
55 @sequence_matcher
56 -def any_char(support, stream):
60
68 return match
69
70
71 -def mks(head, offset):
73
76
78
79 matcher = char()
80 matcher.config.no_full_first_match()
81 result = list(matcher.match_sequence('ab'))
82 assert result == [(['a'], mks('ab', 1))], result
83 matcher = char()[2:,...]
84 matcher.config.no_full_first_match()
85 result = list(matcher.match_sequence('abcd'))
86 assert result == [(['abcd'], mks('abcd', 4)),
87 (['abc'], mks('abcd', 3)),
88 (['ab'], mks('abcd', 2))], result
89 assert char()[:,...].parse('ab') == ['ab']
90
92
93 matcher = char_in('abc')
94 matcher.config.no_full_first_match()
95 result = list(matcher.match_sequence('ab'))
96 assert result == [(['a'], mks('ab', 1))], result
97 result = list(matcher.match_sequence('pqr'))
98 assert result == [], result
99 matcher = char_in('abc')[2:,...]
100 matcher.config.no_full_first_match()
101 result = list(matcher.match_sequence('abcd'))
102 assert result == [(['abc'], mks('abcd', 3)),
103 (['ab'], mks('abcd', 2))], result
104
106
107 matcher = any_char()
108
109 matcher.config.no_full_first_match()
110 result = list(matcher.match_sequence('ab'))
111 assert result == [(['a'], mks('ab', 1)),
112 (['b'], mks('ab', 2))], result
113 matcher = any_char()[2:,...]
114 matcher.config.no_full_first_match()
115 result = list(matcher.match_sequence('abcd'))
116 assert result == [(['abcd'], mks('abcd', 4)),
117 (['abc'], mks('abcd', 3)),
118 (['abd'], mks('abcd', 4)),
119 (['ab'], mks('abcd', 2)),
120 (['acd'], mks('abcd', 4)),
121 (['ac'], mks('abcd', 3)),
122 (['ad'], mks('abcd', 4)),
123 (['bcd'], mks('abcd', 4)),
124 (['bc'], mks('abcd', 3)),
125 (['bd'], mks('abcd', 4)),
126 (['cd'], mks('abcd', 4))], result
127
129 matcher = any_char_in('abc')
130 matcher.config.no_full_first_match()
131 result = list(matcher.match_sequence('ab'))
132 assert result == [(['a'], mks('ab', 1)),
133 (['b'], mks('ab', 2))], result
134 result = list(matcher.match_sequence('pqr'))
135 assert result == [], result
136 matcher = any_char_in('abc')[2:,...]
137 matcher.config.no_full_first_match()
138 result = list(matcher.match_sequence('abcd'))
139 assert result == [(['abc'], mks('abcd', 3)),
140 (['ab'], mks('abcd', 2)),
141 (['ac'], mks('abcd', 3)),
142 (['bc'], mks('abcd', 3))], result
143
145
146 try:
147 char(foo='abc')
148 assert False, 'expected error'
149 except TypeError:
150 pass
151 try:
152 char('abc')
153 assert False, 'expected error'
154 except TypeError:
155 pass
156 try:
157 char_in()
158 assert False, 'expected error'
159 except TypeError:
160 pass
161 try:
162 @function_matcher
163 def foo(a): return
164 assert False, 'expected error'
165 except TypeError:
166 pass
167
170
172
173 from string import ascii_uppercase
174 @function_matcher
175 def capital(support, stream):
176 (char, next_stream) = s_next(stream)
177 if char in ascii_uppercase:
178 return ([char], next_stream)
179 parser = capital()[3]
180 assert parser.parse_string('ABC')
181