• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include "ets_interop_js_gtest.h"
18 
19 namespace ark::ets::interop::js::testing {
20 
21 class EtsInteropJsIntrinsTest : public EtsInteropTest {
22 public:
SetUp()23     void SetUp() override
24     {
25         EtsInteropTest::SetUp();
26         LoadModuleAs("test_intrins", "test_intrins/test_intrins.js");
27     }
28 };
29 
TEST_F(EtsInteropJsIntrinsTest,basic1)30 TEST_F(EtsInteropJsIntrinsTest, basic1)
31 {
32     constexpr double ARG0 = 271;
33     constexpr double ARG1 = 314;
34     constexpr double RES = ARG0 + ARG1;
35     // NOLINTNEXTLINE(modernize-use-auto)
36     auto ret = CallEtsMethod<double>("jsSumWrapperNumNum", ARG0, ARG1);
37     ASSERT_EQ(ret, RES);
38 }
39 
TEST_F(EtsInteropJsIntrinsTest,basic2)40 TEST_F(EtsInteropJsIntrinsTest, basic2)
41 {
42     constexpr double ARG0 = 12.34;
43     constexpr std::string_view ARG1 = "foo";
44     constexpr std::string_view RES = "12.34foo";
45     // NOLINTNEXTLINE(modernize-use-auto)
46     auto ret = CallEtsMethod<std::string>("jsSumWrapperNumStr", ARG0, ARG1);
47     ASSERT_EQ(ret, RES);
48 }
49 
TEST_F(EtsInteropJsIntrinsTest,test_convertors)50 TEST_F(EtsInteropJsIntrinsTest, test_convertors)
51 {
52     ASSERT_EQ(true, CallEtsMethod<bool>("testUndefined"));
53     ASSERT_EQ(true, CallEtsMethod<bool>("testNull"));
54     ASSERT_EQ(true, CallEtsMethod<bool>("testBoolean"));
55     ASSERT_EQ(true, CallEtsMethod<bool>("testNumber"));
56     ASSERT_EQ(true, CallEtsMethod<bool>("testString"));
57     ASSERT_EQ(true, CallEtsMethod<bool>("testObject"));
58     // NOTE(vpukhov): symbol, function, external, bigint
59 
60     ASSERT_EQ(true, CallEtsMethod<bool>("testStringOps"));
61 }
62 
TEST_F(EtsInteropJsIntrinsTest,test_builtin_array_convertors)63 TEST_F(EtsInteropJsIntrinsTest, test_builtin_array_convertors)
64 {
65     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayAny"));
66     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayBoolean"));
67     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayInt"));
68     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayNumber"));
69     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayString"));
70     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayObject"));
71     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayMultidim"));
72     ASSERT_EQ(true, CallEtsMethod<bool>("testBuiltinArrayInstanceof"));
73     ASSERT_EQ(true, CallEtsMethod<bool>("testInitArrayComponent"));
74 }
75 
TEST_F(EtsInteropJsIntrinsTest,test_named_access)76 TEST_F(EtsInteropJsIntrinsTest, test_named_access)
77 {
78     ASSERT_EQ(true, CallEtsMethod<bool>("testNamedAccess"));
79 }
80 
TEST_F(EtsInteropJsIntrinsTest,test_newcall)81 TEST_F(EtsInteropJsIntrinsTest, test_newcall)
82 {
83     ASSERT_EQ(true, CallEtsMethod<bool>("testNewcall"));
84 }
85 
TEST_F(EtsInteropJsIntrinsTest,test_value_call)86 TEST_F(EtsInteropJsIntrinsTest, test_value_call)
87 {
88     ASSERT_EQ(true, CallEtsMethod<bool>("testValueCall"));
89 }
90 
TEST_F(EtsInteropJsIntrinsTest,test_call_stress)91 TEST_F(EtsInteropJsIntrinsTest, test_call_stress)
92 {
93     ASSERT_EQ(true, CallEtsMethod<bool>("testCallStress"));
94 }
95 
96 // Remove after JSValue cast fix
TEST_F(EtsInteropJsIntrinsTest,DISABLED_test_undefined_cast_bug)97 TEST_F(EtsInteropJsIntrinsTest, DISABLED_test_undefined_cast_bug)
98 {
99     ASSERT_EQ(true, CallEtsMethod<bool>("testUndefinedCastBug"));
100 }
101 
TEST_F(EtsInteropJsIntrinsTest,test_lambda_proxy)102 TEST_F(EtsInteropJsIntrinsTest, test_lambda_proxy)
103 {
104     ASSERT_EQ(true, CallEtsMethod<bool>("testLambdaProxy"));
105     ASSERT_EQ(true, CallEtsMethod<bool>("testLambdaProxyRecursive"));
106 }
107 
TEST_F(EtsInteropJsIntrinsTest,test_exception_forwarding)108 TEST_F(EtsInteropJsIntrinsTest, test_exception_forwarding)
109 {
110     ASSERT_EQ(true, CallEtsMethod<bool>("testExceptionForwardingFromjs"));
111     ASSERT_EQ(true, CallEtsMethod<bool>("testExceptionForwardingFromets"));
112     ASSERT_EQ(true, CallEtsMethod<bool>("testExceptionForwardingRecursive"));
113     ASSERT_EQ(true, CallEtsMethod<bool>("testCoreErrorForwarding"));
114 }
115 
TEST_F(EtsInteropJsIntrinsTest,test_typechecks)116 TEST_F(EtsInteropJsIntrinsTest, test_typechecks)
117 {
118     ASSERT_EQ(true, CallEtsMethod<bool>("testTypecheckGetProp"));
119     ASSERT_EQ(true, CallEtsMethod<bool>("testTypecheckJscall"));
120     ASSERT_EQ(true, CallEtsMethod<bool>("testTypecheckCallets"));
121 }
122 
TEST_F(EtsInteropJsIntrinsTest,test_accessor_throws)123 TEST_F(EtsInteropJsIntrinsTest, test_accessor_throws)
124 {
125     ASSERT_EQ(true, CallEtsMethod<bool>("testGetThrows"));
126     ASSERT_EQ(true, CallEtsMethod<bool>("testSetThrows"));
127     ASSERT_EQ(true, CallEtsMethod<bool>("testJscallResolutionThrows1"));
128     ASSERT_EQ(true, CallEtsMethod<bool>("testJscallResolutionThrows2"));
129     ASSERT_EQ(true, CallEtsMethod<bool>("testJscallResolutionThrows3"));
130 }
131 
TEST_F(EtsInteropJsIntrinsTest,test_finalizers)132 TEST_F(EtsInteropJsIntrinsTest, test_finalizers)
133 {
134     ASSERT_EQ(true, CallEtsMethod<bool>("testFinalizers"));
135 }
136 
137 }  // namespace ark::ets::interop::js::testing
138