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 @@ -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 {

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

@ -75,11 +75,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -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 @@ -87,7 +89,8 @@ namespace ICSharpCode.NRefactory.CSharp
textWriter.Write('{');
break;
case BraceStyle.NextLine:
NewLine ();
if (!isAtStartOfLine)
NewLine();
WriteIndentation();
textWriter.Write('{');
break;

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

@ -25,18 +25,18 @@ namespace ICSharpCode.NRefactory.CSharp @@ -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 @@ -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 @@ -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<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]
public void EnumWithBaseType()
{
@ -303,5 +313,21 @@ public abstract class MyClass : MyBase, Interface1, My.Test.Interface2 @@ -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)
}
}});
}
}
}

5
ICSharpCode.NRefactory/PatternMatching/Pattern.cs

@ -31,12 +31,11 @@ namespace ICSharpCode.NRefactory.PatternMatching @@ -31,12 +31,11 @@ namespace ICSharpCode.NRefactory.PatternMatching
/// <summary>
/// Gets the string that matches any string.
/// </summary>
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

Loading…
Cancel
Save