source: pyson/test/JsonSubTypesTest.py

Last change on this file was 1080, checked in by wouter, 8 weeks ago

#359 fixed @JsonSubTypes inheritance

File size: 2.2 KB
Line 
1from pyson.JsonSubTypes import JsonSubTypes, getSubTypes
2from pyson.JsonTypeInfo import JsonTypeInfo
3from pyson.JsonTypeInfo import Id,As
4
5import unittest
6
7
8@JsonSubTypes(['test.JsonSubTypesTest.B','test.JsonSubTypesTest.C','test.JsonSubTypesTest.D'])
9class A:
10 pass
11
12class B(A):
13 pass
14
15class C(B):
16 pass
17
18class D(A):
19 pass
20
21
22class JsonSubTypesTest(unittest.TestCase):
23 def test1(self):
24 @JsonSubTypes(["first.sub.class","subclss2"])
25 class superclass:
26 def print(self, *args):
27 for arg in args:
28 print(arg)
29
30
31
32 class derivedclass(superclass):
33 pass
34
35 print(type(superclass))
36 print(superclass.__jsonsubtypes__)
37
38 pr = superclass()
39 pr.print(1, 2, 3)
40 print(type(pr))
41 print(pr.__jsonsubtypes__)
42
43 print("checking __jsonsubtypes__ function")
44 # WHY is self not added in this call???
45 print(superclass.__jsonsubtypes__)
46
47 print(derivedclass.__jsonsubtypes__)
48
49 class super2nojason:
50 pass
51
52 # even with mixedclass, we inherit the annotation
53 class mixedclass(superclass, super2nojason):
54 pass
55
56 print(mixedclass.__jsonsubtypes__)
57
58
59 @JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
60 @JsonSubTypes(["Cat"])
61 class Animal:
62 def __init__(self, age:int):
63 self._age=age
64 def getage(self):
65 return self_age
66
67
68 class Cat(Animal):
69 def __init__(self, age:int, color:str):
70 super().__init__(age)
71 self._color=color
72 def getcolor(self):
73 return self._color
74
75 cat=Cat(1,"brown")
76 print(cat.__jsonsubtypes__)
77 print(str(cat.__jsontypeinfo__))
78
79 def testInheritance(self):
80 self.assertEqual({A,B,C,D} ,getSubTypes(A))
81 self.assertEqual({B,C} ,getSubTypes(B))
82 self.assertEqual({C} ,getSubTypes(C))
83 self.assertEqual({D} ,getSubTypes(D))
84
85
86
87
Note: See TracBrowser for help on using the repository browser.