8000 Add additional tests. · davidblewett/rure-python@a169224 · GitHub
[go: up one dir, main page]

Skip to content

Commit a169224

Browse files
committed
Add additional tests.
1 parent c3e1fdb commit a169224

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

rure/tests/test_matchobject.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
from __future__ import print_function
4+
import unittest
5+
6+
import rure
7+
8+
9+
class TestMatchObject(unittest.TestCase):
10+
11+
def test_match_is_boolean(self):
12+
pattern = rure.compile(u"d")
13+
self.assertTrue(pattern.search(u"dog"))
14+
self.assertFalse(pattern.search(u"dog", 1))
15+
16+
def test_group(self):
17+
m = rure.match(u"(\\w+) (\\w+)", u"Isaac Newton, physicist")
18+
self.assertEqual(m.group(0), u'Isaac Newton')
19+
self.assertEqual(m.group(1), u'Isaac')
20+
self.assertEqual(m.group(2), u'Newton')
21+
self.assertEqual(m.group(1, 2), ('Isaac', 'Newton'))
22+
23+
def test_complicated_group(self):
24+
m = rure.match(u"(?P<first_name>\\w+) (?P<last_name>\\w+)",
25+
u"Malcolm Reynolds")
26+
self.assertEqual(m.group(u'first_name'), u'Malcolm')
27< 10000 /code>+
self.assertEqual(m.group(u'last_name'), u'Reynolds')
28+
29+
self.assertEqual(m.group(1), u'Malcolm')
30+
self.assertEqual(m.group(2), u'Reynolds')
31+
32+
def test_group_last_match(self):
33+
m = rure.match(u"(..)+", u"a1b2c3")
34+
self.assertEqual(m.group(1), u'c3')
35+
36+
def test_groups(self):
37+
m = rure.match(u"(\\d+)\\.(\\d+)", u"24.1632")
38+
self.assertEqual(m.groups(), ('24', '1632'))
39+
40+
def test_groups_optional(self):
41+
m = rure.match(u"(\\d+)\\.?(\\d+)?", u"24")
42+
self.assertEqual(m.groups(), (u'24', None))
43+
self.assertEqual(m.groups(u'0'), (u'24', u'0'))
44+
45+
def test_groupdict(self):
46+
m = rure.match(u"(?P<first_name>\\w+) (?P<last_name>\\w+)",
47+
u"Malcolm Reynolds")
48+
self.assertEqual(m.groupdict(), {u'first_name': u'Malcolm',
49+
u'last_name': u'Reynolds'})
50+
51+
def test_start_end(self):
52+
m = rure.search(u"remove_this", u"tony@tiremove_thisger.net")
53+
self.assertEqual((m.string[:m.start()] + m.string[m.end():]).decode('utf8'),
54+
u'tony@tiger.net')

rure/tests/test_regexobject.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,40 @@
55
import unittest
66

77
import rure
8+
from rure.lib import RureMatch
89

910

1011
class TestRegexObject(unittest.TestCase):
12+
1113
def test_search(self):
14+
pattern = rure.compile(u"d")
15+
self.assertTrue(pattern.search(u"dog"))
16+
self.assertFalse(pattern.search(u"dog", 1))
17+
18+
def test_match(self):
19+
pattern = rure.compile(u"o")
20+
self.assertFalse(pattern.match(u"dog"))
21+
self.assertTrue(pattern.match(u"dog", 1))
22+
23+
def test_findall(self):
24+
haystack = u"abc xyz"
25+
pattern = rure.compile(u"\\w+(\\w)")
26+
matches = pattern.findall(haystack)
27+
self.assertEqual(len(matches), 2)
28+
self.assertEqual(matches, [u'c', u'z'])
29+
30+
def test_finditer(self):
31+
haystack = u"abc xyz"
32+
pattern = rure.compile(u"\\w+(\\w)")
33+
gen = pattern.finditer(haystack)
34+
match = next(gen)
35+
self.assertIsNotNone(match)
36+
self.assertEqual(match.captures[0], RureMatch(0, 3))
37+
match = next(gen)
38+
self.assertIsNotNone(match)
39+
self.assertEqual(match.captures[-1], RureMatch(6, 7))
40+
41+
def test_nonmatching_captures(self):
1242
ptn = u"(re).*(ger)"
1343
email = u"tony@tiremove_thisger.net"
1444
results = rure.search(ptn, email)

0 commit comments

Comments
 (0)
0