source: pyson/test/OverloadTest.py@ 1421

Last change on this file since 1421 was 826, checked in by wouter, 7 months ago

@293 changed clazz instantiation to make it (barely) plum dispatch compatible

File size: 2.2 KB
Line 
1
2from abc import ABC
3from datetime import datetime
4from decimal import Decimal
5import json
6import re
7import sys, traceback
8from typing import Dict, List, Set, Any, Union, Optional, Collection
9import unittest
10from uuid import uuid4, UUID
11
12from pyson.Deserializer import Deserializer
13from pyson.Serializer import Serializer
14from pyson.JsonSerialize import JsonSerialize
15from pyson.JsonDeserialize import JsonDeserialize
16from pyson.JsonGetter import JsonGetter
17from pyson.JsonSubTypes import JsonSubTypes
18from pyson.JsonTypeInfo import Id, As
19from pyson.JsonTypeInfo import JsonTypeInfo
20from pyson.JsonValue import JsonValue
21from pyson.ObjectMapper import ObjectMapper
22from pickle import NONE
23from test.MyDeserializer import ValueDeserializer2
24from plum import dispatch
25
26'''
27Testing deserialization of overloaded class constructors.
28Note that plum/dispatch is a third party library,
29only imported here for testing. It's NOT part
30of the standard pyson library and you can theoretically
31use other libraries for this.
32
33Also note that MYPY type editor will not like these overloads.
34'''
35class Ovload:
36 @dispatch # signature of primary constructor
37 def __init__(self, data: int ):
38 self.data=data
39 @dispatch
40 def __init__(self, data:str):
41 self.data=int(data)
42 def getData(self)->int:
43 return self.data
44 def __eq__(self, other):
45 return isinstance(other, self.__class__) and \
46 self.data==other.data
47 def __repr__(self):
48 return "Ovload:"+str(self.data)
49
50
51class OverloadTest(unittest.TestCase):
52 '''
53 The class has an overloaded constructor.
54 Check if it works.
55 '''
56 pyson=ObjectMapper()
57
58 def testBasicOverload(self):
59 o=Ovload(1)
60 print(o)
61 print(Ovload("2"))
62
63 def testRawPlum(self):
64 clazz = Ovload
65 # we can call the constructor only with a list, not with a dict. #293
66 clazz(*[1])
67 clazz(*["1"])
68
69 def testOverloadSerialize(self):
70 objson = self.pyson.toJson(Ovload(1))
71 self.assertEqual({'data': 1},objson)
72
73 def testOverloadDeserialize(self):
74 objson= {'data': 1}
75 self.assertEqual(Ovload(1), self.pyson.parse(objson, Ovload))
76
77
78
Note: See TracBrowser for help on using the repository browser.