Browse Source

- Made the InitializerTests pass, through three changes:

* Added new-lines as needed to make the source formatting match what the actual output was.
  * Added code to CodeAssert to make it ignore #regions
  * Added code to output decimal.MinValue and decimal.MaxValue as appropriate
pull/252/head
Alex Lyman 14 years ago committed by Daniel Grunwald
parent
commit
8dcea0b27b
  1. 2
      ICSharpCode.Decompiler/Tests/Helpers/CodeAssert.cs
  2. 407
      ICSharpCode.Decompiler/Tests/InitializerTests.cs
  3. 2
      ICSharpCode.Decompiler/Tests/TestRunner.cs
  4. 11
      NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

2
ICSharpCode.Decompiler/Tests/Helpers/CodeAssert.cs

@ -89,6 +89,8 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -89,6 +89,8 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
var index = line.IndexOf("//");
if (index >= 0) {
return line.Substring(0, index);
} else if (line.StartsWith("#")) {
return string.Empty;
} else {
return line;
}

407
ICSharpCode.Decompiler/Tests/InitializerTests.cs

@ -21,20 +21,21 @@ using System.Collections.Generic; @@ -21,20 +21,21 @@ using System.Collections.Generic;
public class InitializerTests
{
enum MyEnum
private enum MyEnum
{
a,
b
}
enum MyEnum2
private enum MyEnum2
{
c,
d
}
class Data
private class Data
{
public List<InitializerTests.MyEnum2> FieldList = new List<InitializerTests.MyEnum2>();
public InitializerTests.MyEnum a
{
get;
@ -45,218 +46,414 @@ public class InitializerTests @@ -45,218 +46,414 @@ public class InitializerTests
get;
set;
}
public List<InitializerTests.MyEnum2> FieldList = new List<InitializerTests.MyEnum2>();
public InitializerTests.Data MoreData { get; set; }
public InitializerTests.Data MoreData
{
get;
set;
}
}
// Helper methods used to ensure initializers used within expressions work correctly
static void X(object a, object b)
private static void X(object a, object b)
{
}
static object Y()
private static object Y()
{
return null;
}
#region Array Initializers
public static void Array1()
{
X(Y(), new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
InitializerTests.X(InitializerTests.Y(), new int[]
{
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
});
}
public static void Array2(int a, int b, int c)
{
X(Y(), new int[] { a, 0, b, 0, c });
InitializerTests.X(InitializerTests.Y(), new int[]
{
a,
0,
b,
0,
c
});
}
public static void NestedArray(int a, int b, int c)
{
X(Y(), new int[][] {
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new int[] { a, b, c },
new int[] { 1, 2, 3, 4, 5, 6 }
InitializerTests.X(InitializerTests.Y(), new int[][]
{
new int[]
{
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
},
new int[]
{
a,
b,
c
},
new int[]
{
1,
2,
3,
4,
5,
6
}
});
}
public static void ArrayBoolean()
{
X(Y(), new bool[] { true, false, true, false, false, false, true, true });
InitializerTests.X(InitializerTests.Y(), new bool[]
{
true,
false,
true,
false,
false,
false,
true,
true
});
}
public static void ArrayByte()
{
X(Y(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 254, 255 });
InitializerTests.X(InitializerTests.Y(), new byte[]
{
1,
2,
3,
4,
5,
6,
7,
8,
254,
255
});
}
public static void ArraySByte()
{
X(Y(), new sbyte[] { -128, -127, 0, 1, 2, 3, 4, 127 });
InitializerTests.X(InitializerTests.Y(), new sbyte[]
{
-128,
-127,
0,
1,
2,
3,
4,
127
});
}
public static void ArrayShort()
{
X(Y(), new short[] { -32768, -1, 0, 1, 32767 });
InitializerTests.X(InitializerTests.Y(), new short[]
{
-32768,
-1,
0,
1,
32767
});
}
public static void ArrayUShort()
{
X(Y(), new ushort[] { 0, 1, 32767, 32768, 65534, 65535 });
InitializerTests.X(InitializerTests.Y(), new ushort[]
{
0,
1,
32767,
32768,
65534,
65535
});
}
public static void ArrayInt()
{
X(Y(), new int[] { 1, -2, 2000000000, 4, 5, -6, 7, 8, 9, 10 });
InitializerTests.X(InitializerTests.Y(), new int[]
{
1,
-2,
2000000000,
4,
5,
-6,
7,
8,
9,
10
});
}
public static void ArrayUInt()
{
X(Y(), new uint[] { 1, 2000000000, 3000000000, 4, 5, 6, 7, 8, 9, 10 });
InitializerTests.X(InitializerTests.Y(), new uint[]
{
1u,
2000000000u,
3000000000u,
4u,
5u,
6u,
7u,
8u,
9u,
10u
});
}
public static void ArrayLong()
{
X(Y(), new long[] { -4999999999999999999, -1, 0, 1, 4999999999999999999 });
InitializerTests.X(InitializerTests.Y(), new long[]
{
-4999999999999999999L,
-1L,
0L,
1L,
4999999999999999999L
});
}
public static void ArrayULong()
{
X(Y(), new ulong[] { 1, 2000000000, 3000000000, 4, 5, 6, 7, 8, 4999999999999999999, 9999999999999999999 });
InitializerTests.X(InitializerTests.Y(), new ulong[]
{
1uL,
2000000000uL,
3000000000uL,
4uL,
5uL,
6uL,
7uL,
8uL,
4999999999999999999uL,
9999999999999999999uL
});
}
public static void ArrayFloat()
{
X(Y(), new float[] { -1.5f, 0f, 1.5f, float.NegativeInfinity, float.PositiveInfinity, float.NaN });
InitializerTests.X(InitializerTests.Y(), new float[]
{
-1.5f,
0f,
1.5f,
float.NegativeInfinity,
float.PositiveInfinity,
float.NaN
});
}
public static void ArrayDouble()
{
X(Y(), new double[] { -1.5, 0.0, 1.5, double.NegativeInfinity, double.PositiveInfinity, double.NaN });
InitializerTests.X(InitializerTests.Y(), new double[]
{
-1.5,
0.0,
1.5,
double.NegativeInfinity,
double.PositiveInfinity,
double.NaN
});
}
public static void ArrayDecimal()
{
X(Y(), new decimal[] { -100m, 0m, 100m, decimal.MinValue, decimal.MaxValue, 0.0000001m });
InitializerTests.X(InitializerTests.Y(), new decimal[]
{
-100m,
0m,
100m,
decimal.MinValue,
decimal.MaxValue,
0.0000001m
});
}
public static void ArrayString()
{
X(Y(), new string[] { "", null, "Hello", "World" });
InitializerTests.X(InitializerTests.Y(), new string[]
{
"",
null,
"Hello",
"World"
});
}
public static void ArrayEnum()
{
X(Y(), new InitializerTests.MyEnum[] { InitializerTests.MyEnum.a, InitializerTests.MyEnum.b, InitializerTests.MyEnum.a, InitializerTests.MyEnum.b });
InitializerTests.X(InitializerTests.Y(), new InitializerTests.MyEnum[]
{
InitializerTests.MyEnum.a,
InitializerTests.MyEnum.b,
InitializerTests.MyEnum.a,
InitializerTests.MyEnum.b
});
}
#endregion
public static void CollectionInitializerList()
{
X(Y(), new List<int> { 1, 2, 3 });
InitializerTests.X(InitializerTests.Y(), new List<int>
{
1,
2,
3
});
}
public static void CollectionInitializerDictionary()
{
X(Y(), new Dictionary<string, int> {
{ "First", 1 },
{ "Second", 2 },
{ "Third" , 3 }
InitializerTests.X(InitializerTests.Y(), new Dictionary<string, int>
{
{
"First",
1
},
{
"Second",
2
},
{
"Third",
3
}
});
}
public static void CollectionInitializerDictionaryWithEnumTypes()
{
X(Y(), new Dictionary<InitializerTests.MyEnum, InitializerTests.MyEnum2> {
{ InitializerTests.MyEnum.a, InitializerTests.MyEnum2.c },
{ InitializerTests.MyEnum.b, InitializerTests.MyEnum2.d }
InitializerTests.X(InitializerTests.Y(), new Dictionary<InitializerTests.MyEnum, InitializerTests.MyEnum2>
{
{
InitializerTests.MyEnum.a,
InitializerTests.MyEnum2.c
},
{
InitializerTests.MyEnum.b,
InitializerTests.MyEnum2.d
}
});
}
public static void NotACollectionInitializer()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
X(Y(), list);
InitializerTests.X(InitializerTests.Y(), list);
}
public static void ObjectInitializer()
{
X(Y(), new Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
a = InitializerTests.MyEnum.a
a = InitializerTests.MyEnum.a
});
}
public static void NotAObjectInitializer()
{
Data data = new InitializerTests.Data();
InitializerTests.Data data = new InitializerTests.Data();
data.a = InitializerTests.MyEnum.a;
X(Y(), data);
InitializerTests.X(InitializerTests.Y(), data);
}
public static void ObjectInitializerAssignCollectionToField()
{
X(Y(), new InitializerTests.Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
a = InitializerTests.MyEnum.a,
FieldList = new List<InitializerTests.MyEnum2>
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
a = InitializerTests.MyEnum.a,
FieldList = new List<InitializerTests.MyEnum2>
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
});
}
public static void ObjectInitializerAddToCollectionInField()
{
X(Y(), new InitializerTests.Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
a = InitializerTests.MyEnum.a,
FieldList =
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
a = InitializerTests.MyEnum.a,
FieldList =
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
});
}
public static void ObjectInitializerAssignCollectionToProperty()
{
X(Y(), new InitializerTests.Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
a = InitializerTests.MyEnum.a,
PropertyList = new List<InitializerTests.MyEnum2>
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
a = InitializerTests.MyEnum.a,
PropertyList = new List<InitializerTests.MyEnum2>
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
});
}
public static void ObjectInitializerAddToCollectionInProperty()
{
X(Y(), new InitializerTests.Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
a = InitializerTests.MyEnum.a,
PropertyList =
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
a = InitializerTests.MyEnum.a,
PropertyList =
{
InitializerTests.MyEnum2.c,
InitializerTests.MyEnum2.d
}
});
}
public static void ObjectInitializerWithInitializationOfNestedObjects()
{
X(Y(), new InitializerTests.Data
InitializerTests.X(InitializerTests.Y(), new InitializerTests.Data
{
MoreData =
{
a = InitializerTests.MyEnum.a
}
MoreData =
{
a = InitializerTests.MyEnum.a
}
});
}
}

2
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -76,7 +76,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -76,7 +76,7 @@ namespace ICSharpCode.Decompiler.Tests
TestFile(@"..\..\Tests\IncrementDecrement.cs");
}
[Test, Ignore("Formatting issues (array initializers not on single line)")]
[Test]
public void InitializerTests()
{
TestFile(@"..\..\Tests\InitializerTests.cs");

11
NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -872,6 +872,17 @@ namespace ICSharpCode.NRefactory.CSharp @@ -872,6 +872,17 @@ namespace ICSharpCode.NRefactory.CSharp
formatter.WriteToken("'" + ConvertCharLiteral((char)val) + "'");
lastWritten = LastWritten.Other;
} else if (val is decimal) {
decimal d = (decimal) val;
if (d == decimal.MinValue || d == decimal.MaxValue)
{
WriteKeyword("decimal");
WriteToken(".", AstNode.Roles.Dot);
if (d == decimal.MinValue)
WriteIdentifier("MinValue");
else
WriteIdentifier("MaxValue");
return;
}
formatter.WriteToken(((decimal)val).ToString(NumberFormatInfo.InvariantInfo) + "m");
lastWritten = LastWritten.Other;
} else if (val is float) {

Loading…
Cancel
Save