basic_interpreter_test.py 1.87 KB
Newer Older
1 2 3
import unittest
from parameterized import parameterized

4 5 6 7 8
from som.vm.universe       import Universe

from som.vmobjects.integer import Integer
from som.vmobjects.clazz   import Class

9 10 11 12 13
class BasicInterpreterTest(unittest.TestCase):
    @parameterized.expand([
        ("MethodCall",     "test",  42, Integer ),
        ("MethodCall",     "test2", 42, Integer ),

14
        ("NonLocalReturn", "test",  "NonLocalReturn", Class ),
15 16 17 18 19 20 21 22 23 24 25
        ("NonLocalReturn", "test1", 42, Integer ),
        ("NonLocalReturn", "test2", 43, Integer ),
        ("NonLocalReturn", "test3",  3, Integer ),
        ("NonLocalReturn", "test4", 42, Integer ),
        ("NonLocalReturn", "test5", 22, Integer ),

        ("Blocks", "arg1",  42, Integer ),
        ("Blocks", "arg2",  77, Integer ),
        ("Blocks", "argAndLocal",    8, Integer ),
        ("Blocks", "argAndContext",  8, Integer ),

26 27 28 29
        ("Return", "returnSelf",           "Return", Class ),
        ("Return", "returnSelfImplicitly", "Return", Class ),
        ("Return", "noReturnReturnsSelf",  "Return", Class ),
        ("Return", "blockReturnsImplicitlyLastValue", 4, Integer )])
30 31
    def test_basic_interpreter_behavior(self, test_class, test_selector, expected_result, result_type):
        u = Universe()
32
        u.setup_classpath("Smalltalk:TestSuite/BasicInterpreterTests")
33
        
34
        actual_result = u.execute_method(test_class, test_selector)
35 36 37 38 39
        
        self._assertEqualsSOMValue(expected_result, actual_result, result_type)
    
    def _assertEqualsSOMValue(self, expected_result, actual_result, result_type):
        if result_type is Integer:
40
            self.assertEquals(expected_result, actual_result.get_embedded_integer())
41 42
            return
        
43
        if result_type is Class:
44
            self.assertEquals(expected_result, actual_result.get_name().get_string())
45 46 47
            return

        self.fail("SOM Value handler missing")