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#ifndef ECMASCRIPT_TESTS_CONTAINERS_TEST_HELPER_H
17#define ECMASCRIPT_TESTS_CONTAINERS_TEST_HELPER_H
18
19#include "ecmascript/containers/containers_private.h"
20#include "ecmascript/ecma_runtime_call_info.h"
21#include "ecmascript/global_env.h"
22#include "ecmascript/js_api/js_api_list.h"
23#include "ecmascript/js_api/js_api_list_iterator.h"
24#include "ecmascript/js_handle.h"
25#include "ecmascript/js_tagged_value-inl.h"
26#include "ecmascript/js_thread.h"
27#include "ecmascript/object_factory.h"
28#include "ecmascript/tests/test_helper.h"
29#include "ecmascript/ecma_vm.h"
30#include "ecmascript/global_env.h"
31#include "ecmascript/js_function.h"
32#include "ecmascript/js_proxy.h"
33
34namespace panda::ecmascript {
35    static JSFunction *JSObjectTestCreate(JSThread *thread)
36    {
37        EcmaVM *ecmaVM = thread->GetEcmaVM();
38        JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
39        return globalEnv->GetObjectFunction().GetObject<JSFunction>();
40    }
41    [[maybe_unused]] static JSHandle<JSProxy> CreateJSProxyHandle(JSThread *thread)
42    {
43        JSHandle<JSTaggedValue> hclass(thread, JSObjectTestCreate(thread));
44        JSHandle<JSTaggedValue> targetHandle(
45            thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
46        JSHandle<JSTaggedValue> handlerHandle(
47            thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
48        return JSProxy::ProxyCreate(thread, targetHandle, handlerHandle);
49    }
50} // namespace panda::ecmascript
51
52namespace panda::test {
53using panda::ecmascript::EcmaRuntimeCallInfo;
54
55// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
56#define CONTAINERS_API_EXCEPTION_TEST(className, methodName, callInfoName)          \
57{                                                                                   \
58    [[maybe_unused]] auto testPrev = TestHelper::SetupFrame(thread, callInfoName);  \
59    JSTaggedValue testResult = className::methodName(callInfoName);                 \
60    TestHelper::TearDownFrame(thread, testPrev);                                    \
61    EXPECT_EQ(testResult, JSTaggedValue::Exception());                              \
62    EXPECT_EXCEPTION();                                                             \
63}
64
65// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
66#define CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(className, methodName)   \
67{                                                                            \
68    auto callInfo = NewEmptyCallInfo(thread);                                \
69    CONTAINERS_API_EXCEPTION_TEST(className, methodName, callInfo);          \
70}
71
72[[maybe_unused]] static EcmaRuntimeCallInfo* NewEmptyCallInfo(JSThread *thread)
73{
74    auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
75    callInfo->SetFunction(JSTaggedValue::Undefined());
76    callInfo->SetThis(JSTaggedValue::Undefined());
77    callInfo->SetCallArg(0, JSTaggedValue::Hole());
78    callInfo->SetCallArg(1, JSTaggedValue::Hole());
79    return callInfo;
80}
81} // namespace panda::test
82
83#endif // ECMASCRIPT_TESTS_CONTAINERS_TEST_HELPER_H
84