From 6931d66d32b4fc61181c1ad15efc93969ccdca79 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 16 Feb 2024 13:55:17 +0100 Subject: [PATCH 1/2] wip --- ...me.InteropServices.JavaScript.Tests.csproj | 4 +- .../JavaScript/JSExportTest.cs | 417 ++++++++++ ...{JSImportExportTest.cs => JSImportTest.cs} | 755 ++---------------- .../JavaScript/JSInteropTestBase.cs | 340 ++++++++ .../JavaScript/JavaScriptTestHelper.cs | 7 + .../JavaScript/JavaScriptTestHelper.mjs | 31 + 6 files changed, 848 insertions(+), 706 deletions(-) create mode 100644 src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs rename src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/{JSImportExportTest.cs => JSImportTest.cs} (64%) create mode 100644 src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSInteropTestBase.cs diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj index f390f63deaeb67..114a43b3f6a1ee 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj @@ -21,7 +21,9 @@ - + + + diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs new file mode 100644 index 00000000000000..990e755c33e1d6 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs @@ -0,0 +1,417 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using System.Threading; +using Xunit; +using System.Diagnostics.CodeAnalysis; +#pragma warning disable xUnit1026 // Theory methods should use all of their parameters + +namespace System.Runtime.InteropServices.JavaScript.Tests +{ + public class JSExportAsyncTest : JSInteropTestBase, IAsyncLifetime + { + [Theory] + [MemberData(nameof(MarshalBooleanCases))] + public async Task JsExportBooleanAsync(bool value) + { + await JsExportTestAsync(value, + JavaScriptTestHelper.invoke1_BooleanAsync, + nameof(JavaScriptTestHelper.EchoBoolean), + "boolean"); + } + + private async Task JsExportTestAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>(T value + , Func> invoke, string echoName, string jsType, string? jsClass = null) + { + T res; + res = await invoke(value, echoName); + Assert.Equal(value, res); + } + } + + //TODO [ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + public class JSExportTest : JSInteropTestBase, IAsyncLifetime + { + [Theory] + [MemberData(nameof(MarshalBooleanCases))] + public void JsExportBoolean(bool value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Boolean, + nameof(JavaScriptTestHelper.EchoBoolean), + "boolean"); + } + + [Theory] + [MemberData(nameof(MarshalCharCases))] + public void JsExportChar(char value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Char, + nameof(JavaScriptTestHelper.EchoChar), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalByteCases))] + public void JsExportByte(byte value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Byte, + nameof(JavaScriptTestHelper.EchoByte), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalInt16Cases))] + public void JsExportInt16(short value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Int16, + nameof(JavaScriptTestHelper.EchoInt16), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalInt32Cases))] + public void JsExportInt32(int value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Int32, + nameof(JavaScriptTestHelper.EchoInt32), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalInt52Cases))] + public void JsExportInt52(long value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Int52, + nameof(JavaScriptTestHelper.EchoInt52), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalBigInt64Cases))] + public void JsExportBigInt64(long value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_BigInt64, + nameof(JavaScriptTestHelper.EchoBigInt64), + "bigint"); + } + + [Theory] + [MemberData(nameof(MarshalDoubleCases))] + public void JsExportDouble(double value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Double, + nameof(JavaScriptTestHelper.EchoDouble), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalSingleCases))] + public void JsExportSingle(float value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Single, + nameof(JavaScriptTestHelper.EchoSingle), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalIntPtrCases))] + public void JsExportIntPtr(IntPtr value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_IntPtr, + nameof(JavaScriptTestHelper.EchoIntPtr), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalIntPtrCases))] + public unsafe void JsExportVoidPtr(IntPtr xvalue) + { + void* value = (void*)xvalue; + void* res = JavaScriptTestHelper.invoke1_VoidPtr(value, nameof(JavaScriptTestHelper.EchoVoidPtr)); + Assert.True(value == res); + } + + [Theory] + [MemberData(nameof(MarshalDateTimeCases))] + public void JsExportDateTime(DateTime value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_DateTime, + nameof(JavaScriptTestHelper.EchoDateTime), + "object", "Date"); + } + + + [Theory] + [MemberData(nameof(MarshalDateTimeOffsetCases))] + public void JsExportDateTimeOffset(DateTimeOffset value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_DateTimeOffset, + nameof(JavaScriptTestHelper.EchoDateTimeOffset), + "object", "Date"); + } + + [Theory] + [MemberData(nameof(MarshalNullableBooleanCases))] + public void JsExportNullableBoolean(bool? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableBoolean, + nameof(JavaScriptTestHelper.EchoNullableBoolean), + "boolean"); + } + + [Theory] + [MemberData(nameof(MarshalNullableInt32Cases))] + public void JsExportNullableInt32(int? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableInt32, + nameof(JavaScriptTestHelper.EchoNullableInt32), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalNullableBigInt64Cases))] + public void JsExportNullableBigInt64(long? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableBigInt64, + nameof(JavaScriptTestHelper.EchoNullableBigInt64), + "bigint"); + } + + [Theory] + [MemberData(nameof(MarshalNullableIntPtrCases))] + public void JsExportNullableIntPtr(IntPtr? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableIntPtr, + nameof(JavaScriptTestHelper.EchoNullableIntPtr), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalNullableDoubleCases))] + public void JsExportNullableDouble(double? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableDouble, + nameof(JavaScriptTestHelper.EchoNullableDouble), + "number"); + } + + [Theory] + [MemberData(nameof(MarshalNullableDateTimeCases))] + public void JsExportNullableDateTime(DateTime? value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_NullableDateTime, + nameof(JavaScriptTestHelper.EchoNullableDateTime), + "object"); + } + + [Theory] + [MemberData(nameof(MarshalStringCases))] + public void JsExportString(string value) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_String, + nameof(JavaScriptTestHelper.EchoString), + "string"); + } + + [Fact] + public void JsExportStringNoNs() + { + var actual = JavaScriptTestHelper.invoke2_String("test", nameof(JavaScriptTestHelperNoNamespace.EchoString)); + Assert.Equal("test51", actual); + } + + [Fact] + public void JsExportStructClassRecords() + { + var actual = JavaScriptTestHelper.invokeStructClassRecords("test"); + Assert.Equal(48, actual.Length); + Assert.Equal("test11", actual[0]); + Assert.Equal("test12", actual[1]); + Assert.Equal("test13", actual[2]); + Assert.Equal("test14", actual[3]); + Assert.Equal("test15", actual[4]); + Assert.Equal("test16", actual[5]); + Assert.Equal("test17", actual[6]); + Assert.Equal("test18", actual[7]); + Assert.Equal("test19", actual[8]); + Assert.Equal("test21", actual[9]); + Assert.Equal("test22", actual[10]); + Assert.Equal("test23", actual[11]); + Assert.Equal("test24", actual[12]); + Assert.Equal("test25", actual[13]); + Assert.Equal("test31", actual[14]); + Assert.Equal("test32", actual[15]); + Assert.Equal("test33", actual[16]); + Assert.Equal("test34", actual[17]); + Assert.Equal("test35", actual[18]); + Assert.Equal("test41", actual[19]); + Assert.Equal("test42", actual[20]); + Assert.Equal("test43", actual[21]); + Assert.Equal("test44", actual[22]); + Assert.Equal("test45", actual[23]); + Assert.Equal("test51", actual[24]); + Assert.Equal("test52", actual[25]); + Assert.Equal("test53", actual[26]); + Assert.Equal("test54", actual[27]); + Assert.Equal("test55", actual[28]); + Assert.Equal("test56", actual[29]); + Assert.Equal("test57", actual[30]); + Assert.Equal("test58", actual[31]); + Assert.Equal("test59", actual[32]); + Assert.Equal("test61", actual[33]); + Assert.Equal("test62", actual[34]); + Assert.Equal("test63", actual[35]); + Assert.Equal("test64", actual[36]); + Assert.Equal("test65", actual[37]); + Assert.Equal("test71", actual[38]); + Assert.Equal("test72", actual[39]); + Assert.Equal("test73", actual[40]); + Assert.Equal("test74", actual[41]); + Assert.Equal("test75", actual[42]); + Assert.Equal("test81", actual[43]); + Assert.Equal("test82", actual[44]); + Assert.Equal("test83", actual[45]); + Assert.Equal("test84", actual[46]); + Assert.Equal("test85", actual[47]); + } + + [Theory] + [MemberData(nameof(MarshalObjectCases))] + public void JsExportObject(object value, string clazz) + { + JsExportTest(value, + JavaScriptTestHelper.invoke1_Object, + nameof(JavaScriptTestHelper.EchoObject), + "object", clazz); + } + + [Theory] + [MemberData(nameof(MarshalExceptionCases))] + public void JsExportException(Exception value, string clazz) + { + if (clazz == "JSTestError") + { + value = JavaScriptTestHelper.createException("!CreateEx!"); + } + + JsExportTest(value, + JavaScriptTestHelper.invoke1_Exception, + nameof(JavaScriptTestHelper.EchoException), + "object", clazz); + } + + [Fact] + public void JsExportCatchToString() + { + var toString = JavaScriptTestHelper.catch1toString("-t-e-s-t-", nameof(JavaScriptTestHelper.ThrowFromJSExport)); + Assert.DoesNotContain("Unexpected error", toString); + Assert.Contains("-t-e-s-t-", toString); + Assert.DoesNotContain(nameof(JavaScriptTestHelper.ThrowFromJSExport), toString); + } + + [Fact] + public void JsExportCatchStack() + { + var stack = JavaScriptTestHelper.catch1stack("-t-e-s-t-", nameof(JavaScriptTestHelper.ThrowFromJSExport)); + Assert.Contains(nameof(JavaScriptTestHelper.ThrowFromJSExport), stack); + if (PlatformDetection.IsBrowserDomSupportedOrNodeJS) + { + Assert.Contains("catch1stack", stack); + } + } + + [Theory] + [MemberData(nameof(MarshalIJSObjectCases))] + public void JsExportIJSObject(JSObject value, string clazz) + { + if (clazz == "JSData") + { + value = JavaScriptTestHelper.createData("!CreateJS!"); + } + + JsExportTest(value, + JavaScriptTestHelper.invoke1_JSObject, + nameof(JavaScriptTestHelper.EchoIJSObject), + "object", clazz); + } + + [Theory] + [MemberData(nameof(MarshalInt32Cases))] + public async Task JsExportTaskOfInt(int value) + { + TaskCompletionSource tcs = new TaskCompletionSource(); + + var res = JavaScriptTestHelper.invoke1_TaskOfInt(tcs.Task, nameof(JavaScriptTestHelper.AwaitTaskOfObject)); + tcs.SetResult(value); + await Task.Yield(); + var rr = await res; + await Task.Yield(); + Assert.Equal(value, rr); + //GC.Collect(); + } + + [Fact] + public void JsExportCallback_FunctionIntInt() + { + int called = -1; + var chain = JavaScriptTestHelper.invoke1_FuncOfIntInt((int a) => + { + called = a; + return a; + }, nameof(JavaScriptTestHelper.BackFuncOfIntInt)); + + Assert.Equal(-1, called); + var actual = chain(42); + Assert.Equal(42, actual); + Assert.Equal(42, called); + } + + [Fact] + public void JsExportCallback_FunctionIntIntThrow() + { + int called = -1; + var expected = new Exception("test!!"); + var chain = JavaScriptTestHelper.invoke1_FuncOfIntInt((int a) => + { + called = a; + throw expected; + }, nameof(JavaScriptTestHelper.BackFuncOfIntInt)); + + Assert.Equal(-1, called); + var actual = Assert.Throws(() => chain(42)); + Assert.Equal(42, called); + Assert.Same(expected, actual); + } + + private void JsExportTest<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>(T value + , Func invoke, string echoName, string jsType, string? jsClass = null) + { + T res; + res = invoke(value, echoName); + Assert.Equal(value, res); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs similarity index 64% rename from src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs rename to src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs index bd76d9e80f8fd2..78128de5572349 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs @@ -12,7 +12,7 @@ namespace System.Runtime.InteropServices.JavaScript.Tests { - public class JSImportExportTest : IAsyncLifetime + public class JSImportTest : JSInteropTestBase, IAsyncLifetime { [Fact] public unsafe void StructSize() @@ -113,7 +113,7 @@ public unsafe void OutOfRange() Assert.Contains("Overflow: value 9007199254740991 is out of -2147483648 2147483647 range", ex.Message); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public unsafe void OptimizedPaths() { JavaScriptTestHelper.optimizedReached = 0; @@ -211,13 +211,6 @@ public unsafe void CreateFunctionInternal() #region Arrays - public static IEnumerable MarshalByteArrayCases() - { - yield return new object[] { new byte[] { 1, 2, 3, byte.MaxValue, byte.MinValue } }; - yield return new object[] { new byte[] { } }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(MarshalByteArrayCases))] public unsafe void JsImportByteArray(byte[]? expected) @@ -231,13 +224,6 @@ public unsafe void JsImportByteArray(byte[]? expected) } } - public static IEnumerable MarshalIntArrayCases() - { - yield return new object[] { new int[] { 1, 2, 3, int.MaxValue, int.MinValue } }; - yield return new object[] { new int[] { } }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(MarshalIntArrayCases))] public unsafe void JsImportIntArray(int[]? expected) @@ -251,13 +237,6 @@ public unsafe void JsImportIntArray(int[]? expected) } } - public static IEnumerable MarshalDoubleArrayCases() - { - yield return new object[] { new double[] { 1, 2, 3, double.MaxValue, double.MinValue, double.Pi, double.NegativeInfinity, double.PositiveInfinity, double.NaN } }; - yield return new object[] { new double[] { } }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(MarshalDoubleArrayCases))] public unsafe void JsImportDoubleArray(double[]? expected) @@ -271,14 +250,6 @@ public unsafe void JsImportDoubleArray(double[]? expected) } } - public static IEnumerable MarshalStringArrayCases() - { - yield return new object[] { new string[] { "\u0050\u0159\u00ed\u006c\u0069\u0161", "\u017e\u006c\u0075\u0165\u006f\u0075\u010d\u006b\u00fd" } }; - yield return new object[] { new string[] { string.Intern("hello"), string.Empty, null } }; - yield return new object[] { new string[] { } }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(MarshalStringArrayCases))] public unsafe void JsImportStringArray(string[]? expected) @@ -293,28 +264,6 @@ public unsafe void JsImportStringArray(string[]? expected) } } - public class SomethingRef - { - } - - public class SomethingStruct - { - } - - public static IEnumerable MarshalObjectArrayCases() - { - yield return new object[] { new object[] { string.Intern("hello"), string.Empty } }; - yield return new object[] { new object[] { 1.1d, new DateTime(2022, 5, 8, 14, 55, 01, DateTimeKind.Utc), false, true } }; - yield return new object[] { new object[] { new double?(1.1d), new DateTime?(new DateTime(2022, 5, 8, 14, 55, 01, DateTimeKind.Utc)), new bool?(false), new bool?(true) } }; - yield return new object[] { new object[] { null, new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } }; - yield return new object[] { new object[] { "JSData" } }; // special cased, so we call createData in the test itself - yield return new object[] { new object[] { new byte[] { }, new int[] { }, new double[] { }, new string[] { }, new object[] { } } }; - yield return new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object[] { } } }; - yield return new object[] { new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } } }; - yield return new object[] { new object[] { } }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(MarshalObjectArrayCases))] public unsafe void JsImportObjectArray(object[]? expected) @@ -327,19 +276,10 @@ public unsafe void JsImportObjectArray(object[]? expected) Assert.Equal(expected, actual); if (expected != null) for (int i = 0; i < expected.Length; i++) - { - var actualI = JavaScriptTestHelper.store_ObjectArray(expected, i); - Assert.Equal(expected[i], actualI); - } - } - - public static IEnumerable MarshalObjectArrayCasesToDouble() - { - yield return new object[] { new object[] { (byte)42 } }; - yield return new object[] { new object[] { (short)42 } }; - yield return new object[] { new object[] { 42 } }; - yield return new object[] { new object[] { 3.14f } }; - yield return new object[] { new object[] { 'A' } }; + { + var actualI = JavaScriptTestHelper.store_ObjectArray(expected, i); + Assert.Equal(expected[i], actualI); + } } [Theory] @@ -360,25 +300,6 @@ public unsafe void JsImportObjectArrayToDouble(object[]? expected) } } - public static IEnumerable MarshalObjectArrayCasesThrow() - { - yield return new object[] { new object[] { () => { } } }; - yield return new object[] { new object[] { (int a) => { } } }; - yield return new object[] { new object[] { (int a) => { return a; } } }; - yield return new object[] { new object[] { (dummyDelegate)dummyDelegateA } }; - yield return new object[] { new object[] { 0L } }; - yield return new object[] { new object[] { 0UL } }; - yield return new object[] { new object[] { (sbyte)0 } }; - yield return new object[] { new object[] { (ushort)0 } }; - yield return new object[] { new object[] { new SomethingStruct[] { } } }; - yield return new object[] { new object[] { new SomethingRef[] { }, } }; - yield return new object[] { new object[] { new ArraySegment(new byte[] { 11 }), } }; - } - delegate void dummyDelegate(); - static void dummyDelegateA() - { - } - [Theory] [MemberData(nameof(MarshalObjectArrayCasesThrow))] public void JsImportObjectArrayThrows(object[]? expected) @@ -532,11 +453,6 @@ public unsafe void JsImportArraySegmentOfDouble() #endregion #region Boolean - public static IEnumerable MarshalBooleanCases() - { - yield return new object[] { true }; - yield return new object[] { false }; - } [Theory] [MemberData(nameof(MarshalBooleanCases))] @@ -551,28 +467,9 @@ public void JsImportBoolean(bool value) "boolean"); } - [Theory] - [MemberData(nameof(MarshalBooleanCases))] - public void JsExportBoolean(bool value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Boolean, - nameof(JavaScriptTestHelper.EchoBoolean), - "boolean"); - } #endregion Boolean #region Char - public static IEnumerable MarshalCharCases() - { - yield return new object[] { (char)42 }; - yield return new object[] { (char)1 }; - yield return new object[] { '\u017D' }; - yield return new object[] { '\u2661' }; - yield return new object[] { char.MaxValue }; - yield return new object[] { char.MinValue }; - } - [Theory] [MemberData(nameof(MarshalCharCases))] public void JsImportChar(char value) @@ -586,26 +483,9 @@ public void JsImportChar(char value) "number"); } - [Theory] - [MemberData(nameof(MarshalCharCases))] - public void JsExportChar(char value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Char, - nameof(JavaScriptTestHelper.EchoChar), - "number"); - } #endregion Char #region Byte - public static IEnumerable MarshalByteCases() - { - yield return new object[] { (byte)42 }; - yield return new object[] { (byte)1 }; - yield return new object[] { byte.MaxValue }; - yield return new object[] { byte.MinValue }; - } - [Theory] [MemberData(nameof(MarshalByteCases))] public void JsImportByte(byte value) @@ -619,16 +499,6 @@ public void JsImportByte(byte value) "number"); } - [Theory] - [MemberData(nameof(MarshalByteCases))] - public void JsExportByte(byte value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Byte, - nameof(JavaScriptTestHelper.EchoByte), - "number"); - } - [Theory] [MemberData(nameof(OutOfRangeCases))] public void ByteOutOfRange(double value, string message) @@ -641,16 +511,6 @@ public void ByteOutOfRange(double value, string message) #endregion Byte #region Int16 - public static IEnumerable MarshalInt16Cases() - { - yield return new object[] { 42 }; - yield return new object[] { 0 }; - yield return new object[] { 1 }; - yield return new object[] { -1 }; - yield return new object[] { short.MaxValue }; - yield return new object[] { short.MinValue }; - } - [Theory] [MemberData(nameof(MarshalInt16Cases))] public void JsImportInt16(short value) @@ -664,38 +524,9 @@ public void JsImportInt16(short value) "number"); } - [Theory] - [MemberData(nameof(MarshalInt16Cases))] - public void JsExportInt16(short value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Int16, - nameof(JavaScriptTestHelper.EchoInt16), - "number"); - } #endregion Int16 #region Int32 - public static IEnumerable MarshalInt32Cases() - { - yield return new object[] { 42 }; - yield return new object[] { 0 }; - yield return new object[] { 1 }; - yield return new object[] { -1 }; - yield return new object[] { int.MaxValue }; - yield return new object[] { int.MinValue }; - } - - public static IEnumerable OutOfRangeCases() - { - yield return new object[] { double.MaxValue, "Value is not an integer" }; - yield return new object[] { double.MinValue, "Value is not an integer" }; - yield return new object[] { double.NaN, "Value is not an integer" }; - yield return new object[] { double.NegativeInfinity, "Value is not an integer" }; - yield return new object[] { double.PositiveInfinity, "Value is not an integer" }; - yield return new object[] { (double)MAX_SAFE_INTEGER, "Overflow" }; - } - [Theory] [MemberData(nameof(MarshalInt32Cases))] public void JsImportInt32(int value) @@ -709,16 +540,6 @@ public void JsImportInt32(int value) "number"); } - [Theory] - [MemberData(nameof(MarshalInt32Cases))] - public void JsExportInt32(int value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Int32, - nameof(JavaScriptTestHelper.EchoInt32), - "number"); - } - [Theory] [MemberData(nameof(OutOfRangeCases))] public void Int32OutOfRange(double value, string message) @@ -731,17 +552,6 @@ public void Int32OutOfRange(double value, string message) #endregion Int32 #region Int52 - const long MAX_SAFE_INTEGER = 9007199254740991L;// Number.MAX_SAFE_INTEGER - const long MIN_SAFE_INTEGER = -9007199254740991L;// Number.MIN_SAFE_INTEGER - public static IEnumerable MarshalInt52Cases() - { - yield return new object[] { -1 }; - yield return new object[] { 42 }; - yield return new object[] { 0 }; - yield return new object[] { 1 }; - yield return new object[] { MAX_SAFE_INTEGER }; - yield return new object[] { MIN_SAFE_INTEGER }; - } [Theory] [MemberData(nameof(MarshalInt52Cases))] @@ -756,30 +566,9 @@ public void JsImportInt52(long value) "number"); } - [Theory] - [MemberData(nameof(MarshalInt52Cases))] - public void JsExportInt52(long value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Int52, - nameof(JavaScriptTestHelper.EchoInt52), - "number"); - } #endregion Int52 #region BigInt64 - public static IEnumerable MarshalBigInt64Cases() - { - yield return new object[] { -1 }; - yield return new object[] { 42 }; - yield return new object[] { 0 }; - yield return new object[] { 1 }; - yield return new object[] { MAX_SAFE_INTEGER }; - yield return new object[] { MIN_SAFE_INTEGER }; - yield return new object[] { long.MinValue }; - yield return new object[] { long.MaxValue }; - } - [Theory] [MemberData(nameof(MarshalBigInt64Cases))] public void JsImportBigInt64(long value) @@ -793,29 +582,9 @@ public void JsImportBigInt64(long value) "bigint"); } - [Theory] - [MemberData(nameof(MarshalBigInt64Cases))] - public void JsExportBigInt64(long value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_BigInt64, - nameof(JavaScriptTestHelper.EchoBigInt64), - "bigint"); - } #endregion BigInt64 #region Double - public static IEnumerable MarshalDoubleCases() - { - yield return new object[] { Math.PI }; - yield return new object[] { 0.0 }; - yield return new object[] { double.MaxValue }; - yield return new object[] { double.MinValue }; - yield return new object[] { double.NegativeInfinity }; - yield return new object[] { double.PositiveInfinity }; - yield return new object[] { double.NaN }; - } - [Theory] [MemberData(nameof(MarshalDoubleCases))] public void JsImportDouble(double value) @@ -829,29 +598,9 @@ public void JsImportDouble(double value) "number"); } - [Theory] - [MemberData(nameof(MarshalDoubleCases))] - public void JsExportDouble(double value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Double, - nameof(JavaScriptTestHelper.EchoDouble), - "number"); - } #endregion Double #region Single - public static IEnumerable MarshalSingleCases() - { - yield return new object[] { (float)Math.PI }; - yield return new object[] { 0.0f }; - yield return new object[] { float.MaxValue }; - yield return new object[] { float.MinValue }; - yield return new object[] { float.NegativeInfinity }; - yield return new object[] { float.PositiveInfinity }; - yield return new object[] { float.NaN }; - } - [Theory] [MemberData(nameof(MarshalSingleCases))] public void JsImportSingle(float value) @@ -865,28 +614,9 @@ public void JsImportSingle(float value) "number"); } - [Theory] - [MemberData(nameof(MarshalSingleCases))] - public void JsExportSingle(float value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Single, - nameof(JavaScriptTestHelper.EchoSingle), - "number"); - } #endregion Single #region IntPtr - public static IEnumerable MarshalIntPtrCases() - { - yield return new object[] { (IntPtr)42 }; - yield return new object[] { IntPtr.Zero }; - yield return new object[] { (IntPtr)1 }; - yield return new object[] { (IntPtr)(-1) }; - yield return new object[] { IntPtr.MaxValue }; - yield return new object[] { IntPtr.MinValue }; - } - [Theory] [MemberData(nameof(MarshalIntPtrCases))] public void JsImportIntPtr(IntPtr value) @@ -900,15 +630,6 @@ public void JsImportIntPtr(IntPtr value) "number"); } - [Theory] - [MemberData(nameof(MarshalIntPtrCases))] - public void JsExportIntPtr(IntPtr value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_IntPtr, - nameof(JavaScriptTestHelper.EchoIntPtr), - "number"); - } #endregion IntPtr #region VoidPtr @@ -929,24 +650,9 @@ public unsafe void JsImportVoidPtr(IntPtr xvalue) Assert.Equal("number", actualJsType); } - [Theory] - [MemberData(nameof(MarshalIntPtrCases))] - public unsafe void JsExportVoidPtr(IntPtr xvalue) - { - void* value = (void*)xvalue; - void* res = JavaScriptTestHelper.invoke1_VoidPtr(value, nameof(JavaScriptTestHelper.EchoVoidPtr)); - Assert.True(value == res); - } #endregion VoidPtr #region Datetime - public static IEnumerable MarshalDateTimeCases() - { - yield return new object[] { new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; - yield return new object[] { TrimNano(DateTime.UtcNow) }; - yield return new object[] { TrimNano(DateTime.MaxValue) }; - } - [Theory] [MemberData(nameof(MarshalDateTimeCases))] public void JSImportDateTime(DateTime value) @@ -960,25 +666,9 @@ public void JSImportDateTime(DateTime value) "object", "Date"); } - [Theory] - [MemberData(nameof(MarshalDateTimeCases))] - public void JsExportDateTime(DateTime value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_DateTime, - nameof(JavaScriptTestHelper.EchoDateTime), - "object", "Date"); - } #endregion Datetime #region DateTimeOffset - public static IEnumerable MarshalDateTimeOffsetCases() - { - yield return new object[] { DateTimeOffset.FromUnixTimeSeconds(0) }; - yield return new object[] { TrimNano(DateTimeOffset.UtcNow) }; - yield return new object[] { TrimNano(DateTimeOffset.MaxValue) }; - } - [Theory] [MemberData(nameof(MarshalDateTimeOffsetCases))] public void JSImportDateTimeOffset(DateTimeOffset value) @@ -992,25 +682,9 @@ public void JSImportDateTimeOffset(DateTimeOffset value) "object", "Date"); } - [Theory] - [MemberData(nameof(MarshalDateTimeOffsetCases))] - public void JsExportDateTimeOffset(DateTimeOffset value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_DateTimeOffset, - nameof(JavaScriptTestHelper.EchoDateTimeOffset), - "object", "Date"); - } #endregion DateTimeOffset #region NullableBoolean - public static IEnumerable MarshalNullableBooleanCases() - { - yield return new object[] { null }; - yield return new object[] { true }; - yield return new object[] { false }; - } - [Theory] [MemberData(nameof(MarshalNullableBooleanCases))] public void JsImportNullableBoolean(bool? value) @@ -1024,29 +698,9 @@ public void JsImportNullableBoolean(bool? value) "boolean"); } - [Theory] - [MemberData(nameof(MarshalNullableBooleanCases))] - public void JsExportNullableBoolean(bool? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableBoolean, - nameof(JavaScriptTestHelper.EchoNullableBoolean), - "boolean"); - } #endregion NullableBoolean #region NullableInt32 - public static IEnumerable MarshalNullableInt32Cases() - { - yield return new object[] { null }; - yield return new object[] { 42 }; - yield return new object[] { 0 }; - yield return new object[] { 1 }; - yield return new object[] { -1 }; - yield return new object[] { int.MaxValue }; - yield return new object[] { int.MinValue }; - } - [Theory] [MemberData(nameof(MarshalNullableInt32Cases))] public void JsImportNullableInt32(int? value) @@ -1060,31 +714,9 @@ public void JsImportNullableInt32(int? value) "number"); } - [Theory] - [MemberData(nameof(MarshalNullableInt32Cases))] - public void JsExportNullableInt32(int? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableInt32, - nameof(JavaScriptTestHelper.EchoNullableInt32), - "number"); - } #endregion NullableInt32 #region NullableBigInt64 - public static IEnumerable MarshalNullableBigInt64Cases() - { - yield return new object[] { null }; - yield return new object[] { 42L }; - yield return new object[] { 0L }; - yield return new object[] { 1L }; - yield return new object[] { -1L }; - yield return new object[] { MAX_SAFE_INTEGER }; - yield return new object[] { MIN_SAFE_INTEGER }; - yield return new object[] { long.MaxValue }; - yield return new object[] { long.MinValue }; - } - [Theory] [MemberData(nameof(MarshalNullableBigInt64Cases))] public void JsImportNullableBigInt64(long? value) @@ -1098,29 +730,9 @@ public void JsImportNullableBigInt64(long? value) "bigint"); } - [Theory] - [MemberData(nameof(MarshalNullableBigInt64Cases))] - public void JsExportNullableBigInt64(long? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableBigInt64, - nameof(JavaScriptTestHelper.EchoNullableBigInt64), - "bigint"); - } #endregion NullableBigInt64 #region NullableIntPtr - public static IEnumerable MarshalNullableIntPtrCases() - { - yield return new object[] { null }; - yield return new object[] { (IntPtr)42 }; - yield return new object[] { IntPtr.Zero }; - yield return new object[] { (IntPtr)1 }; - yield return new object[] { (IntPtr)(-1) }; - yield return new object[] { IntPtr.MaxValue }; - yield return new object[] { IntPtr.MinValue }; - } - [Theory] [MemberData(nameof(MarshalNullableIntPtrCases))] public void JsImportNullableIntPtr(IntPtr? value) @@ -1134,30 +746,9 @@ public void JsImportNullableIntPtr(IntPtr? value) "number"); } - [Theory] - [MemberData(nameof(MarshalNullableIntPtrCases))] - public void JsExportNullableIntPtr(IntPtr? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableIntPtr, - nameof(JavaScriptTestHelper.EchoNullableIntPtr), - "number"); - } #endregion NullableIntPtr #region NullableDouble - public static IEnumerable MarshalNullableDoubleCases() - { - yield return new object[] { null }; - yield return new object[] { Math.PI }; - yield return new object[] { 0.0 }; - yield return new object[] { double.MaxValue }; - yield return new object[] { double.MinValue }; - yield return new object[] { double.NegativeInfinity }; - yield return new object[] { double.PositiveInfinity }; - yield return new object[] { double.NaN }; - } - [Theory] [MemberData(nameof(MarshalNullableDoubleCases))] public void JsImportNullableDouble(double? value) @@ -1171,26 +762,9 @@ public void JsImportNullableDouble(double? value) "number"); } - [Theory] - [MemberData(nameof(MarshalNullableDoubleCases))] - public void JsExportNullableDouble(double? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableDouble, - nameof(JavaScriptTestHelper.EchoNullableDouble), - "number"); - } #endregion NullableDouble #region NullableDateTime - public static IEnumerable MarshalNullableDateTimeCases() - { - yield return new object[] { null }; - yield return new object[] { new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; - yield return new object[] { TrimNano(DateTime.UtcNow) }; - yield return new object[] { TrimNano(DateTime.MaxValue) }; - } - [Theory] [MemberData(nameof(MarshalNullableDateTimeCases))] public void JsImportNullableDateTime(DateTime? value) @@ -1204,27 +778,9 @@ public void JsImportNullableDateTime(DateTime? value) "object"); } - [Theory] - [MemberData(nameof(MarshalNullableDateTimeCases))] - public void JsExportNullableDateTime(DateTime? value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_NullableDateTime, - nameof(JavaScriptTestHelper.EchoNullableDateTime), - "object"); - } #endregion NullableDateTime #region String - public static IEnumerable MarshalStringCases() - { - yield return new object[] { null }; - yield return new object[] { string.Empty }; - yield return new object[] { "Ahoj" + Random.Shared.Next() };// shorted than 256 -> check in JS interned - yield return new object[] { "Ahoj" + new string('!', 300) };// longer than 256 -> no check in JS interned - yield return new object[] { string.Intern("dotnet") }; - } - [Theory] [MemberData(nameof(MarshalStringCases))] public void JsImportString(string value) @@ -1238,78 +794,6 @@ public void JsImportString(string value) , "string"); } - [Theory] - [MemberData(nameof(MarshalStringCases))] - public void JsExportString(string value) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_String, - nameof(JavaScriptTestHelper.EchoString), - "string"); - } - - [Fact] - public void JsExportStringNoNs() - { - var actual = JavaScriptTestHelper.invoke2_String("test", nameof(JavaScriptTestHelperNoNamespace.EchoString)); - Assert.Equal("test51", actual); - } - - [Fact] - public void JsExportStructClassRecords() - { - var actual = JavaScriptTestHelper.invokeStructClassRecords("test"); - Assert.Equal(48, actual.Length); - Assert.Equal("test11", actual[0]); - Assert.Equal("test12", actual[1]); - Assert.Equal("test13", actual[2]); - Assert.Equal("test14", actual[3]); - Assert.Equal("test15", actual[4]); - Assert.Equal("test16", actual[5]); - Assert.Equal("test17", actual[6]); - Assert.Equal("test18", actual[7]); - Assert.Equal("test19", actual[8]); - Assert.Equal("test21", actual[9]); - Assert.Equal("test22", actual[10]); - Assert.Equal("test23", actual[11]); - Assert.Equal("test24", actual[12]); - Assert.Equal("test25", actual[13]); - Assert.Equal("test31", actual[14]); - Assert.Equal("test32", actual[15]); - Assert.Equal("test33", actual[16]); - Assert.Equal("test34", actual[17]); - Assert.Equal("test35", actual[18]); - Assert.Equal("test41", actual[19]); - Assert.Equal("test42", actual[20]); - Assert.Equal("test43", actual[21]); - Assert.Equal("test44", actual[22]); - Assert.Equal("test45", actual[23]); - Assert.Equal("test51", actual[24]); - Assert.Equal("test52", actual[25]); - Assert.Equal("test53", actual[26]); - Assert.Equal("test54", actual[27]); - Assert.Equal("test55", actual[28]); - Assert.Equal("test56", actual[29]); - Assert.Equal("test57", actual[30]); - Assert.Equal("test58", actual[31]); - Assert.Equal("test59", actual[32]); - Assert.Equal("test61", actual[33]); - Assert.Equal("test62", actual[34]); - Assert.Equal("test63", actual[35]); - Assert.Equal("test64", actual[36]); - Assert.Equal("test65", actual[37]); - Assert.Equal("test71", actual[38]); - Assert.Equal("test72", actual[39]); - Assert.Equal("test73", actual[40]); - Assert.Equal("test74", actual[41]); - Assert.Equal("test75", actual[42]); - Assert.Equal("test81", actual[43]); - Assert.Equal("test82", actual[44]); - Assert.Equal("test83", actual[45]); - Assert.Equal("test84", actual[46]); - Assert.Equal("test85", actual[47]); - } - [Fact] public void JsImportNative() { @@ -1337,12 +821,6 @@ public void JsImportReboundInstanceMember() #endregion String #region Object - public static IEnumerable MarshalObjectCases() - { - yield return new object[] { new object(), "ManagedObject" }; - yield return new object[] { null, null }; - } - [Theory] [MemberData(nameof(MarshalObjectCases))] public void JSImportObject(object value, string clazz) @@ -1356,25 +834,9 @@ public void JSImportObject(object value, string clazz) "object", clazz); } - [Theory] - [MemberData(nameof(MarshalObjectCases))] - public void JsExportObject(object value, string clazz) - { - JsExportTest(value, - JavaScriptTestHelper.invoke1_Object, - nameof(JavaScriptTestHelper.EchoObject), - "object", clazz); - } #endregion Object #region Exception - public static IEnumerable MarshalExceptionCases() - { - yield return new object[] { new Exception("Test"), "ManagedError" }; - yield return new object[] { null, "JSTestError" }; - yield return new object[] { null, null }; - } - [Theory] [MemberData(nameof(MarshalExceptionCases))] public void JSImportException(Exception value, string clazz) @@ -1393,29 +855,6 @@ public void JSImportException(Exception value, string clazz) "object", clazz); } - [Theory] - [MemberData(nameof(MarshalExceptionCases))] - public void JsExportException(Exception value, string clazz) - { - if (clazz == "JSTestError") - { - value = JavaScriptTestHelper.createException("!CreateEx!"); - } - - JsExportTest(value, - JavaScriptTestHelper.invoke1_Exception, - nameof(JavaScriptTestHelper.EchoException), - "object", clazz); - } - - [Fact] - public void JsExportThrows() - { - var ex = Assert.Throws(() => JavaScriptTestHelper.invoke1_String("-t-e-s-t-", nameof(JavaScriptTestHelper.ThrowFromJSExport))); - Assert.DoesNotContain("Unexpected error", ex.Message); - Assert.Contains("-t-e-s-t-", ex.Message); - } - [Fact] public void JSImportReturnError() { @@ -1424,35 +863,9 @@ public void JSImportReturnError() Assert.Contains("this-is-error", err.Message); } - [Fact] - public void JsExportCatchToString() - { - var toString = JavaScriptTestHelper.catch1toString("-t-e-s-t-", nameof(JavaScriptTestHelper.ThrowFromJSExport)); - Assert.DoesNotContain("Unexpected error", toString); - Assert.Contains("-t-e-s-t-", toString); - Assert.DoesNotContain(nameof(JavaScriptTestHelper.ThrowFromJSExport), toString); - } - - [Fact] - public void JsExportCatchStack() - { - var stack = JavaScriptTestHelper.catch1stack("-t-e-s-t-", nameof(JavaScriptTestHelper.ThrowFromJSExport)); - Assert.Contains(nameof(JavaScriptTestHelper.ThrowFromJSExport), stack); - if (PlatformDetection.IsBrowserDomSupportedOrNodeJS) - { - Assert.Contains("catch1stack", stack); - } - } - #endregion Exception #region JSObject - public static IEnumerable MarshalIJSObjectCases() - { - yield return new object[] { null, "JSData" }; - yield return new object[] { null, null }; - } - [Theory] [MemberData(nameof(MarshalIJSObjectCases))] public void JSImportIJSObject(JSObject value, string clazz) @@ -1471,20 +884,6 @@ public void JSImportIJSObject(JSObject value, string clazz) "object", clazz); } - [Theory] - [MemberData(nameof(MarshalIJSObjectCases))] - public void JsExportIJSObject(JSObject value, string clazz) - { - if (clazz == "JSData") - { - value = JavaScriptTestHelper.createData("!CreateJS!"); - } - - JsExportTest(value, - JavaScriptTestHelper.invoke1_JSObject, - nameof(JavaScriptTestHelper.EchoIJSObject), - "object", clazz); - } #endregion JSObject #region ProxyOfProxy @@ -1517,7 +916,7 @@ public async Task JsImportSleep() [Fact] public async Task JsImportTaskTypes() { - for(int i=0;i<100;i++) + for (int i = 0; i < 100; i++) { object a = new object(); Exception e = new Exception(); @@ -1641,14 +1040,6 @@ public async Task JsImportTaskEchoPendingException() await Assert.ThrowsAsync(async () => await task); } - public static IEnumerable TaskCases() - { - yield return new object[] { Math.PI }; - yield return new object[] { 0 }; - yield return new object[] { "test" }; - yield return new object[] { null }; - } - [Theory] [MemberData(nameof(TaskCases))] public async Task JsImportTaskAwaitPendingResult(object result) @@ -1713,26 +1104,11 @@ public async Task JsImportTaskAwait() await task; } - [Theory] - [MemberData(nameof(MarshalInt32Cases))] - public async Task JsExportTaskOfInt(int value) - { - TaskCompletionSource tcs = new TaskCompletionSource(); - - var res = JavaScriptTestHelper.invoke1_TaskOfInt(tcs.Task, nameof(JavaScriptTestHelper.AwaitTaskOfObject)); - tcs.SetResult(value); - await Task.Yield(); - var rr = await res; - await Task.Yield(); - Assert.Equal(value, rr); - //GC.Collect(); - } - #endregion #region Action - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_EchoAction() { bool called = false; @@ -1747,8 +1123,39 @@ public void JsImportCallback_EchoAction() Assert.True(called); } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] + public void JsImportCallback_EchoActionThrows_MT() + { + bool called = false; + Action expected = () => + { + called = true; + }; + var actual = JavaScriptTestHelper.echo1_ActionAction(expected); + Assert.NotEqual(expected, actual); + Assert.False(called); + // with deputy thread, call back to C# from synchronous JS function is not allowed + Assert.Throws(()=>actual()); + Assert.False(called); + } [Fact] + public async Task JsImportCallback_Async() + { + bool called = false; + var promise = JavaScriptTestHelper.backback_FuncIntIntFuncIntIntAsync((a,b) => + { + called = true; + return a + b; + }, 123, 321); + Assert.False(called); + var actual = await promise; + Assert.True(called); + Assert.Equal(444, actual); + } + + + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy [OuterLoop] public async Task JsImportCallback_EchoActionMany() { @@ -1769,7 +1176,7 @@ public async Task JsImportCallback_EchoActionMany() } } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_Action() { bool called = false; @@ -1780,7 +1187,7 @@ public void JsImportCallback_Action() Assert.True(called); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportEcho_ActionAction() { bool called = false; @@ -1793,7 +1200,7 @@ public void JsImportEcho_ActionAction() Assert.True(called); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportEcho_ActionIntActionInt() { int calledA = -1; @@ -1806,7 +1213,7 @@ public void JsImportEcho_ActionIntActionInt() Assert.Equal(42, calledA); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionInt() { int called = -1; @@ -1817,7 +1224,7 @@ public void JsImportCallback_ActionInt() Assert.Equal(42, called); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_FunctionIntInt() { int called = -1; @@ -1830,7 +1237,7 @@ public void JsImportCallback_FunctionIntInt() Assert.Equal(42, res); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportBackCallback_FunctionIntInt() { int called = -1; @@ -1845,7 +1252,7 @@ public void JsImportBackCallback_FunctionIntInt() Assert.Equal(84, called); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportBackCallback_FunctionIntIntIntInt() { int calledA = -1; @@ -1864,7 +1271,7 @@ public void JsImportBackCallback_FunctionIntIntIntInt() Assert.Equal(84, calledB); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntInt() { int calledA = -1; @@ -1878,7 +1285,7 @@ public void JsImportCallback_ActionIntInt() Assert.Equal(43, calledB); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionLongLong() { long calledA = -1; @@ -1892,7 +1299,7 @@ public void JsImportCallback_ActionLongLong() Assert.Equal(43, calledB); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntLong() { int calledA = -1; @@ -1906,7 +1313,7 @@ public void JsImportCallback_ActionIntLong() Assert.Equal(43, calledB); } - [Fact] + //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntThrow() { int called = -1; @@ -1920,39 +1327,6 @@ public void JsImportCallback_ActionIntThrow() Assert.Same(expected, actual); } - [Fact] - public void JsExportCallback_FunctionIntInt() - { - int called = -1; - var chain = JavaScriptTestHelper.invoke1_FuncOfIntInt((int a) => - { - called = a; - return a; - }, nameof(JavaScriptTestHelper.BackFuncOfIntInt)); - - Assert.Equal(-1, called); - var actual = chain(42); - Assert.Equal(42, actual); - Assert.Equal(42, called); - } - - [Fact] - public void JsExportCallback_FunctionIntIntThrow() - { - int called = -1; - var expected = new Exception("test!!"); - var chain = JavaScriptTestHelper.invoke1_FuncOfIntInt((int a) => - { - called = a; - throw expected; - }, nameof(JavaScriptTestHelper.BackFuncOfIntInt)); - - Assert.Equal(-1, called); - var actual = Assert.Throws(() => chain(42)); - Assert.Equal(42, called); - Assert.Same(expected, actual); - } - [Fact] public void JsImportMath() { @@ -1962,14 +1336,6 @@ public void JsImportMath() #endregion - private void JsExportTest<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>(T value - , Func invoke, string echoName, string jsType, string? jsClass = null) - { - T res; - res = invoke(value, echoName); - Assert.Equal(value, res); - } - private void JsImportTest<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>(T value , Action store1 , Func retrieve1 @@ -2112,26 +1478,5 @@ public void JsImportMath() Assert.Equal((Exception)(object)value, resEx); } } - - public async Task InitializeAsync() - { - await JavaScriptTestHelper.InitializeAsync(); - } - - public async Task DisposeAsync() - { - await JavaScriptTestHelper.DisposeAsync(); - } - - // js Date doesn't have nanosecond precision - public static DateTime TrimNano(DateTime date) - { - return new DateTime(date.Ticks - (date.Ticks % TimeSpan.TicksPerMillisecond), DateTimeKind.Utc); - } - - public static DateTimeOffset TrimNano(DateTimeOffset date) - { - return new DateTime(date.Ticks - (date.Ticks % TimeSpan.TicksPerMillisecond), DateTimeKind.Utc); - } } } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSInteropTestBase.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSInteropTestBase.cs new file mode 100644 index 00000000000000..288ad2b227d75e --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSInteropTestBase.cs @@ -0,0 +1,340 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using System.Threading; +using Xunit; +using System.Diagnostics.CodeAnalysis; +#pragma warning disable xUnit1026 // Theory methods should use all of their parameters + +namespace System.Runtime.InteropServices.JavaScript.Tests +{ + public class JSInteropTestBase + { + public static IEnumerable MarshalCharCases() + { + yield return new object[] { (char)42 }; + yield return new object[] { (char)1 }; + yield return new object[] { '\u017D' }; + yield return new object[] { '\u2661' }; + yield return new object[] { char.MaxValue }; + yield return new object[] { char.MinValue }; + } + + public static IEnumerable MarshalByteCases() + { + yield return new object[] { (byte)42 }; + yield return new object[] { (byte)1 }; + yield return new object[] { byte.MaxValue }; + yield return new object[] { byte.MinValue }; + } + + public static IEnumerable MarshalInt16Cases() + { + yield return new object[] { 42 }; + yield return new object[] { 0 }; + yield return new object[] { 1 }; + yield return new object[] { -1 }; + yield return new object[] { short.MaxValue }; + yield return new object[] { short.MinValue }; + } + + public static IEnumerable MarshalInt32Cases() + { + yield return new object[] { 42 }; + yield return new object[] { 0 }; + yield return new object[] { 1 }; + yield return new object[] { -1 }; + yield return new object[] { int.MaxValue }; + yield return new object[] { int.MinValue }; + } + + public static IEnumerable OutOfRangeCases() + { + yield return new object[] { double.MaxValue, "Value is not an integer" }; + yield return new object[] { double.MinValue, "Value is not an integer" }; + yield return new object[] { double.NaN, "Value is not an integer" }; + yield return new object[] { double.NegativeInfinity, "Value is not an integer" }; + yield return new object[] { double.PositiveInfinity, "Value is not an integer" }; + yield return new object[] { (double)MAX_SAFE_INTEGER, "Overflow" }; + } + + const long MAX_SAFE_INTEGER = 9007199254740991L;// Number.MAX_SAFE_INTEGER + const long MIN_SAFE_INTEGER = -9007199254740991L;// Number.MIN_SAFE_INTEGER + public static IEnumerable MarshalInt52Cases() + { + yield return new object[] { -1 }; + yield return new object[] { 42 }; + yield return new object[] { 0 }; + yield return new object[] { 1 }; + yield return new object[] { MAX_SAFE_INTEGER }; + yield return new object[] { MIN_SAFE_INTEGER }; + } + + public static IEnumerable MarshalBigInt64Cases() + { + yield return new object[] { -1 }; + yield return new object[] { 42 }; + yield return new object[] { 0 }; + yield return new object[] { 1 }; + yield return new object[] { MAX_SAFE_INTEGER }; + yield return new object[] { MIN_SAFE_INTEGER }; + yield return new object[] { long.MinValue }; + yield return new object[] { long.MaxValue }; + } + + public static IEnumerable MarshalDoubleCases() + { + yield return new object[] { Math.PI }; + yield return new object[] { 0.0 }; + yield return new object[] { double.MaxValue }; + yield return new object[] { double.MinValue }; + yield return new object[] { double.NegativeInfinity }; + yield return new object[] { double.PositiveInfinity }; + yield return new object[] { double.NaN }; + } + + public static IEnumerable MarshalSingleCases() + { + yield return new object[] { (float)Math.PI }; + yield return new object[] { 0.0f }; + yield return new object[] { float.MaxValue }; + yield return new object[] { float.MinValue }; + yield return new object[] { float.NegativeInfinity }; + yield return new object[] { float.PositiveInfinity }; + yield return new object[] { float.NaN }; + } + + public static IEnumerable MarshalIntPtrCases() + { + yield return new object[] { (IntPtr)42 }; + yield return new object[] { IntPtr.Zero }; + yield return new object[] { (IntPtr)1 }; + yield return new object[] { (IntPtr)(-1) }; + yield return new object[] { IntPtr.MaxValue }; + yield return new object[] { IntPtr.MinValue }; + } + + public static IEnumerable MarshalDateTimeCases() + { + yield return new object[] { new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; + yield return new object[] { TrimNano(DateTime.UtcNow) }; + yield return new object[] { TrimNano(DateTime.MaxValue) }; + } + + public static IEnumerable MarshalDateTimeOffsetCases() + { + yield return new object[] { DateTimeOffset.FromUnixTimeSeconds(0) }; + yield return new object[] { TrimNano(DateTimeOffset.UtcNow) }; + yield return new object[] { TrimNano(DateTimeOffset.MaxValue) }; + } + + public static IEnumerable MarshalByteArrayCases() + { + yield return new object[] { new byte[] { 1, 2, 3, byte.MaxValue, byte.MinValue } }; + yield return new object[] { new byte[] { } }; + yield return new object[] { null }; + } + + public static IEnumerable MarshalIntArrayCases() + { + yield return new object[] { new int[] { 1, 2, 3, int.MaxValue, int.MinValue } }; + yield return new object[] { new int[] { } }; + yield return new object[] { null }; + } + + public static IEnumerable MarshalDoubleArrayCases() + { + yield return new object[] { new double[] { 1, 2, 3, double.MaxValue, double.MinValue, double.Pi, double.NegativeInfinity, double.PositiveInfinity, double.NaN } }; + yield return new object[] { new double[] { } }; + yield return new object[] { null }; + } + + public static IEnumerable MarshalStringArrayCases() + { + yield return new object[] { new string[] { "\u0050\u0159\u00ed\u006c\u0069\u0161", "\u017e\u006c\u0075\u0165\u006f\u0075\u010d\u006b\u00fd" } }; + yield return new object[] { new string[] { string.Intern("hello"), string.Empty, null } }; + yield return new object[] { new string[] { } }; + yield return new object[] { null }; + } + + public static IEnumerable MarshalBooleanCases() + { + yield return new object[] { true }; + yield return new object[] { false }; + } + + public static IEnumerable MarshalObjectArrayCasesToDouble() + { + yield return new object[] { new object[] { (byte)42 } }; + yield return new object[] { new object[] { (short)42 } }; + yield return new object[] { new object[] { 42 } }; + yield return new object[] { new object[] { 3.14f } }; + yield return new object[] { new object[] { 'A' } }; + } + + protected delegate void dummyDelegate(); + protected static void dummyDelegateA() + { + } + + public class SomethingRef + { + } + + public class SomethingStruct + { + } + + public static IEnumerable MarshalObjectArrayCasesThrow() + { + yield return new object[] { new object[] { () => { } } }; + yield return new object[] { new object[] { (int a) => { } } }; + yield return new object[] { new object[] { (int a) => { return a; } } }; + yield return new object[] { new object[] { (dummyDelegate)dummyDelegateA } }; + yield return new object[] { new object[] { 0L } }; + yield return new object[] { new object[] { 0UL } }; + yield return new object[] { new object[] { (sbyte)0 } }; + yield return new object[] { new object[] { (ushort)0 } }; + yield return new object[] { new object[] { new SomethingStruct[] { } } }; + yield return new object[] { new object[] { new SomethingRef[] { }, } }; + yield return new object[] { new object[] { new ArraySegment(new byte[] { 11 }), } }; + } + + public static IEnumerable MarshalObjectArrayCases() + { + yield return new object[] { new object[] { string.Intern("hello"), string.Empty } }; + yield return new object[] { new object[] { 1.1d, new DateTime(2022, 5, 8, 14, 55, 01, DateTimeKind.Utc), false, true } }; + yield return new object[] { new object[] { new double?(1.1d), new DateTime?(new DateTime(2022, 5, 8, 14, 55, 01, DateTimeKind.Utc)), new bool?(false), new bool?(true) } }; + yield return new object[] { new object[] { null, new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } }; + yield return new object[] { new object[] { "JSData" } }; // special cased, so we call createData in the test itself + yield return new object[] { new object[] { new byte[] { }, new int[] { }, new double[] { }, new string[] { }, new object[] { } } }; + yield return new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object[] { } } }; + yield return new object[] { new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } } }; + yield return new object[] { new object[] { } }; + yield return new object[] { null }; + } + + public static IEnumerable MarshalNullableBooleanCases() + { + yield return new object[] { null }; + yield return new object[] { true }; + yield return new object[] { false }; + } + + public static IEnumerable MarshalNullableInt32Cases() + { + yield return new object[] { null }; + yield return new object[] { 42 }; + yield return new object[] { 0 }; + yield return new object[] { 1 }; + yield return new object[] { -1 }; + yield return new object[] { int.MaxValue }; + yield return new object[] { int.MinValue }; + } + + public static IEnumerable MarshalNullableBigInt64Cases() + { + yield return new object[] { null }; + yield return new object[] { 42L }; + yield return new object[] { 0L }; + yield return new object[] { 1L }; + yield return new object[] { -1L }; + yield return new object[] { MAX_SAFE_INTEGER }; + yield return new object[] { MIN_SAFE_INTEGER }; + yield return new object[] { long.MaxValue }; + yield return new object[] { long.MinValue }; + } + + public static IEnumerable MarshalNullableIntPtrCases() + { + yield return new object[] { null }; + yield return new object[] { (IntPtr)42 }; + yield return new object[] { IntPtr.Zero }; + yield return new object[] { (IntPtr)1 }; + yield return new object[] { (IntPtr)(-1) }; + yield return new object[] { IntPtr.MaxValue }; + yield return new object[] { IntPtr.MinValue }; + } + + public static IEnumerable MarshalNullableDoubleCases() + { + yield return new object[] { null }; + yield return new object[] { Math.PI }; + yield return new object[] { 0.0 }; + yield return new object[] { double.MaxValue }; + yield return new object[] { double.MinValue }; + yield return new object[] { double.NegativeInfinity }; + yield return new object[] { double.PositiveInfinity }; + yield return new object[] { double.NaN }; + } + + public static IEnumerable MarshalNullableDateTimeCases() + { + yield return new object[] { null }; + yield return new object[] { new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; + yield return new object[] { TrimNano(DateTime.UtcNow) }; + yield return new object[] { TrimNano(DateTime.MaxValue) }; + } + + public static IEnumerable MarshalStringCases() + { + yield return new object[] { null }; + yield return new object[] { string.Empty }; + yield return new object[] { "Ahoj" + Random.Shared.Next() };// shorted than 256 -> check in JS interned + yield return new object[] { "Ahoj" + new string('!', 300) };// longer than 256 -> no check in JS interned + yield return new object[] { string.Intern("dotnet") }; + } + + public static IEnumerable MarshalObjectCases() + { + yield return new object[] { new object(), "ManagedObject" }; + yield return new object[] { null, null }; + } + + public static IEnumerable MarshalExceptionCases() + { + yield return new object[] { new Exception("Test"), "ManagedError" }; + yield return new object[] { null, "JSTestError" }; + yield return new object[] { null, null }; + } + + public static IEnumerable MarshalIJSObjectCases() + { + yield return new object[] { null, "JSData" }; + yield return new object[] { null, null }; + } + + public static IEnumerable TaskCases() + { + yield return new object[] { Math.PI }; + yield return new object[] { 0 }; + yield return new object[] { "test" }; + yield return new object[] { null }; + } + + public async Task InitializeAsync() + { + await JavaScriptTestHelper.InitializeAsync(); + } + + public async Task DisposeAsync() + { + await JavaScriptTestHelper.DisposeAsync(); + } + + // js Date doesn't have nanosecond precision + public static DateTime TrimNano(DateTime date) + { + return new DateTime(date.Ticks - (date.Ticks % TimeSpan.TicksPerMillisecond), DateTimeKind.Utc); + } + + public static DateTimeOffset TrimNano(DateTimeOffset date) + { + return new DateTime(date.Ticks - (date.Ticks % TimeSpan.TicksPerMillisecond), DateTimeKind.Utc); + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs index bfd645039b56cb..3b3dff2974e782 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs @@ -449,6 +449,9 @@ public static async Task AwaitTaskOfObject([JSMarshalAs>] internal static partial Func backback_FuncIntIntFuncIntInt([JSMarshalAs>] Func fun, [JSMarshalAs] int a, [JSMarshalAs] int b); + [JSImport("backbackAsync", "JavaScriptTestHelper")] + internal static partial Task backback_FuncIntIntFuncIntIntAsync([JSMarshalAs>] Func fun, [JSMarshalAs] int a, [JSMarshalAs] int b); + [JSImport("back3", "JavaScriptTestHelper")] internal static partial void back3_ActionInt([JSMarshalAs>] Action? action, [JSMarshalAs] int a); @@ -500,6 +503,10 @@ public static Func BackFuncOfIntInt([JSMarshalAs] internal static partial bool invoke1_Boolean([JSMarshalAs] bool value, [JSMarshalAs] string name); + + [JSImport("invoke1Async", "JavaScriptTestHelper")] + internal static partial Task invoke1_BooleanAsync(bool value, string name); + [JSExport] [return: JSMarshalAs] public static bool EchoBoolean([JSMarshalAs] bool arg1) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs index baec1cb231c2b8..6b917e3f3fee0b 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs @@ -214,6 +214,24 @@ export function invoke1(arg1, name) { return res; } +export async function invoke1Async(arg1, name) { + if (globalThis.gc) { + // console.log('globalThis.gc'); + globalThis.gc(); + } + // console.log(`invoke1: ${name}(arg1:${arg1 !== null ? typeof arg1 : ''})`) + const JavaScriptTestHelper = dllExports.System.Runtime.InteropServices.JavaScript.Tests.JavaScriptTestHelper; + const fn = JavaScriptTestHelper[name]; + + await delay(10); + + // console.log("invoke1:" + typeof fn); + // console.log("invoke1:" + fn.toString()); + const res = fn(arg1); + // console.log(`invoke1: res ${res !== null ? typeof res : ''}`) + return res; +} + export function invoke2(arg1, name) { const fn = dllExports.JavaScriptTestHelperNoNamespace[name]; //console.log("invoke1:" + fn.toString()); @@ -379,6 +397,15 @@ export function backback(arg1, arg2, arg3) { } } +export async function backbackAsync(arg1, arg2, arg3) { + if (globalThis.gc) { + // console.log('globalThis.gc'); + globalThis.gc(); + } + await delay(10); + return arg1(arg2, arg3); +} + export const instance = {} globalThis.javaScriptTestHelper = instance; @@ -396,3 +423,7 @@ export async function setup() { } // console.log('JavaScriptTestHelper:' Object.keys(globalThis.JavaScriptTestHelper)); + +export function delay(ms) { + return new Promise(resolve => globalThis.setTimeout(resolve, ms)); +} \ No newline at end of file From e4e38c03ea9abecf55824c404ee340c865890660 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 16 Feb 2024 14:26:23 +0100 Subject: [PATCH 2/2] fix --- .../JavaScript/JSImportTest.cs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs index 78128de5572349..b3c57387e46379 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs @@ -113,7 +113,7 @@ public unsafe void OutOfRange() Assert.Contains("Overflow: value 9007199254740991 is out of -2147483648 2147483647 range", ex.Message); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public unsafe void OptimizedPaths() { JavaScriptTestHelper.optimizedReached = 0; @@ -1108,7 +1108,7 @@ public async Task JsImportTaskAwait() #region Action - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_EchoAction() { bool called = false; @@ -1123,6 +1123,7 @@ public void JsImportCallback_EchoAction() Assert.True(called); } + /* TODO deputy [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void JsImportCallback_EchoActionThrows_MT() { @@ -1138,6 +1139,7 @@ public void JsImportCallback_EchoActionThrows_MT() Assert.Throws(()=>actual()); Assert.False(called); } + */ [Fact] public async Task JsImportCallback_Async() @@ -1155,7 +1157,7 @@ public async Task JsImportCallback_Async() } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy [OuterLoop] public async Task JsImportCallback_EchoActionMany() { @@ -1176,7 +1178,7 @@ public async Task JsImportCallback_EchoActionMany() } } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_Action() { bool called = false; @@ -1187,7 +1189,7 @@ public void JsImportCallback_Action() Assert.True(called); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportEcho_ActionAction() { bool called = false; @@ -1200,7 +1202,7 @@ public void JsImportEcho_ActionAction() Assert.True(called); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportEcho_ActionIntActionInt() { int calledA = -1; @@ -1213,7 +1215,7 @@ public void JsImportEcho_ActionIntActionInt() Assert.Equal(42, calledA); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionInt() { int called = -1; @@ -1224,7 +1226,7 @@ public void JsImportCallback_ActionInt() Assert.Equal(42, called); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_FunctionIntInt() { int called = -1; @@ -1237,7 +1239,7 @@ public void JsImportCallback_FunctionIntInt() Assert.Equal(42, res); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportBackCallback_FunctionIntInt() { int called = -1; @@ -1252,7 +1254,7 @@ public void JsImportBackCallback_FunctionIntInt() Assert.Equal(84, called); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportBackCallback_FunctionIntIntIntInt() { int calledA = -1; @@ -1271,7 +1273,7 @@ public void JsImportBackCallback_FunctionIntIntIntInt() Assert.Equal(84, calledB); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntInt() { int calledA = -1; @@ -1285,7 +1287,7 @@ public void JsImportCallback_ActionIntInt() Assert.Equal(43, calledB); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionLongLong() { long calledA = -1; @@ -1299,7 +1301,7 @@ public void JsImportCallback_ActionLongLong() Assert.Equal(43, calledB); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntLong() { int calledA = -1; @@ -1313,7 +1315,7 @@ public void JsImportCallback_ActionIntLong() Assert.Equal(43, calledB); } - //TODO [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy + [Fact] //TODO [Fact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] // this test doesn't make sense with deputy public void JsImportCallback_ActionIntThrow() { int called = -1;