If cursor_pos is `None`, look for `|` as the cursor position. Assert that the token at the cursor position is `expected`.
(expected: str, cell: str, cursor_pos: int | None = None)
| 10 | |
| 11 | |
| 12 | def expect_token(expected: str, cell: str, cursor_pos: int | None = None) -> None: |
| 13 | """ |
| 14 | If cursor_pos is `None`, look for `|` as the cursor position. |
| 15 | Assert that the token at the cursor position is `expected`. |
| 16 | """ |
| 17 | if cursor_pos is None: |
| 18 | assert ( |
| 19 | cursor_count := cell.count("|") |
| 20 | ) == 1, ( |
| 21 | f"Cursor position not specified and found {cursor_count} instance(s) of '|'" |
| 22 | ) |
| 23 | cursor_pos = cell.index("|") |
| 24 | cell = cell.replace("|", "") |
| 25 | token = token_at_cursor(cell, cursor_pos) |
| 26 | offset = 0 |
| 27 | for line in cell.splitlines(): |
| 28 | if offset + len(line) >= cursor_pos: |
| 29 | break |
| 30 | else: |
| 31 | offset += len(line) + 1 |
| 32 | column = cursor_pos - offset |
| 33 | line_with_cursor = "%s|%s" % (line[:column], line[column:]) |
| 34 | assert token == expected, "Expected %r, got %r in: %r (pos %i)" % ( |
| 35 | expected, |
| 36 | token, |
| 37 | line_with_cursor, |
| 38 | cursor_pos, |
| 39 | ) |
| 40 | |
| 41 | |
| 42 | def test_simple(): |
no test coverage detected
searching dependent graphs…