| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
from context import sedparse |
| 7 |
|
| 8 |
TEST_DATA = [ |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 43 |
("1", 1, "NO_COMMAND", "missing command"), |
| 44 |
("1\n", 2, "NO_COMMAND", "missing command"), |
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 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): |
| 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) |
| 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() |