|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | + |
| 4 | +namespace Python.EmbeddingTest |
| 5 | +{ |
| 6 | + public class ConvertibleFee |
| 7 | + { |
| 8 | + public decimal Amount { get; } |
| 9 | + |
| 10 | + public ConvertibleFee(decimal amount) |
| 11 | + { |
| 12 | + Amount = amount; |
| 13 | + } |
| 14 | + |
| 15 | + public static implicit operator decimal(ConvertibleFee fee) => fee.Amount; |
| 16 | + } |
| 17 | + |
| 18 | + public class ConvertibleRatio |
| 19 | + { |
| 20 | + public double Value { get; } |
| 21 | + |
| 22 | + public ConvertibleRatio(double value) |
| 23 | + { |
| 24 | + Value = value; |
| 25 | + } |
| 26 | + |
| 27 | + public static implicit operator double(ConvertibleRatio ratio) => ratio.Value; |
| 28 | + } |
| 29 | + |
| 30 | + public class ConvertibleMoney |
| 31 | + { |
| 32 | + public decimal Amount { get; } |
| 33 | + |
| 34 | + public ConvertibleMoney(decimal amount) |
| 35 | + { |
| 36 | + Amount = amount; |
| 37 | + } |
| 38 | + |
| 39 | + public static implicit operator decimal(ConvertibleMoney money) => money.Amount; |
| 40 | + |
| 41 | + // the type's own operator must keep winning over the synthesized one |
| 42 | + public static ConvertibleMoney operator +(ConvertibleMoney a, ConvertibleMoney b) => new(a.Amount + b.Amount); |
| 43 | + } |
| 44 | + |
| 45 | + public class NotConvertible |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + public class TestImplicitNumericOperators |
| 50 | + { |
| 51 | + private static dynamic module; |
| 52 | + |
| 53 | + private const string TestModule = @" |
| 54 | +from clr import AddReference |
| 55 | +AddReference(""System"") |
| 56 | +AddReference(""Python.EmbeddingTest"") |
| 57 | +from Python.EmbeddingTest import * |
| 58 | +
|
| 59 | +fee = ConvertibleFee(2) |
| 60 | +other = ConvertibleFee(3) |
| 61 | +
|
| 62 | +def arithmetic(): |
| 63 | + return (fee + other, fee + 1, 1 + fee, fee - other, fee - 1, 10 - fee, |
| 64 | + fee * other, fee * 2, 2 * fee, other / fee, fee / 2, 10 / fee, -fee) |
| 65 | +
|
| 66 | +def aggregates(): |
| 67 | + return sum([fee, other]), max(fee, other) is other, sorted([other, fee])[0] is fee |
| 68 | +
|
| 69 | +def comparisons(): |
| 70 | + return (fee < other, fee > other, fee <= other, fee >= other, |
| 71 | + fee > 0, fee < 2, fee <= 2, fee >= 2, 1 < fee, 2 >= fee) |
| 72 | +
|
| 73 | +def conversions(): |
| 74 | + return float(fee), int(ConvertibleRatio(1.75)), float(ConvertibleRatio(1.75)) |
| 75 | +
|
| 76 | +def own_operator_wins(): |
| 77 | + total = ConvertibleMoney(2) + ConvertibleMoney(3) |
| 78 | + return type(total).__name__, total.Amount, ConvertibleMoney(2) - ConvertibleMoney(1) |
| 79 | +
|
| 80 | +def not_convertible(): |
| 81 | + return hasattr(NotConvertible(), '__add__'), hasattr(NotConvertible(), '__float__') |
| 82 | +"; |
| 83 | + |
| 84 | + [OneTimeSetUp] |
| 85 | + public void OneTimeSetUp() |
| 86 | + { |
| 87 | + PythonEngine.Initialize(); |
| 88 | + using var _ = Py.GIL(); |
| 89 | + module = PyModule.FromString("implicit_numeric_operators", TestModule); |
| 90 | + } |
| 91 | + |
| 92 | + [OneTimeTearDown] |
| 93 | + public void Dispose() |
| 94 | + { |
| 95 | + PythonEngine.Shutdown(); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void ArithmeticDelegatesToTheImplicitConversion() |
| 100 | + { |
| 101 | + using var _ = Py.GIL(); |
| 102 | + var result = module.arithmetic(); |
| 103 | + var expected = new[] { 5.0, 3.0, 3.0, -1.0, 1.0, 8.0, 6.0, 4.0, 4.0, 1.5, 1.0, 5.0, -2.0 }; |
| 104 | + for (var i = 0; i < expected.Length; i++) |
| 105 | + { |
| 106 | + Assert.AreEqual(expected[i], (double)result[i], $"index {i}"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void AggregatesWork() |
| 112 | + { |
| 113 | + using var _ = Py.GIL(); |
| 114 | + var result = module.aggregates(); |
| 115 | + Assert.AreEqual(5.0, (double)result[0]); |
| 116 | + Assert.IsTrue((bool)result[1]); |
| 117 | + Assert.IsTrue((bool)result[2]); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public void ComparisonsDelegateToTheImplicitConversion() |
| 122 | + { |
| 123 | + using var _ = Py.GIL(); |
| 124 | + var result = module.comparisons(); |
| 125 | + var expected = new[] { true, false, true, false, true, false, true, true, true, true }; |
| 126 | + for (var i = 0; i < expected.Length; i++) |
| 127 | + { |
| 128 | + Assert.AreEqual(expected[i], (bool)result[i], $"index {i}"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public void FloatAndIntConversions() |
| 134 | + { |
| 135 | + using var _ = Py.GIL(); |
| 136 | + var result = module.conversions(); |
| 137 | + Assert.AreEqual(2.0, (double)result[0]); |
| 138 | + Assert.AreEqual(1, (int)result[1]); |
| 139 | + Assert.AreEqual(1.75, (double)result[2]); |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + public void TheTypesOwnOperatorsKeepWinning() |
| 144 | + { |
| 145 | + using var _ = Py.GIL(); |
| 146 | + var result = module.own_operator_wins(); |
| 147 | + Assert.AreEqual("ConvertibleMoney", (string)result[0]); |
| 148 | + Assert.AreEqual(5m, (decimal)result[1]); |
| 149 | + // subtraction is not declared by the type, so the synthesized one applies |
| 150 | + Assert.AreEqual(1.0, (double)result[2]); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void TypesWithoutAConversionAreUntouched() |
| 155 | + { |
| 156 | + using var _ = Py.GIL(); |
| 157 | + var result = module.not_convertible(); |
| 158 | + Assert.IsFalse((bool)result[0]); |
| 159 | + Assert.IsFalse((bool)result[1]); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments