Browse Source

CSharpOutputTest: don't ignore whitespace.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4481 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 16 years ago
parent
commit
a07a78e80b
  1. 63
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs
  2. 43
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs
  3. 349
      src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs
  4. 23
      src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

63
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

@ -44,6 +44,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public PrettyPrintOptions Options { public PrettyPrintOptions Options {
get { return prettyPrintOptions; } get { return prettyPrintOptions; }
set {
if (value == null)
throw new ArgumentNullException();
prettyPrintOptions = value;
}
} }
public IOutputFormatter OutputFormatter { public IOutputFormatter OutputFormatter {
@ -589,15 +594,44 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.PrintIdentifier(propertyDeclaration.Name); outputFormatter.PrintIdentifier(propertyDeclaration.Name);
outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentPropertyBody); OutputGetAndSetRegion(propertyDeclaration.GetRegion, propertyDeclaration.SetRegion);
TrackVisit(propertyDeclaration.GetRegion, data);
TrackVisit(propertyDeclaration.SetRegion, data);
outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
return null; return null;
} }
void OutputGetAndSetRegion(PropertyGetRegion getRegion, PropertySetRegion setRegion)
{
BraceStyle braceStyle = this.prettyPrintOptions.PropertyBraceStyle;
if (getRegion.Block.IsNull && setRegion.Block.IsNull && getRegion.Attributes.Count == 0 && setRegion.Attributes.Count == 0
&& (braceStyle == BraceStyle.EndOfLine || braceStyle == BraceStyle.EndOfLineWithoutSpace))
{
if (braceStyle == BraceStyle.EndOfLine)
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
// automatic property / abstract property:
// output in a single line: "string Text { get; set; }"
if (!getRegion.IsNull) {
outputFormatter.Space();
OutputModifier(getRegion.Modifier);
outputFormatter.PrintText("get;");
}
if (!setRegion.IsNull) {
outputFormatter.Space();
OutputModifier(setRegion.Modifier);
outputFormatter.PrintText("set;");
}
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
outputFormatter.NewLine();
} else {
outputFormatter.BeginBrace(braceStyle, this.prettyPrintOptions.IndentPropertyBody);
TrackVisit(getRegion, null);
TrackVisit(setRegion, null);
outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
}
}
public override object TrackedVisitPropertyGetRegion(PropertyGetRegion propertyGetRegion, object data) public override object TrackedVisitPropertyGetRegion(PropertyGetRegion propertyGetRegion, object data)
{ {
this.VisitAttributes(propertyGetRegion.Attributes, data); this.VisitAttributes(propertyGetRegion.Attributes, data);
@ -658,7 +692,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
} else { } else {
outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentEventBody); outputFormatter.BeginBrace(this.prettyPrintOptions.EventBraceStyle, this.prettyPrintOptions.IndentEventBody);
TrackVisit(eventDeclaration.AddRegion, data); TrackVisit(eventDeclaration.AddRegion, data);
TrackVisit(eventDeclaration.RemoveRegion, data); TrackVisit(eventDeclaration.RemoveRegion, data);
outputFormatter.EndBrace(this.prettyPrintOptions.IndentEventBody); outputFormatter.EndBrace(this.prettyPrintOptions.IndentEventBody);
@ -950,18 +984,15 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
if (this.prettyPrintOptions.SpacesWithinBrackets) { if (this.prettyPrintOptions.SpacesWithinBrackets) {
outputFormatter.Space(); outputFormatter.Space();
} }
outputFormatter.PrintToken(Tokens.CloseSquareBracket); outputFormatter.PrintToken(Tokens.CloseSquareBracket);
outputFormatter.NewLine();
outputFormatter.Indent(); outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentPropertyBody);
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
outputFormatter.NewLine();
++outputFormatter.IndentationLevel;
TrackVisit(indexerDeclaration.GetRegion, data); TrackVisit(indexerDeclaration.GetRegion, data);
TrackVisit(indexerDeclaration.SetRegion, data); TrackVisit(indexerDeclaration.SetRegion, data);
--outputFormatter.IndentationLevel;
outputFormatter.Indent(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
outputFormatter.NewLine();
return null; return null;
} }
@ -2563,8 +2594,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.Space(); outputFormatter.Space();
outputFormatter.PrintToken(Tokens.LambdaArrow); outputFormatter.PrintToken(Tokens.LambdaArrow);
outputFormatter.Space();
if (!lambdaExpression.ExpressionBody.IsNull) { if (!lambdaExpression.ExpressionBody.IsNull) {
outputFormatter.Space();
TrackVisit(lambdaExpression.ExpressionBody, null); TrackVisit(lambdaExpression.ExpressionBody, null);
} }
if (!lambdaExpression.StatementBody.IsNull) { if (!lambdaExpression.StatementBody.IsNull) {

43
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs

@ -351,45 +351,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
#endregion #endregion
#region NewLines #region NewLines
bool placeCatchOnNewLine = true; public bool PlaceCatchOnNewLine { get; set; }
public bool PlaceCatchOnNewLine { public bool PlaceFinallyOnNewLine { get; set; }
get { public bool PlaceElseOnNewLine { get; set; }
return placeCatchOnNewLine; public bool PlaceWhileOnNewLine { get; set; }
}
set {
placeCatchOnNewLine = value;
}
}
bool placeFinallyOnNewLine = true;
public bool PlaceFinallyOnNewLine {
get {
return placeFinallyOnNewLine;
}
set {
placeFinallyOnNewLine = value;
}
}
bool placeElseOnNewLine = true;
public bool PlaceElseOnNewLine {
get {
return placeElseOnNewLine;
}
set {
placeElseOnNewLine = value;
}
}
bool placeWhileOnNewLine = true;
public bool PlaceWhileOnNewLine {
get {
return placeWhileOnNewLine;
}
set {
placeWhileOnNewLine = value;
}
}
#endregion #endregion
#region Spaces #region Spaces

349
src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs

@ -7,10 +7,11 @@
using System; using System;
using System.IO; using System.IO;
using NUnit.Framework;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.PrettyPrinter; using ICSharpCode.NRefactory.PrettyPrinter;
using NUnit.Framework;
using System.Text;
namespace ICSharpCode.NRefactory.Tests.PrettyPrinter namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
{ {
@ -18,11 +19,17 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public class CSharpOutputTest public class CSharpOutputTest
{ {
void TestProgram(string program) void TestProgram(string program)
{
TestProgram(program, new PrettyPrintOptions());
}
void TestProgram(string program, PrettyPrintOptions options)
{ {
IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(program)); IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(program));
parser.Parse(); parser.Parse();
Assert.AreEqual("", parser.Errors.ErrorOutput); Assert.AreEqual("", parser.Errors.ErrorOutput);
CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor(); CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor();
outputVisitor.Options = options;
outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null); outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
Assert.AreEqual("", outputVisitor.Errors.ErrorOutput); Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
Assert.AreEqual(StripWhitespace(program), StripWhitespace(outputVisitor.Text)); Assert.AreEqual(StripWhitespace(program), StripWhitespace(outputVisitor.Text));
@ -30,17 +37,49 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
internal static string StripWhitespace(string text) internal static string StripWhitespace(string text)
{ {
return text.Trim().Replace("\t", "").Replace("\r", "").Replace("\n", " ").Replace(" ", " "); return text.Trim().Replace("\t", " ").Replace("\r", "");
} }
void TestTypeMember(string program) void TestTypeMember(string program)
{ {
TestProgram("class A { " + program + " }"); TestTypeMember(program, new PrettyPrintOptions());
}
void TestTypeMember(string program, PrettyPrintOptions options)
{
StringBuilder b = new StringBuilder();
b.AppendLine("class A");
b.AppendLine("{");
using (StringReader r = new StringReader(program)) {
string line;
while ((line = r.ReadLine()) != null) {
b.Append(" ");
b.AppendLine(line);
}
}
b.AppendLine("}");
TestProgram(b.ToString(), options);
} }
void TestStatement(string statement) void TestStatement(string statement)
{ {
TestTypeMember("void Method() { " + statement + " }"); TestStatement(statement, new PrettyPrintOptions());
}
void TestStatement(string statement, PrettyPrintOptions options)
{
StringBuilder b = new StringBuilder();
b.AppendLine("void Method()");
b.AppendLine("{");
using (StringReader r = new StringReader(statement)) {
string line;
while ((line = r.ReadLine()) != null) {
b.Append(" ");
b.AppendLine(line);
}
}
b.AppendLine("}");
TestTypeMember(b.ToString(), options);
} }
void TestExpression(string expression) void TestExpression(string expression)
@ -59,15 +98,15 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void Namespace() public void Namespace()
{ {
TestProgram("namespace System { }"); TestProgram("namespace System\n{\n}");
} }
[Test] [Test]
public void CustomEvent() public void CustomEvent()
{ {
TestTypeMember("public event EventHandler Click {" + TestTypeMember("public event EventHandler Click {\n" +
" add { obj.Click += value; }" + " add { obj.Click += value; }\n" +
" remove { obj.Click -= value; } " + " remove { obj.Click -= value; }\n" +
"}"); "}");
} }
@ -86,43 +125,50 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void Method() public void Method()
{ {
TestTypeMember("void Method() { }"); TestTypeMember("void Method()\n{\n}");
} }
[Test] [Test]
public void StaticMethod() public void StaticMethod()
{ {
TestTypeMember("static void Method() { }"); TestTypeMember("static void Method()\n{\n}");
} }
[Test] [Test]
public void PartialModifier() public void PartialModifier()
{ {
TestProgram("public partial class Foo { }"); TestProgram("public partial class Foo\n{\n}");
} }
[Test] [Test]
public void GenericClassDefinition() public void GenericClassDefinition()
{ {
TestProgram("public class Foo<T> where T : IDisposable, ICloneable { }"); TestProgram("public class Foo<T> where T : IDisposable, ICloneable\n{\n}");
} }
[Test] [Test]
public void InterfaceWithOutParameters() public void InterfaceWithOutParameters()
{ {
TestProgram("public interface ITest { void Method(out int a, ref double b); }"); TestProgram("public interface ITest\n" +
"{\n" +
" void Method(out int a, ref double b);\n" +
"}");
} }
[Test] [Test]
public void GenericClassDefinitionWithBaseType() public void GenericClassDefinitionWithBaseType()
{ {
TestProgram("public class Foo<T> : BaseClass where T : IDisposable, ICloneable { }"); TestProgram("public class Foo<T> : BaseClass where T : IDisposable, ICloneable\n" +
"{\n" +
"}");
} }
[Test] [Test]
public void GenericMethodDefinition() public void GenericMethodDefinition()
{ {
TestTypeMember("public void Foo<T>(T arg) where T : IDisposable, ICloneable { }"); TestTypeMember("public void Foo<T>(T arg) where T : IDisposable, ICloneable\n" +
"{\n" +
"}");
} }
[Test] [Test]
@ -140,25 +186,69 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void ArrayInitializer() public void ArrayInitializer()
{ {
TestStatement("object[] a = new object[] { 1, 2, 3 };"); TestStatement("object[] a = new object[] {\n" +
" 1,\n" +
" 2,\n" +
" 3\n" +
"};");
} }
[Test] [Test]
public void IfStatement() public void IfStatement()
{ {
TestStatement("if (a) { m1(); } else { m2(); }"); TestStatement("if (a) {\n" +
" m1();\n" +
TestStatement("if (a) m1();else m2(); "); "} else {\n" +
" m2();\n" +
"}");
TestStatement("if (a) {\n" + TestStatement("if (a) {\n" +
"\tm1();\n" + " m1();\n" +
"} else if (b) {\n" + "} else if (b) {\n" +
"\tm2();\n" + " m2();\n" +
"} else {\n" + "} else {\n" +
"\tm3();\n" + " m3();\n" +
"}"); "}");
} }
[Test]
[Ignore("PlaceElseOnNewLine is currently broken")]
public void IfStatementSeparateLines()
{
PrettyPrintOptions options = new PrettyPrintOptions();
options.PlaceElseOnNewLine = true;
options.StatementBraceStyle = BraceStyle.NextLine;
TestStatement("if (a)\n" +
"{\n" +
" m1();\n" +
"}\n" +
"else\n" +
"{\n" +
" m2();\n" +
"}", options);
TestStatement("if (a)\n" +
"{\n" +
" m1();\n" +
"}\n" +
"else if (b)\n" +
"{\n" +
" m2();\n" +
"}\n" +
"else\n" +
"{\n" +
" m3();\n" +
"}", options);
}
[Test]
[Ignore("Single-line if-else is currently broken.")]
public void SingleLineIfElseStatement()
{
TestStatement("if (a) m1(); else m2();");
}
[Test] [Test]
public void Assignment() public void Assignment()
{ {
@ -174,47 +264,50 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void BlockStatements() public void BlockStatements()
{ {
TestStatement("checked { }"); TestStatement("checked {\n}");
TestStatement("unchecked { }"); TestStatement("unchecked {\n}");
TestStatement("unsafe { }"); TestStatement("unsafe {\n}");
} }
[Test] [Test]
public void ExceptionHandling() public void ExceptionHandling()
{ {
TestStatement("try { throw new Exception(); } " + TestStatement("try {\n" +
"catch (FirstException e) { } " + " throw new Exception();\n" +
"catch (SecondException) { } " + "} catch (FirstException e) {\n" +
"catch { throw; } " + "} catch (SecondException) {\n" +
"finally { }"); "} catch {\n" +
" throw;\n" +
"} finally {\n" +
"}");
} }
[Test] [Test]
public void LoopStatements() public void LoopStatements()
{ {
TestStatement("foreach (Type var in col) { }"); TestStatement("foreach (Type var in col) {\n}");
TestStatement("while (true) { }"); TestStatement("while (true) {\n}");
TestStatement("do { } while (true);"); TestStatement("do {\n} while (true);");
} }
[Test] [Test]
public void Switch() public void Switch()
{ {
TestStatement("switch (a) {" + TestStatement("switch (a) {\n" +
" case 0:" + " case 0:\n" +
" case 1:" + " case 1:\n" +
" break;" + " break;\n" +
" case 2:" + " case 2:\n" +
" return;" + " return;\n" +
" default:" + " default:\n" +
" throw new Exception(); " + " throw new Exception();\n" +
"}"); "}");
} }
[Test] [Test]
public void MultipleVariableForLoop() public void MultipleVariableForLoop()
{ {
TestStatement("for (int a = 0, b = 0; b < 100; ++b,a--) { }"); TestStatement("for (int a = 0, b = 0; b < 100; ++b,a--) {\n}");
} }
[Test] [Test]
@ -336,25 +429,39 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void Enum() public void Enum()
{ {
TestProgram("enum MyTest { Red, Green, Blue, Yellow }"); TestProgram("enum MyTest\n{\n" +
" Red,\n" +
" Green,\n" +
" Blue,\n" +
" Yellow\n" +
"}");
} }
[Test] [Test]
public void EnumWithInitializers() public void EnumWithInitializers()
{ {
TestProgram("enum MyTest { Red = 1, Green = 2, Blue = 4, Yellow = 8 }"); TestProgram("enum MyTest\n{\n" +
" Red = 1,\n" +
" Green = 2,\n" +
" Blue = 4,\n" +
" Yellow = 8\n" +
"}");
} }
[Test] [Test]
public void SyncLock() public void SyncLock()
{ {
TestStatement("lock (a) { Work(); }"); TestStatement("lock (a) {\n" +
" Work();\n" +
"}");
} }
[Test] [Test]
public void Using() public void Using()
{ {
TestStatement("using (A a = new A()) { a.Work(); }"); TestStatement("using (A a = new A()) {\n" +
" a.Work();\n" +
"}");
} }
[Test] [Test]
@ -368,7 +475,9 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void SetOnlyProperty() public void SetOnlyProperty()
{ {
TestTypeMember("public bool ExpectsValue { set { DoSomething(value); } }"); TestTypeMember("public bool ExpectsValue {\n" +
" set { DoSomething(value); }\n" +
"}");
} }
[Test] [Test]
@ -388,51 +497,56 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void Interface() public void Interface()
{ {
TestProgram("interface ITest {" + TestProgram("interface ITest\n" +
" bool GetterAndSetter { get; set; }" + "{\n" +
" bool GetterOnly { get; }" + " bool GetterAndSetter { get; set; }\n" +
" bool SetterOnly { set; }" + " bool GetterOnly { get; }\n" +
" void InterfaceMethod();" + " bool SetterOnly { set; }\n" +
" string InterfaceMethod2();\n" + " void InterfaceMethod();\n" +
" string InterfaceMethod2();\n" +
"}"); "}");
} }
[Test] [Test]
public void IndexerDeclaration() public void IndexerDeclaration()
{ {
TestTypeMember("public string this[int index] { get { return index.ToString(); } set { } }"); TestTypeMember("public string this[int index] {\n" +
TestTypeMember("public string IList.this[int index] { get { return index.ToString(); } set { } }"); " get { return index.ToString(); }\n" +
" set { }\n" +
"}");
TestTypeMember("public string IList.this[int index] {\n" +
" get { return index.ToString(); }\n" +
" set { }\n" +
"}");
} }
[Test] [Test]
public void OverloadedConversionOperators() public void OverloadedConversionOperators()
{ {
TestTypeMember("public static explicit operator TheBug(XmlNode xmlNode) { }"); TestTypeMember("public static explicit operator TheBug(XmlNode xmlNode)\n{\n}");
TestTypeMember("public static implicit operator XmlNode(TheBug bugNode) { }"); TestTypeMember("public static implicit operator XmlNode(TheBug bugNode)\n{\n}");
} }
[Test] [Test]
public void OverloadedTrueFalseOperators() public void OverloadedTrueFalseOperators()
{ {
TestTypeMember("public static bool operator true(TheBug bugNode) { }"); TestTypeMember("public static bool operator true(TheBug bugNode)\n{\n}");
TestTypeMember("public static bool operator false(TheBug bugNode) { }"); TestTypeMember("public static bool operator false(TheBug bugNode)\n{\n}");
} }
[Test] [Test]
public void OverloadedOperators() public void OverloadedOperators()
{ {
TestTypeMember("public static TheBug operator +(TheBug bugNode, TheBug bugNode2) { }"); TestTypeMember("public static TheBug operator +(TheBug bugNode, TheBug bugNode2)\n{\n}");
TestTypeMember("public static TheBug operator >>(TheBug bugNode, int b) { }"); TestTypeMember("public static TheBug operator >>(TheBug bugNode, int b)\n{\n}");
} }
[Test] [Test]
public void PropertyWithAccessorAccessModifiers() public void PropertyWithAccessorAccessModifiers()
{ {
TestTypeMember("public bool ExpectsValue {\n" + TestTypeMember("public bool ExpectsValue {\n" +
"\tinternal get {\n" + " internal get { }\n" +
"\t}\n" + " protected set { }\n" +
"\tprotected set {\n" +
"\t}\n" +
"}"); "}");
} }
@ -463,7 +577,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void ExtensionMethod() public void ExtensionMethod()
{ {
TestTypeMember("public static T[] Slice<T>(this T[] source, int index, int count)\n{ }"); TestTypeMember("public static T[] Slice<T>(this T[] source, int index, int count)\n{\n}");
} }
[Test] [Test]
@ -528,68 +642,125 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
[Test] [Test]
public void ObjectInitializer() public void ObjectInitializer()
{ {
TestExpression("new Point { X = 0, Y = 1 }"); TestExpression("new Point {\n" +
TestExpression("new Rectangle { P1 = new Point { X = 0, Y = 1 }, P2 = new Point { X = 2, Y = 3 } }"); " X = 0,\n" +
TestExpression("new Rectangle(arguments) { P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 } }"); " Y = 1\n" +
"}");
TestExpression("new Rectangle {\n" +
" P1 = new Point {\n" +
" X = 0,\n" +
" Y = 1\n" +
" },\n" +
" P2 = new Point {\n" +
" X = 2,\n" +
" Y = 3\n" +
" }\n" +
"}");
TestExpression("new Rectangle(arguments) {\n" +
" P1 = {\n" +
" X = 0,\n" +
" Y = 1\n" +
" },\n" +
" P2 = {\n" +
" X = 2,\n" +
" Y = 3\n" +
" }\n" +
"}");
} }
[Test] [Test]
public void CollectionInitializer() public void CollectionInitializer()
{ {
TestExpression("new List<int> { 0, 1, 2, 3, 4, 5 }"); TestExpression("new List<int> {\n" +
TestExpression(@"new List<Contact> { new Contact { Name = ""Chris Smith"", PhoneNumbers = { ""206-555-0101"", ""425-882-8080"" } }, new Contact { Name = ""Bob Harris"", PhoneNumbers = { ""650-555-0199"" } } }"); " 0,\n" +
" 1,\n" +
" 2,\n" +
" 3,\n" +
" 4,\n" +
" 5\n" +
"}");
TestExpression(@"new List<Contact> {
new Contact {
Name = ""Chris Smith"",
PhoneNumbers = {
""206-555-0101"",
""425-882-8080""
}
},
new Contact {
Name = ""Bob Harris"",
PhoneNumbers = { ""650-555-0199"" }
}
}");
} }
[Test] [Test]
public void AnonymousTypeCreation() public void AnonymousTypeCreation()
{ {
TestExpression("new { obj.Name, Price = 26.9, ident }"); TestExpression("new {\n" +
" obj.Name,\n" +
" Price = 26.9,\n" +
" ident\n" +
"}");
} }
[Test] [Test]
public void ImplicitlyTypedArrayCreation() public void ImplicitlyTypedArrayCreation()
{ {
TestExpression("new[] { 1, 10, 100, 1000 }"); TestExpression("new[] {\n" +
" 1,\n" +
" 10,\n" +
" 100,\n" +
" 1000\n" +
"}");
} }
[Test] [Test]
public void QuerySimpleWhere() public void QuerySimpleWhere()
{ {
TestExpression("from n in numbers where n < 5 select n"); TestExpression("from n in numbers\n" +
" where n < 5\n" +
" select n");
} }
[Test] [Test]
public void QueryMultipleFrom() public void QueryMultipleFrom()
{ {
TestExpression("from c in customers" + TestExpression("from c in customers\n" +
" where c.Region == \"WA\"" + " where c.Region == \"WA\"\n" +
" from o in c.Orders" + " from o in c.Orders\n" +
" where o.OrderDate >= cutoffDate" + " where o.OrderDate >= cutoffDate\n" +
" select new { c.CustomerID, o.OrderID }"); " select new {\n" +
" c.CustomerID,\n" +
" o.OrderID\n" +
" }");
} }
[Test] [Test]
public void QuerySimpleOrdering() public void QuerySimpleOrdering()
{ {
TestExpression("from w in words" + TestExpression("from w in words\n" +
" orderby w" + " orderby w\n" +
" select w"); " select w");
} }
[Test] [Test]
public void QueryComplexOrdering() public void QueryComplexOrdering()
{ {
TestExpression("from w in words" + TestExpression("from w in words\n" +
" orderby w.Length descending, w ascending" + " orderby w.Length descending, w ascending\n" +
" select w"); " select w");
} }
[Test] [Test]
public void QueryGroupInto() public void QueryGroupInto()
{ {
TestExpression("from n in numbers" + TestExpression("from n in numbers\n" +
" group n by n % 5 into g" + " group n by n % 5 into g\n" +
" select new { Remainder = g.Key, Numbers = g }"); " select new {\n" +
" Remainder = g.Key,\n" +
" Numbers = g\n" +
" }");
} }
[Test] [Test]

23
src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

@ -168,21 +168,21 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
public void AbstractProperty1() public void AbstractProperty1()
{ {
TestMember("Public MustOverride Property Salary() As Decimal", TestMember("Public MustOverride Property Salary() As Decimal",
"public abstract decimal Salary {\n get;\n set;\n}"); "public abstract decimal Salary { get; set; }");
} }
[Test] [Test]
public void AbstractProperty2() public void AbstractProperty2()
{ {
TestMember("Public ReadOnly MustOverride Property Salary() As Decimal", TestMember("Public ReadOnly MustOverride Property Salary() As Decimal",
"public abstract decimal Salary {\n get;\n}"); "public abstract decimal Salary { get; }");
} }
[Test] [Test]
public void AbstractProperty3() public void AbstractProperty3()
{ {
TestMember("Public WriteOnly MustOverride Property Salary() As Decimal", TestMember("Public WriteOnly MustOverride Property Salary() As Decimal",
"public abstract decimal Salary {\n set;\n}"); "public abstract decimal Salary { set; }");
} }
[Test] [Test]
@ -358,12 +358,10 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
{ {
TestStatement("Do \n Loop", TestStatement("Do \n Loop",
"do {\n" + "do {\n" +
"}\n" + "} while (true);");
"while (true);");
TestStatement("Do \n Loop Until i = 10000", TestStatement("Do \n Loop Until i = 10000",
"do {\n" + "do {\n" +
"}\n" + "} while (!(i == 10000));");
"while (!(i == 10000));");
} }
[Test] [Test]
@ -615,10 +613,7 @@ static int static_Test2_j = 0;");
"public interface ITest\n" + "public interface ITest\n" +
"{\n" + "{\n" +
" void Test();\n" + " void Test();\n" +
" string Name {\n" + " string Name { get; set; }\n" +
" get;\n" +
" set;\n" +
" }\n" +
"}"); "}");
} }
@ -655,10 +650,8 @@ static int static_Test2_j = 0;");
{ {
TestStatement("If a Then\nElse If b Then\nElse\nEnd If", TestStatement("If a Then\nElse If b Then\nElse\nEnd If",
"if (a) {\n" + "if (a) {\n" +
"}\n" + "} else if (b) {\n" +
"else if (b) {\n" + "} else {\n" +
"}\n" +
"else {\n" +
"}"); "}");
} }

Loading…
Cancel
Save