Browse Source

#3518: Add tests that try unboxing to the wrong type in a number of different ways.

null-coalescing-assignment
Daniel Grunwald 4 months ago
parent
commit
83298210e4
  1. 45
      ICSharpCode.Decompiler.Tests/TestCases/Correctness/TrickyTypes.cs

45
ICSharpCode.Decompiler.Tests/TestCases/Correctness/TrickyTypes.cs

@ -30,6 +30,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
StringConcat(); StringConcat();
LinqNullableMin(); LinqNullableMin();
LinqNullableMin(1, 2, 3); LinqNullableMin(1, 2, 3);
UnboxingToWrongType();
} }
static void Print<T>(T val) static void Print<T>(T val)
@ -110,5 +111,49 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
Print(string.Format("LinqNullableMin {0}:", arr.Length)); Print(string.Format("LinqNullableMin {0}:", arr.Length));
Print(arr.Min(v => (int?)v)); Print(arr.Min(v => (int?)v));
} }
static void UnboxingToWrongType()
{
Console.WriteLine("UnboxingToWrongType:");
object o = 3.50;
try
{
Print((long)o);
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
try
{
Print(o as long?);
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
try
{
Print((long)(o as long?));
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
try
{
Print((long)(o is long ? o : null));
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
#if CS90
if (o is not 0L)
{
Console.WriteLine("Not 0L");
}
#endif
}
} }
} }

Loading…
Cancel
Save