|
| 1 | +package completion |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/reeflective/readline/internal/core" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAutopairInsertOrJump(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + line string |
| 13 | + cursor int |
| 14 | + key rune |
| 15 | + wantLine string |
| 16 | + wantSkip bool |
| 17 | + wantCursor int // Relative to original if not specified? No, absolute. |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Empty line, insert quote", |
| 21 | + line: "", |
| 22 | + cursor: 0, |
| 23 | + key: '"', |
| 24 | + wantLine: "\"", // Function inserts closer |
| 25 | + wantSkip: false, // selfInsert will insert opener |
| 26 | + wantCursor: 0, // Cursor stays same (caller handles insert) |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Inside quote, type closing quote", |
| 30 | + line: "\"foo", |
| 31 | + cursor: 4, |
| 32 | + key: '"', |
| 33 | + wantLine: "\"foo", // Should NOT insert pair |
| 34 | + wantSkip: false, // selfInsert will insert '"' -> "foo" |
| 35 | + wantCursor: 4, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Balanced quotes, type new quote", |
| 39 | + line: "\"foo\"", |
| 40 | + cursor: 5, |
| 41 | + key: '"', |
| 42 | + wantLine: "\"foo\"\"", // Inserts closer |
| 43 | + wantSkip: false, |
| 44 | + wantCursor: 5, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Escaped quote inside double, type quote", |
| 48 | + line: "\"foo \\\"", |
| 49 | + cursor: 7, |
| 50 | + key: '"', |
| 51 | + wantLine: "\"foo \\\"", // Should detect unclosed and NOT insert pair |
| 52 | + wantSkip: false, |
| 53 | + wantCursor: 7, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Jump over closing quote", |
| 57 | + line: "\"foo\"", |
| 58 | + cursor: 4, // before last " |
| 59 | + key: '"', |
| 60 | + wantLine: "\"foo\"", |
| 61 | + wantSkip: true, |
| 62 | + wantCursor: 5, // Inc |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + line := core.Line([]rune(tt.line)) |
| 69 | + cur := core.NewCursor(&line) |
| 70 | + cur.Set(tt.cursor) |
| 71 | + |
| 72 | + skip := AutopairInsertOrJump(tt.key, &line, cur) |
| 73 | + |
| 74 | + if skip != tt.wantSkip { |
| 75 | + t.Errorf("AutopairInsertOrJump() skip = %v, want %v", skip, tt.wantSkip) |
| 76 | + } |
| 77 | + |
| 78 | + if string(line) != tt.wantLine { |
| 79 | + t.Errorf("AutopairInsertOrJump() line = %q, want %q", string(line), tt.wantLine) |
| 80 | + } |
| 81 | + |
| 82 | + if cur.Pos() != tt.wantCursor { |
| 83 | + t.Errorf("AutopairInsertOrJump() cursor = %v, want %v", cur.Pos(), tt.wantCursor) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments