-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_oore.py
187 lines (140 loc) · 4.2 KB
/
test_oore.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
# encoding: utf-8
"""
test_oore
~~~~~~~~~
:copyright: 2014 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details
"""
import re
import sys
from oore import r
from pytest import raises
if sys.version_info[0] == 2:
text_type = unicode
else:
text_type = str
def test_add_text():
foo = r(u'foo')
bar = r(u'bar')
foobar = foo + bar
assert isinstance(foobar.pattern, text_type)
assert foobar.match(u'foobar')
def test_add_bytes():
foo = r(b'foo')
bar = r(b'bar')
foobar = foo + bar
assert isinstance(foobar.pattern, bytes)
assert foobar.match(b'foobar')
def test_add_mixed():
foo = r(u'foo')
bar = r(b'bar')
with raises(TypeError):
foo + bar
def test_or_text():
a = r(u'foo|bar')
b = r(u'foo') | r(u'bar')
for expr in [a, b]:
assert expr.match(u'foo')
assert expr.match(u'bar')
def test_or_bytes():
a = r(b'foo|bar')
b = r(b'foo') | r(b'bar')
for expr in [a, b]:
assert expr.match(b'foo')
assert expr.match(b'bar')
def test_or_mixed():
foo = r(u'foo')
bar = r(b'bar')
with raises(TypeError):
foo | bar
def test_repeat_text():
foo = r(u'foo')
foo_3 = foo[3]
assert isinstance(foo_3.pattern, text_type)
assert foo_3.match(u'foo' * 3)
def test_repeat_bytes():
foo = r(b'foo')
foo_3 = foo[3]
assert isinstance(foo_3.pattern, bytes)
assert foo_3.match(b'foo' * 3)
def test_repeat_from_to_text():
foo = r(u'foo')
foo_2_to_4 = foo[2, 4]
assert isinstance(foo_2_to_4.pattern, text_type)
for i in range(2, 5):
assert foo_2_to_4.match(u'foo' * i)
def test_repeat_from_to_bytes():
foo = r(b'foo')
foo_2_to_4 = foo[2, 4]
assert isinstance(foo_2_to_4.pattern, bytes)
for i in range(2, 5):
assert foo_2_to_4.match(b'foo' * i)
def test_repeat_zero_or_more_text():
foo = r(u'foo')
foo_zero_or_more = foo[0, ...]
assert isinstance(foo_zero_or_more.pattern, text_type)
for i in range(10):
assert foo_zero_or_more.match(u'foo' * i)
def test_repeat_zero_or_more_bytes():
foo = r(b'foo')
foo_zero_or_more = foo[0, ...]
assert isinstance(foo_zero_or_more.pattern, bytes)
for i in range(10):
assert foo_zero_or_more.match(b'foo' * i)
def test_repeat_one_or_more_text():
foo = r(u'foo')
foo_one_or_more = foo[1, ...]
assert isinstance(foo_one_or_more.pattern, text_type)
for i in range(1, 10):
assert foo_one_or_more.match(u'foo' * i)
def test_repeat_one_or_more_bytes():
foo = r(b'foo')
foo_one_or_more = foo[1, ...]
assert isinstance(foo_one_or_more.pattern, bytes)
for i in range(1, 10):
assert foo_one_or_more.match(b'foo' * i)
def test_repeat_n_or_more_text():
foo = r(u'foo')
for n in range(2, 5):
foo_n_or_more = foo[n, ...]
assert isinstance(foo_n_or_more.pattern, text_type)
for i in range(n, 10):
assert foo_n_or_more.match(u'foo' * i)
def test_repeat_n_or_more_bytes():
foo = r(b'foo')
for n in range(2, 5):
foo_n_or_more = foo[n, ...]
assert isinstance(foo_n_or_more.pattern, bytes)
for i in range(n, 10):
assert foo_n_or_more.match(b'foo' * i)
def test_check_r_argument_is_valid_regexp():
with raises(re.error):
r('(')
def test_numbered_groups_text():
foo = r(u'foo').grouped()
assert isinstance(foo.pattern, text_type)
bar = r(u'bar')
foobar = foo + bar
match = foobar.match(u'foobar')
assert match.group(1) == u'foo'
def test_numbered_groups_bytes():
foo = r(b'foo').grouped()
assert isinstance(foo.pattern, bytes)
bar = r(b'bar')
foobar = foo + bar
match = foobar.match(b'foobar')
assert match.group(1) == b'foo'
def test_named_groups_text():
foo = r(u'foo').grouped('group')
assert isinstance(foo.pattern, text_type)
bar = r(u'bar')
foobar = foo + bar
match = foobar.match(u'foobar')
assert match.group('group') == u'foo'
def test_named_groups_bytes():
foo = r(b'foo').grouped('group')
assert isinstance(foo.pattern, bytes)
bar = r(b'bar')
foobar = foo + bar
match = foobar.match(b'foobar')
assert match.group('group') == b'foo'