-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
complete_word_spec.rb
190 lines (140 loc) · 4.59 KB
/
complete_word_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true
RSpec.describe TTY::Reader, "complete word" do
let(:input) { StringIO.new }
let(:output) { StringIO.new }
let(:env) { {"TTY_TEST" => true} }
let(:left) { "\e[D" }
let(:options) {
{input: input, output: output, env: env,
completion_handler: ->(word) do
@completions.grep(/\A#{Regexp.escape(word)}/)
end}
}
subject(:reader) { described_class.new(**options) }
it "finds no completions for a word" do
@completions = %w[aa ab ac]
input << "x" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x\n")
output.rewind
expect(output.string).to eq("\e[2K\e[1Gx\e[2K\e[1Gx\e[2K\e[1Gx\n")
end
it "completes an empty line with the first suggestion" do
@completions = %w[aa ab ac]
input << "" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("aa\n")
output.rewind
expect(output.string).to eq("\e[2K\e[1Gaa\e[2K\e[1Gaa\n")
end
it "completes space inside line with the first suggestion" do
@completions = %w[aa ab ac]
input << "x" << " " << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aa\n")
end
it "completes a word using the first suggestion" do
@completions = %w[aa ab ac]
input << "x" << " " << "a" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aa\n")
end
it "completes a word using the next suggestion" do
@completions = %w[aa ab ac]
input << "x" << " " << "a" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x ab\n")
end
it "completes a word using the last suggestion" do
@completions = %w[aa ab ac]
input << "x " << "a" << "\t" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x ac\n")
end
it "cycles through completions back to the initial word" do
@completions = %w[aa ab ac]
input << "x " << "a" << "\t" << "\t" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x a\n")
end
it "cycles through completions and completes using the first suggestion" do
@completions = %w[aa ab ac]
input << "x " << "a" << "\t" << "\t" << "\t" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aa\n")
end
it "resets suggestions when a new character is entered" do
@completions = %w[aa ab ac]
input << "x " << "a" << "\t" << "\b" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aa\n")
end
it "completes edited text within a line" do
@completions = %w[aa ab ac]
input << "bb" << left << left << " " << left
input << "a" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("aa bb\n")
end
it "completes within a word" do
@completions = %w[aa ab ac]
input << "x " << "aa" << left << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aba\n")
end
it "completes a multiline input " do
@completions = %w[aa ab ac]
input << "a" << "\t" << "\n"
input << "a" << "\t" << "\t" << "\C-d"
input.rewind
answer = reader.read_multiline
expect(answer).to eq(%W[aa\n ab])
end
it "adds space suffix to suggested word completion on the first tab" do
@completions = %w[aa ab ac]
options[:completion_suffix] = " "
reader = described_class.new(**options)
input << "x " << "a" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x aa \n")
end
it "adds space suffix to suggested word completion on the second tab" do
@completions = %w[aa ab ac]
options[:completion_suffix] = " "
reader = described_class.new(**options)
input << "x " << "a" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x ab \n")
end
it "skips adding space suffix to the original word" do
@completions = %w[aa ab ac]
options[:completion_suffix] = " "
reader = described_class.new(**options)
input << "x " << "a" << "\t" << "\t" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x a\n")
end
it "adds two chars suffix to suggested word completion" do
@completions = %w[aa ab ac]
options[:completion_suffix] = "??"
reader = described_class.new(**options)
input << "x " << "a" << "\t" << "\t" << "\n"
input.rewind
answer = reader.read_line
expect(answer).to eq("x ab??\n")
end
end