basic_interpreter_test.py 2.17 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
class BasicInterpreterTest(unittest.TestCase):
    @parameterized.expand([
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        ("MethodCall",     "test",  42, Integer),
        ("MethodCall",     "test2", 42, Integer),

        ("NonLocalReturn", "test",  "NonLocalReturn", Class),
        ("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),

        ("Return", "returnSelf",           "Return", Class),
        ("Return", "returnSelfImplicitly", "Return", Class),
        ("Return", "noReturnReturnsSelf",  "Return", Class),
        ("Return", "blockReturnsImplicitlyLastValue", 4, Integer)])
    def test_basic_interpreter_behavior(self, test_class, test_selector,
                                        expected_result, result_type):
33
        u = Universe()
34
        u.setup_classpath("Smalltalk:TestSuite/BasicInterpreterTests")
35

36
        actual_result = u.execute_method(test_class, test_selector)
37 38 39 40 41 42

        self._assert_equals_SOM_value(expected_result, actual_result,
                                      result_type)

    def _assert_equals_SOM_value(self, expected_result, actual_result,
                                 result_type):
43
        if result_type is Integer:
44 45
            self.assertEquals(expected_result,
                              actual_result.get_embedded_integer())
46
            return
47

48
        if result_type is Class:
49 50
            self.assertEquals(expected_result,
                              actual_result.get_name().get_string())
51 52 53
            return

        self.fail("SOM Value handler missing")
54 55 56 57

import sys
if sys.modules.has_key('pytest'):
    # hack to make pytest not to collect the unexpanded test method
58
    delattr(BasicInterpreterTest, "test_basic_interpreter_behavior")