Browse Source

Added some failing parser tests for bugs discovered trying to roundtrip NRefactory itself.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
bf88746a7a
  1. 5
      ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs
  2. 7
      ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs
  3. 28
      ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs
  4. 26
      ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs
  5. 5
      ICSharpCode.NRefactory/PatternMatching/Pattern.cs

5
ICSharpCode.NRefactory.CSharp/Ast/TypeMembers/Accessor.cs

@ -46,6 +46,11 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
return default (S); return default (S);
} }
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
return other == null || other.IsNull;
}
} }
public override NodeType NodeType { public override NodeType NodeType {

7
ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.cs

@ -75,11 +75,13 @@ namespace ICSharpCode.NRefactory.CSharp
public void OpenBrace(BraceStyle style) public void OpenBrace(BraceStyle style)
{ {
bool isAtStartOfLine = needsIndent;
switch (style) { switch (style) {
case BraceStyle.DoNotChange: case BraceStyle.DoNotChange:
case BraceStyle.EndOfLine: case BraceStyle.EndOfLine:
WriteIndentation(); WriteIndentation();
textWriter.Write(' '); if (!isAtStartOfLine)
textWriter.Write(' ');
textWriter.Write('{'); textWriter.Write('{');
break; break;
case BraceStyle.EndOfLineWithoutSpace: case BraceStyle.EndOfLineWithoutSpace:
@ -87,7 +89,8 @@ namespace ICSharpCode.NRefactory.CSharp
textWriter.Write('{'); textWriter.Write('{');
break; break;
case BraceStyle.NextLine: case BraceStyle.NextLine:
NewLine (); if (!isAtStartOfLine)
NewLine();
WriteIndentation(); WriteIndentation();
textWriter.Write('{'); textWriter.Write('{');
break; break;

28
ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs

@ -25,18 +25,18 @@ namespace ICSharpCode.NRefactory.CSharp
[TestFixture] [TestFixture]
public class CSharpOutputVisitorTests public class CSharpOutputVisitorTests
{ {
void AssertOutput(string expected, Expression expr, CSharpFormattingOptions policy = null) void AssertOutput(string expected, AstNode node, CSharpFormattingOptions policy = null)
{ {
if (policy == null) if (policy == null)
policy = new CSharpFormattingOptions();; policy = new CSharpFormattingOptions();
StringWriter w = new StringWriter(); StringWriter w = new StringWriter();
w.NewLine = "\n"; 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()); Assert.AreEqual(expected.Replace("\r", ""), w.ToString());
} }
[Test, Ignore("Incorrect whitespace")] [Test]
public void AssignmentInCollectionInitialize() public void AssignmentInCollectionInitializer()
{ {
Expression expr = new ObjectCreateExpression { Expression expr = new ObjectCreateExpression {
Type = new SimpleType("List"), 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);
} }
} }
} }

26
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); Assert.AreEqual(10, ((PrimitiveExpression)member.Initializer).Value);
} }
[Test]
public void EnumWithInitializerAndWindowsNewline()
{
TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("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] [Test]
public void EnumWithBaseType() public void EnumWithBaseType()
{ {
@ -303,5 +313,21 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2
Assert.AreEqual("MyEnum", td.Name); Assert.AreEqual("MyEnum", td.Name);
Assert.AreEqual("short", ((PrimitiveType)td.BaseTypes.Single()).Keyword); 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)
}
}});
}
} }
} }

5
ICSharpCode.NRefactory/PatternMatching/Pattern.cs

@ -31,12 +31,11 @@ namespace ICSharpCode.NRefactory.PatternMatching
/// <summary> /// <summary>
/// Gets the string that matches any string. /// Gets the string that matches any string.
/// </summary> /// </summary>
public static readonly string AnyString = string.Empty; public static readonly string AnyString = "$any$";
// TODO: use something other than string.Empty so that 'no value' and 'any value' can be distinguished
public static bool MatchString(string pattern, string text) public static bool MatchString(string pattern, string text)
{ {
return string.IsNullOrEmpty(pattern) || pattern == text; return pattern == AnyString || pattern == text;
} }
internal struct PossibleMatch internal struct PossibleMatch

Loading…
Cancel
Save