aboutsummaryrefslogtreecommitdiffstats
path: root/t/mop_test.py
blob: 125b1700f3c6db798941375327e58e168d5bcd92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest

import mop

class MopTest(unittest.TestCase):
    def test_bootstrap(self):
        assert mop.Class is not None
        assert mop.Object is not None
        assert mop.Method is not None
        assert mop.Attribute is not None

        assert mop.Object.metaclass is mop.Class
        assert mop.Class.metaclass is mop.Class

        assert mop.Object in mop.Class.get_mro()
        assert mop.Class in mop.Class.get_mro()
        assert mop.Object in mop.Object.get_mro()
        assert mop.Class not in mop.Object.get_mro()

        # XXX no idea what's going on here
        # assert mop.Class.isa(mop.Object)
        # assert mop.Class.isa(mop.Class)
        # assert mop.Object.isa(mop.Object)
        # assert not mop.Object.isa(mop.Class)

        assert mop.Method.metaclass is mop.Class
        assert mop.Attribute.metaclass is mop.Class

        assert mop.Object in mop.Method.get_mro()
        assert mop.Object in mop.Attribute.get_mro()

        assert mop.Class.get_name() == "Class"
        assert mop.Object.get_name() == "Object"
        assert mop.Method.get_name() == "Method"
        assert mop.Attribute.get_name() == "Attribute"

        assert mop.Class.get_superclass() is mop.Object
        assert mop.Object.get_superclass() is None
        assert mop.Method.get_superclass() is mop.Object
        assert mop.Attribute.get_superclass() is mop.Object

        assert mop.Class.get_mro() == [ mop.Class, mop.Object ]
        assert mop.Object.get_mro() == [ mop.Object ]
        assert mop.Method.get_mro() == [ mop.Method, mop.Object ]
        assert mop.Attribute.get_mro() == [ mop.Attribute, mop.Object ]

    def test_class_creation(self):
        Point = mop.Class.new(
            name="Point",
            superclass=mop.Class.base_object_class()
        )

        Point.add_attribute(Point.attribute_class().new(name="x"))
        Point.add_attribute(Point.attribute_class().new(name="y"))

        Point.add_method(Point.method_class().new(
            name="x",
            body=lambda self: self.metaclass.get_all_attributes()["x"].get_value(self)
        ))
        Point.add_method(Point.method_class().new(
            name="y",
            body=lambda self: self.metaclass.get_all_attributes()["y"].get_value(self)
        ))
        Point.finalize()

        point = Point.new(x=1, y=2)
        assert point.x() == 1
        assert point.y() == 2

        Point3D = Point.metaclass.new(
            name="Point3D",
            superclass=Point,
        )
        Point3D.add_attribute(Point3D.attribute_class().new(name="z"))
        Point3D.add_method(Point3D.method_class().new(
            name="z",
            body=lambda self: self.metaclass.get_all_attributes()["z"].get_value(self)
        ))
        Point3D.finalize()

        point3d = Point3D.new(x=3, y=4, z=5)
        assert point3d.x() == 3
        assert point3d.y() == 4
        assert point3d.z() == 5

        assert not point.can("z")
        assert point3d.can("z")

        assert point.isa(Point)
        assert point3d.isa(Point)
        assert not point.isa(Point3D)
        assert point3d.isa(Point3D)