From bf88746a7aaec9c5c5bb51adc52fd6baa5298bab Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 26 Nov 2011 13:51:11 +0100 Subject: [PATCH] Added some failing parser tests for bugs discovered trying to roundtrip NRefactory itself. --- .../Ast/TypeMembers/Accessor.cs | 5 ++++ .../TextWriterOutputFormatter.cs | 7 +++-- .../CSharp/CSharpOutputVisitorTests.cs | 28 +++++++++++++++---- .../GeneralScope/TypeDeclarationTests.cs | 26 +++++++++++++++++ .../PatternMatching/Pattern.cs | 5 ++-- 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs b/ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs index 0755e04369..1da7986bb3 100644 --- a/ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs +++ b/ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs @@ -46,6 +46,11 @@ namespace ICSharpCode.NRefactory.CSharp { return default (S); } + + protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) + { + return other == null || other.IsNull; + } } public override NodeType NodeType { diff --git a/ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs b/ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs index b85ab642a7..5ed7870657 100644 --- a/ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs +++ b/ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs @@ -75,11 +75,13 @@ namespace ICSharpCode.NRefactory.CSharp public void OpenBrace(BraceStyle style) { + bool isAtStartOfLine = needsIndent; switch (style) { case BraceStyle.DoNotChange: case BraceStyle.EndOfLine: WriteIndentation(); - textWriter.Write(' '); + if (!isAtStartOfLine) + textWriter.Write(' '); textWriter.Write('{'); break; case BraceStyle.EndOfLineWithoutSpace: @@ -87,7 +89,8 @@ namespace ICSharpCode.NRefactory.CSharp textWriter.Write('{'); break; case BraceStyle.NextLine: - NewLine (); + if (!isAtStartOfLine) + NewLine(); WriteIndentation(); textWriter.Write('{'); break; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs index d836308e51..d8661b44bd 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs @@ -25,18 +25,18 @@ namespace ICSharpCode.NRefactory.CSharp [TestFixture] public class CSharpOutputVisitorTests { - void AssertOutput(string expected, Expression expr, CSharpFormattingOptions policy = null) + void AssertOutput(string expected, AstNode node, CSharpFormattingOptions policy = null) { if (policy == null) - policy = new CSharpFormattingOptions();; + policy = new CSharpFormattingOptions(); StringWriter w = new StringWriter(); w.NewLine = "\n"; - expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "\t" }, policy), null); + node.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "$" }, policy), null); Assert.AreEqual(expected.Replace("\r", ""), w.ToString()); } - [Test, Ignore("Incorrect whitespace")] - public void AssignmentInCollectionInitialize() + [Test] + public void AssignmentInCollectionInitializer() { Expression expr = new ObjectCreateExpression { Type = new SimpleType("List"), @@ -47,7 +47,23 @@ namespace ICSharpCode.NRefactory.CSharp ) }; - AssertOutput("new List {\n {\n a = 1\n }\n}", expr); + AssertOutput("new List {\n${\n$$a = 1\n$}\n}", expr); + } + + [Test] + public void EnumDeclarationWithInitializers() + { + TypeDeclaration type = new TypeDeclaration { + ClassType = ClassType.Enum, + Name = "DisplayFlags", + Members = { + new EnumMemberDeclaration { + Name = "D", + Initializer = new PrimitiveExpression(4) + } + }}; + + AssertOutput("enum DisplayFlags\n{\n$D = 4\n}\n", type); } } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs index 9893f9ef85..87a7c009d4 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs @@ -296,6 +296,16 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 Assert.AreEqual(10, ((PrimitiveExpression)member.Initializer).Value); } + [Test] + public void EnumWithInitializerAndWindowsNewline() + { + TypeDeclaration td = ParseUtilCSharp.ParseGlobal("enum MyEnum { Val1 = 10\r\n}"); + EnumMemberDeclaration member = (EnumMemberDeclaration)td.Members.Single(); + Assert.AreEqual("Val1", member.Name); + Assert.AreEqual(10, ((PrimitiveExpression)member.Initializer).Value); + Assert.AreEqual("10", ((PrimitiveExpression)member.Initializer).LiteralValue); + } + [Test] public void EnumWithBaseType() { @@ -303,5 +313,21 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 Assert.AreEqual("MyEnum", td.Name); Assert.AreEqual("short", ((PrimitiveType)td.BaseTypes.Single()).Keyword); } + + [Test] + public void EnumWithIncorrectNewlineAfterIntegerLiteral() + { + ParseUtilCSharp.AssertGlobal( + "enum DisplayFlags { D = 4\r\r\n}", + new TypeDeclaration { + ClassType = ClassType.Enum, + Name = "DisplayFlags", + Members = { + new EnumMemberDeclaration { + Name = "D", + Initializer = new PrimitiveExpression(4) + } + }}); + } } } diff --git a/ICSharpCode.NRefactory/PatternMatching/Pattern.cs b/ICSharpCode.NRefactory/PatternMatching/Pattern.cs index c3d22cce44..cb54b8b3c3 100644 --- a/ICSharpCode.NRefactory/PatternMatching/Pattern.cs +++ b/ICSharpCode.NRefactory/PatternMatching/Pattern.cs @@ -31,12 +31,11 @@ namespace ICSharpCode.NRefactory.PatternMatching /// /// Gets the string that matches any string. /// - public static readonly string AnyString = string.Empty; - // TODO: use something other than string.Empty so that 'no value' and 'any value' can be distinguished + public static readonly string AnyString = "$any$"; public static bool MatchString(string pattern, string text) { - return string.IsNullOrEmpty(pattern) || pattern == text; + return pattern == AnyString || pattern == text; } internal struct PossibleMatch