Debian Codegraph @

tests/test_str.py

raw symbols SCIP
1 # Test the str() output from the structs.
2 # Note: test_repr.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 TestSedparsestr(unittest.TestCase): # pylint: disable=unused-variable
11 def test_str_struct_addr(self):
12 """Check str for all kinds of addresses"""
13 data = [
14 ("$", "$", "None"),
15 ("91,92", "91", "92"),
16 ("91~992", "91~992", "None"),
17 ("91,~992", "91", "~992"),
18 ("91,+992", "91", "+992"),
19 ("/foo/IM", "/foo/IM", "None"),
20 ]
21 for address, str_a1, str_a2 in data:
22 script = address + "p"
23 parsed = parse_string(script)
24 self.assertEqual(str_a1, str(parsed[0].a1), msg=script)
25 self.assertEqual(str_a2, str(parsed[0].a2), msg=script)
26
27 def test_str_struct_text_buf(self):
28 script = "a L1\\\nL2"
29 expected = "L1\\\nL2"
30 self.assertEqual(expected, str(parse_string(script)[0].x.cmd_txt))
31
32 def test_str_struct_text_buf_raw(self):
33 """Test using the struct only, not the parser"""
34 struct = sedparse.struct_text_buf()
35 struct.text = ["f", "o", "o"]
36 expected = "fo" # last char (usually a \n) is chopped
37 self.assertEqual(expected, str(struct))
38
39 def test_str_struct_misc(self):
40 """Check str for struct_{regex,replacement,output}"""
41 script = "s/foo/bar/igw file"
42 parsed = parse_string(script)
43 self.assertEqual("/foo/igw", str(parsed[0].x.cmd_subst.regx))
44 self.assertEqual("bar", str(parsed[0].x.cmd_subst.replacement))
45 self.assertEqual("file", str(parsed[0].x.cmd_subst.outf))
46
47 def test_str_int_arg(self):
48 """Check str for commands with int_arg"""
49 data = [("q0", "q 0"), ("q1", "q 1"), ("q99", "q 99")]
50 for script, expected in data:
51 parsed = parse_string(script)
52 self.assertEqual(expected, str(parsed[0]), msg=script)
53
54
55 if __name__ == "__main__":
56 unittest.main()