-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
99 lines (89 loc) · 2.62 KB
/
tests.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ilsp.common.podmienka import PodmienkaASTGroup as G, PodmienkaASTLiteral as L, PodmienkaASTEmpty as E, Podmienka
good_inputs = [
('', [], E()),
('1', [1], L(1)),
('123', [123], L(123)),
('123 OR 456', [123, 'OR', 456], G('OR', L(123), L(456))),
('(123 OR 456)', ['(', 123, 'OR', 456, ')'], G('OR', L(123), L(456))),
('(123 OR 456 OR 567)', ['(', 123, 'OR', 456, 'OR', 567, ')'], G('OR', L(123), L(456), L(567))),
('123 OR 456 OR 567', [123, 'OR', 456, 'OR', 567], G('OR', L(123), L(456), L(567))),
('123 AND 456', [123, 'AND', 456], G('AND', L(123), L(456))),
('(123 AND 456) OR (234 AND 567)', ['(', 123, 'AND', 456, ')', 'OR', '(', 234, 'AND', 567, ')'],
G('OR', G('AND', L(123), L(456)), G('AND', L(234), L(567)))
),
('((123 AND 456) AND (321 AND 654)) OR (234 AND 567)',
['(', '(', 123, 'AND', 456, ')', 'AND', '(', 321, 'AND', 654, ')', ')', 'OR', '(', 234, 'AND', 567, ')'],
G('OR', G('AND', G('AND', L(123), L(456)), G('AND', L(321), L(654))), G('AND', L(234), L(567)))
),
]
bad_inputs = [
'a',
'OR',
'(',
')',
'()',
'(AND)',
')(',
'(1 AND)',
'(1 AND',
'(1 1)',
'1 2',
]
vyhodnot = [
('', [], True),
('', [1], True),
('1', [1], True),
('1', [2], False),
('1 OR 2', [], False),
('1 OR 2', [1], True),
('1 OR 2', [2], True),
('1 OR 2', [1, 2], True),
('1 OR 2', [1, 2, 3], True),
('1 AND 2', [], False),
('1 AND 2', [1], False),
('1 AND 2', [2], False),
('1 AND 2', [1, 2], True),
('1 AND 2', [1, 2, 3], True),
]
def check_podmienka(text, tokens, ast):
p = Podmienka(text)
try:
assert tokens == p._tokens
except AssertionError:
print '{!r} == {!r}'.format(tokens, p._tokens)
raise
try:
assert ast == p._ast
except AssertionError:
print '{!r} == {!r}'.format(ast, p._ast)
raise
canonical_repr = text.replace('(', '( ').replace(')', ' )')
serialized = p.serialize()
try:
assert canonical_repr == serialized
except AssertionError:
print '{!r} == {!r}'.format(canonical_repr, serialized)
raise
def test_podmienka():
for text, tokens, ast in good_inputs:
yield check_podmienka, text, tokens, ast
def check_podmienka_exc(text):
try:
Podmienka(text)
assert False
except ValueError:
pass
def test_podmienka_exc():
for text in bad_inputs:
yield check_podmienka_exc, text
def check_vyhodnot(podm, predmety, expected):
p = Podmienka(podm)
assert p.vyhodnot(predmety) == expected
def test_vyhodnot():
for podm, predmety, expected in vyhodnot:
yield check_vyhodnot, podm, predmety, expected
if __name__ == "__main__":
import nose
nose.main()