-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint_tests.py
More file actions
203 lines (167 loc) · 5.45 KB
/
constraint_tests.py
File metadata and controls
203 lines (167 loc) · 5.45 KB
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
# Unit Testing
import unittest
from pdb import set_trace as D
from constraint import *
class ConstraintTestCase(unittest.TestCase):
def match(self, constraint, tokens):
return self.assertTrue(
match(constraint, tokens)
)
def nomatch(self, constraint, tokens):
return self.assertFalse(match(constraint, tokens))
class TestExamples(ConstraintTestCase):
def setUp(self):
# Define common character classes.
import string
self.letters = Member(string.ascii_letters)
self.digits = Member(string.digits)
self.punctuation = Member(string.punctuation)
def testCompoundExamples(self):
c = And(Ascending(), Unique())
good = 'abefgz'
bad = 'aaaabcdefg'
self.match(c, good)
self.nomatch(c, bad)
def testName(self):
_alpha = Or(Member('_'), self.letters)
_alpha_num = Or(_alpha, self.digits)
first_char = And(Single(), _alpha)
c = Sequence(first_char, _alpha_num)
self.match(first_char, '_')
self.match(first_char, 'A')
self.match(first_char, 'b')
self.nomatch(first_char, '')
self.nomatch(first_char, '5')
self.nomatch(first_char, '$')
self.nomatch(first_char, 'xx')
self.match(_alpha_num, '')
self.match(_alpha_num, '123')
self.match(_alpha_num, 'abc')
self.nomatch(_alpha_num, '@#$')
self.match(_alpha_num, 'x81x2')
self.match(_alpha_num, '_3_1_ssa_1_')
self.match(c, '_test')
self.match(c, 'Blah')
self.match(c, 'bLAH')
self.match(c, 'a1')
self.match(c, '_B_2_23')
self.nomatch(c, '12C')
self.nomatch(c, '#$asdf')
self.nomatch(c, 'cat!')
class TestBasic(ConstraintTestCase):
def testNull(self):
self.match( Null(), [] )
self.nomatch( Null(), [1] )
def testAny(self):
c = Any()
self.match(c, [] )
self.match(c, [1, 2, 3] )
self.match(c, [1, 2, 3] * 3 )
self.match(c, range(100) )
self.match(c, 'abcdef' )
def testMember(self):
nine, ten = range(9), range(10)
self.match(Member(ten), nine)
self.nomatch(Member(nine), ten)
def testBetween(self):
c = Between(1,6)
self.match(c, [1,2,3,4,5,6])
self.nomatch(c, [0])
self.nomatch(c, [7])
def testRepeat(self):
c = Repeat(min=1,max=3)
self.nomatch(c, '')
self.match(c, 'a')
self.match(c, 'ab')
self.match(c, 'abc')
self.nomatch(c, 'abcd')
c = Repeat(min=2,max=None)
self.nomatch(c, '')
self.nomatch(c, 'a')
self.match(c, 'ab')
self.match(c, 'abc')
self.match(c, 'abcd')
def testUnique(self):
c = Unique()
self.match(c, 'abcdefghijklmno9231')
self.nomatch(c, 'abca')
def testRange(self):
c = Range(3,15,2)
self.match(c, [3,5,7,9,11,13])
def testAnd(self):
c = And(Null())
self.match(c, [])
self.nomatch(c, [1])
c = And(Any(), Any())
self.match(c, [])
self.match(c, [1])
self.match(c, [1,1])
c = And(Null(), Any())
self.match(c, [])
self.nomatch(c, [1])
self.nomatch(c, [1,1])
c = And(Repeat(1,2), Member('abc'))
self.match(c, 'a')
self.match(c, 'bc')
self.nomatch(c, 'abc')
self.nomatch(c, '')
def testOr(self):
c = Or(Repeat(1,1), Repeat(3,4))
self.nomatch(c, '')
self.match(c, 'a')
self.nomatch(c, 'ab')
self.match(c, 'abc')
self.match(c, 'abcd')
self.nomatch(c, 'abcde')
def testGroup(self):
digits = Between('0','9')
dashes = Member('-')
digit = And(Single(), digits)
dash = And(Single(), dashes)
self.nomatch(dash, '')
self.match(dash, '-')
self.nomatch(dash, '--')
self.nomatch(dash, 'x')
areacode = And(Repeat(min=3, max=4), digits)
self.nomatch(areacode, '')
self.nomatch(areacode, '1')
self.nomatch(areacode, '12')
self.match(areacode, '123')
self.match(areacode, '1234')
phone1 = Group(digits, dashes)
self.match(phone1, '123-456-7890')
phone2 = Group(digit, dash)
self.match(phone2, '123-456-7890')
phone3 = Group(areacode, dash)
self.match(phone3, '123-456-7890')
phone4 = Group(areacode, dash, meta=Alternate())
self.match(phone4, '123-456-7890')
def testAlternate(self):
c = Alternate()
self.match(c, '')
self.match(c, 'a')
self.match(c, 'ab')
self.match(c, 'abababa')
self.match(c, 'abcbabcabacb')
self.nomatch(c, 'aa')
self.nomatch(c, 'bacc')
def testAscending(self):
good = [1,2,2,3,3,4,5,6,7]
bad = [1,2,3,0]
self.match(Ascending(), good)
self.nomatch(Ascending(), bad)
good = 'aaaabcdefg'
bad = 'xyza'
self.match(Ascending(), good)
self.nomatch(Ascending(), bad)
def testAttribute(self):
from fractions import Fraction as F
c = Attribute('denominator', Member([1]))
self.match(c, [1,2,34,53,2])
self.nomatch(c, [F(1,2), F(3,4), F(7,8)])
def testKey(self):
c = Key('x', Member([True]))
self.match(c, [dict(x=True)])
self.nomatch(c, [dict(x=False)])
if __name__ == '__main__':
unittest.main()