|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | + |
| 4 | +namespace Python.EmbeddingTest |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Passing a Python float where a .NET integer is expected. |
| 8 | + /// |
| 9 | + /// A float that holds an integral value (e.g. 5.0) is accepted and converted; |
| 10 | + /// a non-integral float (e.g. 5.5) is rejected rather than silently truncated. |
| 11 | + /// This must hold regardless of whether the target method/constructor has a |
| 12 | + /// single signature or several overloads (the latter reproduces Lean's |
| 13 | + /// RangeConsolidator(period), which has two int-first constructor overloads). |
| 14 | + /// </summary> |
| 15 | + public class TestFloatToIntConversion |
| 16 | + { |
| 17 | + private PyModule _module; |
| 18 | + |
| 19 | + private const string TestModule = @" |
| 20 | +from clr import AddReference |
| 21 | +AddReference(""Python.EmbeddingTest"") |
| 22 | +from Python.EmbeddingTest import IntTaker, OverloadedIntTaker |
| 23 | +
|
| 24 | +def single_ctor(value): |
| 25 | + return IntTaker(value).Value |
| 26 | +
|
| 27 | +def single_method(value): |
| 28 | + return IntTaker(0).Echo(value) |
| 29 | +
|
| 30 | +def overloaded_ctor(value): |
| 31 | + return OverloadedIntTaker(value).Value |
| 32 | +
|
| 33 | +def overloaded_method(value): |
| 34 | + return OverloadedIntTaker(0).Echo(value) |
| 35 | +
|
| 36 | +def single_named(value): |
| 37 | + return IntTaker(0).ComputeValue(value) |
| 38 | +
|
| 39 | +def overloaded_named(value): |
| 40 | + return OverloadedIntTaker(0).ComputeRange(value) |
| 41 | +
|
| 42 | +def single_params(value): |
| 43 | + return IntTaker(0).ComputeScaled(value) |
| 44 | +"; |
| 45 | + |
| 46 | + [OneTimeSetUp] |
| 47 | + public void Setup() |
| 48 | + { |
| 49 | + PythonEngine.Initialize(); |
| 50 | + _module = PyModule.FromString("float_to_int_module", TestModule); |
| 51 | + } |
| 52 | + |
| 53 | + [OneTimeTearDown] |
| 54 | + public void TearDown() |
| 55 | + { |
| 56 | + _module.Dispose(); |
| 57 | + PythonEngine.Shutdown(); |
| 58 | + } |
| 59 | + |
| 60 | + private int Call(string func, double value) |
| 61 | + { |
| 62 | + using (Py.GIL()) |
| 63 | + using (var arg = value.ToPython()) |
| 64 | + { |
| 65 | + return _module.InvokeMethod(func, arg).As<int>(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // An integral-valued float is accepted and converted, single or overloaded. |
| 70 | + [TestCase("single_ctor")] |
| 71 | + [TestCase("single_method")] |
| 72 | + [TestCase("overloaded_ctor")] |
| 73 | + [TestCase("overloaded_method")] |
| 74 | + public void IntegralFloat_IsAccepted(string func) |
| 75 | + { |
| 76 | + Assert.AreEqual(5, Call(func, 5.0)); |
| 77 | + } |
| 78 | + |
| 79 | + // A non-integral float is rejected (no silent truncation) for every target. |
| 80 | + [TestCase("single_ctor")] |
| 81 | + [TestCase("single_method")] |
| 82 | + [TestCase("overloaded_ctor")] |
| 83 | + [TestCase("overloaded_method")] |
| 84 | + public void NonIntegralFloat_IsRejected(string func) |
| 85 | + { |
| 86 | + var ex = Assert.Throws<PythonException>(() => Call(func, 5.5)); |
| 87 | + Assert.AreEqual("TypeError", ex.Type.Name); |
| 88 | + } |
| 89 | + |
| 90 | + // When no overload matches, the error should hint the expected signature(s). |
| 91 | + [Test] |
| 92 | + public void ErrorMessage_SingleOverload_ShowsExpectedSignature() |
| 93 | + { |
| 94 | + var ex = Assert.Throws<PythonException>(() => Call("single_ctor", 5.5)); |
| 95 | + StringAssert.Contains("The expected signature is:", ex.Message); |
| 96 | + StringAssert.Contains("Int32 value", ex.Message); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void ErrorMessage_MultipleOverloads_ListsCandidates() |
| 101 | + { |
| 102 | + var ex = Assert.Throws<PythonException>(() => Call("overloaded_ctor", 5.5)); |
| 103 | + StringAssert.Contains("The following overloads are available:", ex.Message); |
| 104 | + // The int overload is surfaced, hinting an integer was expected. |
| 105 | + StringAssert.Contains("Int32 range", ex.Message); |
| 106 | + } |
| 107 | + |
| 108 | + // The hinted signatures use the snake_case name Python callers use, not the |
| 109 | + // original C# name. |
| 110 | + [Test] |
| 111 | + public void ErrorMessage_SingleOverload_UsesSnakeCaseMethodName() |
| 112 | + { |
| 113 | + var ex = Assert.Throws<PythonException>(() => Call("single_named", 5.5)); |
| 114 | + StringAssert.Contains("compute_value(", ex.Message); |
| 115 | + StringAssert.DoesNotContain("ComputeValue", ex.Message); |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public void ErrorMessage_MultipleOverloads_UseSnakeCaseMethodName() |
| 120 | + { |
| 121 | + var ex = Assert.Throws<PythonException>(() => Call("overloaded_named", 5.5)); |
| 122 | + StringAssert.Contains("compute_range(", ex.Message); |
| 123 | + StringAssert.DoesNotContain("ComputeRange", ex.Message); |
| 124 | + } |
| 125 | + |
| 126 | + // The hinted signatures also snake_case the parameter names. |
| 127 | + [Test] |
| 128 | + public void ErrorMessage_SignatureParameters_AreSnakeCase() |
| 129 | + { |
| 130 | + var ex = Assert.Throws<PythonException>(() => Call("single_params", 5.5)); |
| 131 | + StringAssert.Contains("scale_factor", ex.Message); |
| 132 | + StringAssert.DoesNotContain("scaleFactor", ex.Message); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public class IntTaker |
| 137 | + { |
| 138 | + public int Value { get; } |
| 139 | + |
| 140 | + public IntTaker(int value) |
| 141 | + { |
| 142 | + Value = value; |
| 143 | + } |
| 144 | + |
| 145 | + public int Echo(int value) => value; |
| 146 | + |
| 147 | + public int ComputeValue(int value) => value; |
| 148 | + |
| 149 | + public int ComputeScaled(int scaleFactor) => scaleFactor; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Mimics Lean's RangeConsolidator: two overloads that both take an int first |
| 154 | + /// parameter, differing only in the (defaulted) later parameters. This forces the |
| 155 | + /// binder through its overload-disambiguation path. |
| 156 | + /// </summary> |
| 157 | + public class OverloadedIntTaker |
| 158 | + { |
| 159 | + public int Value { get; } |
| 160 | + |
| 161 | + public OverloadedIntTaker(int range, System.Func<int, int> selector = null) |
| 162 | + { |
| 163 | + Value = range; |
| 164 | + } |
| 165 | + |
| 166 | + public OverloadedIntTaker(int range, PyObject selector, PyObject volumeSelector = null) |
| 167 | + { |
| 168 | + Value = range; |
| 169 | + } |
| 170 | + |
| 171 | + public int Echo(int value, System.Func<int, int> selector = null) => value; |
| 172 | + |
| 173 | + public int Echo(int value, PyObject selector, PyObject other = null) => value; |
| 174 | + |
| 175 | + public int ComputeRange(int value, System.Func<int, int> selector = null) => value; |
| 176 | + |
| 177 | + public int ComputeRange(int value, PyObject selector, PyObject other = null) => value; |
| 178 | + } |
| 179 | +} |
0 commit comments