~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// CompositeFormatStringParserTests.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
using NUnit.Framework;
 
27
using System.Linq;
 
28
using System;
 
29
using System.Collections.Generic;
 
30
 
 
31
namespace ICSharpCode.NRefactory.Utils
 
32
{
 
33
        [TestFixture]
 
34
        public class CompositeFormatStringParserTests
 
35
        {
 
36
 
 
37
                IList<IFormatStringSegment> ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments)
 
38
                {
 
39
                        var parser = new CompositeFormatStringParser();
 
40
                        var formatStringParseResult = parser.Parse(format);
 
41
                        var actualFormatSegments = formatStringParseResult.Segments;
 
42
 
 
43
                        Console.WriteLine("Expected format segments:");
 
44
                        foreach (var item in expectedFormatSegments) {
 
45
                                Console.WriteLine(item);
 
46
                        }
 
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);
 
52
                                }
 
53
                        }
 
54
 
 
55
                        Assert.AreEqual(expectedFormatSegments, actualFormatSegments);
 
56
                        return actualFormatSegments;
 
57
                }
 
58
 
 
59
                static IList<IFormatStringError> SegmentTest(int count, IFormatStringSegment segment)
 
60
                {
 
61
                        var errors = segment.Errors.ToList();
 
62
                        Assert.AreEqual(count, errors.Count, "Too many or too few errors.");
 
63
                        return errors;
 
64
                }
 
65
 
 
66
                static void ErrorTest(IFormatStringError error, string originalText, string replacementText, int startLocation, int endLocation)
 
67
                {
 
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.");
 
72
                }
 
73
 
 
74
                [Test]
 
75
                public void Index()
 
76
                {
 
77
                        ParseTest("{0}", new FormatItem(0) { StartLocation = 0, EndLocation = 3 });
 
78
                }
 
79
                
 
80
                [Test]
 
81
                public void PositiveAlignment()
 
82
                {
 
83
                        ParseTest("{0,4}", new FormatItem(0, 4) { StartLocation = 0, EndLocation = 5 });
 
84
                }
 
85
                
 
86
                [Test]
 
87
                public void NegativeAlignment()
 
88
                {
 
89
                        ParseTest("{0,-4}", new FormatItem(0, -4) { StartLocation = 0, EndLocation = 6 });
 
90
                }
 
91
                
 
92
                [Test]
 
93
                public void AlignmentWhiteSpace()
 
94
                {
 
95
                        ParseTest("{0, -4}", new FormatItem(0, -4) { StartLocation = 0, EndLocation = 7 });
 
96
                }
 
97
                
 
98
                [Test]
 
99
                public void SubFormatString()
 
100
                {
 
101
                        ParseTest("{0:aaaa}", new FormatItem(0, null, "aaaa") { StartLocation = 0, EndLocation = 8 });
 
102
                }
 
103
                
 
104
                [Test]
 
105
                public void CompleteFormatItem()
 
106
                {
 
107
                        ParseTest("{0, -45:aaaa}", new FormatItem(0, -45, "aaaa") { StartLocation = 0, EndLocation = 13 });
 
108
                }
 
109
                
 
110
                [Test]
 
111
                public void MultipleCompleteFormatItems()
 
112
                {
 
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 });
 
116
                }
 
117
                
 
118
                [Test]
 
119
                public void BraceEscape()
 
120
                {
 
121
                        var segments = ParseTest("{{}}", new TextSegment("{}"));
 
122
                        Assert.IsFalse(segments.First().HasErrors);
 
123
                }
 
124
                
 
125
                [Test]
 
126
                public void CloseAndEscapeAfterIndex()
 
127
                {
 
128
                        ParseTest("{0}}}",
 
129
                                  new FormatItem(0) { StartLocation = 0, EndLocation = 3},
 
130
                        new TextSegment("}") { StartLocation = 3, EndLocation = 5});
 
131
                }
 
132
                
 
133
                [Test]
 
134
                public void CloseAndEscapeAfterAlignment()
 
135
                {
 
136
                        ParseTest("{0,-15}}}",
 
137
                                  new FormatItem(0, -15) { StartLocation = 0, EndLocation = 7},
 
138
                        new TextSegment("}") { StartLocation = 7, EndLocation = 9});
 
139
                }
 
140
 
 
141
                [Test]
 
142
                public void TextSegment()
 
143
                {
 
144
                        ParseTest("Some Text", new TextSegment("Some Text"));
 
145
                }
 
146
 
 
147
                [Test]
 
148
                public void SingleCharacterTextSegment()
 
