219 lines
9.0 KiB
Python
219 lines
9.0 KiB
Python
import ast
|
|
|
|
import core.builtin_helpers
|
|
import pytest
|
|
from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts
|
|
from core.concept import Concept
|
|
|
|
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].body
|
|
|
|
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
|
|
|
|
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])
|
|
|
|
@pytest.mark.parametrize("concepts, expected", [
|
|
([], []),
|
|
([Concept("foo", pre="False"), Concept("bar")], ["bar"]),
|
|
([Concept("foo", pre="True"), Concept("bar")], ["foo"]),
|
|
([Concept("foo").def_var("a"), Concept("bar")], ["bar"]), # less variables is better
|
|
([Concept("foo"), Concept("bar")], ["foo", "bar"]),
|
|
([Concept("foo", pre="is_question()"), Concept("bar")], ["bar"]),
|
|
])
|
|
def test_i_can_resolve_ambiguity_when_empty(self, concepts, expected):
|
|
context = self.get_context()
|
|
res = core.builtin_helpers.resolve_ambiguity(context, concepts)
|
|
assert [c.name for c in res] == expected
|
|
|
|
# @pytest.mark.parametrize("return_values", [
|
|
# None,
|
|
# []
|
|
# ])
|
|
# def test_i_can_resolve_simple_ambiguity_when_no_return_values(self, return_values):
|
|
# context = self.get_context()
|
|
#
|
|
# assert core.builtin_helpers.remove_ambiguity(context, return_values) == return_values
|
|
|
|
# def test_resolve_ambiguity_concepts_with_no_variable_take_precedence(self):
|
|
# context = self.get_context()
|
|
# return_values = [
|
|
# self.pretval(Concept("hello a").def_var("a", "world"), "hello word"),
|
|
# self.pretval(Concept("hello world"), "hello word"),
|
|
# # self.pretval(Concept("hello world", pre="False"), "hello word"),
|
|
# self.retval(Concept("not a parser result")),
|
|
# self.retval(Concept("status is false"), status=False),
|
|
# self.pretval(Concept("false parser result"), status=False),
|
|
# ]
|
|
#
|
|
# ret = core.builtin_helpers.remove_ambiguity(context, return_values)
|
|
# assert ret.status
|
|
# assert ret.parents == return_values
|
|
#
|
|
# filtered = ret.body
|
|
# assert context.sheerka.isinstance(ret.body, BuiltinConcepts.FILTERED)
|
|
# assert filtered.body == [
|
|
# return_values[2],
|
|
# return_values[3],
|
|
# return_values[4],
|
|
# return_values[1],
|
|
# ]
|
|
# assert filtered.iterable == return_values
|
|
# assert filtered.predicate == "remove_ambiguity(context, iterable)"
|
|
#
|
|
# def test_resolve_ambiguity_failed_pre_condition_are_discarded(self):
|
|
# context = self.get_context()
|
|
# return_values = [
|
|
# self.pretval(Concept("hello world"), "hello word"),
|
|
# self.pretval(Concept("hello world", pre="False"), "hello word"),
|
|
# ]
|
|
#
|
|
# ret = core.builtin_helpers.remove_ambiguity(context, return_values)
|
|
# filtered = ret.body
|
|
# assert context.sheerka.isinstance(ret.body, BuiltinConcepts.FILTERED)
|
|
# assert filtered.body == [
|
|
# return_values[0],
|
|
# ]
|
|
#
|
|
# def test_resolve_ambiguity_original_return_value_is_returned_when_nothing_to_filter(self):
|
|
# context = self.get_context()
|
|
# return_values = [
|
|
# self.pretval(Concept("hello a").def_var("a", "world"), "hello word"),
|
|
# self.retval(Concept("not a parser result")),
|
|
# self.retval(Concept("status is false"), status=False),
|
|
# self.pretval(Concept("false parser result"), status=False),
|
|
# ]
|
|
#
|
|
# assert core.builtin_helpers.remove_ambiguity(context, return_values) == return_values
|