Debian Codegraph @

tests/test_errors.py

raw symbols SCIP
1 # Test all error messages.
2 # coding: utf-8
3
4 import unittest
5
6 from context import sedparse
7
8 TEST_DATA = [
9 # pylint: disable=bad-whitespace
10 # fmt: off
11
12 # Note: Not all possible error messages are tested, those that were left
13 # out are not available in the port.
14 # Format: (sed script, char nr, constant, error message)
15
16 # ANCIENT_VERSION expected newer version of sed
17 ("!!p", 2, "BAD_BANG", "multiple `!'s"),
18 ("1,p", 3, "BAD_COMMA", "unexpected `,'"),
19 ("s★a★b★", 2, "BAD_DELIM",
20 "delimiter character is not a single-byte character"),
21 ("+1p", 2, "BAD_STEP", "invalid usage of +N or ~N as first address"),
22 ("~1p", 2, "BAD_STEP", "invalid usage of +N or ~N as first address"),
23 (":", 1, "COLON_LACKS_LABEL", '":" lacks a label'),
24 # DISALLOWED_CMD e/r/w commands disabled in sandbox mode
25 ("}", 1, "EXCESS_CLOSE_BRACE", "unexpected `}'"),
26 ("b}", 2, "EXCESS_CLOSE_BRACE", "unexpected `}'"),
27 ("bx}", 3, "EXCESS_CLOSE_BRACE", "unexpected `}'"),
28 ("s/a/b/gg", 8, "EXCESS_G_OPT", "multiple `g' options to `s' command"),
29 ("dp", 2, "EXCESS_JUNK", "extra characters after command"),
30 ("xx", 2, "EXCESS_JUNK", "extra characters after command"),
31 ("s/a/b/2p2", 9, "EXCESS_N_OPT", "multiple number options to `s' command"),
32 ("{", 0, "EXCESS_OPEN_BRACE", "unmatched `{'"),
33 # ^here it should be 1. It's a GNU sed bug that sedparse emulates.
34 ("s/a/b/pp", 8, "EXCESS_P_OPT", "multiple `p' options to `s' command"),
35 ("a", 1, "EXPECTED_SLASH", "expected \\ after `a', `c' or `i'"),
36 # INCOMPLETE_CMD incomplete command
37 ("0p", 2, "INVALID_LINE_0", "invalid usage of line address 0"),
38 ("0,5p", 4, "INVALID_LINE_0", "invalid usage of line address 0"),
39 ("s/a/b/w", 7, "MISSING_FILENAME", "missing filename in r/R/w/W commands"),
40 ("r", 1, "MISSING_FILENAME", "missing filename in r/R/w/W commands"),
41 ("{p;$}", 5, "NO_CLOSE_BRACE_ADDR", "`}' doesn't want any addresses"),
42 # NO_COLON_ADDR : doesn't want any addresses
43 ("1", 1, "NO_COMMAND", "missing command"),
44 ("1\n", 2, "NO_COMMAND", "missing command"),
45 # NO_SHARP_ADDR comments don't accept any addresses
46 # ONE_ADDR command only uses one address
47 # RECURSIVE_ESCAPE_C recursive escaping after \\c not allowed
48 ("u", 1, "UNKNOWN_CMD", "unknown command: `u'"),
49 ("s/a/b/z", 7, "UNKNOWN_S_OPT", "unknown option to `s'"),
50 ("s/a/b/\r", 7, "UNKNOWN_S_OPT", "unknown option to `s'"),
51 ("/a", 2, "UNTERM_ADDR_RE", "unterminated address regex"),
52 ("s/a/b", 5, "UNTERM_S_CMD", "unterminated `s' command"),
53 ("y/a/", 4, "UNTERM_Y_CMD", "unterminated `y' command"),
54 # Y_CMD_LEN strings for `y' command are different lengths
55 ("s/a/b/0", 7, "ZERO_N_OPT", "number option to `s' command may not be zero"),
56 ]
57
58
59 class TestSedparseErrors(unittest.TestCase): # pylint: disable=unused-variable
60 def test_errors(self):
61 for script, char_nr, _, message in TEST_DATA:
62 expected = "sedparse: -e expression #%d, char %d: %s" % (
63 sedparse.cur_input.string_expr_count + 1,
64 char_nr,
65 message,
66 )
67
68 try:
69 parsed = []
70 sedparse.compile_string(parsed, script)
71 sedparse.check_final_program()
72 self.fail(msg=script) # it should never reach here
73 except sedparse.ParseError as err:
74 self.assertEqual(expected, err.message, msg=script)
75 self.assertEqual(sedparse.EXIT_BAD_USAGE, err.exitcode, msg=script)
76
77
78 if __name__ == "__main__":
79 unittest.main()