|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Microsoft.CSharp.RuntimeBinder.Tests |
| 12 | +{ |
| 13 | + public class AssignmentTests |
| 14 | + { |
| 15 | + public static object[][] Additions = |
| 16 | + { |
| 17 | + new object[]{1, 2, 3}, |
| 18 | + new object[]{1L, 2, 3L}, |
| 19 | + new object[]{1, 2L, 3L}, |
| 20 | + new object[]{1U, 2U, 3U}, |
| 21 | + new object[]{1, 2U, 3L}, |
| 22 | + new object[]{1, 2M, 3M}, |
| 23 | + new object[]{1.0, 2, 3.0}, |
| 24 | + new object[]{1, 2.0, 3.0} |
| 25 | + }; |
| 26 | + |
| 27 | + private sealed class HasProperty<T> |
| 28 | + { |
| 29 | + public T Prop { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + private static HasProperty<T> GetHasProperty<T>(T initial) => new HasProperty<T>{ Prop = initial }; |
| 33 | + |
| 34 | + public static object[][] PropertyAdditions = |
| 35 | + { |
| 36 | + new object[]{GetHasProperty(1), 2, 3}, |
| 37 | + new object[]{GetHasProperty(1L), 2, 3L}, |
| 38 | + new object[]{GetHasProperty(1), 2L, 3}, |
| 39 | + new object[]{GetHasProperty(1U), 2U, 3U}, |
| 40 | + new object[]{GetHasProperty(1), 2U, 3}, |
| 41 | + new object[]{GetHasProperty(1), 2M, 3}, |
| 42 | + new object[]{GetHasProperty(1.0), 2, 3.0}, |
| 43 | + new object[]{GetHasProperty(1), 2.0, 3} |
| 44 | + }; |
| 45 | + |
| 46 | + public static object[][] PropertyAssigments = |
| 47 | + { |
| 48 | + new object[]{GetHasProperty(1), 2, 2}, |
| 49 | + new object[]{GetHasProperty(1L), 2, 2L}, |
| 50 | + new object[]{GetHasProperty(1), 2L, 2}, |
| 51 | + new object[]{GetHasProperty(1U), 2U, 2U}, |
| 52 | + new object[]{GetHasProperty(1), 2U, 2}, |
| 53 | + new object[]{GetHasProperty(1), 2M, 2}, |
| 54 | + new object[]{GetHasProperty(1.0), 2, 2.0}, |
| 55 | + new object[]{GetHasProperty(1), 2.0, 2} |
| 56 | + }; |
| 57 | + |
| 58 | + public static object[][] PropertyBadAssigments = |
| 59 | + { |
| 60 | + new object[]{GetHasProperty(1), decimal.MaxValue, true, false}, |
| 61 | + new object[]{GetHasProperty(1), DateTime.MinValue, false, false}, |
| 62 | + new object[]{GetHasProperty(2), "hello", false, false}, |
| 63 | + new object[]{GetHasProperty(1), null, false, false}, |
| 64 | + new object[]{GetHasProperty(true), 2, false, false}, |
| 65 | + new object[]{GetHasProperty(2.0), false, false, false}, |
| 66 | + new object[]{GetHasProperty("abc"), 2.0, false, false}, |
| 67 | + new object[]{GetHasProperty("abc"), new object(), false, true}, |
| 68 | + new object[]{GetHasProperty(2), new object(), false, true} |
| 69 | + }; |
| 70 | + |
| 71 | + public static IEnumerable<object[]> PropertyBadCheckedAssigments = |
| 72 | + new[] |
| 73 | + { |
| 74 | + new object[] {GetHasProperty(1), long.MaxValue, true, false}, |
| 75 | + new object[] {GetHasProperty(1), (double)long.MaxValue, true, false}, |
| 76 | + new object[] {GetHasProperty(1U), -1, true, false}, |
| 77 | + new object[] {GetHasProperty(1), uint.MaxValue, true, false}, |
| 78 | + new object[] {GetHasProperty(default(short)), int.MaxValue, true, false}, |
| 79 | + new object[]{GetHasProperty(2UL), -1, true, false} |
| 80 | + }.Concat(PropertyBadAssigments); |
| 81 | + |
| 82 | + [Theory, MemberData(nameof(Additions))] |
| 83 | + public void AddAssignment(dynamic lhs, dynamic rhs, object expected) |
| 84 | + { |
| 85 | + lhs += rhs; |
| 86 | + Assert.Equal(expected, lhs); |
| 87 | + Assert.IsType(expected.GetType(), lhs); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory, MemberData(nameof(PropertyAdditions))] |
| 91 | + public void PropertyAddAssignment(dynamic lhs, dynamic rhs, object expected) |
| 92 | + { |
| 93 | + lhs.Prop += rhs; |
| 94 | + Assert.Equal(expected, lhs.Prop); |
| 95 | + } |
| 96 | + |
| 97 | + [Theory, MemberData(nameof(PropertyAssigments))] |
| 98 | + public void PropertyAddResultAssigment(object lhs, object rhs, object expected) |
| 99 | + { |
| 100 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 101 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 102 | + Binder.SetMember( |
| 103 | + CSharpBinderFlags.ValueFromCompoundAssignment, "Prop", GetType(), |
| 104 | + new[] |
| 105 | + { |
| 106 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 107 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |
| 108 | + })); |
| 109 | + object result = callSite.Target(callSite, lhs, rhs); |
| 110 | + Assert.Equal(expected, result); |
| 111 | + Assert.Equal(expected, ((dynamic)lhs).Prop); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory, MemberData(nameof(PropertyAssigments))] |
| 115 | + public void PropertyAddConstantResultAssigment(object lhs, object rhs, object expected) |
| 116 | + { |
| 117 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 118 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 119 | + Binder.SetMember( |
| 120 | + CSharpBinderFlags.ValueFromCompoundAssignment, "Prop", GetType(), |
| 121 | + new[] |
| 122 | + { |
| 123 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 124 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant, null) |
| 125 | + })); |
| 126 | + object result = callSite.Target(callSite, lhs, rhs); |
| 127 | + Assert.Equal(expected, result); |
| 128 | + Assert.Equal(expected, ((dynamic)lhs).Prop); |
| 129 | + } |
| 130 | + |
| 131 | + [Theory, MemberData(nameof(PropertyBadAssigments))] |
| 132 | + public void PropertyIncompatibleAssigment(object lhs, object rhs, bool overflow, bool invalidCast) |
| 133 | + { |
| 134 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 135 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 136 | + Binder.SetMember( |
| 137 | + CSharpBinderFlags.ValueFromCompoundAssignment, "Prop", GetType(), |
| 138 | + new[] |
| 139 | + { |
| 140 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 141 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |
| 142 | + })); |
| 143 | + if (invalidCast) // Invalid cast at runtime |
| 144 | + { |
| 145 | + Assert.Throws<InvalidCastException>(() => callSite.Target(callSite, lhs, rhs)); |
| 146 | + } |
| 147 | + else if (overflow) // Overflows at runtime, rather than fail at bind time |
| 148 | + { |
| 149 | + Assert.Throws<OverflowException>(() => callSite.Target(callSite, lhs, rhs)); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + Assert.Throws<RuntimeBinderException>(() => callSite.Target(callSite, lhs, rhs)); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + [Theory, MemberData(nameof(PropertyBadAssigments))] |
| 158 | + public void PropertyIncompatibleConstantAssigment(object lhs, object rhs, bool _, bool invalidCast) |
| 159 | + { |
| 160 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 161 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 162 | + Binder.SetMember( |
| 163 | + CSharpBinderFlags.ValueFromCompoundAssignment, "Prop", GetType(), |
| 164 | + new[] |
| 165 | + { |
| 166 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 167 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant, null) |
| 168 | + })); |
| 169 | + if (invalidCast) |
| 170 | + { |
| 171 | + Assert.Throws<InvalidCastException>(() => callSite.Target(callSite, lhs, rhs)); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + Assert.Throws<RuntimeBinderException>(() => callSite.Target(callSite, lhs, rhs)); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + [Theory, MemberData(nameof(PropertyBadCheckedAssigments))] |
| 180 | + public void PropertyIncompatibleCheckedAssigment(object lhs, object rhs, bool overflow, bool invalidCast) |
| 181 | + { |
| 182 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 183 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 184 | + Binder.SetMember( |
| 185 | + CSharpBinderFlags.ValueFromCompoundAssignment | CSharpBinderFlags.CheckedContext, "Prop", |
| 186 | + GetType(), |
| 187 | + new[] |
| 188 | + { |
| 189 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 190 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |
| 191 | + })); |
| 192 | + if (invalidCast) |
| 193 | + { |
| 194 | + Assert.Throws<InvalidCastException>(() => callSite.Target(callSite, lhs, rhs)); |
| 195 | + } |
| 196 | + else if (overflow) // Overflows at runtime, rather than fail at bind time |
| 197 | + { |
| 198 | + Assert.Throws<OverflowException>(() => callSite.Target(callSite, lhs, rhs)); |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + Assert.Throws<RuntimeBinderException>(() => callSite.Target(callSite, lhs, rhs)); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + [Theory, MemberData(nameof(PropertyBadCheckedAssigments))] |
| 207 | + public void PropertyIncompatibleConstantCheckedAssigment(object lhs, object rhs, bool _, bool invalidCast) |
| 208 | + { |
| 209 | + CallSite<Func<CallSite, object, object, object>> callSite = |
| 210 | + CallSite<Func<CallSite, object, object, object>>.Create( |
| 211 | + Binder.SetMember( |
| 212 | + CSharpBinderFlags.ValueFromCompoundAssignment | CSharpBinderFlags.CheckedContext, "Prop", |
| 213 | + GetType(), |
| 214 | + new[] |
| 215 | + { |
| 216 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), |
| 217 | + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant, null) |
| 218 | + })); |
| 219 | + if (invalidCast) // Invalid cast at runtime |
| 220 | + { |
| 221 | + Assert.Throws<InvalidCastException>(() => callSite.Target(callSite, lhs, rhs)); |
| 222 | + } |
| 223 | + else |
| 224 | + { |
| 225 | + Assert.Throws<RuntimeBinderException>(() => callSite.Target(callSite, lhs, rhs)); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments