From 06dc80ca496e92dad28db02825f95a685ab77047 Mon Sep 17 00:00:00 2001 From: Cody Merritt Anhorn Date: Mon, 13 Sep 2021 22:54:38 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20Number=20Format=20Exception=20when=20Con?= =?UTF-8?q?verting=20decimal=20from=20JavaScript=20=F0=9F=90=9B=20Convert.?= =?UTF-8?q?ChangeType=20would=20throw=20a=20Number=20Format=20Exception=20?= =?UTF-8?q?when=20the=20current=20Culture=20was=20not=20that=20of=20a=20'.?= =?UTF-8?q?'=20decimal=20place.=20The=20fix=20was=20to=20update=20the=20Co?= =?UTF-8?q?nvert.ChangeType=20to=20use=20an=20InvariantCluture=20when=20ch?= =?UTF-8?q?anging=20the=20type=20of=20a=20decimal/float=20number.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #32 --- .../InteropDecimalNumberClutureInfoTest.razor | 78 +++++++++++++++++++ .../Validation/InteropValidationsPage.razor | 1 + .../EventHorizonBlazorInterop.cs | 10 ++- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropDecimalNumberClutureInfoTest.razor diff --git a/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropDecimalNumberClutureInfoTest.razor b/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropDecimalNumberClutureInfoTest.razor new file mode 100644 index 0000000..11806ea --- /dev/null +++ b/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropDecimalNumberClutureInfoTest.razor @@ -0,0 +1,78 @@ +@using System.Globalization +
+

Literal Decimal Number Cluture Info Validation

+
Interop Get
+
+ Status: + @if (TestStatus == "Passed") + { + @TestStatus + } + else if (TestStatus == "Failed") + { + @TestStatus + } + else + { + @TestStatus + } +
+ +
+ + + +@code { + public string TestStatus = "Pending"; + + private string testId => "InteropDecimalNumberClutureInfoTest"; + private decimal result; + private decimal expected = 0.0000000000000001334646852585m; + + private void HandleRunTest() + { + var clutureInfo = CultureInfo.CurrentCulture; + try + { + // Using de-DE because the decimal place is ',' for decimal/numbers. + CultureInfo.CurrentCulture = new CultureInfo( + "de-DE" + ); + + RunTest(); + ValidateTest(); + } + catch { } + finally + { + // Reset back to ClutureInfo before test changed it. + CultureInfo.CurrentCulture = clutureInfo; + } + } + + public void RunTest() + { + result = EventHorizonBlazorInterop.Get( + testId, + "value" + ); + } + + public void ValidateTest() + { + if (result == expected) + { + TestStatus = "Passed"; + } + else + { + TestStatus = "Failed"; + } + } +} diff --git a/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropValidationsPage.razor b/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropValidationsPage.razor index 701e0ab..0387fcf 100644 --- a/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropValidationsPage.razor +++ b/EventHorizon.Blazor.Interop.Sample/Pages/Testing/InteropTesting/Validation/InteropValidationsPage.razor @@ -3,6 +3,7 @@

Interop Validations

+ diff --git a/EventHorizon.Blazor.Interop/EventHorizonBlazorInterop.cs b/EventHorizon.Blazor.Interop/EventHorizonBlazorInterop.cs index 3d90258..640f315 100644 --- a/EventHorizon.Blazor.Interop/EventHorizonBlazorInterop.cs +++ b/EventHorizon.Blazor.Interop/EventHorizonBlazorInterop.cs @@ -1,6 +1,7 @@ namespace EventHorizon.Blazor.Interop { using System; + using System.Globalization; using System.Threading.Tasks; using Microsoft.JSInterop; using Microsoft.JSInterop.WebAssembly; @@ -209,9 +210,9 @@ params object[] args /// /// /// ( - /// entity => new Vector3(entity), - /// "document.createElement" + /// EventHorizonBlazorInterop.Get( + /// "document", + /// "nodeType" /// ); /// ]]> /// @@ -240,7 +241,8 @@ string prop } return (T)Convert.ChangeType( result, - Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T) + Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), + CultureInfo.InvariantCulture ); }