Debian Codegraph @

tty/tty_win_test.go

raw symbols SCIP
1 // Copyright 2026 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build windows
16 // +build windows
17
18 package tty
19
20 import (
21 "encoding/binary"
22 "testing"
23 )
24
25 func makeWinKeyRecord(keyDown bool, repeat, virtualKey, scanCode, unicodeChar uint16, controlState uint32) [16]byte {
26 var data [16]byte
27 if keyDown {
28 binary.LittleEndian.PutUint32(data[0:], 1)
29 }
30 binary.LittleEndian.PutUint16(data[4:], repeat)
31 binary.LittleEndian.PutUint16(data[6:], virtualKey)
32 binary.LittleEndian.PutUint16(data[8:], scanCode)
33 binary.LittleEndian.PutUint16(data[10:], unicodeChar)
34 binary.LittleEndian.PutUint32(data[12:], controlState)
35 return data
36 }
37
38 func TestEncodeWinKeyRecordPreservesKeyState(t *testing.T) {
39 tests := []struct {
40 name string
41 data [16]byte
42 want string
43 }{
44 {
45 name: "press",
46 data: makeWinKeyRecord(true, 3, 65, 30, 'A', 0x10),
47 want: "\x1b[65;30;65;1;16;3_",
48 },
49 {
50 name: "release",
51 data: makeWinKeyRecord(false, 1, 65, 30, 'A', 0x10),
52 want: "\x1b[65;30;65;0;16;1_",
53 },
54 {
55 name: "zero repeat becomes one",
56 data: makeWinKeyRecord(true, 0, 65, 30, 'A', 0),
57 want: "\x1b[65;30;65;1;0;1_",
58 },
59 }
60
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 var surrogate rune
64 if got := string(encodeWinKeyRecord(tt.data, &surrogate)); got != tt.want {
65 t.Fatalf("encodeWinKeyRecord() = %q, want %q", got, tt.want)
66 }
67 })
68 }
69 }
70
71 func TestEncodeWinKeyRecordFallbackIgnoresRelease(t *testing.T) {
72 var surrogate rune
73 data := makeWinKeyRecord(false, 1, 0, 0, 'A', 0)
74 if got := encodeWinKeyRecord(data, &surrogate); len(got) != 0 {
75 t.Fatalf("encodeWinKeyRecord() = %q, want no bytes", string(got))
76 }
77
78 data = makeWinKeyRecord(true, 1, 0, 0, 'A', 0)
79 if got := string(encodeWinKeyRecord(data, &surrogate)); got != "A" {
80 t.Fatalf("encodeWinKeyRecord() = %q, want %q", got, "A")
81 }
82
83 data = makeWinKeyRecord(true, 3, 0, 0, 'A', 0)
84 if got := string(encodeWinKeyRecord(data, &surrogate)); got != "AAA" {
85 t.Fatalf("encodeWinKeyRecord() = %q, want %q", got, "AAA")
86 }
87 }