Restarting the project.
Fixing unit tests. Continuing SyaParser
This commit is contained in:
@@ -3,91 +3,92 @@ import pytest
|
||||
from base import BaseTest
|
||||
from conftest import NewOntology, comparable_tokens
|
||||
from evaluators.base_evaluator import MultipleChoices
|
||||
from helpers import _mt, get_concept, get_concepts, get_parser_input
|
||||
from helpers import get_concept, get_concepts, get_parser_input
|
||||
from parsers.SyaConceptsParser import SyaConceptsParser
|
||||
from parsers.tokenizer import Tokenizer
|
||||
from tests.parsers.conftest import _mtsya
|
||||
|
||||
|
||||
class TestSyaConceptsParser(BaseTest):
|
||||
|
||||
@pytest.fixture()
|
||||
def parser(self):
|
||||
return SyaConceptsParser()
|
||||
|
||||
@pytest.mark.parametrize("concept_key, expected_list", [
|
||||
["a long token name", [("a long token name", 0)]],
|
||||
["__var__0 __var__1 __var__2", [("", 3)]],
|
||||
["__var__0 __var__1 prefixed", [(" prefixed", 2)]],
|
||||
["suffixed __var__0 __var__1", [("suffixed ", 0), ["", 2]]],
|
||||
["__var__0 __var__1 infixed __var__0 __var__1", [(" infixed ", 2), ["", 2]]],
|
||||
["if __var__0 __var__1 then __var__2 end", [("if ", 0), (" then ", 2), (" end", 1)]]
|
||||
])
|
||||
def test_i_can_initialize_expected_parameters(self, parser, concept_key, expected_list):
|
||||
resolved_expected_list = [(list(Tokenizer(source, yield_eof=False)), nb) for source, nb in expected_list]
|
||||
actual = parser._get_expected_tokens(concept_key)
|
||||
|
||||
with comparable_tokens():
|
||||
assert actual == resolved_expected_list
|
||||
|
||||
@pytest.mark.parametrize("concept", [
|
||||
get_concept("a plus b", variables=["a", "b"]),
|
||||
get_concept("add a b", variables=["a", "b"]),
|
||||
get_concept("a b add", variables=["a", "b"]),
|
||||
])
|
||||
def test_i_can_parse_a_simple_case(self, context, parser, concept):
|
||||
with NewOntology(context, "test_i_can_parse_a_simple_case"):
|
||||
get_concepts(context, concept, use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 plus 2")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [_mt("1001", a="1 ", b=" 2")]
|
||||
assert res == MultipleChoices([expected])
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_parse_long_names_concept(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_parse_a_simple_case"):
|
||||
get_concepts(context, get_concept("a long named concept b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 long named concept 2")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [_mt("1001", a="1 ", b=" 2")]
|
||||
assert res == MultipleChoices([expected])
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_parse_sequence(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_parse_sequence"):
|
||||
get_concepts(context, get_concept("a plus b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 plus 2 3 plus 7")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [[_mt("1001", a="1 ", b=" 2")], [_mt("1001", a=" 3 ", b=" 7")]]
|
||||
assert res == MultipleChoices(expected)
|
||||
assert not error_sink
|
||||
|
||||
def test_not_enough_parameters(self, context, parser):
|
||||
with NewOntology(context, "test_not_enough_parameters"):
|
||||
get_concepts(context, get_concept("a plus b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 plus 2 3 plus 7")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [[_mt("1001", a="1 ", b=" 2")], [_mt("1001", a=" 3 ", b=" 7")]]
|
||||
assert res == MultipleChoices(expected)
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_detect_when_name_does_not_match(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_detect_when_name_does_not_match"):
|
||||
get_concepts(context, get_concept("a long named concept b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 long named mismatch 2")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
assert error_sink
|
||||
|
||||
@pytest.fixture()
|
||||
def parser(self):
|
||||
return SyaConceptsParser()
|
||||
|
||||
@pytest.mark.parametrize("concept_key, expected_list", [
|
||||
["a long token name", [("a long token name", 0)]],
|
||||
["__var__0 __var__1 __var__2", [("", 3)]],
|
||||
["__var__0 __var__1 prefixed", [(" prefixed", 2)]],
|
||||
["suffixed __var__0 __var__1", [("suffixed ", 0), ["", 2]]],
|
||||
["__var__0 __var__1 infixed __var__0 __var__1", [(" infixed ", 2), ["", 2]]],
|
||||
["if __var__0 __var__1 then __var__2 end", [("if ", 0), (" then ", 2), (" end", 1)]]
|
||||
])
|
||||
def test_i_can_initialize_expected_parameters(self, parser, concept_key, expected_list):
|
||||
resolved_expected_list = [(list(Tokenizer(source, yield_eof=False)), nb) for source, nb in expected_list]
|
||||
actual = parser._get_expected_tokens(concept_key)
|
||||
|
||||
with comparable_tokens():
|
||||
assert actual == resolved_expected_list
|
||||
|
||||
@pytest.mark.parametrize("concept, _input", [
|
||||
(get_concept("a plus b", variables=["a", "b"]), "1 plus 2"),
|
||||
(get_concept("add a b", variables=["a", "b"]), "add 1 2"),
|
||||
(get_concept("a b add", variables=["a", "b"]), "1 2 add")
|
||||
])
|
||||
def test_i_can_parse_a_simple_case(self, context, parser, concept, _input):
|
||||
with NewOntology(context, "test_i_can_parse_a_simple_case"):
|
||||
get_concepts(context, concept, use_sheerka=True)
|
||||
|
||||
pi = get_parser_input(_input)
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [_mtsya("1001", a="1 ", b=" 2")]
|
||||
assert res == MultipleChoices([expected])
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_parse_long_names_concept(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_parse_a_simple_case"):
|
||||
get_concepts(context, get_concept("a long named concept b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 long named concept 2")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [_mtsya("1001", a="1 ", b=" 2")]
|
||||
assert res == MultipleChoices([expected])
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_parse_sequence(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_parse_sequence"):
|
||||
get_concepts(context, get_concept("a plus b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 plus 2 3 plus 7")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [[_mtsya("1001", a="1 ", b=" 2")], [_mtsya("1001", a=" 3 ", b=" 7")]]
|
||||
assert res == MultipleChoices(expected)
|
||||
assert not error_sink
|
||||
|
||||
def test_not_enough_parameters(self, context, parser):
|
||||
with NewOntology(context, "test_not_enough_parameters"):
|
||||
get_concepts(context, get_concept("a plus b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 plus ")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
expected = [[_mtsya("1001", a="1 ", b=" 2")], [_mtsya("1001", a=" 3 ", b=" 7")]]
|
||||
assert res == MultipleChoices(expected)
|
||||
assert not error_sink
|
||||
|
||||
def test_i_can_detect_when_name_does_not_match(self, context, parser):
|
||||
with NewOntology(context, "test_i_can_detect_when_name_does_not_match"):
|
||||
get_concepts(context, get_concept("a long named concept b", variables=["a", "b"]), use_sheerka=True)
|
||||
|
||||
pi = get_parser_input("1 long named mismatch 2")
|
||||
error_sink = []
|
||||
res = parser.parse(context, pi, error_sink)
|
||||
|
||||
assert error_sink
|
||||
|
||||
Reference in New Issue
Block a user