| 1 |
package tui |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestIncompleteEscape(t *testing.T) { |
| 9 |
for _, c := range []struct { |
| 10 |
buffer string |
| 11 |
want bool |
| 12 |
}{ |
| 13 |
// Complete sequences: nothing to wait for |
| 14 |
{"\x1b[A", false}, |
| 15 |
{"\x1bOA", false}, |
| 16 |
{"\x1b[1;5A", false}, |
| 17 |
{"\x1b[200~", false}, |
| 18 |
{"\x1b[<0;1;1M", false}, |
| 19 |
{"\x1b[12;34R", false}, |
| 20 |
{"\x1b[?2004;2$y", false}, |
| 21 |
{"\x1b[?1;2c", false}, |
| 22 |
|
| 23 |
// Fragments: keep waiting |
| 24 |
{"\x1b[", true}, |
| 25 |
{"\x1b[?", true}, |
| 26 |
{"\x1b[1;", true}, |
| 27 |
{"\x1b[?2004;2$", true}, |
| 28 |
{"\x1bO", true}, |
| 29 |
{"\x1b[<0;1;", true}, |
| 30 |
|
| 31 |
// Only the trailing sequence matters |
| 32 |
{"ab\x1b[?2004;2$", true}, |
| 33 |
{"\x1b[A\x1b[", true}, |
| 34 |
{"\x1b[A\x1b[B", false}, |
| 35 |
|
| 36 |
// Long buffers: only the tail is scanned, so an introducer further |
| 37 |
// back than escapeLookback is not waited for |
| 38 |
{strings.Repeat("a", 100000), false}, |
| 39 |
{"\x1b[" + strings.Repeat("a", 100000), false}, |
| 40 |
{strings.Repeat("a", 100000) + "\x1b[1;", true}, |
| 41 |
|
| 42 |
// Not a sequence fzf waits on |
| 43 |
{"", false}, |
| 44 |
{"abc", false}, |
| 45 |
{"\x1b", false}, // lone ESC, handled by the existing escDelay branch |
| 46 |
{"\x1ba", false}, // ALT-a |
| 47 |
{"\x1b[\x01", false}, // malformed, do not stall on it |
| 48 |
} { |
| 49 |
if got := incompleteEscape([]byte(c.buffer)); got != c.want { |
| 50 |
t.Errorf("incompleteEscape(%q) = %v, want %v", c.buffer, got, c.want) |
| 51 |
} |
| 52 |
} |
| 53 |
} |