From 0ed54cf3bd6ac342047da4e6b7a3335cb8d95140 Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Thu, 27 Jul 2023 20:23:25 +0300 Subject: [PATCH] interop_js: Add tests for generic types List of changes: 1. Implement generic types test 2. Add tests for: - js primitives - js objects - ets objects - compat objects Signed-off-by: Vyacheslav Cherkashin --- .../interop_js/js_refconvert_builtin.cpp | 2 +- .../interop_js/gtest_plugin/gtest_launcher.js | 2 - .../ets/tests/interop_js/tests/CMakeLists.txt | 1 + .../tests/generic_types/CMakeLists.txt | 17 ++++++ .../generic_types/check_compat_objects.js | 60 +++++++++++++++++++ .../tests/generic_types/check_ets_objects.js | 23 +++++++ .../tests/generic_types/check_js_objects.js | 23 +++++++ .../generic_types/check_js_primitives.js | 43 +++++++++++++ .../tests/generic_types/generic_types.test.js | 56 +++++++++++++++++ .../generic_types/test_generic_types.cpp | 43 +++++++++++++ .../generic_types/test_generic_types.ets | 48 +++++++++++++++ 11 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/CMakeLists.txt create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/check_compat_objects.js create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/check_ets_objects.js create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/check_js_objects.js create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/check_js_primitives.js create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/generic_types.test.js create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.cpp create mode 100644 plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.ets diff --git a/plugins/ets/runtime/interop_js/js_refconvert_builtin.cpp b/plugins/ets/runtime/interop_js/js_refconvert_builtin.cpp index 68deb456a..fb5114a94 100644 --- a/plugins/ets/runtime/interop_js/js_refconvert_builtin.cpp +++ b/plugins/ets/runtime/interop_js/js_refconvert_builtin.cpp @@ -273,7 +273,7 @@ static void RegisterCompatConvertors(InteropCtx *ctx) bool is_instanceof; NAPI_CHECK_FATAL(napi_is_array(env, js_value, &is_instanceof)); if (is_instanceof) { - not_implemented("Array"); + return m_array(ctxx, js_value); } NAPI_CHECK_FATAL(napi_is_arraybuffer(env, js_value, &is_instanceof)); if (is_instanceof) { diff --git a/plugins/ets/tests/interop_js/gtest_plugin/gtest_launcher.js b/plugins/ets/tests/interop_js/gtest_plugin/gtest_launcher.js index 4fe9fff73..3a09e63b7 100644 --- a/plugins/ets/tests/interop_js/gtest_plugin/gtest_launcher.js +++ b/plugins/ets/tests/interop_js/gtest_plugin/gtest_launcher.js @@ -16,7 +16,6 @@ globalThis.ASSERT_TRUE = function ASSERT_TRUE(v, msg = "") { if (v !== true) { msg = `ASSERTION FAILED: ` + msg; - console.error(msg); throw Error(msg); } } @@ -24,7 +23,6 @@ globalThis.ASSERT_TRUE = function ASSERT_TRUE(v, msg = "") { globalThis.ASSERT_EQ = function ASSERT_EQ(v0, v1) { if (!Object.is(v0, v1)) { let msg = `ASSERTION FAILED: ${v0}[${typeof v0}] !== ${v1}[${typeof v1}]`; - console.error(msg); throw Error(msg); } } diff --git a/plugins/ets/tests/interop_js/tests/CMakeLists.txt b/plugins/ets/tests/interop_js/tests/CMakeLists.txt index d42a0101a..c419879be 100644 --- a/plugins/ets/tests/interop_js/tests/CMakeLists.txt +++ b/plugins/ets/tests/interop_js/tests/CMakeLists.txt @@ -124,6 +124,7 @@ add_subdirectory(number_subtypes) add_subdirectory(promise) add_subdirectory(escompat_demo) add_subdirectory(escompat) +add_subdirectory(generic_types) add_subdirectory(proxies) add_subdirectory(checked) add_subdirectory(perf) diff --git a/plugins/ets/tests/interop_js/tests/generic_types/CMakeLists.txt b/plugins/ets/tests/interop_js/tests/generic_types/CMakeLists.txt new file mode 100644 index 000000000..938981114 --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +panda_ets_interop_js_gtest(ets_interop_js__ets_proxy_test_generic_types + CPP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test_generic_types.cpp + ETS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test_generic_types.ets +) diff --git a/plugins/ets/tests/interop_js/tests/generic_types/check_compat_objects.js b/plugins/ets/tests/interop_js/tests/generic_types/check_compat_objects.js new file mode 100644 index 000000000..815104bca --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/check_compat_objects.js @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { + EtsGenericErrorHandle, + check_generic_value, + ets_get_generic_error_identity, +} = require("generic_types.test.js"); + + +function check_error_message(err, err_msg) { + check_generic_value(err); + + let ets_error_handle = new EtsGenericErrorHandle(err); + let msg = ets_error_handle.get_error_message(); + + ASSERT_EQ(msg, err_msg); + + let gen_error = ets_get_generic_error_identity(err); + ASSERT_EQ(err, gen_error); +} + +function check_incorrect_js_object() { + let err_msg = "incorrect js object" + let obj = {message: err_msg}; + + ASSERT_THROWS(TypeError, () => (check_error_message(obj, err_msg))); + ASSERT_THROWS(TypeError, () => (ets_get_generic_error_identity(obj))); +} + +function check_incorrect_ets_object() { + let err_msg = "incorrect ets object" + let obj = new ets.Object(); + + ASSERT_THROWS(TypeError, () => (check_error_message(obj, err_msg))); + ASSERT_THROWS(TypeError, () => (ets_get_generic_error_identity(obj))); +} + + +// TODO(v.cherkashin): Enable when implemented +// Check js/ets errors +check_error_message(new ets.Error("ets Error message"), "ets Error message"); +// check_error_message(new Error("js Error message"), "js Error message"); +// check_error_message(new ets.TypeError("ets TypeError message"), "ets TypeError message"); +// check_error_message(new TypeError("js TypeError message"), "js TypeError message"); + +check_incorrect_ets_object() +check_incorrect_js_object() diff --git a/plugins/ets/tests/interop_js/tests/generic_types/check_ets_objects.js b/plugins/ets/tests/interop_js/tests/generic_types/check_ets_objects.js new file mode 100644 index 000000000..f73112ca9 --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/check_ets_objects.js @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { + check_generic_value, + EtsGenericValueHandle, +} = require("generic_types.test.js"); + + +check_generic_value(new EtsGenericValueHandle(0)); +check_generic_value(new ets.Object()); diff --git a/plugins/ets/tests/interop_js/tests/generic_types/check_js_objects.js b/plugins/ets/tests/interop_js/tests/generic_types/check_js_objects.js new file mode 100644 index 000000000..282e723f2 --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/check_js_objects.js @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { check_generic_value } = require("generic_types.test.js"); + +// Object +check_generic_value({}); +check_generic_value({"a": 1, "b": 2}); + +// Function +check_generic_value(() => {}); diff --git a/plugins/ets/tests/interop_js/tests/generic_types/check_js_primitives.js b/plugins/ets/tests/interop_js/tests/generic_types/check_js_primitives.js new file mode 100644 index 000000000..19601bb27 --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/check_js_primitives.js @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const { check_generic_value } = require("generic_types.test.js"); + +// Null +check_generic_value(null); + +// Undefined (ArkTS doesn't support the undefined type) +ASSERT_THROWS(Error, () => (check_generic_value(undefined))); + +// Boolean +check_generic_value(false); +check_generic_value(true); +check_generic_value(new Boolean(false)); +check_generic_value(new Boolean(true)); + +// Number +check_generic_value(234); +check_generic_value(4.234); +check_generic_value(new Number(34)); +check_generic_value(new Number(-643.23566)); + +// BigInt +// TODO(v.cherkashin): Enable when implemented +// check_generic_value(9007199254740991n); +// check_generic_value(new BigInt("0b11010101")); + +// String +check_generic_value("abcdefg"); +check_generic_value(new String("ABCDEFG")); diff --git a/plugins/ets/tests/interop_js/tests/generic_types/generic_types.test.js b/plugins/ets/tests/interop_js/tests/generic_types/generic_types.test.js new file mode 100644 index 000000000..1ad548f74 --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/generic_types.test.js @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const etsVm = require("lib/module/ets_interop_js_napi"); + +let EtsGenericValueHandle = etsVm.getClass("Lgeneric_types/test/EtsGenericValueHandle;"); +let EtsGenericErrorHandle = etsVm.getClass("Lgeneric_types/test/EtsGenericErrorHandle;"); +let ets_get_generic_value_identity = etsVm.getFunction("Lgeneric_types/test/ETSGLOBAL;", "ets_get_generic_value_identity"); +let ets_get_generic_error_identity = etsVm.getFunction("Lgeneric_types/test/ETSGLOBAL;", "ets_get_generic_error_identity"); + + +function check_generic_value_for_function(v) { + // Check the parameter and the return value of the function + let gen_value = ets_get_generic_value_identity(v); + ASSERT_EQ(v, gen_value); +} + +function check_generic_value_for_instance(v) { + let gen_handle = new EtsGenericValueHandle(v); + + // Check the parameter and the return value of the class method + let value = gen_handle.get_value(); + ASSERT_EQ(v, value); + + gen_handle.set_value(new ets.Object()); + + // Check access to generic field + gen_handle.value = v; + let field_value = gen_handle.value; + ASSERT_EQ(v, field_value); +} + +function check_generic_value(v) { + check_generic_value_for_function(v); + check_generic_value_for_instance(v); +} + + +module.exports = { + EtsGenericValueHandle, + EtsGenericErrorHandle, + check_generic_value, + ets_get_generic_error_identity, +} diff --git a/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.cpp b/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.cpp new file mode 100644 index 000000000..c939e4b3b --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.cpp @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "ets_interop_js_gtest.h" + +namespace panda::ets::interop::js::testing { + +class GenericTypesTest : public EtsInteropTest {}; + +TEST_F(GenericTypesTest, check_compat_objects) +{ + ASSERT_TRUE(RunJsTestSute("check_compat_objects.js")); +} + +TEST_F(GenericTypesTest, check_js_objects) +{ + ASSERT_TRUE(RunJsTestSute("check_js_objects.js")); +} + +TEST_F(GenericTypesTest, check_ets_objects) +{ + ASSERT_TRUE(RunJsTestSute("check_ets_objects.js")); +} + +TEST_F(GenericTypesTest, check_js_primitives) +{ + ASSERT_TRUE(RunJsTestSute("check_js_primitives.js")); +} + +} // namespace panda::ets::interop::js::testing diff --git a/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.ets b/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.ets new file mode 100644 index 000000000..a50fe515e --- /dev/null +++ b/plugins/ets/tests/interop_js/tests/generic_types/test_generic_types.ets @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package generic_types.test; + + +class EtsGenericValueHandle { + constructor(value: T) { + this.value = value; + } + public set_value(value: T) { + this.value = value; + } + public get_value(): T { + return this.value; + } + public value: T; +} + +class EtsGenericErrorHandle { + constructor(err: T) { + this.err = err; + } + public get_error_message(): String { + return this.err.message; + } + public err: T; +} + +function ets_get_generic_value_identity(v: T): T { + return v; +} + +function ets_get_generic_error_identity(e: T): T { + return e; +} -- Gitee