1package component
2
3import "testing"
4
5func TestInlineGhostSelection(t *testing.T) {
6 tests := []struct {
7 name string
8 realInput string
9 ghostFullText string
10 selectionStart int
11 selectionEnd int
12 hasSelection bool
13 want bool
14 }{
15 {
16 name: "selected suffix matches typed prefix",
17 realInput: "goo",
18 ghostFullText: "google.com",
19 selectionStart: 3,
20 selectionEnd: 10,
21 hasSelection: true,
22 want: true,
23 },
24 {
25 name: "no selection means no ghost suffix",
26 realInput: "goo",
27 ghostFullText: "google.com",
28 selectionStart: 10,
29 selectionEnd: 10,
30 hasSelection: false,
31 want: false,
32 },
33 {
34 name: "collapsed selection should not be treated as ghost",
35 realInput: "goo",
36 ghostFullText: "google.com",
37 selectionStart: 10,
38 selectionEnd: 10,
39 hasSelection: true,
40 want: false,
41 },
42 {
43 name: "wrong selection range should not be treated as ghost",
44 realInput: "goo",
45 ghostFullText: "google.com",
46 selectionStart: 1,
47 selectionEnd: 10,
48 hasSelection: true,
49 want: false,
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 got := isInlineGhostSelection(
56 tt.realInput,
57 tt.ghostFullText,
58 tt.selectionStart,
59 tt.selectionEnd,
60 tt.hasSelection,
61 )
62 if got != tt.want {
63 t.Fatalf("isInlineGhostSelection() = %v, want %v", got, tt.want)
64 }
65 })
66 }
67}
68
69func TestAcceptedGhostInput(t *testing.T) {
70 tests := []struct {
71 name string
72 hasGhost bool
73 ghostFullText string
74 wantText string
75 wantAccepted bool
76 }{
77 {
78 name: "accepts explicit ghost",
79 hasGhost: true,
80 ghostFullText: "google.com",
81 wantText: "google.com",
82 wantAccepted: true,
83 },
84 {
85 name: "rejects without ghost",
86 hasGhost: false,
87 ghostFullText: "google.com",
88 wantText: "",
89 wantAccepted: false,
90 },
91 {
92 name: "rejects empty ghost text",
93 hasGhost: true,
94 ghostFullText: "",
95 wantText: "",
96 wantAccepted: false,
97 },
98 }
99
100 for _, tt := range tests {
101 t.Run(tt.name, func(t *testing.T) {
102 gotText, gotAccepted := acceptedGhostInput(tt.hasGhost, tt.ghostFullText)
103 if gotText != tt.wantText {
104 t.Fatalf("acceptedGhostInput() text = %q, want %q", gotText, tt.wantText)
105 }
106 if gotAccepted != tt.wantAccepted {
107 t.Fatalf("acceptedGhostInput() accepted = %v, want %v", gotAccepted, tt.wantAccepted)
108 }
109 })
110 }
111}