Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import unittest
  2. from prefix import PrefixHolder
  3.  
  4. class TestPrefixReader(unittest.TestCase):
  5.     def setUp(self):
  6.         "Normally setup goes here."
  7.  
  8.     def test_add(self):
  9.         expr = PrefixHolder('["+", 1, 2]')
  10.         # Test for simple addition
  11.         self.assertEqual(expr.evaluate(), 3)
  12.  
  13.     def test_sqrt(self):
  14.         expr = PrefixHolder('["sqrt", 9]')
  15.         self.assertEqual(expr.evaluate(), 3)
  16.         # Test that invalid number of arguments throws exception TypeError
  17.         expr = PrefixHOlder('["sqrt", 9, 3]')
  18.         self.assertRaises(TypeError, exp.evaluate())
  19.  
  20.     def test_variables(self):
  21.         # Test that values can be referenced in an addition
  22.         expr = PrefixHolder('["+", "$var1", 1]')
  23.         self.assertEqual(expr.evaluate({"var1":2}), 3)
  24.         expr = PrefixHolder('["+", 1, "$var"]')
  25.         self.assertEqual(expr.evaluate({"var1":2}), 3)
  26.         # Test that variables that don't exist throw NameError
  27.         # var1 is referenced in the expression but isn't provided.
  28.         self.assertRaises(NameError, expr.evaluate())
  29.  
  30.     def test_recursion(self):
  31.         # Test that nested expressions work
  32.         expr = PrefixHolder('["+", ["+", 1, 2], 3]')
  33.         self.assertEqual(expr.evaluate(), 6)
  34.         expr = PrefixHolder('["+", 3, ["+", 1, 2]]')
  35.         self.assertEqual(expr.evaluate(), 6)
  36.         # Test that double-nested expressions also work
  37.         expr = PrefixHolder('["+", ["+", ["+", 1, 2], 3], 4]')
  38.         self.assertEqual(expr.evaluate(), 10)
  39.         # Test that nested expressions of various arg lengths work
  40.         expr = PrefixHolder('["+", ["sqrt", 9], 3]')
  41.         self.assertEqual(expr.evaluate(), 6)
  42.  
  43. if __name__=="__main__":
  44.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement