-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAutoFormatTableCommandTests.cs
312 lines (260 loc) · 11.1 KB
/
AutoFormatTableCommandTests.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using Deveroom.VisualStudio.Diagonostics;
using Deveroom.VisualStudio.Editor.Commands;
using Deveroom.VisualStudio.VsxStubs;
using Deveroom.VisualStudio.VsxStubs.ProjectSystem;
using Xunit;
using Xunit.Abstractions;
namespace Deveroom.VisualStudio.Tests.Editor.Commands
{
public class AutoFormatTableCommandTests
{
private readonly StubIdeScope _ideScope;
private readonly TestText _unformattedText = new TestText(
@"Feature: foo", //12+2
@"Scenario: bar", //13+2 (29)
@"Given table", //11+2 (42)
@" | foo | bar |", //21+2 (65)
@" | bazbaz | qux | ", //31+2 (98)
@"| qu\\| c\n\| |", //20+2 (120)
@"");
private readonly TestText _expectedText = new TestText(
@"Feature: foo",
@"Scenario: bar",
@"Given table",
@" | foo | bar |",
@" | bazbaz | qux |",
@" | qu\\ | c\n\| |",
@"");
public AutoFormatTableCommandTests(ITestOutputHelper testOutputHelper)
{
_ideScope = new StubIdeScope(testOutputHelper);
}
private StubWpfTextView CreateTextView(TestText inputText, string newLine = null)
{
return StubWpfTextView.CreateTextView(_ideScope, inputText, newLine);
}
[Fact]
public void Formats_data_table_when_last_pipe_typed()
{
var command = CreateSUT();
var inputText = _unformattedText.Replace(-2, " |");
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, -1);
textView.SimulateType(command, '|');
Assert.Equal(_expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Formats_data_table_when_mid_pipe_typed()
{
var command = CreateSUT();
var inputText = _unformattedText.Replace(-3, "bazbaz | qux", "bazbaz qux");
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -3, " | bozboz".Length);
textView.SimulateType(command, '|');
Assert.Equal(_expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Formats_data_table_when_pipe_typed_of_an_incomplete_table()
{
var command = CreateSUT();
var inputText = _unformattedText.Replace(-2, @"| c\n\| |"); // remove last cell
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, -1);
textView.SimulateType(command, '|');
var expectedText = new TestText(
@"Feature: foo",
@"Scenario: bar",
@"Given table",
@" | foo | bar |",
@" | bazbaz | qux |",
@" | qu\\ |",
@"");
Assert.Equal(expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Should_not_remove_unfinished_cells_when_formattig_table()
{
var command = CreateSUT();
var inputText = new TestText(
@"Feature: foo",
@"Scenario: bar",
@"Given table",
@" | foo | bar ",
@" | foo | bar baz ",
@" | foo ",
@" | ",
@" | foo | ",
@" | foo | \| ",
@" | foo | \|",
@" | foo | bar \\| ",
@" | foo | bar ",
@"");
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, -1);
textView.SimulateType(command, '|');
var expectedText = new TestText(
@"Feature: foo",
@"Scenario: bar",
@"Given table",
@" | foo | bar",
@" | foo | bar baz",
@" | foo",
@" |",
@" | foo |",
@" | foo | \|",
@" | foo | \|",
@" | foo | bar \\ |",
@" | foo | bar |",
@"");
Assert.Equal(expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Should_not_format_data_table_when_masked_pipe_typed_of_an_incomplete_table()
{
var command = CreateSUT();
var inputText = _unformattedText.Replace(-2, @"c\n\| |", @" x\"); // remove last cell
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, -1);
textView.SimulateType(command, '|');
Assert.Equal(inputText.ToString().Replace(@"x\", @"x\|"), textView.TextSnapshot.GetText());
}
[Theory]
[InlineData("beginning of line", "", "")]
[InlineData("before first cell", " ", " ")]
[InlineData("right after pipe", " |", " | ")]
[InlineData("at padding after pipe", " | ", " | ")]
[InlineData("inside a cell", " | baz", " | baz")]
[InlineData("inside a non-first cell", " | bazbaz | q", " | bazbaz | q")]
[InlineData("non-existing cell position", " | bazbaz | qux ", " | bazbaz | qux ")]
[InlineData("after last pipe", " | bazbaz | qux |", null)]
[InlineData("end of line", null, null)]
public void Position_cursor_after_formatting(string test, string caretColonStr, string expectedCaretColonStr)
{
_ideScope.Logger.LogInfo($"running: {test}");
var caretColon = caretColonStr?.Length ?? -1;
var expectedCaretColon = expectedCaretColonStr?.Length ?? -1;
var command = CreateSUT();
var inputText = _unformattedText;
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -3, caretColon);
command.PostExec(textView, '|'); // this does not enter the char itself
Assert.Equal(_expectedText.ToString(), textView.TextSnapshot.GetText());
// cursor position preserved
_expectedText.AssertCaretAt(textView, -3, expectedCaretColon);
}
[Fact]
public void Formats_table_with_empty_and_comment_lines()
{
var command = CreateSUT();
var inputText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" #comment",
" | bazbaz | qux | ",
" ", // empty line
"| quux| corge| ",
"");
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, 0);
command.PostExec(textView, '|'); // this does not enter the char itself
var expectedText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" #comment",
" | bazbaz | qux |",
" ",
" | quux | corge |",
"");
Assert.Equal(expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Uses_indent_of_first_line()
{
var command = CreateSUT();
var inputText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux | ",
"| quux| corge| ",
"");
var textView = CreateTextView(inputText);
inputText.MoveCaretTo(textView, -2, 0);
command.PostExec(textView, '|'); // this does not enter the char itself
var expectedText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux |",
" | quux | corge |",
"");
Assert.Equal(expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Detects_line_ending()
{
var command = CreateSUT();
var inputText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux | ",
"| quux| corge| ",
"");
var textView = CreateTextView(inputText);
textView.StubEditorOptions.ReplicateNewLineCharacterOption = true;
textView.StubEditorOptions.NewLineCharacterOption = "\n"; // not used, because it detects
inputText.MoveCaretTo(textView, -2, 0);
command.PostExec(textView, '|'); // this does not enter the char itself
var expectedText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux |",
" | quux | corge |",
"");
Assert.Equal(expectedText.ToString(), textView.TextSnapshot.GetText());
}
[Fact]
public void Uses_configured_line_ending()
{
var command = CreateSUT();
var inputText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux | ",
"| quux| corge| ",
"");
var configuredNewLine = "\n";
var textView = CreateTextView(inputText, configuredNewLine);
textView.StubEditorOptions.ReplicateNewLineCharacterOption = false;
textView.StubEditorOptions.NewLineCharacterOption = configuredNewLine;
inputText.MoveCaretTo(textView, -2, 0);
command.PostExec(textView, '|'); // this does not enter the char itself
var expectedText = new TestText(
"Feature: foo",
"Scenario: bar",
"Given table",
" | foo | bar |",
" | bazbaz | qux |",
" | quux | corge |",
"");
Assert.Equal(expectedText.ToString(configuredNewLine), textView.TextSnapshot.GetText());
}
private AutoFormatTableCommand CreateSUT()
{
return new AutoFormatTableCommand(_ideScope, new StubBufferTagAggregatorFactoryService(_ideScope), _ideScope.MonitoringService);
}
}
}