File: test_magicgen.py

package info (click to toggle)
golang-github-checkpoint-restore-go-criu 7.2.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,796 kB
  • sloc: makefile: 231; ansic: 195; python: 137; sh: 110
file content (73 lines) | stat: -rw-r--r-- 1,951 bytes parent folder | download
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
import unittest
from io import StringIO

from magicgen import read_magics, write_magics


class TestReadMagics(unittest.TestCase):
    def test_simple(self):
        self.assertDictEqual(
            read_magics(StringIO("#define TEST 0xbc614e")), {"TEST": 12345678}
        )

    def test_duplicate(self):
        self.assertDictEqual(
            read_magics(
                StringIO(
                    "#define TEST 0xbc614e\n#define V1 1\n#define TEST_COPY TEST\n#define RAW 0x0"
                )
            ),
            {"V1": 1, "TEST": 12345678, "RAW": 0},
        )


class TestWriteMagics(unittest.TestCase):
    prefix = """// Code generated by magicgen. DO NOT EDIT.

package magic

type MagicMap struct {
\tByName  map[string]uint64
\tByValue map[uint64]string
}

func LoadMagic() MagicMap {
\tmagicMap := MagicMap{
\t\tByName:  make(map[string]uint64),
\t\tByValue: make(map[uint64]string),
\t}"""
    suffix = "\n\treturn magicMap\n}\n"

    def test_simple(self):
        f = StringIO()
        write_magics(f, {"TEST": 12345678})
        self.assertEqual(
            f.getvalue(),
            self.prefix
            + '\n\tmagicMap.ByName["TEST"] = 12345678\n\tmagicMap.ByValue[12345678] = "TEST"'
            + self.suffix,
        )

    def test_suffix(self):
        f = StringIO()
        write_magics(f, {"TEST_MAGIC": 12345678})
        self.assertEqual(
            f.getvalue(),
            self.prefix
            + '\n\tmagicMap.ByName["TEST"] = 12345678\n\tmagicMap.ByValue[12345678] = "TEST"'
            + self.suffix,
        )

    def test_ignored(self):
        f = StringIO()
        write_magics(f, {"V1": 1, "TEST": 12345678, "RAW": 0})
        self.assertEqual(
            f.getvalue(),
            self.prefix
            + '\n\tmagicMap.ByName["TEST"] = 12345678\n\tmagicMap.ByValue[12345678] = "TEST"'
            + self.suffix,
        )


if __name__ == "__main__":
    unittest.main(verbosity=2)