|
| 1 | +#include "parts.h" |
| 2 | + |
| 3 | +#include "../../Parser/tokenizer/cursor.h" |
| 4 | + |
| 5 | +static int |
| 6 | +check(int condition, const char *message) |
| 7 | +{ |
| 8 | + if (condition) { |
| 9 | + return 0; |
| 10 | + } |
| 11 | + PyErr_SetString(PyExc_AssertionError, message); |
| 12 | + return -1; |
| 13 | +} |
| 14 | + |
| 15 | +static int |
| 16 | +append_line(_PyTok_SourceText *source, const char *text, Py_ssize_t len, |
| 17 | + int implicit) |
| 18 | +{ |
| 19 | + _PyTok_Off start = _PyTok_SourceAppend(source, text, len); |
| 20 | + if (start < 0) { |
| 21 | + return -1; |
| 22 | + } |
| 23 | + return _PyTok_SourceAddLine(source, start, start + len, implicit); |
| 24 | +} |
| 25 | + |
| 26 | +static PyObject * |
| 27 | +test_tokenizer_source(PyObject *Py_UNUSED(module), |
| 28 | + PyObject *Py_UNUSED(args)) |
| 29 | +{ |
| 30 | + _PyTok_SourceText source; |
| 31 | + _PyTok_SourceInit(&source); |
| 32 | + |
| 33 | + if (append_line(&source, "alpha\n", 6, 0) < 0 || |
| 34 | + append_line(&source, "\xce\xb2\n", 3, 1) < 0 || |
| 35 | + append_line(&source, "nul\0x\n", 6, 0) < 0) { |
| 36 | + goto error; |
| 37 | + } |
| 38 | + for (int i = 0; i < 300; i++) { |
| 39 | + if (append_line(&source, "x\n", 2, 0) < 0) { |
| 40 | + goto error; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + _PyTok_Line line; |
| 45 | + if (check(source.nlines == 303, "wrong source line count") < 0 || |
| 46 | + check(source.ncheckpoints == 2, |
| 47 | + "wrong source checkpoint count") < 0 || |
| 48 | + check(_PyTok_SourceLine(&source, 2, &line) == 0, |
| 49 | + "cannot find second source line") < 0 || |
| 50 | + check(line.start == 6 && line.end == 9, |
| 51 | + "wrong second source line span") < 0 || |
| 52 | + check(line.implicit_newline, |
| 53 | + "missing implicit newline flag") < 0 || |
| 54 | + check(!line.contains_nul, |
| 55 | + "unexpected null byte flag") < 0 || |
| 56 | + check(_PyTok_SourceLine(&source, 3, &line) == 0, |
| 57 | + "cannot find third source line") < 0 || |
| 58 | + check(line.contains_nul, "missing null byte flag") < 0 || |
| 59 | + check(_PyTok_SourceLine(&source, 257, &line) == 0, |
| 60 | + "cannot find checkpointed source line") < 0 || |
| 61 | + check(line.end - line.start == 2, |
| 62 | + "wrong checkpointed source line span") < 0) { |
| 63 | + goto error; |
| 64 | + } |
| 65 | + |
| 66 | + Py_ssize_t len; |
| 67 | + const char *view = _PyTok_SourceSpanView( |
| 68 | + &source, _PyTok_SpanFromBounds(6, 8), &len); |
| 69 | + if (check(view != NULL, "cannot view source span") < 0 || |
| 70 | + check(len == 2 && memcmp(view, "\xce\xb2", 2) == 0, |
| 71 | + "wrong source span contents") < 0) { |
| 72 | + goto error; |
| 73 | + } |
| 74 | + |
| 75 | + _PyTok_Loc loc; |
| 76 | + if (check(_PyTok_SourceLocation(&source, 8, &loc) == 0, |
| 77 | + "cannot locate source offset") < 0 || |
| 78 | + check(loc.lineno == 2 && loc.col == 2, |
| 79 | + "wrong source location") < 0 || |
| 80 | + check(_PyTok_SourceLocation(&source, 9, &loc) == 0, |
| 81 | + "cannot locate line boundary") < 0 || |
| 82 | + check(loc.lineno == 3 && loc.col == 0, |
| 83 | + "wrong line boundary location") < 0) { |
| 84 | + goto error; |
| 85 | + } |
| 86 | + |
| 87 | + view = _PyTok_SourceSpanView( |
| 88 | + &source, _PyTok_SpanFromBounds(0, source.len + 1), &len); |
| 89 | + if (check(view == NULL, "accepted invalid source span") < 0 || |
| 90 | + check(PyErr_ExceptionMatches(PyExc_SystemError), |
| 91 | + "invalid source span raised wrong error") < 0) { |
| 92 | + goto error; |
| 93 | + } |
| 94 | + PyErr_Clear(); |
| 95 | + _PyTok_SourceClear(&source); |
| 96 | + Py_RETURN_NONE; |
| 97 | + |
| 98 | +error: |
| 99 | + _PyTok_SourceClear(&source); |
| 100 | + return NULL; |
| 101 | +} |
| 102 | + |
| 103 | +static PyObject * |
| 104 | +test_tokenizer_cursor(PyObject *Py_UNUSED(module), |
| 105 | + PyObject *Py_UNUSED(args)) |
| 106 | +{ |
| 107 | + _PyTok_SourceText source; |
| 108 | + _PyTok_SourceInit(&source); |
| 109 | + if (append_line(&source, "ab\n", 3, 0) < 0 || |
| 110 | + append_line(&source, "cd\n", 3, 0) < 0) { |
| 111 | + goto error; |
| 112 | + } |
| 113 | + |
| 114 | + _PyTok_Cursor cursor; |
| 115 | + _PyTok_CursorInit(&cursor); |
| 116 | + if (check(_PyTok_CursorSetLine(&cursor, &source, 1) == 0, |
| 117 | + "cannot set cursor line") < 0 || |
| 118 | + check(_PyTok_CursorPeek(&cursor, &source, 0) == 'a', |
| 119 | + "wrong cursor peek") < 0 || |
| 120 | + check(_PyTok_CursorPeek(&cursor, &source, 1) == 'b', |
| 121 | + "wrong distant cursor peek") < 0 || |
| 122 | + check(_PyTok_CursorMark(&cursor) == 0, |
| 123 | + "wrong cursor mark") < 0 || |
| 124 | + check(_PyTok_CursorAdvance(&cursor, &source) == 'a', |
| 125 | + "wrong first cursor byte") < 0 || |
| 126 | + check(_PyTok_CursorAdvance(&cursor, &source) == 'b', |
| 127 | + "wrong second cursor byte") < 0 || |
| 128 | + check(_PyTok_CursorAdvance(&cursor, &source) == '\n', |
| 129 | + "wrong final cursor byte") < 0 || |
| 130 | + check(_PyTok_CursorAdvance(&cursor, &source) == EOF, |
| 131 | + "cursor advanced past line") < 0 || |
| 132 | + check(_PyTok_CursorSetOffset(&cursor, &source, 3) == 0, |
| 133 | + "cannot set cursor offset") < 0 || |
| 134 | + check(cursor.lineno == 2 && cursor.line_start == 3, |
| 135 | + "wrong cursor line") < 0 || |
| 136 | + check(_PyTok_CursorAdvance(&cursor, &source) == 'c', |
| 137 | + "wrong cursor byte after reset") < 0 || |
| 138 | + check(_PyTok_CursorSetOffset( |
| 139 | + &cursor, &source, source.len + 1) < 0, |
| 140 | + "accepted invalid cursor offset") < 0) { |
| 141 | + goto error; |
| 142 | + } |
| 143 | + |
| 144 | + _PyTok_SourceClear(&source); |
| 145 | + Py_RETURN_NONE; |
| 146 | + |
| 147 | +error: |
| 148 | + _PyTok_SourceClear(&source); |
| 149 | + return NULL; |
| 150 | +} |
| 151 | + |
| 152 | +static PyMethodDef test_methods[] = { |
| 153 | + {"test_tokenizer_source", test_tokenizer_source, METH_NOARGS}, |
| 154 | + {"test_tokenizer_cursor", test_tokenizer_cursor, METH_NOARGS}, |
| 155 | + {NULL}, |
| 156 | +}; |
| 157 | + |
| 158 | +int |
| 159 | +_PyTestInternalCapi_Init_Tokenizer(PyObject *module) |
| 160 | +{ |
| 161 | + return PyModule_AddFunctions(module, test_methods); |
| 162 | +} |
0 commit comments