aboutsummaryrefslogtreecommitdiffstats
path: root/t/mop_test.py
blob: 1b5b6c80b6eb8dfcd3efb82b9a07f385f40a41f1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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", default=0))
        Point.add_attribute(Point.attribute_class().new(name="y", default=0))

        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.add_method(Point.method_class().new(
            name="set_x",
            body=lambda self, new_value: self.metaclass.get_all_attributes()["x"].set_value(self, new_value)
        ))
        Point.finalize()

        assert Point.metaclass is mop.Class
        assert Point.isa(mop.Object)
        assert Point.get_superclass() is mop.Object
        assert Point.get_mro() == [ Point, mop.Object ]

        point = Point.new(x=1, y=2)
        assert point.isa(Point)
        assert point.metaclass is Point
        assert point.x() == 1
        assert point.y() == 2
        point.set_x(10)
        assert point.x() == 10

        point2 = Point.new(x=3, y=4)
        assert point is not point2
        assert point.x() == 10
        assert point.y() == 2
        assert point2.x() == 3
        assert point2.y() == 4

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

        assert Point3D.metaclass is mop.Class
        assert Point3D.isa(mop.Object)
        assert Point3D.get_superclass() is Point
        assert Point3D.get_mro() == [ Point3D, Point, mop.Object ]

        point3d = Point3D.new(x=3, y=4, z=5)
        assert point3d.isa(Point3D)
        assert point3d.isa(Point)
        assert point3d.isa(mop.Object)
        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)

        point_default = Point.new()
        assert point_default.x() == 0
        assert point_default.y() == 0
        point3d_default = Point3D.new()
        assert point3d_default.x() == 0
        assert point3d_default.y() == 0