Beispiel #1
0
 def test_logical_expression(self):
     assert Filter("0 and 0").visit_res() is False
     assert Filter("1 and 0").visit_res() is False
     assert Filter("1 and 1").visit_res() is True
     assert Filter("0 && 0").visit_res() is False
     assert Filter("1 or 1").visit_res() is True
     assert Filter("1 or 0").visit_res() is True
     assert Filter("1 || 0").visit_res() is True
     assert Filter("not 1").visit_res() is False
     assert Filter("not 0").visit_res() is True
     assert Filter("! 0").visit_res() is True
Beispiel #2
0
 def test_expression_1(self):
     rule = "a='综合工时' and b!='实习' "
     assert Filter(rule).visit_res({"a": "综合工时", "b": "实习"}) is False
     assert Filter(rule).visit_res({"a": "综合工时", "b": "全职"}) is True
     assert Filter(rule).visit_res({"a": "标准工时", "b": "实习"}) is False
     assert Filter(rule).visit_res({"a": "标准工时", "b": "全职"}) is False
     rule_1 = "not (a='综合工时' and b!='实习') "
     assert Filter(rule_1).visit_res({"a": "综合工时", "b": "实习"}) is True
     assert Filter(rule_1).visit_res({"a": "综合工时", "b": "全职"}) is False
     assert Filter(rule_1).visit_res({"a": "标准工时", "b": "实习"}) is True
     assert Filter(rule_1).visit_res({"a": "标准工时", "b": "全职"}) is True
Beispiel #3
0
 def test_compare_expression(self):
     assert Filter("3 > 2").visit_res() is True
     assert Filter("3 <= 2").visit_res() is False
     assert Filter("3 != 2").visit_res() is True
     assert Filter("3 == 2").visit_res() is False
     assert Filter("3 <= 2").visit_res() is False
     assert Filter("3 > 3").visit_res() is False
     assert Filter("3 > 2").visit_res() is True
Beispiel #4
0
    def text_expression_2(self, data):
        rule_1 = "wht==1 && et='实习'"
        res = Filter(rule_1).filter(data)
        assert self.cmp_list(list(res.items()), [100, 106])

        rule_2 = "not (wht==1 && et=='实习')"
        res = Filter(rule_2).filter(data)
        real_res = [num for num in range(100, 116)]
        real_res.remove(100)
        real_res.remove(106)
        assert self.cmp_list(list(res.items()), real_res)
Beispiel #5
0
 def test_parents_expr(self):
     assert Filter("not (not (not (1)))").visit_res() is False
     assert Filter("((1+2)*3 + (2+3)*5)*2").visit_res() == 68
     assert Filter("(4*5+2)%3").visit_res() == 1
     assert Filter("5 % 3 * (2 + 1)").visit_res() == 6
Beispiel #6
0
 def test_identifier(self):
     assert Filter("name").visit_res({"name": "jack"}) == "jack"
     assert Filter("age").visit_res({"age": 12}) == 12
     assert Filter("add1").visit_res({"add1": 30}) == 30
Beispiel #7
0
    def test_priority(self):
        assert Filter("1 + 2 * 4 + 2").visit_res() == 11
        assert Filter("3 * (4 + 5) / 3").visit_res() == 9
        assert Filter("1 + 3 > 2").visit_res() is True
        assert Filter("2 * 4 < 3").visit_res() is False
        assert Filter("0 and 1 or not 1").visit_res() is False
        assert Filter("(0 and 1) or (not 1)").visit_res() is False
        assert Filter("").visit_res() is True
        assert Filter("1 or 1 and 0").visit_res() is True

        assert Filter("not not not 1").visit_res() is False
        assert Filter("1 and 0 or 1 and 0").visit_res() is False

        assert Filter("not 1 or 1").visit_res() is True
        assert Filter("not (1 or 1)").visit_res() is False

        assert Filter("not 0 and 0").visit_res() is False
        assert Filter("not (0 and 0)").visit_res() is True

        assert Filter("1 + 3 and 0").visit_res() is False
        assert Filter("1 * 3 and 0").visit_res() is False

        assert Filter("3 > 1 and 0").visit_res() is False
        assert Filter("3 > 1 and 1").visit_res() is True
        assert Filter("3 > 1 or 0").visit_res() is True

        assert Filter("3 > 1 and 4 > 2").visit_res() is True
        assert Filter("3 + 4 < 5 and 2 + 1 < 2").visit_res() is False
        assert Filter("3 + 4 < 5 or 2 + 1 > 2").visit_res() is True
