1 /** 2 * Copyright (c) 2021-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 #ifndef PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H 17 #define PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H 18 19 #include <gtest/gtest.h> 20 #include <gmock/gmock.h> 21 22 #include "plugins/ets/runtime/napi/ets_scoped_objects_fix.h" 23 24 namespace ark::ets::test { 25 26 class EtsNapiTestBaseClass : public testing::Test { 27 public: 28 template <typename R, typename... Args> CallEtsFuntion(R * ret,std::string_view methodName,Args &&...args)29 void CallEtsFuntion(R *ret, std::string_view methodName, Args &&...args) 30 { 31 ets_class cls = env_->FindClass("ETSGLOBAL"); 32 ASSERT_NE(cls, nullptr); 33 ets_method fn = env_->GetStaticp_method(cls, methodName.data(), nullptr); 34 35 // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg) 36 if constexpr (std::is_same_v<R, ets_boolean>) { 37 *ret = env_->CallStaticBooleanMethod(cls, fn, args...); 38 } else if constexpr (std::is_same_v<R, ets_byte>) { 39 *ret = env_->CallStaticByteMethod(cls, fn, args...); 40 } else if constexpr (std::is_same_v<R, ets_char>) { 41 *ret = env_->CallStaticCharMethod(cls, fn, args...); 42 } else if constexpr (std::is_same_v<R, ets_short>) { 43 *ret = env_->CallStaticShortMethod(cls, fn, args...); 44 } else if constexpr (std::is_same_v<R, ets_int>) { 45 *ret = env_->CallStaticIntMethod(cls, fn, args...); 46 } else if constexpr (std::is_same_v<R, ets_long>) { 47 *ret = env_->CallStaticLongMethod(cls, fn, args...); 48 } else if constexpr (std::is_same_v<R, ets_float>) { 49 *ret = env_->CallStaticFloatMethod(cls, fn, args...); 50 } else if constexpr (std::is_same_v<R, ets_double>) { 51 *ret = env_->CallStaticDoubleMethod(cls, fn, args...); 52 } else if constexpr (std::is_same_v<R, ets_object> || std::is_same_v<R, ets_array>) { 53 *ret = static_cast<R>(env_->CallStaticObjectMethod(cls, fn, args...)); 54 } else if constexpr (std::is_same_v<R, void>) { 55 // do nothing 56 } else { 57 enum { INCORRECT_TEMPLATE_TYPE = false }; 58 static_assert(INCORRECT_TEMPLATE_TYPE, "Incorrect template type"); 59 } 60 // NOLINTEND(cppcoreguidelines-pro-type-vararg) 61 } 62 63 protected: SetUp()64 void SetUp() override 65 { 66 const char *stdlib = std::getenv("ARK_ETS_STDLIB_PATH"); 67 ASSERT_NE(stdlib, nullptr); 68 69 // clang-format off 70 std::vector<EtsVMOption> optionsVector{ 71 {EtsOptionType::ETS_GC_TYPE, "epsilon"}, 72 {EtsOptionType::ETS_NO_JIT, nullptr}, 73 {EtsOptionType::ETS_BOOT_FILE, stdlib} 74 }; 75 // clang-format on 76 77 abcPath_ = std::getenv("ARK_ETS_GTEST_ABC_PATH"); 78 if (abcPath_ != nullptr) { 79 optionsVector.push_back({EtsOptionType::ETS_BOOT_FILE, abcPath_}); 80 } 81 82 EtsVMInitArgs vmArgs; 83 vmArgs.version = ETS_NAPI_VERSION_1_0; 84 vmArgs.options = optionsVector.data(); 85 vmArgs.nOptions = static_cast<ets_int>(optionsVector.size()); 86 87 ASSERT_TRUE(ETS_CreateVM(&vm_, &env_, &vmArgs) == ETS_OK) << "Cannot create ETS VM"; 88 } 89 TearDown()90 void TearDown() override 91 { 92 ASSERT_TRUE(vm_->DestroyEtsVM() == ETS_OK) << "Cannot destroy ETS VM"; 93 } 94 95 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 96 const char *abcPath_ {nullptr}; 97 EtsEnv *env_ {nullptr}; 98 EtsVM *vm_ {nullptr}; 99 // NOLINTEND(misc-non-private-member-variables-in-classes) 100 }; 101 102 } // namespace ark::ets::test 103 104 #endif // PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H 105