|
| 1 | +using System.Globalization; |
| 2 | +using Lua.Standard; |
| 3 | + |
| 4 | +namespace Lua.Tests; |
| 5 | + |
| 6 | +public class NumericTextPolicyTests |
| 7 | +{ |
| 8 | + [TestCase("f")] |
| 9 | + [TestCase("e")] |
| 10 | + [TestCase("E")] |
| 11 | + [TestCase("g")] |
| 12 | + [TestCase("G")] |
| 13 | + [TestCase("a")] |
| 14 | + [TestCase("A")] |
| 15 | + public async Task ExplicitSpecialValuesUseCanonicalWords(string specifier) |
| 16 | + { |
| 17 | + using var state = LuaState.Create(); |
| 18 | + state.OpenStandardLibraries(); |
| 19 | + state.Environment["nan"] = double.NaN; |
| 20 | + var values = await state.DoStringAsync( |
| 21 | + $"return string.format('%{specifier}',math.huge),string.format('%{specifier}',-math.huge),string.format('%{specifier}',nan)" |
| 22 | + ); |
| 23 | + var expected = new[] { "inf", "-inf", "-nan" }; |
| 24 | + if (specifier is "E" or "G" or "A") |
| 25 | + expected = expected.Select(s => s.ToUpperInvariant()).ToArray(); |
| 26 | + Assert.That(values.Select(v => v.Read<string>()), Is.EqualTo(expected)); |
| 27 | + } |
| 28 | + |
| 29 | + [TestCase(1d / 3, "0.33333333333333")] |
| 30 | + [TestCase(1e20, "1e+20")] |
| 31 | + [TestCase(double.Epsilon, "4.9406564584125e-324")] |
| 32 | + [TestCase(double.PositiveInfinity, "inf")] |
| 33 | + [TestCase(double.NegativeInfinity, "-inf")] |
| 34 | + [TestCase(double.NaN, "-nan")] |
| 35 | + public async Task LuaFacingPathsUseTheProposedInvariantText(double number, string expected) |
| 36 | + { |
| 37 | + using var state = LuaState.Create(); |
| 38 | + state.OpenStandardLibraries(); |
| 39 | + state.Environment["number"] = number; |
| 40 | + var culture = CultureInfo.CurrentCulture; |
| 41 | + try |
| 42 | + { |
| 43 | + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); |
| 44 | + var values = await state.DoStringAsync( |
| 45 | + """ |
| 46 | + local x=setmetatable({}, {__tostring=function() return number end}) |
| 47 | + local direct=string.gsub('x','x',number) |
| 48 | + local callback=string.gsub('x','x',function() return number end) |
| 49 | + local lookup=string.gsub('x','x',{x=number}) |
| 50 | + return tostring(number),string.format('%s',number),number..'', |
| 51 | + table.concat({number}),tostring(x),direct,callback,lookup |
| 52 | + """ |
| 53 | + ); |
| 54 | + Assert.That(values.Select(v => v.Read<string>()), Is.All.EqualTo(expected)); |
| 55 | + } |
| 56 | + finally |
| 57 | + { |
| 58 | + CultureInfo.CurrentCulture = culture; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void HostToStringRetainsManagedPrecisionAndSpecialWords() |
| 64 | + { |
| 65 | + Assert.That(new LuaValue(1d / 3).ToString(), Is.EqualTo("0.3333333333333333")); |
| 66 | + Assert.That(new LuaValue(double.PositiveInfinity).ToString(), Is.EqualTo("Infinity")); |
| 67 | + Assert.That(new LuaValue(double.NaN).ToString(), Is.EqualTo("NaN")); |
| 68 | + } |
| 69 | +} |
0 commit comments