Beispiel #8
0
 def test_math_expression(self):
     assert Filter("1 + 2 + 4 * 5").visit_res() == 23
     assert Filter("3 * 5 + 4").visit_res() == 19
     assert Filter("1 + 1.5 + 3 + (2 + 4)*2 + 4*3").visit_res() == 29.5
     assert Filter("a + b * c +d").visit_res({"a": 1, "b": 3, "c": 3, "d": 4}) == 14
Beispiel #9
0
    def test_func(self):
        assert (Filter("1") + Filter("4")).visit_res() == 5
        assert (Filter("5") * Filter("4")).visit_res() == 20
        assert (Filter("1") / Filter("4")).visit_res() == 0.25
        assert (Filter("a") + Filter("4")).visit_res(a=8) == 12
        assert (Filter("2") * Filter("b")).visit_res(b=4) == 8

        assert (Filter("a") + Filter("b") + Filter("c")).visit_res(a=1,
                                                                   b=2,
                                                                   c=4) == 7

        assert (Filter("a + b") * Filter("c + d")).visit_res(a=1,
                                                             b=2,
                                                             c=3,
                                                             d=4) == 21
Beispiel #10
0
 def test_decimal_literal(self):
     assert Filter("3.44").visit_res() == 3.44
     assert Filter("3").visit_res() == 3
     assert Filter("3.0").visit_res() == 3.0
     assert Filter("4.455").visit_res() == 4.455
Beispiel #11
0
 def test_string_literal(self):
     assert Filter("\"jack\"").visit_res() == "jack"
     assert Filter("'jack'").visit_res() == "jack"
     assert Filter("'中文'").visit_res() == "中文"
Beispiel #12
0
    def test_var(self):
        assert Filter("a > b").visit_res({"a": 2, "b": 1}) is True
        assert Filter("a > b").visit_res({"a": 1, "b": 2}) is False
        assert Filter("a is b").visit_res({"a": 2, "b": 1}) is False
        assert Filter("a is not b").visit_res({"a": 2, "b": 1}) is True
        assert Filter("a is b").visit_res({"a": 1, "b": 1}) is True

        f = Filter("1")
        assert f.visit_res() == 1
        assert f.visit_res() == 1

        f = Filter("1 in (1, 2, 3)")
        assert f.visit_res() is True
        assert f.visit_res() is True
        assert f.visit_res() is True

        f = Filter("(1 + 1) in (a, b, 4, 6)")
        assert f.visit_res({"a": 2, "b": 5}) is True
        assert f.visit_res({"a": 7, "b": 5}) is False

        assert Filter("0 and a").visit_res({"a": 1}) is False
        assert Filter("1 and a").visit_res({"a": 1}) is True
        assert Filter("1 and a").visit_res({"a": 0}) is False

        assert Filter("1 or a").visit_res({"a": 1}) is True
        assert Filter("0 or a").visit_res({"a": 1}) is True
        assert Filter("0 or a").visit_res({"a": 0}) is False

        assert Filter("a and b or not c").visit_res({"a": 0, "b": 1, "c": 1}) is False
        assert Filter("(a and b) or (not c)").visit_res({"a": 0, "b": 1, "c": 1}) is False
        assert Filter("a or b and c").visit_res({"a": 1, "b": 1, "c": 0}) is True

        assert Filter("not not not a").visit_res({"a": 1}) is False
        assert Filter("a and b or c and d").visit_res({"a": 1, "b": 0, "c": 1, "d": 0}) is False

        assert Filter("not a or b").visit_res({"a": 1, "b": 1}) is True
        assert Filter("not (a or b)").visit_res({"a": 1, "b": 1}) is False

        assert Filter("not a and b").visit_res({"a": 0, "b": 0}) is False
        assert Filter("not (a and b)").visit_res({"a": 0, "b": 0}) is True

        assert Filter("1 + a and 0").visit_res({"a": 3}) is False
        assert Filter("a * b and c").visit_res({"a": 1, "b": 3, "c": 0}) is False

        assert Filter("a > b and c").visit_res({"a": 3, "b": 1, "c": 0}) is False
        assert Filter("a > 1 and b").visit_res({"a": 3, "b": 1}) is True
        assert Filter("a > 1 or b").visit_res({"a": 3, "b": 0}) is True

        assert Filter("a > 1 and 4 > b").visit_res({"a": 3, "b": 2}) is True
        assert Filter("a + 4 < 5 and b + 1 < c").visit_res({"a": 3, "b": 2, "c": 2}) is False
        assert Filter("a + 4 < b or 2 + c > 2").visit_res({"a": 3, "b": 5, "c": 1}) is True

        assert Filter("a xor b").visit_res({"a": 1, "b": 0}) is True

        assert Filter("a xor b").visit_res({"a": 1, "b": 1}) is False

        assert Filter("a + b").visit_res(value_map=None, a=1, b=4) == 5
