-
Notifications
You must be signed in to change notification settings - Fork 11
/
regex_rules_test.py
107 lines (92 loc) · 3.46 KB
/
regex_rules_test.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
#!python
# encoding: utf-8
import unittest
from regex_rules import process_flags, RegularExpressionRule, RegularExpressionRuleset
from regex_rules import DEFAULT_FLAGS
import re
class TestProcessFlags(unittest.TestCase):
def test_process_flags_all(self):
flags = 'SLUMIX'
default = 0
expected = re.S | re.L | re.U | re.M | re.I | re.X
self.assertEqual(expected, process_flags(flags, default))
def test_process_flags_minus(self):
flags = '-U+X'
expected = re.S | re.X
self.assertEqual(expected, process_flags(flags, DEFAULT_FLAGS))
class TestRegularExpressionRule(unittest.TestCase):
def setUp(self):
self.expr = '\w+'
self.prefix = r'\b'
self.suffix = r'\b'
self.rule_data = {'re': self.expr, 'flags': 'I'}
self.rule = RegularExpressionRule(None,
{ 'prefix': self.prefix,
'suffix': self.suffix },
self.rule_data, DEFAULT_FLAGS, {})
self.text = u'Some text\n with spaces\nи другими символами'
def test_itermatches(self):
x = list(self.rule.itermatches(self.text))
self.assertEqual(len(x), 1)
pattern, matches = x[0]
flags = process_flags(self.rule_data['flags'], DEFAULT_FLAGS)
self.assertEqual(pattern['original'], self.expr)
self.assertEqual(pattern['compiled'],
re.compile(self.prefix + self.expr + self.suffix, flags))
matches_list = list(matches)
self.assertEqual(len(matches_list), 7)
self.assertEqual(matches_list[0], (0, 4))
self.assertEqual(matches_list[1], (5, 9))
self.assertEqual(matches_list[2], (14, 18))
self.assertEqual(matches_list[3], (19, 25))
self.assertEqual(matches_list[4], (26, 27))
self.assertEqual(matches_list[5], (28, 35))
self.assertEqual(matches_list[6], (36, 45))
def test_process(self):
result = self.rule.process(self.text)
self.assertEqual(result.lines,
dict(enumerate(self.text.splitlines(), 1)))
self.assertEqual(result.pattern_matches,
{
self.expr: {
1: [1, [(0, 4), (5, 9)]],
2: [1, [(4, 8), (9, 15)]],
3: [1, [(0, 1), (2, 9), (10, 19)]]
}
}
)
class TestRegularExpressionRuleSet(unittest.TestCase):
def setUp(self):
self.expr = '[A-Z]+'
self.prefix = r'\b'
self.suffix = r'\b'
self.rule_data = {
're': self.expr
}
self.text = 'Some text'
self.data = {
'ruleset': 'Test ruleset',
'prefix': self.prefix,
'suffix': self.suffix,
'flags': 'I',
'rules': [
self.rule_data
]
}
self.ruleset = RegularExpressionRuleset(self.data, 'testdata')
def test_process(self):
result = self.ruleset.process(self.text)
self.assertEqual(result.lines, {1: self.text})
self.assertEqual(len(result.rules), 1)
processed_rule = result.rules[0]
self.assertEqual(processed_rule.rule.data, self.rule_data)
self.assertEqual(result.ruleset.data, self.data)
self.assertEqual(processed_rule.pattern_matches,
{
self.expr: {
1: [1, [(0, 4), (5, 9)]]
}
}
)
if __name__ == '__main__':
unittest.main()