| 1 |
//go:build !windows |
| 2 |
|
| 3 |
package tui |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
// Drives queryStartup against a terminal simulated by pipes, with the replies |
| 12 |
// already queued so the exchange is deterministic. |
| 13 |
func replyingTerminal(t *testing.T, replies string) (*LightRenderer, func() string) { |
| 14 |
t.Helper() |
| 15 |
inR, inW, err := os.Pipe() |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
outR, outW, err := os.Pipe() |
| 20 |
if err != nil { |
| 21 |
t.Fatal(err) |
| 22 |
} |
| 23 |
if _, err := inW.WriteString(replies); err != nil { |
| 24 |
t.Fatal(err) |
| 25 |
} |
| 26 |
t.Cleanup(func() { inR.Close(); inW.Close(); outR.Close() }) |
| 27 |
|
| 28 |
r := &LightRenderer{ttyin: inR, ttyout: outW, escDelay: defaultEscDelay} |
| 29 |
written := func() string { |
| 30 |
outW.Close() |
| 31 |
var sb strings.Builder |
| 32 |
buf := make([]byte, 4096) |
| 33 |
for { |
| 34 |
n, err := outR.Read(buf) |
| 35 |
sb.Write(buf[:n]) |
| 36 |
if err != nil { |
| 37 |
break |
| 38 |
} |
| 39 |
} |
| 40 |
return sb.String() |
| 41 |
} |
| 42 |
return r, written |
| 43 |
} |
| 44 |
|
| 45 |
const eraseEcho = "\b \b" |
| 46 |
|
| 47 |
func TestQueryStartup(t *testing.T) { |
| 48 |
for _, tc := range []struct { |
| 49 |
name string |
| 50 |
replies string |
| 51 |
row, col int |
| 52 |
paste string // "", "true" or "false" |
| 53 |
erase int // columns the echo took, and so erasures expected |
| 54 |
}{ |
| 55 |
{ |
| 56 |
name: "mode already set", |
| 57 |
replies: "\x1b[5;10R\x1b[?2004;1$y\x1b[5;10R", |
| 58 |
row: 4, col: 9, paste: "true", |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "mode reset", |
| 62 |
replies: "\x1b[5;10R\x1b[?2004;2$y\x1b[5;10R", |
| 63 |
row: 4, col: 9, paste: "false", |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "mode not recognized", |
| 67 |
replies: "\x1b[5;10R\x1b[?2004;0$y\x1b[5;10R", |
| 68 |
row: 4, col: 9, |
| 69 |
}, |
| 70 |
{ |
| 71 |
// Answers the position queries but ignores DECRQM without printing |
| 72 |
name: "query ignored", |
| 73 |
replies: "\x1b[5;10R\x1b[5;10R", |
| 74 |
row: 4, col: 9, |
| 75 |
}, |
| 76 |
{ |
| 77 |
// macOS Terminal.app: ends the sequence at '$' and prints the 'p' |
| 78 |
name: "query echoed", |
| 79 |
replies: "\x1b[5;10R\x1b[5;11R", |
| 80 |
row: 4, col: 9, erase: 1, |
| 81 |
}, |
| 82 |
{ |
| 83 |
// A terminal that prints more of what it could not parse |
| 84 |
name: "longer echo", |
| 85 |
replies: "\x1b[5;10R\x1b[5;13R", |
| 86 |
row: 4, col: 9, erase: 3, |
| 87 |
}, |
| 88 |
{ |
| 89 |
// The echo wrapped to the next line, where erasing would guess |
| 90 |
name: "echo wrapped", |
| 91 |
replies: "\x1b[5;80R\x1b[6;1R", |
| 92 |
row: 4, col: 79, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "no reply at all", |
| 96 |
replies: "", |
| 97 |
row: -1, col: -1, |
| 98 |
}, |
| 99 |
} { |
| 100 |
t.Run(tc.name, func(t *testing.T) { |
| 101 |
r, written := replyingTerminal(t, tc.replies) |
| 102 |
row, col, pasteWasSet := r.queryStartup() |
| 103 |
|
| 104 |
if row != tc.row || col != tc.col { |
| 105 |
t.Errorf("offset (%d,%d), want (%d,%d)", row, col, tc.row, tc.col) |
| 106 |
} |
| 107 |
paste := "" |
| 108 |
if pasteWasSet != nil { |
| 109 |
paste = "false" |
| 110 |
if *pasteWasSet { |
| 111 |
paste = "true" |
| 112 |
} |
| 113 |
} |
| 114 |
if paste != tc.paste { |
| 115 |
t.Errorf("pasteWasSet %q, want %q", paste, tc.paste) |
| 116 |
} |
| 117 |
|
| 118 |
out := written() |
| 119 |
if got := strings.Count(out, eraseEcho); got != tc.erase { |
| 120 |
t.Errorf("erased %d columns, want %d (wrote %q)", got, tc.erase, out) |
| 121 |
} |
| 122 |
// The paste mode query has to sit between two position queries |
| 123 |
if want := "\x1b[6n\x1b[?2004$p\x1b[6n"; !strings.Contains(out, want) { |
| 124 |
t.Errorf("queries %q, want %q in order", out, want) |
| 125 |
} |
| 126 |
}) |
| 127 |
} |
| 128 |
} |