Beispiel #13
0
 def test_not(self):
     assert Filter("1 is 1").visit_res() is True
     assert Filter("1 is not 1").visit_res() is False
     assert Filter("1 is not 2").visit_res() is True
Beispiel #14
0
 def test_nexted(self):
     FuncService.register("ad", lambda x, y: x + y)
     rule = "ad(1, ad(2, ad(c, ad(a, b))))"
     assert Filter(rule).visit_res({"a": 1, "b": 2, "c": 4}) == 10
     FuncService.unregister("ad")
Beispiel #15
0
 def test_in(self):
     rule = "a in (1, 2, 3, b, c, d)"
     assert Filter(rule).visit_res({"a": 1, "b": 4, "c": 5, "d": 6}) is True
     no_rule = "a not in (1, 2, 3, b, c, d)"
     assert Filter(no_rule).visit_res({"a": 1, "b": 4, "c": 5, "d": 6}) is False
Beispiel #16
0
    def test_or(self):
        assert (Filter("0") | Filter("1")).visit_res() is True
        assert (Filter("1") | Filter("0")).visit_res() is True
        assert (Filter("0") | Filter("0")).visit_res() is False
        assert (Filter("1") | Filter("1")).visit_res() is True

        assert (Filter("0") | Filter("a")).visit_res(a=0) is False
        assert (Filter("1") | Filter("a")).visit_res(a=0) is True
        assert (Filter("a") | Filter("1")).visit_res(a=0) is True
        assert (Filter("a") | Filter("0")).visit_res(a=1) is True

        assert (Filter("a") | Filter("b")).visit_res(a=0, b=2) is True
Beispiel #17
0
 def test_func_call(self):
     FuncService.register("add3", lambda x, y, z: x + y + z)
     assert Filter("add3(1, 2, 3)").visit_res() == 6
     assert Filter("add3(b, 4, c)").visit_res({"b": 4, "c": 5}) == 13
     FuncService.unregister("div4")
Beispiel #18
0
    def test_and(self):
        assert (Filter("0") & Filter("1")).visit_res() is False
        assert (Filter("0") & Filter("0")).visit_res() is False
        assert (Filter("1") & Filter("1")).visit_res() is True
        assert (Filter("1") & Filter("0")).visit_res() is False

        assert (Filter("a") & Filter("0")).visit_res(a=1) is False
        assert (Filter("a") & Filter("1")).visit_res(a=1) is True
        assert (Filter("1") & Filter("a")).visit_res(a=0) is False
        assert (Filter("0") & Filter("a")).visit_res(a=0) is False
        assert (Filter("a") & Filter("b")).visit_res(a=1, b=2) is True
        assert (Filter("a") & Filter("b")).visit_res(a=1, b=0) is False