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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
//
// ParserBugTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Parser.Bugs
{
[TestFixture]
public class ParserBugTests
{
/// <summary>
/// Bug 4252 - override bug in mcs ast
/// </summary>
[Test]
public void TestBug4252()
{
string code = @"
class Foo
{
class Bar
{
override foo
}
public Foo ()
{
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var constructor = type.Members.Skip(1).First() as ConstructorDeclaration;
var passed = !constructor.HasModifier(Modifiers.Override);
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 4059 - Return statement without semicolon missing in the AST
/// </summary>
[Test]
public void TestBug4059()
{
string code = @"
class Stub
{
Test A ()
{
return new Test ()
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = method.Body.Statements.FirstOrDefault() is ReturnStatement;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 4058 - Unattached parameter attributes should be included in the AST
/// </summary>
[Test]
public void TestBug4058()
{
string code = @"
class TestClass
{
TestClass([attr])
{
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var constructor = type.Members.First() as ConstructorDeclaration;
bool passed = constructor.GetNodeAt<AttributeSection>(constructor.LParToken.StartLocation.Line, constructor.LParToken.StartLocation.Column + 1) != null;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3952 - Syntax errors that causes AST not inserted
/// </summary>
[Ignore("Still open 03/2013")]
[Test]
public void TestBug3952()
{
string code = @"
class Foo
{
void Bar()
{
Test(new Foo (
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = !method.Body.IsNull;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3578 - For condition not in the AST.
/// </summary>
[Test]
public void TestBug3578()
{
string code =
@"class Foo
{
void Bar ()
{
for (int i = 0; i < foo.bar)
}
}";
var unit = SyntaxTree.Parse(code);
bool passed = @"class Foo
{
void Bar ()
{
for (int i = 0; i < foo.bar;)
}
}" == unit.ToString().Trim ();
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3517 - Incomplete conditional operator in the AST request.
/// </summary>
[Test]
public void TestBug3517()
{
string code =
@"class Test
{
void Foo ()
{
a = cond ? expr
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
var exprStmt = method.Body.Statements.FirstOrDefault() as ExpressionStatement;
var expr = exprStmt.Expression as AssignmentExpression;
bool passed = expr != null && expr.Right is ConditionalExpression;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
[Test]
public void TestBug3517Case2()
{
string code =
@"class Test
{
void Foo ()
{
a = cond ? expr :
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
var exprStmt = method.Body.Statements.FirstOrDefault() as ExpressionStatement;
var expr = exprStmt.Expression as AssignmentExpression;
bool passed = expr != null && expr.Right is ConditionalExpression;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3468 - Local variable declarations are not inserted in ast & break declaring member locations.
/// </summary>
[Test]
public void TestBug3468()
{
string code =
@"class C
{
public static void Main (string[] args)
{
string str =
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = !method.Body.IsNull;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3288 - Try ... catch not added to the ast if catch block is missing
/// </summary>
[Test]
public void TestBug3288()
{
string code =
@"class Test
{
public void Main(string[] args)
{
try {
} catch (Exception name)
}
}";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = method.Body.Statements.FirstOrDefault() is TryCatchStatement;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 3155 - Anonymous methods in variable declarations don't produce an ast, if the ';' is missing.
/// </summary>
[Test]
public void TestBug3155()
{
string code =
@"using System;
class Test
{
void Foo ()
{
Action<int> act = delegate (int testMe) {
}
}
}
";
var unit = SyntaxTree.Parse(code);
bool passed = unit.ToString().Trim() == @"using System;
class Test
{
void Foo ()
{
Action<int> act = delegate (int testMe) {
};
}
}";
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 4556 - AST broken for unclosed invocation
/// </summary>
[Test]
public void TestBug4556()
{
string code =
@"using System;
class Foo
{
public static void Main (string[] args)
{
Console.WriteLine (""foo"",
}
}
";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First(m => m is TypeDeclaration) as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = !method.Body.IsNull;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 5064 - Autocomplete doesn't include object initializer properties in yield return
/// </summary>
[Test]
public void TestBug5064()
{
string code =
@"public class Bar
{
public IEnumerable<Foo> GetFoos()
{
yield return new Foo { }
}
}";
var unit = SyntaxTree.Parse(code);
string text = unit.ToString().Trim();
string expected = @"public class Bar
{
public IEnumerable<Foo> GetFoos()
{
yield return new Foo { };
}
}";
int i = 0, j = 0;
while (i < text.Length && j < expected.Length) {
if (char.IsWhiteSpace (text[i])) {
i++;
continue;
}
if (char.IsWhiteSpace (expected[j])) {
j++;
continue;
}
if (text [i] != expected [j]) {
break;
}
i++;j++;
}
bool passed = i == text.Length && j == expected.Length;
if (!passed) {
Console.WriteLine("Expected:" + expected);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
/// <summary>
/// Bug 5389 - Code completion does not work well inside a dictionary initializer
/// </summary>
[Test()]
public void TestBug5389()
{
string code =
@"class Foo
{
static Dictionary<Tuple<Type, string>, string> CreatePropertyMap ()
{
return new Dictionary<Tuple<Type, string>, string> {
{ Tuple.Create (typeof (MainClass), ""Prop1""), ""Prop2"" },
{ Tuple.C }
}
}
}
";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
var stmt = method.Body.Statements.First () as ReturnStatement;
bool passed = stmt.Expression is ObjectCreateExpression;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
[Test()]
public void TestIncompleteParameter()
{
string code =
@"class Foo
{
void Bar (params System.A) {}
}
";
var unit = SyntaxTree.Parse(code);
var type = unit.Members.First() as TypeDeclaration;
var method = type.Members.First() as MethodDeclaration;
bool passed = method.Parameters.Count == 1;
if (!passed) {
Console.WriteLine("Expected:" + code);
Console.WriteLine("Was:" + unit);
}
Assert.IsTrue(passed);
}
[Test]
public void TestMultipleNestedPartialClassesInSameFile ()
{
string code = @"public partial class PartialClassInSameFile
{
public partial class NestedPartial
{
void Foo ()
{
}
}
}
public partial class PartialClassInSameFile
{
public partial class NestedPartial
{
void FooBar ()
{
}
}
}
";
var unit = SyntaxTree.Parse(code);
Console.WriteLine(unit);
var type = unit.Members.First() as TypeDeclaration;
Assert.IsTrue(type.Members.FirstOrDefault() is TypeDeclaration, "1st nested partial not found.");
type = unit.Members.Skip (1).First() as TypeDeclaration;
Assert.IsTrue(type.Members.FirstOrDefault() is TypeDeclaration, "2nd nested partial not found.");
}
}
}
|