From 220deac75743241457590a6726f80b57eba4834d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 22 Aug 2020 21:31:13 +0200 Subject: [PATCH] Add more deconstruction tests --- .../TestCases/Pretty/DeconstructionTests.cs | 301 +++++++++++++++++- 1 file changed, 285 insertions(+), 16 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs index 5c2e604c2..d03deab4a 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs @@ -79,6 +79,17 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty private class AssignmentTargets { public int IntField; + public long LongField; + public float FloatField; + public double DoubleField; + public decimal DecimalField; + + public MyInt MyField; + public MyInt? NMyField; + + public string StringField; + public object ObjectField; + public dynamic DynamicField; public int? NullableIntField; @@ -91,6 +102,41 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty set; } + public long Long { + get; + set; + } + + public float Float { + get; + set; + } + + public double Double { + get; + set; + } + + public decimal Decimal { + get; + set; + } + + public string String { + get; + set; + } + + public object Object { + get; + set; + } + + public dynamic Dynamic { + get; + set; + } + public int? NInt { get; set; @@ -147,49 +193,133 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty return null; } - public void LocalVariable_NoConversion() + public void LocalVariable_NoConversion_Custom() { var (myInt3, x) = GetSource(); Console.WriteLine(myInt3); Console.WriteLine(x); } - public void LocalVariable_NoConversion_DiscardFirst() + public void LocalVariable_NoConversion_Tuple() + { + var (myInt, x) = GetTuple(); + Console.WriteLine(myInt); + Console.WriteLine(x); + } + + public void LocalVariable_NoConversion_Custom_DiscardFirst() { var (_, x, value) = GetSource(); Console.WriteLine(x); Console.WriteLine(value); } - public void LocalVariable_NoConversion_DiscardLast() + // currently we detect deconstruction, iff the first element is not discarded + //public void LocalVariable_NoConversion_Tuple_DiscardFirst() + //{ + // var (_, x, value) = GetTuple(); + // Console.WriteLine(x); + // Console.WriteLine(value); + //} + + public void LocalVariable_NoConversion_Custom_DiscardLast() { var (myInt3, x, _) = GetSource(); Console.WriteLine(myInt3); Console.WriteLine(x); } - public void LocalVariable_NoConversion_DiscardSecond() + public void LocalVariable_NoConversion_Tuple_DiscardLast() + { + var (myInt, x, _) = GetTuple(); + Console.WriteLine(myInt); + Console.WriteLine(x); + } + + public void LocalVariable_NoConversion_Custom_DiscardSecond() { var (myInt3, _, value) = GetSource(); Console.WriteLine(myInt3); Console.WriteLine(value); } - public void LocalVariable_NoConversion_ReferenceTypes() + public void LocalVariable_NoConversion_Tuple_DiscardSecond() + { + var (myInt, _, value) = GetTuple(); + Console.WriteLine(myInt); + Console.WriteLine(value); + } + + public void LocalVariable_NoConversion_Custom_ReferenceTypes() { var (value, value2) = GetSource(); Console.WriteLine(value); Console.WriteLine(value2); } - public void LocalVariable_IntToLongConversion() + public void LocalVariable_NoConversion_Tuple_ReferenceTypes() + { + var (value, value2) = GetTuple(); + Console.WriteLine(value); + Console.WriteLine(value2); + } + + public void LocalVariable_IntToLongConversion_Custom() + { + int value; + long value2; + (value, value2) = GetSource(); + Console.WriteLine(value); + Console.WriteLine(value2); + } + + public void LocalVariable_IntToLongConversion_Tuple() + { + int value; + long value2; + (value, value2) = GetTuple(); + Console.WriteLine(value); + Console.WriteLine(value2); + } + + public void LocalVariable_FloatToDoubleConversion_Custom() { - var (value, value2) = GetSource(); + int value; + double value2; + (value, value2) = GetSource(); Console.WriteLine(value); Console.WriteLine(value2); } - public void LocalVariable_NoConversion_ComplexValue() + public void LocalVariable_FloatToDoubleConversion_Tuple() + { + int value; + double value2; + (value, value2) = GetTuple(); + Console.WriteLine(value); + Console.WriteLine(value2); + } + + // dynamic conversion is currently not supported + //public void LocalVariable_ImplicitReferenceConversion_Custom() + //{ + // object value; + // dynamic value2; + // (value, value2) = GetSource(); + // Console.WriteLine(value); + // value2.UseMe(); + //} + + //public void LocalVariable_ImplicitReferenceConversion_Tuple() + //{ + // object value; + // dynamic value2; + // (value, value2) = GetTuple(); + // Console.WriteLine(value); + // value2.UseMe(); + //} + + public void LocalVariable_NoConversion_ComplexValue_Custom() { var (myInt3, x) = new DeconstructionSource { Dummy = 3 @@ -198,46 +328,185 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Console.WriteLine(x); } - public void Property_NoConversion() + public void Property_NoConversion_Custom() { (Get(0).NMy, Get(1).My) = GetSource(); } - public void Property_NoConversion_DiscardFirst() + public void Property_IntToLongConversion_Custom() + { + (Get(0).Int, Get(1).Long) = GetSource(); + } + + public void Property_FloatToDoubleConversion_Custom() + { + (Get(0).Int, Get(1).Double) = GetSource(); + } + + // dynamic conversion is not supported + //public void Property_ImplicitReferenceConversion_Custom() + //{ + // (Get(0).Object, Get(1).Dynamic) = GetSource(); + //} + + public void Property_NoConversion_Custom_DiscardFirst() { (_, Get(1).My) = GetSource(); } - public void Property_NoConversion_DiscardLast() + public void Property_NoConversion_Custom_DiscardLast() { (Get(0).NMy, _) = GetSource(); } - public void Tuple_Property_NoConversion() + public void Property_NoConversion_Tuple() { (Get(0).NMy, Get(1).My) = GetTuple(); } - public void Tuple_Property_NoConversion_DiscardLast() + public void Property_NoConversion_Tuple_DiscardLast() { (Get(0).NMy, Get(1).My, _) = GetTuple(); } - public void RefLocal_FloatToDoubleConversion(out double a) + // currently we detect deconstruction, iff the first element is not discarded + //public void Property_NoConversion_Tuple_DiscardFirst() + //{ + // (_, Get(1).My, Get(2).Int) = GetTuple(); + //} + + public void Property_NoConversion_Custom_DiscardSecond() + { + (Get(0).NMy, _, Get(2).Int) = GetSource(); + } + + public void Property_NoConversion_Tuple_DiscardSecond() + { + (Get(0).NMy, _, Get(2).Int) = GetTuple(); + } + + public void Property_NoConversion_Custom_ReferenceTypes() + { + (Get(0).String, Get(1).String) = GetSource(); + } + + public void Property_NoConversion_Tuple_ReferenceTypes() + { + (Get(0).String, Get(1).String) = GetTuple(); + } + + public void Property_IntToLongConversion_Tuple() + { + (Get(0).Int, Get(1).Long) = GetTuple(); + } + + public void Property_FloatToDoubleConversion_Tuple() + { + (Get(0).Int, Get(1).Double) = GetTuple(); + } + + public void RefLocal_NoConversion_Custom(out double a) + { + (a, GetRef()) = GetSource(); + } + + public void RefLocal_NoConversion_Tuple(out double a) + { + (a, GetRef()) = GetTuple(); + } + + public void RefLocal_FloatToDoubleConversion_Custom(out double a) { (a, GetRef()) = GetSource(); } - //public void ArrayAssign_FloatToDoubleConversion(double[] arr) + public void RefLocal_FloatToDoubleConversion_Tuple(out double a) + { + (a, GetRef()) = GetTuple(); + } + + //public void ArrayAssign_FloatToDoubleConversion_Custom(double[] arr) //{ // (arr[0], arr[1], arr[2]) = GetSource(); //} - public void Field_NoConversion() + public void Field_NoConversion_Custom() { (Get(0).IntField, Get(1).IntField) = GetSource(); } + public void Field_NoConversion_Tuple() + { + (Get(0).IntField, Get(1).IntField) = GetTuple(); + } + + public void Field_IntToLongConversion_Custom() + { + (Get(0).IntField, Get(1).LongField) = GetSource(); + } + + public void Field_IntToLongConversion_Tuple() + { + (Get(0).IntField, Get(1).LongField) = GetTuple(); + } + + public void Field_FloatToDoubleConversion_Custom() + { + (Get(0).DoubleField, Get(1).DoubleField) = GetSource(); + } + + public void Field_FloatToDoubleConversion_Tuple() + { + (Get(0).DoubleField, Get(1).DoubleField) = GetTuple(); + } + + // dynamic conversion is not supported + //public void Field_ImplicitReferenceConversion_Custom() + //{ + // (Get(0).ObjectField, Get(1).DynamicField) = GetSource(); + //} + + public void Field_NoConversion_Custom_DiscardFirst() + { + (_, Get(1).MyField) = GetSource(); + } + + public void Field_NoConversion_Custom_DiscardLast() + { + (Get(0).NMyField, _) = GetSource(); + } + + public void Field_NoConversion_Tuple_DiscardLast() + { + (Get(0).NMyField, Get(1).MyField, _) = GetTuple(); + } + + // currently we detect deconstruction, iff the first element is not discarded + //public void Field_NoConversion_Tuple_DiscardFirst() + //{ + // (_, Get(1).MyField, Get(2).IntField) = GetTuple(); + //} + + public void Field_NoConversion_Custom_DiscardSecond() + { + (Get(0).NMyField, _, Get(2).IntField) = GetSource(); + } + + public void Field_NoConversion_Tuple_DiscardSecond() + { + (Get(0).NMyField, _, Get(2).IntField) = GetTuple(); + } + + public void Field_NoConversion_Custom_ReferenceTypes() + { + (Get(0).StringField, Get(1).StringField) = GetSource(); + } + + public void Field_NoConversion_Tuple_ReferenceTypes() + { + (Get(0).StringField, Get(1).StringField) = GetTuple(); + } + public void DeconstructDictionaryForEach(Dictionary dictionary) { foreach (var (str, num2) in dictionary) {