def test_parse(self): data = [ (('A',), ('A',)), (('A', '+', 'B', '*', 'C'), ('A', 'B', 'C', '*', '+')), (('A', '/', 'B', '*', 'C'), ('A', 'B', '/', 'C', '*')), (('A', '*', 'B', '+', 'C'), ('A', 'B', '*', 'C', '+')), (('A', '*', '(', 'B', '+', 'C', ')'), ('A', 'B', 'C', '+', '*')), (('(', 'A', '-', 'B', ')', '+', 'C'), ('A', 'B', '-', 'C', '+')), (('10', '/', '(', '1.0', '-', '0.5', ')'), ('10', '1.0', '0.5', '-', '/')) ] for tokens, result in data: self.assertEqual(Formula.parse(tokens), result)
def test(self, formula, string, cases): print("Testing %s" % (string)) parsed = Formula.parse(string) self.compare(parsed.toString(), string, "Formula.parse(XXX).toString() == XXX") # This expects subf() to work correctly, if it doesn't # we still will catch the difference in toplevel formulas # or evals below. self.compareFormulas(parsed, formula) for interpretation, result in cases: print(" interpretation %s:" % (repr(interpretation),)) self.compare(parsed.eval(interpretation), result, "eval")