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