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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Bugs/ParserBugTests.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
// ParserBugTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
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 System;
 
27
using NUnit.Framework;
 
28
using ICSharpCode.NRefactory.CSharp;
 
29
using System.Linq;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Parser.Bugs
 
32
{
 
33
        [TestFixture]
 
34
        public class ParserBugTests
 
35
        {
 
36
                /// <summary>
 
37
                /// Bug 4252 - override bug in mcs ast
 
38
                /// </summary>
 
39
                [Test]
 
40
                public void TestBug4252()
 
41
                {
 
42
                        string code = @"
 
43
class Foo
 
44
{
 
45
 
 
46
    class Bar
 
47
    {
 
48
          override foo
 
49
    }
 
50
 
 
51
    public Foo () 
 
52
    {
 
53
    }
 
54
}";
 
55
                        var unit = SyntaxTree.Parse(code);
 
56
                        var type = unit.Members.First() as TypeDeclaration;
 
57
                        var constructor = type.Members.Skip(1).First() as ConstructorDeclaration;
 
58
                        var passed = !constructor.HasModifier(Modifiers.Override);
 
59
                        if (!passed) {
 
60
                                Console.WriteLine("Expected:" + code);
 
61
                                Console.WriteLine("Was:" + unit.GetText());
 
62
                        }
 
63
                        Assert.IsTrue(passed);
 
64
                }
 
65
                
 
66
                /// <summary>
 
67
                /// Bug 4059 - Return statement without semicolon missing in the AST
 
68
                /// </summary>
 
69
                [Test]
 
70
                public void TestBug4059()
 
71
                {
 
72
                        string code = @"
 
73
class Stub
 
74
{
 
75
    Test A ()
 
76
    {
 
77
        return new Test ()
 
78
    }
 
79
}";
 
80
                        var unit = SyntaxTree.Parse(code);
 
81
                        var type = unit.Members.First() as TypeDeclaration;
 
82
                        var method = type.Members.First() as MethodDeclaration;
 
83
                        bool passed = method.Body.Statements.FirstOrDefault() is ReturnStatement;
 
84
                        if (!passed) {
 
85
                                Console.WriteLine("Expected:" + code);
 
86
                                Console.WriteLine("Was:" + unit.GetText());
 
87
                        }
 
88
                        Assert.IsTrue(passed);
 
89
                }
 
90
                
 
91
                /// <summary>
 
92
                /// Bug 4058 - Unattached parameter attributes should be included in the AST
 
93
                /// </summary>
 
94
                [Test]
 
95
                public void TestBug4058()
 
96
                {
 
97
                        string code = @"
 
98
class TestClass
 
99
{
 
100
  TestClass([attr])
 
101
  {
 
102
  }
 
103
}";
 
104
                        var unit = SyntaxTree.Parse(code);
 
105
                        
 
106
                        var type = unit.Members.First() as TypeDeclaration;
 
107
                        var constructor = type.Members.First() as ConstructorDeclaration;
 
108
                        bool passed = constructor.GetNodeAt<AttributeSection>(constructor.LParToken.StartLocation.Line, constructor.LParToken.StartLocation.Column + 1) != null;
 
109
                        if (!passed) {
 
110
                                Console.WriteLine("Expected:" + code);
 
111
                                Console.WriteLine("Was:" + unit.GetText());
 
112
                        }
 
113
                        Assert.IsTrue(passed);
 
114
                }
 
115
                
 
116
                
 
117
                
 
118
                /// <summary>
 
119
                /// Bug 3952 - Syntax errors that causes AST not inserted 
 
120
                /// </summary>
 
121
                [Ignore("Still open 03/2013")]
 
122
                [Test]
 
123
                public void TestBug3952()
 
124
                {
 
125
                        string code = @"
 
126
class Foo
 
127
{
 
128
        void Bar()
 
129
        {
 
130
                Test(new Foo (
 
131
        }
 
132
}";
 
133
                        var unit = SyntaxTree.Parse(code);
 
134
                        
 
135
                        var type = unit.Members.First() as TypeDeclaration;
 
136
                        var method = type.Members.First() as MethodDeclaration;
 
137
                        bool passed = !method.Body.IsNull;
 
138
                        if (!passed) {
 
139
                                Console.WriteLine("Expected:" + code);
 
140
                                Console.WriteLine("Was:" + unit.GetText());
 
141
                        }
 
142
                        Assert.IsTrue(passed);
 
143
                }
 
144
                
 
145
                /// <summary>
 
146
                /// Bug 3578 - For condition not in the AST.
 
147
                /// </summary>
 
148
                [Test]
 
149
                public void TestBug3578()
 
150
                {
 
151
                        string code = 
 
152
@"class Foo
 
153
{
 
154
        void Bar ()
 
155
        {
 
156
                for (int i = 0; i < foo.bar)
 
157
        }
 
158
}";
 
159
                        var unit = SyntaxTree.Parse(code);
 
160
                        
 
161
                        bool passed = @"class Foo
 
162
{
 
163
        void Bar ()
 
164
        {
 
165
                for (int i = 0; i < foo.bar;)
 
166
        }
 
167
}" == unit.GetText ().Trim ();
 
168
                        if (!passed) {
 
169
                                Console.WriteLine("Expected:" + code);
 
170
                                Console.WriteLine("Was:" + unit.GetText());
 
171
                        }
 
172
                        Assert.IsTrue(passed);
 
173
                }
 
174
                
 
175
                /// <summary>
 
176
                /// Bug 3517 - Incomplete conditional operator in the AST request.
 
177
                /// </summary>
 
178
                [Test]
 
179
                public void TestBug3517()
 
180
                {
 
181
                        string code = 
 
182
@"class Test
 
183
{
 
184
        void Foo ()
 
185
        {
 
186
                a = cond ? expr
 
187
        }
 
188
}";
 
189
                        var unit = SyntaxTree.Parse(code);
 
190
                        
 
191
                        var type = unit.Members.First() as TypeDeclaration;
 
192
                        var method = type.Members.First() as MethodDeclaration;
 
193
                        var exprStmt = method.Body.Statements.FirstOrDefault() as ExpressionStatement;
 
194
                        var expr = exprStmt.Expression as AssignmentExpression;
 
195
                        bool passed = expr != null && expr.Right is ConditionalExpression;
 
196
                        
 
197
                        if (!passed) {
 
198
                                Console.WriteLine("Expected:" + code);
 
199
                                Console.WriteLine("Was:" + unit.GetText());
 
200
                        }
 
201
                        Assert.IsTrue(passed);
 
202
                }
 
203
                
 
204
                [Test]
 
205
                public void TestBug3517Case2()
 
206
                {
 
207
                        string code = 
 
208
@"class Test
 
209
{
 
210
        void Foo ()
 
211
        {
 
212
                a = cond ? expr :
 
213
        }
 
214
}";
 
215
                        var unit = SyntaxTree.Parse(code);
 
216
                        
 
217
                        var type = unit.Members.First() as TypeDeclaration;
 
218
                        var method = type.Members.First() as MethodDeclaration;
 
219
                        var exprStmt = method.Body.Statements.FirstOrDefault() as ExpressionStatement;
 
220
                        var expr = exprStmt.Expression as AssignmentExpression;
 
221
                        bool passed = expr != null && expr.Right is ConditionalExpression;
 
222
                        
 
223
                        if (!passed) {
 
224
                                Console.WriteLine("Expected:" + code);
 
225
                                Console.WriteLine("Was:" + unit.GetText());
 
226
                        }
 
227
                        Assert.IsTrue(passed);
 
228
                }
 
229
                
 
230
                /// <summary>
 
231
                /// Bug 3468 - Local variable declarations are not inserted in ast & break declaring member locations.
 
232
                /// </summary>
 
233
                [Test]
 
234
                public void TestBug3468()
 
235
                {
 
236
                        string code = 
 
237
@"class C
 
238
{
 
239
    public static void Main (string[] args)
 
240
    {
 
241
        string str = 
 
242
    }
 
243
}";
 
244
                        var unit = SyntaxTree.Parse(code);
 
245
                        
 
246
                        var type = unit.Members.First() as TypeDeclaration;
 
247
                        var method = type.Members.First() as MethodDeclaration;
 
248
                        bool passed = !method.Body.IsNull;
 
249
                        if (!passed) {
 
250
                                Console.WriteLine("Expected:" + code);
 
251
                                Console.WriteLine("Was:" + unit.GetText());
 
252
                        }
 
253
                        Assert.IsTrue(passed);
 
254
                }
 
255
                
 
256
                /// <summary>
 
257
                /// Bug 3288 - Try ... catch not added to the ast if catch block is missing
 
258
                /// </summary>
 
259
                [Test]
 
260
                public void TestBug3288()
 
261
                {
 
262
                        string code = 
 
263
@"class Test
 
264
{
 
265
        public void Main(string[] args)
 
266
        {
 
267
                try {
 
268
                } catch (Exception name)
 
269
        }
 
270
}";
 
271
                        var unit = SyntaxTree.Parse(code);
 
272
                        
 
273
                        var type = unit.Members.First() as TypeDeclaration;
 
274
                        var method = type.Members.First() as MethodDeclaration;
 
275
                        bool passed = method.Body.Statements.FirstOrDefault() is TryCatchStatement;
 
276
                        if (!passed) {
 
277
                                Console.WriteLine("Expected:" + code);
 
278
                                Console.WriteLine("Was:" + unit.GetText());
 
279
                        }
 
280
                        Assert.IsTrue(passed);
 
281
                }
 
282
                
 
283
                /// <summary>
 
284
                /// Bug 3155 - Anonymous methods in variable declarations don't produce an ast, if the ';' is missing.
 
285
                /// </summary>
 
286
                [Test]
 
287
                public void TestBug3155()
 
288
                {
 
289
                        string code = 
 
290
@"using System;
 
291
 
 
292
class Test
 
293
{
 
294
    void Foo ()
 
295
    {
 
296
        Action<int> act = delegate (int testMe) {
 
297
 
 
298
        }
 
299
    }
 
300
}
 
301
";
 
302
                        var unit = SyntaxTree.Parse(code);
 
303
                        
 
304
                        bool passed = unit.GetText().Trim() == @"using System;
 
305
class Test
 
306
{
 
307
        void Foo ()
 
308
        {
 
309
                Action<int> act = delegate (int testMe) {
 
310
                };
 
311
        }
 
312
}";
 
313
                        if (!passed) {
 
314
                                Console.WriteLine("Expected:" + code);
 
315
                                Console.WriteLine("Was:" + unit.GetText());
 
316
                        }
 
317
                        Assert.IsTrue(passed);
 
318
                }
 
319
 
 
320
                /// <summary>
 
321
                /// Bug 4556 - AST broken for unclosed invocation
 
322
                /// </summary>
 
323
                [Test]
 
324
                public void TestBug4556()
 
325
                {
 
326
                        string code = 
 
327
@"using System;
 
328
 
 
329
class Foo
 
330
{
 
331
    public static void Main (string[] args)
 
332
    {
 
333
        Console.WriteLine (""foo"", 
 
334
    }
 
335
}
 
336
";
 
337
                        var unit = SyntaxTree.Parse(code);
 
338
                        var type = unit.Members.First(m => m is TypeDeclaration) as TypeDeclaration;
 
339
                        var method = type.Members.First() as MethodDeclaration;
 
340
                        bool passed = !method.Body.IsNull;
 
341
                        if (!passed) {
 
342
                                Console.WriteLine("Expected:" + code);
 
343
                                Console.WriteLine("Was:" + unit.GetText());
 
344
                        }
 
345
                        Assert.IsTrue(passed);
 
346
                }
 
347
                
 
348
                /// <summary>
 
349
                /// Bug 5064 - Autocomplete doesn't include object initializer properties in yield return 
 
350
                /// </summary>
 
351
                [Test]
 
352
                public void TestBug5064()
 
353
                {
 
354
                        string code = 
 
355
@"public class Bar
 
356
{
 
357
    public IEnumerable<Foo> GetFoos()
 
358
    {
 
359
        yield return new Foo { }
 
360
    }
 
361
}";
 
362
                        var unit = SyntaxTree.Parse(code);
 
363
                        
 
364
                        string text = unit.GetText().Trim();
 
365
                        string expected = @"public class Bar
 
366
{
 
367
        public IEnumerable<Foo> GetFoos()
 
368
        {
 
369
                yield return new Foo { };
 
370
        }
 
371
}";
 
372
                        int i = 0, j = 0;
 
373
                        while (i < text.Length && j < expected.Length) {
 
374
                                if (char.IsWhiteSpace (text[i])) {
 
375
                                        i++;
 
376
                                        continue;
 
377
                                }
 
378
                                if (char.IsWhiteSpace (expected[j])) {
 
379
                                        j++;
 
380
                                        continue;
 
381
                                }
 
382
                                if (text [i] != expected [j]) {
 
383
                                        break;
 
384
                                }
 
385
                                i++;j++;
 
386
                        }
 
387
                        bool passed = i == text.Length && j == expected.Length;
 
388
                        if (!passed) {
 
389
                                Console.WriteLine("Expected:" + expected);
 
390
                                Console.WriteLine("Was:" + unit.GetText());
 
391
                        }
 
392
                        Assert.IsTrue(passed);
 
393
                }
 
394
 
 
395
                /// <summary>
 
396
                /// Bug 5389 - Code completion does not work well inside a dictionary initializer
 
397
                /// </summary>
 
398
                [Test()]
 
399
                public void TestBug5389()
 
400
                {
 
401
                        string code = 
 
402
                                @"class Foo
 
403
{
 
404
        static Dictionary<Tuple<Type, string>, string> CreatePropertyMap ()
 
405
        {
 
406
                return new Dictionary<Tuple<Type, string>, string> {
 
407
                        { Tuple.Create (typeof (MainClass), ""Prop1""), ""Prop2"" },
 
408
                        { Tuple.C }
 
409
                }
 
410
        }
 
411
}
 
412
";
 
413
                        var unit = SyntaxTree.Parse(code);
 
414
                        
 
415
                        var type = unit.Members.First() as TypeDeclaration;
 
416
                        var method = type.Members.First() as MethodDeclaration;
 
417
                        var stmt = method.Body.Statements.First () as ReturnStatement;
 
418
                        bool passed = stmt.Expression is ObjectCreateExpression;
 
419
                        if (!passed) {
 
420
                                Console.WriteLine("Expected:" + code);
 
421
                                Console.WriteLine("Was:" + unit.GetText());
 
422
                        }
 
423
                        Assert.IsTrue(passed);
 
424
                }
 
425
 
 
426
                [Test()]
 
427
                public void TestIncompleteParameter()
 
428
                {
 
429
                        string code = 
 
430
                                @"class Foo
 
431
{
 
432
        void Bar (params System.A) {}
 
433
}
 
434
";
 
435
                        var unit = SyntaxTree.Parse(code);
 
436
                        
 
437
                        var type = unit.Members.First() as TypeDeclaration;
 
438
                        var method = type.Members.First() as MethodDeclaration;
 
439
                        bool passed = method.Parameters.Count == 1;
 
440
                        if (!passed) {
 
441
                                Console.WriteLine("Expected:" + code);
 
442
                                Console.WriteLine("Was:" + unit.GetText());
 
443
                        }
 
444
                        Assert.IsTrue(passed);
 
445
                }
 
446
        }
 
447
}
 
448