Debian Codegraph @

tests/test_output.py

raw symbols SCIP
1 # Test output from struct.to_dict() and struct.to_json().
2
3 import json
4 import unittest
5
6 from utils import parse_string
7
8
9 class TestSedparseOutput(unittest.TestCase): # pylint: disable=unused-variable
10 def test_diet(self):
11 data = [
12 (
13 "0~2p",
14 {
15 "a1": {"addr_type": 4, "addr_number": 0, "addr_step": 2},
16 "cmd": "p",
17 "line": 1,
18 },
19 ),
20 (
21 "1,~2p",
22 {
23 "a1": {"addr_type": 3, "addr_number": 1},
24 "a2": {"addr_type": 6, "addr_number": 0, "addr_step": 2},
25 "cmd": "p",
26 "line": 1,
27 },
28 ),
29 (
30 "1,+2p",
31 {
32 "a1": {"addr_type": 3, "addr_number": 1},
33 "a2": {"addr_type": 5, "addr_number": 0, "addr_step": 2},
34 "cmd": "p",
35 "line": 1,
36 },
37 ),
38 (
39 "/x/p",
40 {
41 "a1": {
42 "addr_type": 2,
43 "addr_regex": {"pattern": "x", "slash": "/"},
44 },
45 "cmd": "p",
46 "line": 1,
47 },
48 ),
49 (
50 "s/x/y/",
51 {
52 "cmd": "s",
53 "x": {
54 "cmd_subst": {
55 "regx": {"pattern": "x", "slash": "/"},
56 "replacement": {"text": "y"},
57 }
58 },
59 "line": 1,
60 },
61 ),
62 ("$p", {"a1": {"addr_type": 7}, "cmd": "p", "line": 1}),
63 ("q0", {"cmd": "q", "x": {"int_arg": 0}, "line": 1}),
64 ("!p", {"addr_bang": True, "cmd": "p", "line": 1}),
65 ("p", {"cmd": "p", "line": 1}),
66 ]
67 for script, output in data:
68 parsed = parse_string(script)
69 self.assertEqual(output, parsed[0].to_dict(), msg=script)
70 self.assertEqual(output, json.loads(parsed[0].to_json()), msg=script)
71
72 def test_full(self):
73 script = "p"
74 output = {
75 "a1": None,
76 "a2": None,
77 "addr_bang": False,
78 "cmd": "p",
79 "x": {
80 "cmd_txt": {"text": []},
81 "int_arg": -1,
82 "fname": "",
83 "cmd_subst": {
84 "regx": {"pattern": "", "flags": "", "slash": ""},
85 "replacement": {"text": ""},
86 "outf": {"name": ""},
87 },
88 "label_name": "",
89 "comment": "",
90 },
91 "line": 1,
92 }
93 parsed = parse_string(script)
94 self.assertEqual(output, parsed[0].to_dict(remove_empty=False), msg=script)
95 self.assertEqual(
96 output, json.loads(parsed[0].to_json(remove_empty=False)), msg=script
97 )
98
99
100 if __name__ == "__main__":
101 unittest.main()