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.core.config module.
32 '''
33
34
35 from unittest import TestCase
36
37
38 from lepl.matchers.variables import TraceVariables
39 from lepl.matchers.operators import DroppedSpace
40 from lepl.matchers.derived import Drop, Word, String
41 from lepl.matchers.core import Any, Lookahead
42 from lepl._test.base import assert_str
43 from lepl.stream.maxdepth import FullFirstMatchException
44 from lepl.regexp.core import RegexpError
45 from lepl.support.lib import StringIO
46
47
49
50 - def run_test(self, name, text, parse, match2, match3, error,
51 config=lambda x: None, **kargs):
52 matcher = Any()[:, ...]
53 config(matcher)
54 parser = getattr(matcher, 'parse' + name)
55 result = str(parser(text, **kargs))
56 assert_str(result, parse)
57 matcher = Any()[2, ...]
58 matcher.config.no_full_first_match()
59 config(matcher)
60 parser = getattr(matcher, 'match' + name)
61 result = str(list(parser(text, **kargs)))
62 assert_str(result, match2)
63 matcher = Any()[3, ...]
64 matcher.config.no_full_first_match()
65 config(matcher)
66 parser = getattr(matcher, 'match' + name)
67 result = str(list(parser(text, **kargs)))
68 assert_str(result, match3)
69 matcher = Any()
70 config(matcher)
71 parser = getattr(matcher, 'parse' + name)
72 try:
73 parser(text, **kargs)
74 except FullFirstMatchException as e:
75 assert_str(e, error)
76
78 self.run_test('_string', 'abc',
79 "['abc']",
80 "[(['ab'], (2, <helper>))]",
81 "[(['abc'], (3, <helper>))]",
82 "The match failed in <string> at 'bc' (line 1, character 2).")
83 self.run_test('', 'abc',
84 "['abc']",
85 "[(['ab'], (2, <helper>))]",
86 "[(['abc'], (3, <helper>))]",
87 "The match failed in <string> at 'bc' (line 1, character 2).")
88 self.run_test('_sequence', 'abc',
89 "['abc']",
90 "[(['ab'], (2, <helper>))]",
91 "[(['abc'], (3, <helper>))]",
92 "The match failed in <str> at 'bc' (offset 1, value 'b').")
93
95 self.run_test('_list', ['a', 'b', 'c'],
96 "[['a', 'b', 'c']]",
97 "[([['a', 'b']], (2, <helper>))]",
98 "[([['a', 'b', 'c']], (3, <helper>))]",
99 "The match failed in <list<str>> at ['b', 'c'] (offset 1, value 'b').",
100 config=lambda m: m.config.no_compile_to_regexp())
101
103
104 try:
105
106
107 self.run_test('_list', [1, 2, 3], [], [], [], """""")
108 except RegexpError as e:
109 assert 'no_compile_to_regexp' in str(e), str(e)
110 self.run_test('_list', [1, 2, 3],
111 "[[1, 2, 3]]",
112 "[([[1, 2]], (2, <helper>))]",
113 "[([[1, 2, 3]], (3, <helper>))]",
114 "The match failed in <list<int>> at [2, 3] (offset 1, value 2).",
115 config=lambda m: m.config.no_compile_to_regexp())
116
117
125
126
128
130 buffer = StringIO()
131 with TraceVariables(out=buffer):
132 word = ~Lookahead('OR') & Word()
133 phrase = String()
134 with DroppedSpace():
135 text = (phrase | word)[1:] > list
136 query = text[:, Drop('OR')]
137 expected = ''' phrase failed stream = 'spicy meatballs OR...
138 word = ['spicy'] stream = ' meatballs OR "el ...
139 phrase failed stream = 'meatballs OR "el b...
140 word = ['meatballs'] stream = ' OR "el bulli rest...
141 phrase failed stream = 'OR "el bulli resta...
142 word failed stream = 'OR "el bulli resta...
143 phrase failed stream = ' OR "el bulli rest...
144 word failed stream = ' OR "el bulli rest...
145 text = [['spicy', 'meatballs']] stream = ' OR "el bulli rest...
146 phrase = ['el bulli restaurant'] stream = ''
147 phrase failed stream = ''
148 word failed stream = ''
149 text = [['el bulli restaurant']] stream = ''
150 query = [['spicy', 'meatballs'], ['el... stream = ''
151 '''
152 query.config.auto_memoize(full=True)
153 query.parse('spicy meatballs OR "el bulli restaurant"')
154 trace = buffer.getvalue()
155 assert trace == expected, '"""' + trace + '"""'
156
157 query.parse('spicy meatballs OR "el bulli restaurant"')
158 trace = buffer.getvalue()
159 assert trace == expected, '"""' + trace + '"""'
160