-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stringbooleanexpression.py
More file actions
119 lines (94 loc) · 6.34 KB
/
test_stringbooleanexpression.py
File metadata and controls
119 lines (94 loc) · 6.34 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
from unittest import TestCase
from stringbooleanexpression import StringBooleanExpression
class TestStringBooleanExpression(TestCase):
def test_check(self):
# Test the operators
less_than = StringBooleanExpression("F$salary<<10")
self.assertTrue(less_than.check({"salary": -10}), "Less than operator check")
self.assertTrue(less_than.check({"salary": 1}), "Less than operator check")
self.assertFalse(less_than.check({"salary": 10}), "Less than operator check")
self.assertFalse(less_than.check({"salary": 100}), "Less than operator check")
less_than_or_equal = StringBooleanExpression("F$salary<=10")
self.assertTrue(less_than_or_equal.check({"salary": 1}), "Less than or equal operator check")
self.assertTrue(less_than_or_equal.check({"salary": 10}), "Less than or equal operator check")
self.assertFalse(less_than_or_equal.check({"salary": 100}), "Less than or equal operator check")
greater_than = StringBooleanExpression("F$salary>>10")
self.assertFalse(greater_than.check({"salary": 1}), "Greater than operator check")
self.assertFalse(greater_than.check({"salary": 10}), "Greater than operator check")
self.assertTrue(greater_than.check({"salary": 100}), "Greater than operator check")
greater_than_or_equal = StringBooleanExpression("F$salary>=10")
self.assertFalse(greater_than_or_equal.check({"salary": 1}), "Greater than or equal operator check")
self.assertTrue(greater_than_or_equal.check({"salary": 10}), "Greater than or equal operator check")
self.assertTrue(greater_than_or_equal.check({"salary": 100}), "Greater than or equal operator check")
not_equal = StringBooleanExpression("F$salary!=10")
self.assertTrue(not_equal.check({"salary": 1}), "Not equal operator check")
self.assertFalse(not_equal.check({"salary": 10}), "Not equal operator check")
self.assertTrue(not_equal.check({"salary": 100}), "Not equal operator check")
equal = StringBooleanExpression("F$salary==10")
self.assertFalse(equal.check({"salary": 1}), "Equal operator check")
self.assertTrue(equal.check({"salary": 10}), "Equal operator check")
self.assertFalse(equal.check({"salary": 100}), "Equal operator check")
# Testing the check functionality
simple_example = StringBooleanExpression("S$name==bob")
self.assertFalse(simple_example.check({}), "An empty input should return false")
self.assertFalse(simple_example.check({"name": ""}), "Does not match expression requirement")
self.assertTrue(simple_example.check({"name": "bob"}), "Matches simple condition")
simple_reverse_example = StringBooleanExpression("bob==S$name")
self.assertFalse(simple_reverse_example.check({}), "An empty input should return false")
self.assertFalse(simple_reverse_example.check({"name": ""}), "Does not match expression requirement")
self.assertTrue(simple_reverse_example.check({"name": "bob"}), "Matches simple condition")
complex_example = StringBooleanExpression(
"something with a space==S$name||"
"(S$name==bob||jim==S$name||(EMPTY_STRING==S$name&&S$name==EMPTY_STRING&&F$salary>=10.5))")
self.assertFalse(complex_example.check({"name": "", "salary": 0}), "Checking complex false case")
self.assertTrue(complex_example.check({"name": "", "salary": 10.5}), "Checking complex true case")
self.assertTrue(complex_example.check({"name": "", "salary": 1000}), "Checking complex true case")
self.assertFalse(complex_example.check({"name": "", "salary": 0}), "Checking complex false case")
self.assertTrue(complex_example.check({"name": "bob", "salary": 0}), "Checking complex true case")
self.assertTrue(complex_example.check({"name": "jim", "salary": 0}), "Checking complex true case")
self.assertTrue(complex_example.check({"name": "something with a space", "salary": 0}),
"Checking complex space true case")
def test__set_up_function(self):
# Testing the setup function
variables = ["var_one", "var_two"]
example_equal_expression = "var_one == var_two"
function = StringBooleanExpression._set_up_function(example_equal_expression, variables, variable_wrap="")
self.assertTrue(function("a", "a"))
self.assertFalse(function("a", "b"))
def test__parse_input(self):
# Testing the parse function
input_string = "S$var_one==var_two||S$var_one==TESTING"
output_parsed_string, variables = StringBooleanExpression._parse_input(input_string, {"TESTING": "an example"})
self.assertTrue("var_one" in variables)
self.assertTrue("TESTING" not in variables)
self.assertTrue("TESTING" not in output_parsed_string)
self.assertTrue("an example" in output_parsed_string)
def test__handle_comparison(self):
# Testing handle comparison
input_string = "S$var_one==var_two||S$var_one==TESTING"
output_parsed_string, variables = StringBooleanExpression._handle_comparison(input_string, "==", "GARBAGE",
{"TESTING": "an example"})
self.assertTrue("var_one" in variables)
self.assertTrue("var_two" not in variables)
self.assertTrue("==" not in output_parsed_string)
self.assertTrue("GARBAGE" in output_parsed_string)
self.assertTrue("an example" in output_parsed_string)
self.assertTrue("TESTING" not in output_parsed_string)
def test__wrap_string(self):
wrap = "wrapping"
not_wrapped = "value"
expected_wrap = wrap + not_wrapped + wrap
self.assertTrue(StringBooleanExpression._wrap_string(not_wrapped, wrap, True) == expected_wrap)
self.assertTrue(StringBooleanExpression._wrap_string(not_wrapped, wrap, False) == not_wrapped)
def test__check_for_invalid(self):
invalid = "eval()"
valid = "S$var_one==var_two||S$var_one==TESTING"
try:
StringBooleanExpression._check_for_invalid(invalid)
self.fail("Failed to find invalid sequence in input")
except ValueError:
pass
try:
StringBooleanExpression._check_for_invalid(valid)
except ValueError:
self.fail("Incorrectly threw exception on valid string")