Added basic validation functionality.

parent 566a7f5f
from src.parser import Parser from src.parser import Parser
from tests.parser import ParserWrapper from tests.parser import ParserWrapper
from src.possibilities import apply_suggestion
class ValidationNode(object):
pass
def validate(exp, result): def validate(exp, result):
...@@ -11,23 +8,25 @@ def validate(exp, result): ...@@ -11,23 +8,25 @@ def validate(exp, result):
Validate that exp =>* result. Validate that exp =>* result.
""" """
parser = ParserWrapper(Parser) parser = ParserWrapper(Parser)
exp = parser.run([exp])
result = parser.run([result]) result = parser.run([result])
return validate_graph(exp, result) return traverse_preorder(parser, exp, result)
def iter_preorder(exp, possibility): def traverse_preorder(parser, exp, result):
""" """
Traverse the possibility tree using pre-order traversal. Traverse the possibility tree using pre-order traversal.
""" """
pass root = parser.run([exp])
if root.equals(result):
return root
def validate_graph(exp, result): possibilities = parser.parser.possibilities
"""
Validate that "exp" =>* "result". for p in possibilities:
""" child = apply_suggestion(root, p)
# TODO: Traverse the tree of possibility applications next_root = traverse_preorder(parser, str(child), result)
return False
if next_root:
return next_root
from unittest import TestCase
from src.validation import validate
class TestValidation(TestCase):
def test_simple_success(self):
self.assertTrue(validate('3a+a', '4a'))
def test_simple_failure(self):
self.assertFalse(validate('3a+a', '4a+1'))
def test_intermediate_success(self):
self.assertTrue(validate('3a+a+b+2b', '4a+3b'))
def test_intermediate_failure(self):
self.assertFalse(validate('3a+a+b+2b', '4a+4b'))
#def test_intermediate_failure(self):
# self.assertFalse(validate('(x-1)^3+(x-1)^3', '4a+4b'))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment