-
Notifications
You must be signed in to change notification settings - Fork 1
/
realTest.py
109 lines (86 loc) · 3.47 KB
/
realTest.py
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
import ruamel.yaml
from pathlib import Path
import unittest
import enum
from kaitaiStructCompile.specImport import importKSSpec
try:
thisDir = Path(__file__).parent
except:
thisDir = Path(".")
realTestsDir = thisDir / "realTests"
testFilesDir = thisDir / "testedFormatsInstances"
convertedSpecsDir = thisDir / "results"
formatsRepoDir = thisDir / "formats"
def getByPath(o, path):
path = path.split(".")
for pc in path:
if isinstance(o, list):
o = o[pc]
else:
o = getattr(o, pc)
return o
def normalizeValueForComparison(v):
if isinstance(v, enum.Enum):
v = v.value
return v
class RealTest(unittest.TestCase):
def processIdentitiesDict(self, dic, convertedRoot, nativeRoot):
for k, v in dic.items():
self.stepDown(k, v, convertedRoot, nativeRoot)
def stepDown(self, key, value, convertedRoot, nativeRoot):
if isinstance(value, dict):
s = key.split("->")
if len(s) == 1:
newConvertedRoot = getByPath(convertedRoot, key)
newNativeRoot = nativeRoot
elif len(s) == 2:
newConvertedRoot = getByPath(convertedRoot, s[0].strip())
newNativeRoot = getByPath(nativeRoot, s[1].strip())
else:
raise ValueError("key must be either a path in the converted spec or a par of paths (path in converted spec, path in original spec) separated by `->`")
self.processIdentitiesDict(value, newConvertedRoot, newNativeRoot)
elif isinstance(value, str):
with self.subTest(key=key, value=value):
convertedV=getByPath(convertedRoot, key)
nativeV=getByPath(nativeRoot, value)
self.assertEqual(convertedV, nativeV)
def assertEqual(self, converted, native):
super().assertEqual(normalizeValueForComparison(converted), normalizeValueForComparison(native))
def doTesting(self, convertedSpecClass, kaitaiNativeSpec, testYaml):
for fn in testYaml["test_files"]:
f = testFilesDir / fn
convertedSpecFile=convertedSpecClass.from_file(str(f))
kaitaiNativeSpecFile=kaitaiNativeSpec.from_file(str(f))
self.processIdentitiesDict(testYaml["identities"], convertedSpecFile, kaitaiNativeSpecFile)
def testShit(self):
for testYamlFile in realTestsDir.glob("*.yaml"):
with self.subTest(yamlFileName = testYamlFile.name):
yamlRt = ruamel.yaml.YAML(typ="rt")
testYaml = yamlRt.load(testYamlFile.read_text())
convertedSpec, _ = importKSSpec(convertedSpecsDir / (testYaml["spec_synalysis"]+".ksy"))
kaitaiNativeSpec, _ = importKSSpec(formatsRepoDir / (testYaml["spec_kaitai"]+".ksy"))
self.doTesting(convertedSpec, kaitaiNativeSpec, testYaml)
@unittest.skip
def testGif(self):
yamlRt = ruamel.yaml.YAML(typ="rt")
testYaml = yamlRt.load((realTestsDir / "gif.yaml").read_text())
from gif_native import Gif as kaitaiNativeSpec
from gif_converted import Gif as convertedSpec
self.doTesting(convertedSpec, kaitaiNativeSpec, testYaml)
@unittest.skip
def testBmp(self):
yamlRt = ruamel.yaml.YAML(typ="rt")
testYaml = yamlRt.load((realTestsDir / "bmp.yaml").read_text())
from bmp_native import Bmp as kaitaiNativeSpec
from windows_bitmaps_converted import WindowsBitmaps as convertedSpec
self.doTesting(convertedSpec, kaitaiNativeSpec, testYaml)
@unittest.skip
def testZip(self):
yamlRt = ruamel.yaml.YAML(typ="rt")
testYaml = yamlRt.load((realTestsDir / "zip.yaml").read_text())
from bmp_native import Bmp as kaitaiNativeSpec
from windows_bitmaps_converted import WindowsBitmaps as convertedSpec
self.doTesting(convertedSpec, kaitaiNativeSpec, testYaml)
#FUUUUU
if __name__ == '__main__':
unittest.main()