import ast import pytest import core.builtin_helpers from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka class TestBuiltinHelpers(TestUsingMemoryBasedSheerka): def test_i_can_use_expect_one_when_empty(self): sheerka = self.get_sheerka() res = core.builtin_helpers.expect_one(self.get_context(sheerka), []) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.IS_EMPTY) def test_i_can_use_expect_one_when_too_many_success(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", True, "value1"), ReturnValueConcept("who", True, "value2"), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_SUCCESS) assert res.value.body == items assert res.parents == items def test_i_can_use_expect_one_when_same_success(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", True, "value"), ReturnValueConcept("who", True, "value"), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert res.status assert res.value == items[0].value assert res.parents == items def test_i_can_use_expect_when_only_errors_1(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", False, sheerka.new(BuiltinConcepts.ERROR)), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert not res.status assert res.value, items[0] assert res.parents == items def test_i_can_use_expect_when_only_errors_2(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", False, None), ReturnValueConcept("who", False, None), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS) assert res.value.body == items assert res.parents == items def test_i_can_use_expect_one_when_one_success_1(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", True, None), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert res.status assert res.body == items[0].body assert res.parents == items def test_i_can_use_expect_one_when_one_success_2(self): sheerka = self.get_sheerka() items = [ ReturnValueConcept("who", False, None), ReturnValueConcept("who", True, None), ReturnValueConcept("who", False, None), ] res = core.builtin_helpers.expect_one(self.get_context(sheerka), items) assert res.status assert res.body == items[1].body assert res.parents == items def test_i_can_use_expect_one_when_not_a_list_true(self): sheerka = self.get_sheerka() item = ReturnValueConcept("who", True, None) res = core.builtin_helpers.expect_one(self.get_context(sheerka), item) assert res.status assert res == item def test_i_can_use_expect_one_when_not_a_list_false(self): sheerka = self.get_sheerka() item = ReturnValueConcept("who", False, None) res = core.builtin_helpers.expect_one(self.get_context(sheerka), item) assert not res.status assert res == item @pytest.mark.parametrize("expression, vars_to_include, vars_to_exclude, expected_expr", [ ("a == 1", [], [], []), ("a == 1", ["a"], [], ["a == 1"]), ("a == 1", [], ["a"], []), ("predicate(a)", [], [], []), ("predicate(a)", ["a"], [], ["predicate(a)"]), ("predicate(a, b)", ["a"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["b"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["a", "b"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["a"], ["b"], []), ("a + b == 1", [], [], []), ("a + b == 1", ["a"], [], ["a + b == 1"]), ("a + b == 1", ["a"], ["b"], []), ("a + b == 1", ["b"], [], ["a + b == 1"]), ("a + b == 1", ["a", "b"], [], ["a + b == 1"]), ("a == 1 and b == 2", [], [], []), ("a == 1 and b == 2", ["a"], [], ["a == 1"]), ("a == 1 and b == 2", ["b"], [], ["b == 2"]), ("a == 1 and b == 2", ["a"], ["b"], ["a == 1"]), ("a == 1 and b == 2", ["a", "b"], [], ["a == 1 and b == 2"]), ("predicate(a,c) and predicate(b,c)", ["a", "b"], [], ["predicate(a,c) and predicate(b,c)"]), ("not(a == 1)", [], [], []), ("not(a == 1)", ["a"], [], ["not(a==1)"]), ("a == 1 or b == 2", [], [], []), ("a == 1 or b == 2", ["a"], [], ["a == 1"]), ("a == 1 or b == 2", ["b"], [], ["b == 2"]), ("a == 1 or b == 2", ["a", "b"], [], ["a == 1 or b == 2"]), ("predicate(a,c) or predicate(b,c)", ["a", "b"], [], ["predicate(a,c) or predicate(b,c)"]), ("a < 1 and a > b", ["a"], [], ["a < 1 and a > b"]), ]) def test_i_can_extract_predicates(self, expression, vars_to_include, vars_to_exclude, expected_expr): sheerka = self.get_sheerka() expected = [ast.parse(expr, mode="eval") for expr in expected_expr] actual = core.builtin_helpers.extract_predicates(sheerka, expression, vars_to_include, vars_to_exclude) assert len(actual) == len(expected) for i in range(len(actual)): assert self.dump_ast(actual[i]) == self.dump_ast(expected[i])