Debian Codegraph @

tests/test_repr.py

raw symbols SCIP
1 # Test the repr() output from the structs.
2 # Note: test_str.py uses the same tests, keep both in sync.
3
4 import unittest
5
6 from context import sedparse
7 from utils import parse_string
8
9
10 class TestSedparseRepr(unittest.TestCase): # pylint: disable=unused-variable
11 def test_repr_struct_addr(self):
12 """Check repr for all kinds of addresses"""
13 data = [
14 # pylint: disable=line-too-long
15 (
16 "$",
17 "struct_addr(addr_type=7, addr_number=0, addr_step=0, addr_regex=None)",
18 "None",
19 ),
20 (
21 "91,92",
22 "struct_addr(addr_type=3, addr_number=91, addr_step=0, addr_regex=None)",
23 "struct_addr(addr_type=3, addr_number=92, addr_step=0, addr_regex=None)",
24 ),
25 (
26 "91~992",
27 "struct_addr(addr_type=4, addr_number=91, addr_step=992, addr_regex=None)",
28 "None",
29 ),
30 (
31 "91,~992",
32 "struct_addr(addr_type=3, addr_number=91, addr_step=0, addr_regex=None)",
33 "struct_addr(addr_type=6, addr_number=0, addr_step=992, addr_regex=None)",
34 ),
35 (
36 "91,+992",
37 "struct_addr(addr_type=3, addr_number=91, addr_step=0, addr_regex=None)",
38 "struct_addr(addr_type=5, addr_number=0, addr_step=992, addr_regex=None)",
39 ),
40 (
41 "/foo/IM",
42 "struct_addr(addr_type=2, addr_number=0, addr_step=0,"
43 " addr_regex=struct_regex(slash='/', pattern='foo', flags='IM'))",
44 "None",
45 ),
46 ]
47 for address, repr_a1, repr_a2 in data:
48 script = address + "p"
49 parsed = parse_string(script)
50 self.assertEqual(repr_a1, repr(parsed[0].a1), msg=script)
51 self.assertEqual(repr_a2, repr(parsed[0].a2), msg=script)
52
53 def test_repr_struct_text_buf(self):
54 script = "a L1\\\nL2"
55 expected = "struct_text_buf(text=['L', '1', '\\\\', '\\n', 'L', '2', '\\n'])"
56 self.assertEqual(expected, repr(parse_string(script)[0].x.cmd_txt))
57
58 def test_repr_struct_text_buf_raw(self):
59 """Test using the struct only, not the parser"""
60 struct = sedparse.struct_text_buf()
61 struct.text = ["f", "o", "o"]
62 expected = "struct_text_buf(text=['f', 'o', 'o'])"
63 self.assertEqual(expected, repr(struct))
64
65 def test_repr_struct_misc(self):
66 """Check repr for struct_{regex,replacement,output}"""
67 script = "s/foo/bar/igw file"
68 parsed = parse_string(script)
69 self.assertEqual(
70 "struct_regex(slash='/', pattern='foo', flags='igw')",
71 repr(parsed[0].x.cmd_subst.regx),
72 )
73 self.assertEqual(
74 "struct_replacement(text='bar')", repr(parsed[0].x.cmd_subst.replacement)
75 )
76 self.assertEqual("struct_output(name='file')", repr(parsed[0].x.cmd_subst.outf))
77
78
79 if __name__ == "__main__":
80 unittest.main()