-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_rex.py
215 lines (138 loc) · 4.88 KB
/
test_rex.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import re
import pytest
import rex as rex_module
from rex import rex_clear_cache, RexMatch, rex
def test_value_error():
pytest.raises(ValueError, rex, '/test')
pytest.raises(ValueError, rex, 'm/test')
pytest.raises(ValueError, rex, 's/test/')
pytest.raises(ValueError, rex, 's//test/')
def test_no_action():
r = rex('/test/')
assert r.action == 'm'
assert r.pattern == 'test'
assert r.flags == 0
def test_str():
m = "This is dog!" == rex('/[a-z]+!/')
assert str(m) == 'dog!'
def test_empty_str():
m = "This is dog!" == rex('/[0-9]+!/')
assert str(m) == ''
def test_unicode():
m = "This is dog!" == rex('/[a-z]+!/')
assert m.__unicode__() == u'dog!'
def test_empty_unicode():
m = "This is dog!" == rex('/[0-9]+!/')
assert m.__unicode__() == u''
def test_no_action_ex():
r = rex('!test!')
assert r.action == 'm'
assert r.pattern == 'test'
assert r.flags == 0
def test_m_action():
r = rex('m/test/')
assert r.action == 'm'
assert r.pattern == 'test'
assert r.flags == 0
def test_m_action_ex():
r = rex('m!test!')
assert r.action == 'm'
assert r.pattern == 'test'
assert r.flags == 0
def test_s_action():
r = rex('s/test/ohh/')
assert r.action == 's'
assert r.pattern == 'test'
assert r.replacement == 'ohh'
assert r.flags == 0
def test_s_action_flags():
r = rex('/test/im')
assert r.action == 'm'
assert r.pattern == 'test'
assert r.flags == re.I | re.M
def test_s_action_bad():
pytest.raises(ValueError, rex, '/test/imH')
def test_m_true():
assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
def test_m_true_orthodox():
assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")
def test_m_false_noncache():
assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx", cache=False)
assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa bb cc xx", cache=False)
def test_m_false():
assert not ("Aa 9-9 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
def test_m_false_orthodox():
assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 xx")
def test_m_value():
assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))['t'] == '88'
assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))[2] == '88'
assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))['tttt'] is None
def test_m_value_orthodox():
assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")['t'] == '88'
assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")[2] == '88'
assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")['tttt'] is None
def test_m_true_call():
r = rex('/([0-9-]+) (?P<t>[0-9-]+)/')
assert r("Aa 9-9 88 xx")
def test_m_false_call():
r = rex('/([0-9-]+) (?P<t>[0-9-]+)/')
assert not r("Aa 9-9 xx")
def test_m_g():
assert (("Aa 9-9 88 xx" == rex('/(\d)/g')) == ['9', '9', '8', '8'])
assert (("Aa 9-9 88 xx" == rex('/([aA])/g')) == ['A', 'a'])
assert (("Aa 9-9 88 xx" == rex('/(ttt)/g')) == [])
def test_s():
s = ("This is a cat" == rex('s/cat/dog/'))
assert s == 'This is a dog'
def test_s_i():
s = "This is a cat" == rex('s/CAT/dog/i')
assert s == 'This is a dog'
def test_s_multi():
s = "This is a cat cat cat cat" == rex('s/cat/dog/')
assert s == 'This is a dog dog dog dog'
def test_s_orthodox():
assert rex('s/cat/dog/', "This is a cat") == 'This is a dog'
def test_s_i_orthodox():
assert rex('s/CAT/dog/i', "This is a cat") == 'This is a dog'
def test_s_multi_orthodox():
assert rex('s/cat/dog/', "This is a cat cat cat cat") == 'This is a dog dog dog dog'
def test_cache():
rex('s/cache/test/')
assert 's/cache/test/' in rex_module.REX_CACHE
def test_cache_2():
a = rex('s/cache/test/')
b = rex('s/cache/test/')
assert a is b
def test_no_cache_2():
a = rex('s/cache/test/', cache=False)
b = rex('s/cache/test/', cache=False)
assert not (a is b)
def test_not_cache():
rex('s/cache/test1/', cache=False)
assert not 's/cache/test1/' in rex_module.REX_CACHE
def test_clear_cache():
rex('s/cache/test/')
rex_clear_cache()
assert not 's/cache/test/' in rex_module.REX_CACHE
def test_rex_match():
rm = RexMatch(((0, 'some match'), ('a', 1), ('b', 2)))
assert rm['a'] == 1
assert rm['b'] == 2
assert str(rm) == 'some match'
assert rm.__unicode__() == u'some match'
def test_rex_match_empty():
rm = RexMatch()
assert rm['a'] is None
assert rm['b'] is None
assert str(rm) == ''
assert rm.__unicode__() == u''
def test_rex_match_get_empty():
rm = RexMatch((('c', None),))
assert rm.get('a') is None
assert rm.get('a', 'b') == 'b'
assert rm.get('c', 'b') == 'b'
def test_rex_group():
m = "This is cat! A kitten is a cat but not a dog." == rex('/[a-z]+!.*(kitten\s\S{2}).*but.*(dog)\./')
assert m == rex.group
if __name__ == "__main__":
pytest.main()