2
// CompositeFormatStringParserTests.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
using NUnit.Framework;
29
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.Utils
34
public class CompositeFormatStringParserTests
37
IList<IFormatStringSegment> ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments)
39
var parser = new CompositeFormatStringParser();
40
var formatStringParseResult = parser.Parse(format);
41
var actualFormatSegments = formatStringParseResult.Segments;
43
Console.WriteLine("Expected format segments:");
44
foreach (var item in expectedFormatSegments) {
45
Console.WriteLine(item);
47
Console.WriteLine("Actual format segments:");
48
foreach (var item in actualFormatSegments) {
49
Console.WriteLine(item);
50
foreach (var error in item.Errors) {
51
Console.WriteLine("\t{0}", error);
55
Assert.AreEqual(expectedFormatSegments, actualFormatSegments);
56
return actualFormatSegments;
59
static IList<IFormatStringError> SegmentTest(int count, IFormatStringSegment segment)
61
var errors = segment.Errors.ToList();
62
Assert.AreEqual(count, errors.Count, "Too many or too few errors.");
66
static void ErrorTest(IFormatStringError error, string originalText, string replacementText, int startLocation, int endLocation)
68
Assert.AreEqual(originalText, error.OriginalText, "OriginalText is incorrect.");
69
Assert.AreEqual(replacementText, error.SuggestedReplacementText, "SuggestedReplacementText is incorrect.");
70
Assert.AreEqual(startLocation, error.StartLocation, "StartLocation is incorrect.");
71
Assert.AreEqual(endLocation, error.EndLocation, "EndLocation is incorrect.");
77
ParseTest("{0}", new FormatItem(0) { StartLocation = 0, EndLocation = 3 });
81
public void PositiveAlignment()
83
ParseTest("{0,4}", new FormatItem(0, 4) { StartLocation = 0, EndLocation = 5 });
87
public void NegativeAlignment()
89
ParseTest("{0,-4}", new FormatItem(0, -4) { StartLocation = 0, EndLocation = 6 });
93
public void AlignmentWhiteSpace()
95
ParseTest("{0, -4}", new FormatItem(0, -4) { StartLocation = 0, EndLocation = 7 });
99
public void SubFormatString()
101
ParseTest("{0:aaaa}", new FormatItem(0, null, "aaaa") { StartLocation = 0, EndLocation = 8 });
105
public void CompleteFormatItem()
107
ParseTest("{0, -45:aaaa}", new FormatItem(0, -45, "aaaa") { StartLocation = 0, EndLocation = 13 });
111
public void MultipleCompleteFormatItems()
113
ParseTest("{0, -45:aaaa}{3, 67:bbbb}",
114
new FormatItem(0, -45, "aaaa") { StartLocation = 0, EndLocation = 13 },
115
new FormatItem(3, 67, "bbbb") { StartLocation = 13, EndLocation = 25 });
119
public void BraceEscape()
121
var segments = ParseTest("{{}}", new TextSegment("{}"));
122
Assert.IsFalse(segments.First().HasErrors);
126
public void CloseAndEscapeAfterIndex()
129
new FormatItem(0) { StartLocation = 0, EndLocation = 3},
130
new TextSegment("}") { StartLocation = 3, EndLocation = 5});
134
public void CloseAndEscapeAfterAlignment()
136
ParseTest("{0,-15}}}",
137
new FormatItem(0, -15) { StartLocation = 0, EndLocation = 7},
138
new TextSegment("}") { StartLocation = 7, EndLocation = 9});
142
public void TextSegment()
144
ParseTest("Some Text", new TextSegment("Some Text"));
148
public void SingleCharacterTextSegment()
150
ParseTest("A", new TextSegment("A"));
154
public void FormatStringWithPrefixText()
156
ParseTest("Some Text {0}",
157
new TextSegment("Some Text "),
158
new FormatItem(0) { StartLocation = 10, EndLocation = 13 });
162
public void FormatStringWithPostfixText()
164
ParseTest("{0} Some Text",
165
new FormatItem(0) { StartLocation = 0, EndLocation = 3 },
166
new TextSegment(" Some Text", 3));
170
public void FormatStringWithEscapableBracesInSubFormatString()
172
ParseTest("A weird string: {0:{{}}}",
173
new TextSegment("A weird string: "),
174
new FormatItem(0, null, "{}") { StartLocation = 16, EndLocation = 24 });
178
public void EmptySubFormatString()
180
ParseTest("{0:}", new FormatItem(0, null, "") { StartLocation = 0, EndLocation = 4 });
184
public void EndsAfterOpenBrace()
186
var segments = ParseTest("{", new TextSegment("{"));
187
var errors = SegmentTest(1, segments.First());
188
ErrorTest(errors[0], "{", "{{", 0, 1);
192
public void UnescapedOpenBracesInFixedText()
194
var segments = ParseTest("a { { a", new TextSegment("a { { a"));
195
var errors = SegmentTest(2, segments.First());
196
ErrorTest(errors[0], "{", "{{", 2, 3);
197
ErrorTest(errors[1], "{", "{{", 4, 5);
201
public void UnescapedLoneEndingBrace()
203
var segments = ParseTest("Some text {", new TextSegment("Some text {"));
204
var errors = SegmentTest(1, segments.First());
205
ErrorTest(errors[0], "{", "{{", 10, 11);
209
public void EndAfterIndex()
211
var segments = ParseTest("Some text {0",
212
new TextSegment("Some text "),
213
new FormatItem(0) { StartLocation = 10, EndLocation = 12 });
214
var errors = SegmentTest(1, segments.Skip(1).First());
215
ErrorTest(errors[0], "", "}", 12, 12);
219
public void EndAfterComma()
221
var segments = ParseTest("Some text {0,",
222
new TextSegment("Some text "),
223
new FormatItem(0, 0) { StartLocation = 10, EndLocation = 13 });
224
var errors = SegmentTest(2, segments.Skip(1).First());
225
ErrorTest(errors[0], "", "0", 13, 13);
226
ErrorTest(errors[1], "", "}", 13, 13);
230
public void EndAfterCommaAndSpaces()
232
var segments = ParseTest("Some text {0, ",
233
new TextSegment("Some text "),
234
new FormatItem(0, 0) { StartLocation = 10, EndLocation = 16 });
235
var errors = SegmentTest(2, segments.Skip(1).First());
236
ErrorTest(errors[0], "", "0", 16, 16);
237
ErrorTest(errors[1], "", "}", 16, 16);
241
public void EndAfterAlignment()
243
var segments = ParseTest("Some text {0, -34",
244
new TextSegment("Some text "),
245
new FormatItem(0, -34) { StartLocation = 10, EndLocation = 17 });
246
var errors = SegmentTest(1, segments.Skip(1).First());
247
ErrorTest(errors[0], "", "}", 17, 17);
251
public void EndAfterColon()
253
var segments = ParseTest("Some text {0:",
254
new TextSegment("Some text "),
255
new FormatItem(0, null, "") { StartLocation = 10, EndLocation = 13 });
256
var errors = SegmentTest(1, segments.Skip(1).First());
257
ErrorTest(errors[0], "", "}", 13, 13);
261
public void EndAfterSubFormatString()
263
var segments = ParseTest("Some text {0: asdf",
264
new TextSegment("Some text "),
265
new FormatItem(0, null, " asdf") { StartLocation = 10, EndLocation = 18 });
266
var errors = SegmentTest(1, segments.Skip(1).First());
267
ErrorTest(errors[0], "", "}", 18, 18);
271
public void MissingIndex()
273
var segments = ParseTest("Some text {}",
274
new TextSegment("Some text "),
275
new FormatItem(0) { StartLocation = 10, EndLocation = 12 });
276
var errors = SegmentTest(1, segments.Skip(1).First());
277
ErrorTest(errors[0], "", "0", 11, 11);
281
public void MissingAlignment()
283
var segments = ParseTest("Some text {0,}",
284
new TextSegment("Some text "),
285
new FormatItem(0, 0) { StartLocation = 10, EndLocation = 14 });
286
var errors = SegmentTest(1, segments.Skip(1).First());
287
ErrorTest(errors[0], "", "0", 13, 13);
291
public void MissingEveryThing()
293
var segments = ParseTest("{,:", new FormatItem(0, 0, "") { StartLocation = 0, EndLocation = 3 });
294
var errors = SegmentTest(3, segments.First());
295
ErrorTest(errors[0], "", "0", 1, 1);
296
ErrorTest(errors[1], "", "0", 2, 2);
297
ErrorTest(errors[2], "", "}", 3, 3);
301
public void InvalidNumberFormatInIndex()
303
var segments = ParseTest("{0 and then some invalid text}",
304
new FormatItem(0) { StartLocation = 0, EndLocation = 30 });
305
var errors = SegmentTest(1, segments.First());
306
ErrorTest(errors[0], "0 and then some invalid text", "0", 1, 29);
310
public void InvalidNumberFormatTextBeforeDigitsInIndex()
312
var segments = ParseTest("{Some text 55}",
313
new FormatItem(0) { StartLocation = 0, EndLocation = 14 });
314
var errors = SegmentTest(1, segments.First());
315
ErrorTest(errors[0], "Some text 55", "0", 1, 13);
319
public void InvalidNumberFormatInAlignment()
321
var segments = ParseTest("{0, 100 and then some invalid text}",
322
new FormatItem(0, 100) { StartLocation = 0, EndLocation = 35 });
323
var errors = SegmentTest(1, segments.First());
324
ErrorTest(errors[0], " 100 and then some invalid text", "100", 3, 34);
328
public void InvalidNumberFormatTextBeforeDigitsInAlignment()
330
var segments = ParseTest("{0, Some text 55}",
331
new FormatItem(0, 0) { StartLocation = 0, EndLocation = 17 });
332
var errors = SegmentTest(1, segments.First());
333
ErrorTest(errors[0], " Some text 55", "0", 3, 16);
337
public void MissingEndBraceInsideFixedText()
339
var segments = ParseTest("Text {0 Text",
340
new TextSegment("Text "),
341
new FormatItem(0) { StartLocation = 5, EndLocation = 7 },
342
new TextSegment(" Text", 7));
343
var errors = SegmentTest(1, segments.Skip(1).First());
344
ErrorTest(errors[0], "", "}", 7, 7);
348
public void MissingEndBraceInsideFixedTextEndingInAnotherFormatItem()
350
var segments = ParseTest("Text {0 Text {1}",
351
new TextSegment("Text "),
352
new FormatItem(0) { StartLocation = 5, EndLocation = 7 },
353
new TextSegment(" Text ", 7),
354
new FormatItem(1) { StartLocation = 13, EndLocation = 16 });
355
var errors = SegmentTest(1, segments.Skip(1).First());
356
ErrorTest(errors[0], "", "}", 7, 7);
360
public void EndWithEscapedBrace()
362
var segments = ParseTest("{0:}}", new FormatItem(0, null, "}") { StartLocation = 0, EndLocation = 5 });
363
var errors = SegmentTest(1, segments.First());
364
ErrorTest(errors[0], "", "}", 5, 5);