5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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
27
using NUnit.Framework;
28
using ICSharpCode.NRefactory.CSharp;
31
namespace ICSharpCode.NRefactory.CSharp.Parser.Bugs
34
public class ParserBugTests
37
/// Bug 4252 - override bug in mcs ast
40
public void TestBug4252()
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);
60
Console.WriteLine("Expected:" + code);
61
Console.WriteLine("Was:" + unit.GetText());
63
Assert.IsTrue(passed);
67
/// Bug 4059 - Return statement without semicolon missing in the AST
70
public void TestBug4059()
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;
85
Console.WriteLine("Expected:" + code);
86
Console.WriteLine("Was:" + unit.GetText());
88
Assert.IsTrue(passed);
92
/// Bug 4058 - Unattached parameter attributes should be included in the AST
95
public void TestBug4058()
104
var unit = SyntaxTree.Parse(code);
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;
110
Console.WriteLine("Expected:" + code);
111
Console.WriteLine("Was:" + unit.GetText());
113
Assert.IsTrue(passed);
119
/// Bug 3952 - Syntax errors that causes AST not inserted
121
[Ignore("Still open 03/2013")]
123
public void TestBug3952()
133
var unit = SyntaxTree.Parse(code);
135
var type = unit.Members.First() as TypeDeclaration;
136
var method = type.Members.First() as MethodDeclaration;
137
bool passed = !method.Body.IsNull;
139
Console.WriteLine("Expected:" + code);
140
Console.WriteLine("Was:" + unit.GetText());
142
Assert.IsTrue(passed);
146
/// Bug 3578 - For condition not in the AST.
149
public void TestBug3578()
156
for (int i = 0; i < foo.bar)
159
var unit = SyntaxTree.Parse(code);
161
bool passed = @"class Foo
165
for (int i = 0; i < foo.bar;)
167
}" == unit.GetText ().Trim ();
169
Console.WriteLine("Expected:" + code);
170
Console.WriteLine("Was:" + unit.GetText());
172
Assert.IsTrue(passed);
176
/// Bug 3517 - Incomplete conditional operator in the AST request.
179
public void TestBug3517()
189
var unit = SyntaxTree.Parse(code);
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;
198
Console.WriteLine("Expected:" + code);
199
Console.WriteLine("Was:" + unit.GetText());
201
Assert.IsTrue(passed);
205
public void TestBug3517Case2()
215
var unit = SyntaxTree.Parse(code);
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;
224
Console.WriteLine("Expected:" + code);
225
Console.WriteLine("Was:" + unit.GetText());
227
Assert.IsTrue(passed);
231
/// Bug 3468 - Local variable declarations are not inserted in ast & break declaring member locations.
234
public void TestBug3468()
239
public static void Main (string[] args)
244
var unit = SyntaxTree.Parse(code);
246
var type = unit.Members.First() as TypeDeclaration;
247
var method = type.Members.First() as MethodDeclaration;
248
bool passed = !method.Body.IsNull;
250
Console.WriteLine("Expected:" + code);
251
Console.WriteLine("Was:" + unit.GetText());
253
Assert.IsTrue(passed);
257
/// Bug 3288 - Try ... catch not added to the ast if catch block is missing
260
public void TestBug3288()
265
public void Main(string[] args)
268
} catch (Exception name)
271
var unit = SyntaxTree.Parse(code);
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;
277
Console.WriteLine("Expected:" + code);
278
Console.WriteLine("Was:" + unit.GetText());
280
Assert.IsTrue(passed);
284
/// Bug 3155 - Anonymous methods in variable declarations don't produce an ast, if the ';' is missing.
287
public void TestBug3155()
296
Action<int> act = delegate (int testMe) {
302
var unit = SyntaxTree.Parse(code);
304
bool passed = unit.GetText().Trim() == @"using System;
309
Action<int> act = delegate (int testMe) {
314
Console.WriteLine("Expected:" + code);
315
Console.WriteLine("Was:" + unit.GetText());
317
Assert.IsTrue(passed);
321
/// Bug 4556 - AST broken for unclosed invocation
324
public void TestBug4556()
331
public static void Main (string[] args)
333
Console.WriteLine (""foo"",
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;
342
Console.WriteLine("Expected:" + code);
343
Console.WriteLine("Was:" + unit.GetText());
345
Assert.IsTrue(passed);
349
/// Bug 5064 - Autocomplete doesn't include object initializer properties in yield return
352
public void TestBug5064()
357
public IEnumerable<Foo> GetFoos()
359
yield return new Foo { }
362
var unit = SyntaxTree.Parse(code);
364
string text = unit.GetText().Trim();
365
string expected = @"public class Bar
367
public IEnumerable<Foo> GetFoos()
369
yield return new Foo { };
373
while (i < text.Length && j < expected.Length) {
374
if (char.IsWhiteSpace (text[i])) {
378
if (char.IsWhiteSpace (expected[j])) {
382
if (text [i] != expected [j]) {
387
bool passed = i == text.Length && j == expected.Length;
389
Console.WriteLine("Expected:" + expected);
390
Console.WriteLine("Was:" + unit.GetText());
392
Assert.IsTrue(passed);
396
/// Bug 5389 - Code completion does not work well inside a dictionary initializer
399
public void TestBug5389()
404
static Dictionary<Tuple<Type, string>, string> CreatePropertyMap ()
406
return new Dictionary<Tuple<Type, string>, string> {
407
{ Tuple.Create (typeof (MainClass), ""Prop1""), ""Prop2"" },
413
var unit = SyntaxTree.Parse(code);
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;
420
Console.WriteLine("Expected:" + code);
421
Console.WriteLine("Was:" + unit.GetText());
423
Assert.IsTrue(passed);
427
public void TestIncompleteParameter()
432
void Bar (params System.A) {}
435
var unit = SyntaxTree.Parse(code);
437
var type = unit.Members.First() as TypeDeclaration;
438
var method = type.Members.First() as MethodDeclaration;
439
bool passed = method.Parameters.Count == 1;
441
Console.WriteLine("Expected:" + code);
442
Console.WriteLine("Was:" + unit.GetText());
444
Assert.IsTrue(passed);