Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- from prefix import PrefixHolder
- class TestPrefixReader(unittest.TestCase):
- def setUp(self):
- "Normally setup goes here."
- def test_add(self):
- expr = PrefixHolder('["+", 1, 2]')
- # Test for simple addition
- self.assertEqual(expr.evaluate(), 3)
- def test_sqrt(self):
- expr = PrefixHolder('["sqrt", 9]')
- self.assertEqual(expr.evaluate(), 3)
- # Test that invalid number of arguments throws exception TypeError
- expr = PrefixHOlder('["sqrt", 9, 3]')
- self.assertRaises(TypeError, exp.evaluate())
- def test_variables(self):
- # Test that values can be referenced in an addition
- expr = PrefixHolder('["+", "$var1", 1]')
- self.assertEqual(expr.evaluate({"var1":2}), 3)
- expr = PrefixHolder('["+", 1, "$var"]')
- self.assertEqual(expr.evaluate({"var1":2}), 3)
- # Test that variables that don't exist throw NameError
- # var1 is referenced in the expression but isn't provided.
- self.assertRaises(NameError, expr.evaluate())
- def test_recursion(self):
- # Test that nested expressions work
- expr = PrefixHolder('["+", ["+", 1, 2], 3]')
- self.assertEqual(expr.evaluate(), 6)
- expr = PrefixHolder('["+", 3, ["+", 1, 2]]')
- self.assertEqual(expr.evaluate(), 6)
- # Test that double-nested expressions also work
- expr = PrefixHolder('["+", ["+", ["+", 1, 2], 3], 4]')
- self.assertEqual(expr.evaluate(), 10)
- # Test that nested expressions of various arg lengths work
- expr = PrefixHolder('["+", ["sqrt", 9], 3]')
- self.assertEqual(expr.evaluate(), 6)
- if __name__=="__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement