My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
#LICENCE


from lepl.rxpy.engine._test.base import BaseTest


class ReTest(BaseTest):

def assert_groups(self, pattern, text, groups, target):
try:
results = self._re.compile(pattern).match(text).group(*groups)
assert results == target, repr(results)
except Exception as e:
if isinstance(target, type):
assert isinstance(e, target), repr(e)
else:
assert False, repr(e)

def test_zero(self):
self.assert_groups('.*', 'abc', [], 'abc')
self.assert_groups('.*', 'abc', [0], 'abc')
self.assert_groups('.(.).', 'abc', [], 'abc')
self.assert_groups('.(.).', 'abc', [0], 'abc')

def test_numbered(self):
self.assert_groups('.(.).', 'abc', [1], 'b')
self.assert_groups('.(.)*', 'abc', [1], 'c')
self.assert_groups('.(.).(.?)', 'abc', [2], '')
self.assert_groups('.(.).(.)?', 'abc', [2], None)
self.assert_groups('.(.).(.?)', 'abc', [3], IndexError)
self.assert_groups('(?_g)(?P<4>.)(?P<4>.)', 'ab', [4], 'b')
results = self._re.compile('(?_g)(?P<4>.)(?P<4>.)').match('ab')
assert results.groups() == ('b',), results.groups()


def assert_split(self, pattern, text, target, *args):
result = self._re.compile(pattern).split(text, *args)
assert result == target, result

def test_split_from_docs(self):
self.assert_split(r'[^A-Za-z]+', 'Words, words, words.',
['Words', 'words', 'words', ''])
self.assert_split(r'([^A-Za-z]+)', 'Words, words, words.',
['Words', ', ', 'words', ', ', 'words', '.', ''])
self.assert_split(r'[^A-Za-z]+', 'Words, words, words.',
['Words', 'words, words.'], 1)
self.assert_split(r'\W+', 'Words, words, words.',
['Words', 'words', 'words', ''])
self.assert_split(r'(\W+)', 'Words, words, words.',
['Words', ', ', 'words', ', ', 'words', '.', ''])
self.assert_split(r'\W+', 'Words, words, words.',
['Words', 'words, words.'], 1)

def test_match(self):
results = self._re.compile('.*?(x+)').match('axxb')
assert results
assert results.group(1) == 'xx', results.group(1)

def test_findall(self):
match = self._re.compile('(a|(b))').match('aba')
assert match.re.groups == 2, match.re.groups
assert match.group(0) == 'a', match.group(0)
assert match.group(1) == 'a', match.group(1)
assert match.group(2) == None, match.group(2)

results = self._re.compile('(a|(b))').findall('aba')
assert results == [('a', ''), ('b', 'b'), ('a', '')], results

results = self._re.compile('x*').findall('a')
assert len(results) == 2, results

def test_find_from_docs(self):
assert self._re.search(r"[a-zA-Z]+ly",
"He was carefully disguised but captured quickly by police.")
results = self._re.findall(r"[a-zA-Z]+ly",
"He was carefully disguised but captured quickly by police.")
assert results == ['carefully', 'quickly'], results
results = self._re.findall(r"\w+ly",
"He was carefully disguised but captured quickly by police.")
assert results == ['carefully', 'quickly'], results

def test_search(self):
results = self._re.search('a*b', 'aab')
assert results
assert results.group(0) == 'aab', results.group(0)

def test_findall_empty(self):
results = self._re.findall('x+', 'abxd')
assert results == ['x'], results
results = self._re.findall('x*', 'abxd')
# this checks against actual behaviour
assert results == ['', '', 'x', '', ''], results

def test_findall_sub(self):
# this also checks against behaviour
results = self._re.sub('x*', '-', 'abxd')
assert results == '-a-b-d-', results
# this too
results = self._re.sub('x*?', '-', 'abxd')
assert results == '-a-b-x-d-', results

def test_end_of_line(self):
results = list(self._re.compile('$').finditer('ab\n'))
assert len(results) == 2, len(results)
assert results[0].group(0) == '', results[0].group(0)
assert results[0].span(0) == (2,2), results[0].span(0)
assert results[1].group(0) == '', results[1].group(0)
assert results[1].span(0) == (3,3), results[1].span(0)

results = self._re.sub('$', 'x', 'ab\n')
assert results == 'abx\nx', results

# TODO - make this work
# def test_pickle(self):
# import pickle
# self.pickle_test(pickle)

def pickle_test(self, pickle):
oldpat = self._re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
s = pickle.dumps(oldpat)
newpat = pickle.loads(s)
assert oldpat.deep_eq(newpat)


def test_escape(self):
text = '123abc;.,}{? '
esc = self._re.escape('123abc;.,}{? ')
assert esc == '123abc\\;\\.\\,\\}\\{\\?\\ ', esc
result = self._re.compile(esc).match(text)
assert result


# TODO - how should this work in 3?
# def test_types(self):
# for pattern in ('.', 'a', u'u'):
# for text in ('a', 'u', u'a', u'u'):
# for repl in ('A', u'U'):
# for flags in (0, ParserState.ASCII, ParserState.UNICODE):
# s = self._re.sub(pattern, repl, text, flags=flags)
# print(pattern, text, repl, flags, s)
# if flags:
# if flags == ParserState.ASCII and type(text) == str:
# assert type(s) == str, type(s)
# else:
# assert type(s) == unicode, type(s)
# else:
# if type(text) == str and type(pattern) == str:
# assert type(s) == str, type(s)
# else:
# assert type(s) == unicode, type(s)

Change log

5b14542ff43b by and...@acooke.org on Jan 10, 2012   Diff
various small fixes for bytes
Go to: 
Project members, sign in to write a code review

Older revisions

8c1f54a55b94 by and...@acooke.org on Jul 3, 2011   Diff
all simple tests working (excluding
pickle)
b34ed179617e by and...@acooke.org on Jul 3, 2011   Diff
integration (re) added; looks like
multiple matches are failing
1edaa9297972 by and...@acooke.org on Jun 26, 2011   Diff
most tests working, but need to think
about how p3 handles bytes
All revisions of this file

File info

Size: 6234 bytes, 151 lines
Powered by Google Project Hosting