validation.py 761 B

1234567891011121314151617181920212223242526272829
  1. from parser import Parser
  2. from possibilities import apply_suggestion
  3. from strategy import find_possibilities
  4. from tests.parser import ParserWrapper
  5. def validate(exp, result):
  6. """
  7. Validate that exp =>* result.
  8. """
  9. parser = ParserWrapper(Parser)
  10. exp = parser.run([exp])
  11. result = parser.run([result])
  12. def traverse_preorder(node, result):
  13. if node.equals(result):
  14. return True
  15. for p in find_possibilities(node):
  16. # Clone the root node because it will be used in multiple
  17. # substitutions
  18. child = apply_suggestion(node.clone(), p)
  19. if traverse_preorder(child, result):
  20. return True
  21. return False
  22. return traverse_preorder(exp, result)