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_TOOLING_TEST_UTILS_TESTCASES_JS_RANGE_ERROR_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_RANGE_ERROR_TEST_H
18
19#include "test/utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsRangeErrorTest : public TestEvents {
23public:
24    JsRangeErrorTest()
25    {
26        breakpoint = [this](const JSPtLocation &location) {
27            ASSERT_TRUE(location.GetMethodId().IsValid());
28            ASSERT_LOCATION_EQ(location, location_);
29            ++breakpointCounter_;
30            std::vector<std::unique_ptr<CallFrame>> callFrames;
31            ASSERT_TRUE(debugger_->GenerateCallFrames(&callFrames, true));
32            ASSERT_TRUE(callFrames.size() > 0);
33            auto jsLocation = callFrames[0]->GetLocation();
34            ASSERT_TRUE(jsLocation != nullptr);
35            ASSERT_EQ(jsLocation->GetLine(), 21); // 21: breakpoint line
36            ASSERT_EQ(jsLocation->GetColumn(), 4); // 4: breakpoint column
37            TestUtil::SuspendUntilContinue(DebugEvent::BREAKPOINT, location);
38            return true;
39        };
40
41        exception = [this](const JSPtLocation &location) {
42            auto sourceLocation = TestUtil::GetSourceLocation(location, pandaFile_.c_str());
43            ASSERT_EQ(sourceLocation.line, 16); // 16: exception line
44            ASSERT_EQ(sourceLocation.column, 12); // 12: exception column
45            ++exceptionCounter_;
46            std::vector<std::unique_ptr<CallFrame>> callFrames;
47            ASSERT_TRUE(debugger_->GenerateCallFrames(&callFrames, true));
48            ASSERT_TRUE(callFrames.size() > 0);
49            auto jsLocation = callFrames[0]->GetLocation();
50            ASSERT_TRUE(jsLocation != nullptr);
51            ASSERT_EQ(jsLocation->GetLine(), 16); // 16: exception line
52            ASSERT_EQ(jsLocation->GetColumn(), 12); // 12: exception column
53            TestUtil::SuspendUntilContinue(DebugEvent::EXCEPTION, location);
54            return true;
55        };
56
57        loadModule = [this](std::string_view moduleName) {
58            runtime_->Enable();
59            // 21: breakpointer line, 4: breakpointer column
60            location_ = TestUtil::GetLocation(sourceFile_.c_str(), 21, 4, pandaFile_.c_str());
61            ASSERT_TRUE(location_.GetMethodId().IsValid());
62            TestUtil::SuspendUntilContinue(DebugEvent::LOAD_MODULE);
63            ASSERT_EQ(moduleName, pandaFile_);
64            ASSERT_TRUE(debugger_->NotifyScriptParsed(0, pandaFile_));
65            auto condFuncRef = FunctionRef::Undefined(vm_);
66            auto ret = debugInterface_->SetBreakpoint(location_, condFuncRef);
67            ASSERT_TRUE(ret);
68            return true;
69        };
70
71        scenario = [this]() {
72            TestUtil::WaitForLoadModule();
73            TestUtil::Continue();
74            TestUtil::WaitForBreakpoint(location_);
75            TestUtil::Continue();
76            TestUtil::WaitForException();
77            TestUtil::Continue();
78            auto ret = debugInterface_->RemoveBreakpoint(location_);
79            ASSERT_TRUE(ret);
80            ASSERT_EXITED();
81            return true;
82        };
83
84        vmDeath = [this]() {
85            ASSERT_EQ(breakpointCounter_, 1U); // 1: break point counter
86            ASSERT_EQ(exceptionCounter_, 1U);  // 1: exception counter
87            return true;
88        };
89    }
90
91    std::pair<std::string, std::string> GetEntryPoint() override
92    {
93        return {pandaFile_, entryPoint_};
94    }
95    ~JsRangeErrorTest() = default;
96
97private:
98    std::string pandaFile_ = DEBUGGER_ABC_DIR "range_error.abc";
99    std::string sourceFile_ = DEBUGGER_JS_DIR "range_error.js";
100    std::string entryPoint_ = "_GLOBAL::func_main_0";
101    JSPtLocation location_ {nullptr, JSPtLocation::EntityId(0), 0};
102    size_t breakpointCounter_ = 0;
103    size_t exceptionCounter_ = 0;
104};
105
106std::unique_ptr<TestEvents> GetJsRangeErrorTest()
107{
108    return std::make_unique<JsRangeErrorTest>();
109}
110}  // namespace panda::ecmascript::tooling::test
111
112#endif  // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_RANGE_ERROR_TEST_H
113