1/*
2 * Copyright (c) 2022 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 "ecmascript/global_env.h"
17#include "ecmascript/interpreter/slow_runtime_stub.h"
18#include "ecmascript/js_tagged_value-inl.h"
19#include "ecmascript/require/js_cjs_module.h"
20#include "ecmascript/require/js_cjs_module_cache.h"
21#include "ecmascript/require/js_require_manager.h"
22#include "ecmascript/tests/test_helper.h"
23
24using namespace panda::ecmascript;
25namespace panda::test {
26class CjsManagerTest : public testing::Test {
27public:
28    static void SetUpTestCase()
29    {
30        GTEST_LOG_(INFO) << "SetUpTestCase";
31    }
32
33    static void TearDownTestCase()
34    {
35        GTEST_LOG_(INFO) << "TearDownCase";
36    }
37
38    void SetUp() override
39    {
40        TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41    }
42
43    void TearDown() override
44    {
45        TestHelper::DestroyEcmaVMWithScope(instance, scope);
46    }
47
48    EcmaVM *instance {nullptr};
49    EcmaHandleScope *scope {nullptr};
50    JSThread *thread {nullptr};
51};
52
53/**
54 * @tc.name: InitializeCommonJS
55 * @tc.desc: Call "InitializeCommonJS" function, connect relationship between objects.
56 * @tc.type: FUNC
57 * @tc.require:
58 */
59HWTEST_F_L0(CjsManagerTest, InitializeCommonJS)
60{
61    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
62    auto *globalConst = thread->GlobalConstants();
63
64    JSHandle<CjsModule> module = factory->NewCjsModule();
65    JSHandle<JSTaggedValue> require = thread->GetEcmaVM()->GetGlobalEnv()->GetCjsRequireFunction();
66    JSHandle<CjsExports> exports = factory->NewCjsExports();
67    JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
68    JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
69    CJSInfo cjsInfo(module, require, exports, fileName, dirName);
70    RequireManager::InitializeCommonJS(thread, cjsInfo);
71
72    JSHandle<JSTaggedValue> exportsKey = globalConst->GetHandledCjsExportsString();
73    JSTaggedValue exportsVal =
74        SlowRuntimeStub::LdObjByName(thread, module.GetTaggedValue(),
75                                     exportsKey.GetTaggedValue(), false, JSTaggedValue::Undefined());
76
77    EXPECT_EQ(JSTaggedValue::SameValue(exportsVal, JSTaggedValue::Hole()), false);
78}
79
80/**
81 * @tc.name: CollectExecutedExp
82 * @tc.desc: Call "CollectExecutedExp" function,check whether the same-name module is replaced.
83 * @tc.type: FUNC
84 * @tc.require:
85 */
86HWTEST_F_L0(CjsManagerTest, CollectExecutedExp)
87{
88    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
89    auto *globalConst = thread->GlobalConstants();
90    auto vm = thread->GetEcmaVM();
91
92    JSHandle<CjsModule> module1 = factory->NewCjsModule();
93    JSHandle<JSTaggedValue> require = thread->GetEcmaVM()->GetGlobalEnv()->GetCjsRequireFunction();
94    JSHandle<CjsExports> exports = factory->NewCjsExports();
95    JSHandle<JSTaggedValue> fileName(factory->NewFromUtf8("ark/js_runtime/test.js"));
96    JSHandle<JSTaggedValue> dirName(factory->NewFromUtf8("ark/js_runtime"));
97    CJSInfo cjsInfo1(module1, require, exports, fileName, dirName);
98    RequireManager::InitializeCommonJS(thread, cjsInfo1);
99
100    JSHandle<CjsModule> module2 = factory->NewCjsModule();
101    JSHandle<EcmaString> exportsStr(factory->NewFromUtf8("test"));
102    CjsModule::InitializeModule(thread, module2, fileName, dirName);
103    JSHandle<JSTaggedValue> exportsName = globalConst->GetHandledCjsExportsString();
104    SlowRuntimeStub::StObjByName(thread, module2.GetTaggedValue(), exportsName.GetTaggedValue(),
105                                 exportsStr.GetTaggedValue());
106
107    CJSInfo cjsInfo2(module2, require, exports, fileName, dirName);
108    RequireManager::CollectExecutedExp(thread, cjsInfo2);
109    JSHandle<JSTaggedValue> test = CjsModule::SearchFromModuleCache(thread, fileName);
110
111    EXPECT_EQ(EcmaStringAccessor::Compare(vm, JSHandle<EcmaString>(test), exportsStr), 0);
112}
113} // namespace panda::test
114