270 lines
6.4 KiB
Python
270 lines
6.4 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
|
||
|
|
import json
|
||
|
|
from defl.testing_ import Tester, Test, TestState
|
||
|
|
from dataclasses import dataclass, field, KW_ONLY
|
||
|
|
import sys
|
||
|
|
import re
|
||
|
|
import os
|
||
|
|
import enum
|
||
|
|
import itertools
|
||
|
|
from functools import partial, partialmethod
|
||
|
|
from time import sleep
|
||
|
|
from operator import itemgetter
|
||
|
|
from defl import log, cl, Path, Undefined, Null, Assert, Obj
|
||
|
|
from defl import CLIError
|
||
|
|
from defl._typing_ import *
|
||
|
|
from defl._typing_ import false, true, none
|
||
|
|
import defl
|
||
|
|
|
||
|
|
tester = Tester(name=__file__)
|
||
|
|
|
||
|
|
@dataclass(slots=True, frozen=False)
|
||
|
|
class ObjSlot(Obj):
|
||
|
|
a: int
|
||
|
|
b: ClassVar = [2, 3, 4]
|
||
|
|
|
||
|
|
@dataclass(frozen=False)
|
||
|
|
class ObjDict(Obj):
|
||
|
|
a: int
|
||
|
|
b: ClassVar = [2, 3, 4]
|
||
|
|
|
||
|
|
@dataclass(frozen=False)
|
||
|
|
class ObjComplex(Obj):
|
||
|
|
a: int
|
||
|
|
b: list = field(default_factory=lambda: [1, 2, 3])
|
||
|
|
d: Callable = sum
|
||
|
|
c: dict = field(
|
||
|
|
default_factory=lambda: {
|
||
|
|
# 'defl': defl,
|
||
|
|
"a": {
|
||
|
|
'b': 1,
|
||
|
|
'c': ObjSlot(1),
|
||
|
|
'd': ObjDict(2),
|
||
|
|
},
|
||
|
|
'd': [
|
||
|
|
ObjSlot(3),
|
||
|
|
ObjDict(4),
|
||
|
|
]
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
@dataclass(frozen=False)
|
||
|
|
class ObjCall:
|
||
|
|
def a(_):
|
||
|
|
...
|
||
|
|
|
||
|
|
def b(_):
|
||
|
|
...
|
||
|
|
|
||
|
|
def _c(_):
|
||
|
|
...
|
||
|
|
|
||
|
|
objSlot = ObjSlot(7)
|
||
|
|
objDict = ObjDict(8)
|
||
|
|
objComplex = ObjComplex(9)
|
||
|
|
# breakpoint()
|
||
|
|
|
||
|
|
######################################################
|
||
|
|
objDict
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjKeys():
|
||
|
|
Assert(Obj.getObjKeys(0)).eq(None)
|
||
|
|
Assert(Obj.getObjKeys(objSlot)).eq(('a', ))
|
||
|
|
Assert(Obj.getObjKeys(objDict)).eq(('a', ))
|
||
|
|
Assert(Obj.getObjKeys(objComplex)).eq(('a', 'b', 'd', 'c'))
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjVals():
|
||
|
|
Assert(Obj.getObjVals(0)).eq(None)
|
||
|
|
Assert(Obj.getObjVals(objSlot)).eq((7, ))
|
||
|
|
Assert(Obj.getObjVals(objDict)).eq((8, ))
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjRecurse():
|
||
|
|
Assert(Obj.getObjRecurse(0, fmtVal=str)).eq('0')
|
||
|
|
Assert(Obj.getObjRecurse(objSlot, fmtVal=str)).eq({'a': '7'})
|
||
|
|
Assert(Obj.getObjRecurse(objDict, fmtVal=str)).eq({'a': '8'})
|
||
|
|
Assert(Obj.getObjRecurse(objComplex, fmtVal=str)).eq({
|
||
|
|
'a': '9',
|
||
|
|
'b': ['1', '2', '3'],
|
||
|
|
'd': '<built-in function sum>',
|
||
|
|
'c': {
|
||
|
|
'a': {
|
||
|
|
'b': '1',
|
||
|
|
'c': {
|
||
|
|
'a': '1'
|
||
|
|
},
|
||
|
|
'd': {
|
||
|
|
'a': '2'
|
||
|
|
}
|
||
|
|
},
|
||
|
|
'd': [{
|
||
|
|
'a': '3'
|
||
|
|
}, {
|
||
|
|
'a': '4'
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjRecurse():
|
||
|
|
Assert(Obj.getObjRecurse(0)).eq(0)
|
||
|
|
Assert(Obj.getObjRecurse(objSlot)).eq({'a': 7})
|
||
|
|
Assert(Obj.getObjRecurse(objDict)).eq({'a': 8})
|
||
|
|
Assert(Obj.getObjRecurse(objComplex)).eq({
|
||
|
|
'a': 9,
|
||
|
|
'b': [1, 2, 3],
|
||
|
|
'd': sum,
|
||
|
|
'c': {
|
||
|
|
'a': {
|
||
|
|
'b': 1,
|
||
|
|
'c': {
|
||
|
|
'a': 1
|
||
|
|
},
|
||
|
|
'd': {
|
||
|
|
'a': 2
|
||
|
|
}
|
||
|
|
},
|
||
|
|
'd': [{
|
||
|
|
'a': 3
|
||
|
|
}, {
|
||
|
|
'a': 4
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjRecurseType():
|
||
|
|
Assert(Obj.getObjRecurseTypes(0)).eq('int')
|
||
|
|
Assert(Obj.getObjRecurseTypes(objSlot)).eq({'a': 'int'})
|
||
|
|
Assert(Obj.getObjRecurseTypes(objDict)).eq({'a': 'int'})
|
||
|
|
Assert(Obj.getObjRecurseTypes(objComplex)).eq({
|
||
|
|
'a': 'int',
|
||
|
|
'b': ['int', 'int', 'int'],
|
||
|
|
'd': 'builtin_function_or_method',
|
||
|
|
'c': {
|
||
|
|
'a': {
|
||
|
|
'b': 'int',
|
||
|
|
'c': {
|
||
|
|
'a': 'int'
|
||
|
|
},
|
||
|
|
'd': {
|
||
|
|
'a': 'int'
|
||
|
|
}
|
||
|
|
},
|
||
|
|
'd': [{
|
||
|
|
'a': 'int'
|
||
|
|
}, {
|
||
|
|
'a': 'int'
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjRecurse():
|
||
|
|
Assert(Obj.getObjRecurse(0, fmtVal=lambda x: None)).eq(None)
|
||
|
|
Assert(Obj.getObjRecurse(objSlot, fmtVal=lambda x: None)).eq({'a': None})
|
||
|
|
Assert(Obj.getObjRecurse(objDict, fmtVal=lambda x: None)).eq({'a': None})
|
||
|
|
Assert(Obj.getObjRecurse(objComplex, fmtVal=lambda x: None)).eq({
|
||
|
|
'a': None,
|
||
|
|
'b': [None, None, None],
|
||
|
|
'd': None,
|
||
|
|
'c': {
|
||
|
|
'a': {
|
||
|
|
'b': None,
|
||
|
|
'c': {
|
||
|
|
'a': None
|
||
|
|
},
|
||
|
|
'd': {
|
||
|
|
'a': None
|
||
|
|
}
|
||
|
|
},
|
||
|
|
'd': [{
|
||
|
|
'a': None
|
||
|
|
}, {
|
||
|
|
'a': None
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjAsDict():
|
||
|
|
Assert(Obj.getObjAsDict(0)).eq(0)
|
||
|
|
Assert(Obj.getObjAsDict(objSlot)).eq({'a': 7})
|
||
|
|
Assert(Obj.getObjAsDict(objDict)).eq({'a': 8})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def toJson():
|
||
|
|
Assert(Obj.toJson(0)).eq(0)
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def toJson():
|
||
|
|
Assert(Obj.toJson(objSlot)).eq({'a': 7})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def toJson():
|
||
|
|
Assert(Obj.toJson(objDict)).eq({'a': 8})
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def toJson():
|
||
|
|
Assert(objComplex.toJson()) == {
|
||
|
|
'a': 9,
|
||
|
|
'b': [1, 2, 3],
|
||
|
|
'd': sum,
|
||
|
|
'c': {
|
||
|
|
'a': {
|
||
|
|
'b': 1,
|
||
|
|
'c': ObjSlot(a=1),
|
||
|
|
'd': ObjDict(a=2)
|
||
|
|
},
|
||
|
|
'd': [ObjSlot(a=3), ObjDict(a=4)]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def toJson():
|
||
|
|
Assert(defl.jdumps(objComplex.toJson())).eq(
|
||
|
|
defl.jdumps({
|
||
|
|
"a": 9,
|
||
|
|
"b": [1, 2, 3],
|
||
|
|
"d": "<built-in function sum>",
|
||
|
|
"c": {
|
||
|
|
"a": {
|
||
|
|
"b": 1,
|
||
|
|
"c": {
|
||
|
|
"a": 1
|
||
|
|
},
|
||
|
|
"d": {
|
||
|
|
"a": 2
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"d": [{
|
||
|
|
"a": 3
|
||
|
|
}, {
|
||
|
|
"a": 4
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
})
|
||
|
|
)
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjFuncs():
|
||
|
|
def run(**kargs):
|
||
|
|
return set(Obj.getObjFuncs(ObjCall, **kargs).keys())
|
||
|
|
|
||
|
|
Assert(run(public=False, private=False)).eq(set([]))
|
||
|
|
Assert(run(public=true, private=true)).eq(set(['a', 'b', '_c', '__init__', '__repr__', '__eq__']))
|
||
|
|
Assert(run(public=true, private=False)).eq(set(['a', 'b']))
|
||
|
|
Assert(run(public=false, private=true)).eq(set(['_c', '__init__', '__repr__', '__eq__']))
|
||
|
|
|
||
|
|
@tester.add()
|
||
|
|
def getObjFuncs():
|
||
|
|
r = defl.dictToObj({'a': 1, 'b': 2, 'c': [1, 2], 'd': {'a': 1, 'b': []}, 'e': None})
|
||
|
|
log.info('r', lambda x: x.r)
|
||
|
|
log.info('r', lambda jc: jc.r)
|
||
|
|
|
||
|
|
log.info(tester.run())
|
||
|
|
tester.exitWithStatus()
|