def test_reparse_on_scope_change(self): self.top.comp.x = 99.5 self.top.comp.y = -3.14 ex = ExprEvaluator('comp.x', self.top) self.assertEqual(99.5, ex.evaluate()) self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") ex.scope = self.top.a try: ex.set(0.5) except AttributeError as err: self.assertEqual(str(err), "a: object has no attribute 'comp.x'") else: self.fail("AttributeError expected") self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") self.assertEqual(99.5, ex.evaluate(self.top)) # set scope back to self.top self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") ex.text = 'comp.y' try: ex.evaluate(self.top.a) except AttributeError as err: self.assertEqual(str(err), "can't evaluate expression 'comp.y': a: object has no attribute 'comp.y'") else: self.fail("AttributeError expected") ex.scope = self.top ex.set(11.1) self.assertEqual(11.1, self.top.comp.y) self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.y')")
def test_reparse_on_scope_change(self): self.top.comp.x = 99.5 self.top.comp.y = -3.14 ex = ExprEvaluator('comp.x', self.top) self.assertEqual(99.5, ex.evaluate()) self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") ex.scope = self.top.a try: ex.set(0.5) except AttributeError as err: self.assertEqual(str(err), "a: object has no attribute 'comp.x'") else: self.fail("AttributeError expected") self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") self.assertEqual(99.5, ex.evaluate(self.top)) # set scope back to self.top self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.x')") ex.text = 'comp.y' try: ex.evaluate(self.top.a) except AttributeError as err: self.assertEqual( str(err), "can't evaluate expression 'comp.y': a: 'A' object has no attribute 'comp'" ) else: self.fail("AttributeError expected") ex.scope = self.top ex.set(11.1) self.assertEqual(11.1, self.top.comp.y) self.assertEqual(self._ast_to_text(ex._parse()), "scope.get('comp.y')")
def _do_tests(self, tests, top): # each test is a tuple of the form (input, expected output) for tst in tests: ex = ExprEvaluator(tst[0], top) self.assertEqual(self._ast_to_text(ex._parse()), tst[1])