149
                {
 
150
                        ParseTest("A", new TextSegment("A"));
 
151
                }
 
152
                
 
153
                [Test]
 
154
                public void FormatStringWithPrefixText()
 
155
                {
 
156
                        ParseTest("Some Text {0}",
 
157
                                  new TextSegment("Some Text "),
 
158
                                  new FormatItem(0) { StartLocation = 10, EndLocation = 13 });
 
159
                }
 
160
                
 
161
                [Test]
 
162
                public void FormatStringWithPostfixText()
 
163
                {
 
164
                        ParseTest("{0} Some Text",
 
165
                                  new FormatItem(0)  { StartLocation = 0, EndLocation = 3 },
 
166
                                          new TextSegment(" Some Text", 3));
 
167
                }
 
168
                
 
169
                [Test]
 
170
                public void FormatStringWithEscapableBracesInSubFormatString()
 
171
                {
 
172
                        ParseTest("A weird string: {0:{{}}}",
 
173
                                  new TextSegment("A weird string: "),
 
174
                                  new FormatItem(0, null, "{}") { StartLocation = 16, EndLocation = 24 });
 
175
                }
 
176
                
 
177
                [Test]
 
178
                public void EmptySubFormatString()
 
179
                {
 
180
                        ParseTest("{0:}", new FormatItem(0, null, "") { StartLocation = 0, EndLocation = 4 });
 
181
                }
 
182
                
 
183
                [Test]
 
184
                public void EndsAfterOpenBrace()
 
185
                {
 
186
                        var segments = ParseTest("{", new TextSegment("{"));
 
187
                        var errors = SegmentTest(1, segments.First());
 
188
                        ErrorTest(errors[0], "{", "{{", 0, 1);
 
189
                }
 
190
                
 
191
                [Test]
 
192
                public void UnescapedOpenBracesInFixedText()
 
193
                {
 
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);
 
198
                }
 
199
                
 
200
                [Test]
 
201
                public void UnescapedLoneEndingBrace()
 
202
                {
 
203
                        var segments = ParseTest("Some text {", new TextSegment("Some text {"));
 
204
                        var errors = SegmentTest(1, segments.First());
 
205
                        ErrorTest(errors[0], "{", "{{", 10, 11);
 
206
                }
 
207
                
 
208
                [Test]
 
209
                public void EndAfterIndex()
 
210
                {
 
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);
 
216
                }
 
217
                
 
218
                [Test]
 
219
                public void EndAfterComma()
 
220
                {
 
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);
 
227
                }
 
228
                
 
229
                [Test]
 
230
                public void EndAfterCommaAndSpaces()
 
231
                {
 
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);
 
238
                }
 
239
                
 
240
                [Test]
 
241
                public void EndAfterAlignment()
 
242
                {
 
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);
 
248
                }
 
249
                
 
250
                [Test]
 
251
                public void EndAfterColon()
 
252
                {
 
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);
 
258
                }
 
259
                
 
260
                [Test]
 
261
                public void EndAfterSubFormatString()
 
262
                {
 
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);
 
268
                }
 
269
                
 
270
                [Test]
 
271
                public void MissingIndex()
 
272
                {
 
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);
 
278
                }
 
279
                
 
280
                [Test]
 
281
                public void MissingAlignment()
 
282
                {
 
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);
 
288
                }
 
289
                
 
290
                [Test]
 
291
                public void MissingEveryThing()
 
292
                {
 
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);
 
298
                }
 
299
                
 
300
                [Test]
 
301
                public void InvalidNumberFormatInIndex()
 
302
                {
 
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);
 
307
                }
 
308
                
 
309
                [Test]
 
310
                public void InvalidNumberFormatTextBeforeDigitsInIndex()
 
311
                {
 
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);
 
316
                }
 
317
                
 
318
                [Test]
 
319
                public void InvalidNumberFormatInAlignment()
 
320
                {
 
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);
 
325
                }
 
326
                
 
327
                [Test]
 
328
                public void InvalidNumberFormatTextBeforeDigitsInAlignment()
 
329
                {
 
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);
 
334
                }
 
335
                
 
336
                [Test]
 
337
                public void MissingEndBraceInsideFixedText()
 
338
                {
 
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);
 
345
                }
 
346
                
 
347
                [Test]
 
348
                public void MissingEndBraceInsideFixedTextEndingInAnotherFormatItem()
 
349
                {
 
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);
 
357
                }
 
358
                
 
359
                [Test]
 
360
                public void EndWithEscapedBrace()
 
361
                {
 
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);
 
365
                }
 
366
        }
 
367
}
 
368