test_scope.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import unittest
  2. from scope import *
  3. from typing import *
  4. class TestScope(unittest.TestCase):
  5. def test_scope(self):
  6. builtin = Scope("builtin", parent=None)
  7. # Lookup LHS value (creating it in the current scope if not found)
  8. variable = builtin.put_variable_assignment("in_state", Callable[[List[str]], bool])
  9. self.assertEqual(variable.offset, 0)
  10. globals = Scope("globals", parent=builtin)
  11. variable = globals.put_variable_assignment("x", int)
  12. self.assertEqual(variable.offset, 1)
  13. variable = globals.put_variable_assignment("in_state", Callable[[List[str]], bool])
  14. self.assertEqual(variable.offset, 0)
  15. local = Scope("local", parent=globals)
  16. variable = local.put_variable_assignment("x", int)
  17. self.assertEqual(variable.offset, 1)
  18. # Lookup RHS value (returning None if not found)
  19. variable = local.get("x")
  20. self.assertEqual(variable.offset, 1)
  21. # name 'y' doesn't exist in any scope
  22. self.assertRaises(Exception, lambda: local.get("y"))
  23. self.assertEqual(builtin.total_size(), 1)
  24. self.assertEqual(globals.total_size(), 2)
  25. self.assertEqual(local.total_size(), 2)