1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
// assign short name to the fake reflection type
using dynamic = ICSharpCode.NRefactory.TypeSystem.ReflectionHelper.Dynamic;
[TestFixture]
public class CastTests : ResolverTestBase
{
CSharpResolver resolver;
public override void SetUp()
{
base.SetUp();
resolver = new CSharpResolver(compilation);
}
void TestCast(Type targetType, ResolveResult input, Conversion expectedConversion)
{
IType type = compilation.FindType(targetType);
ResolveResult rr = resolver.ResolveCast(type, input);
AssertType(targetType, rr);
Assert.AreEqual(typeof(ConversionResolveResult), rr.GetType());
var crr = (ConversionResolveResult)rr;
Assert.AreEqual(expectedConversion, crr.Conversion, "ConversionResolveResult.Conversion");
Assert.AreSame(input, crr.Input, "ConversionResolveResult.Input");
}
[Test]
public void SimpleCast()
{
TestCast(typeof(int), MakeResult(typeof(float)), Conversion.ExplicitNumericConversion);
TestCast(typeof(string), MakeResult(typeof(object)), Conversion.ExplicitReferenceConversion);
TestCast(typeof(byte), MakeResult(typeof(dynamic)), Conversion.ExplicitDynamicConversion);
TestCast(typeof(dynamic), MakeResult(typeof(double)), Conversion.BoxingConversion);
}
[Test]
public void NullableCasts()
{
TestCast(typeof(int), MakeResult(typeof(int?)), Conversion.ExplicitNullableConversion);
TestCast(typeof(int?), MakeResult(typeof(int)), Conversion.ImplicitNullableConversion);
TestCast(typeof(int?), MakeResult(typeof(long?)), Conversion.ExplicitLiftedNumericConversion);
TestCast(typeof(long?), MakeResult(typeof(int?)), Conversion.ImplicitLiftedNumericConversion);
}
[Test]
public void ConstantValueCast()
{
AssertConstant("Hello", resolver.ResolveCast(ResolveType(typeof(string)), MakeConstant("Hello")));
AssertConstant((byte)1L, resolver.ResolveCast(ResolveType(typeof(byte)), MakeConstant(1L)));
AssertConstant(3, resolver.ResolveCast(ResolveType(typeof(int)), MakeConstant(3.1415)));
AssertConstant(3, resolver.ResolveCast(ResolveType(typeof(int)), MakeConstant(3.99)));
AssertConstant((short)-3, resolver.ResolveCast(ResolveType(typeof(short)), MakeConstant(-3.99f)));
AssertConstant(-3L, resolver.ResolveCast(ResolveType(typeof(long)), MakeConstant(-3.5)));
}
[Ignore("Broken on mcs/mac os x")]
[Test]
public void OverflowingCast()
{
AssertConstant(uint.MaxValue, resolver.WithCheckForOverflow(false).ResolveCast(ResolveType(typeof(uint)), MakeConstant(-1.6)));
AssertError(typeof(uint), resolver.WithCheckForOverflow(true).ResolveCast(ResolveType(typeof(uint)), MakeConstant(-1.6)));
}
[Test]
public void FailingStringCast()
{
AssertError(typeof(string), resolver.ResolveCast(ResolveType(typeof(string)), MakeConstant(1)));
}
[Test]
public void OverflowingCastToEnum()
{
AssertError(typeof(StringComparison), resolver.WithCheckForOverflow(true).ResolveCast(ResolveType(typeof(StringComparison)), MakeConstant(long.MaxValue)));
}
[Test]
public void ImplicitCastInConstant()
{
string program = @"using System;
class Test {
const int $MAXSIZE = ushort.MaxValue;
}";
var rr = ResolveAtLocation<MemberResolveResult>(program);
IField field = (IField)rr.Member;
Assert.IsTrue(field.IsConst);
Assert.AreEqual("System.Int32", field.Type.FullName);
Assert.AreEqual(typeof(int), field.ConstantValue.GetType());
Assert.AreEqual(ushort.MaxValue, (int)field.ConstantValue);
}
[Test]
public void ImplicitCastInLocalConstant()
{
string program = @"using System;
class Test {
void M() {
const int $MAXSIZE = ushort.MaxValue;
}
}";
var rr = ResolveAtLocation<LocalResolveResult>(program);
Assert.IsTrue(rr.Variable.IsConst);
Assert.AreEqual("System.Int32", rr.Variable.Type.FullName);
Assert.AreEqual(typeof(int), rr.Variable.ConstantValue.GetType());
Assert.AreEqual(ushort.MaxValue, (int)rr.Variable.ConstantValue);
}
}
}
|