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_SYNTAX_EXCEPTION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_SYNTAX_EXCEPTION_TEST_H
18
19 #include "test/utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsSyntaxExceptionTest : public TestEvents {
23 public:
JsSyntaxExceptionTest()24 JsSyntaxExceptionTest()
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(), 27); // 27: 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, 18); // 18: exception line
44 ASSERT_EQ(sourceLocation.column, 8); // 8: 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(), 18); // 18: exception line
52 ASSERT_EQ(jsLocation->GetColumn(), 8); // 8: exception column
53 TestUtil::SuspendUntilContinue(DebugEvent::EXCEPTION, location);
54 return true;
55 };
56
57 loadModule = [this](std::string_view moduleName) {
58 runtime_->Enable();
59 // 27: breakpointer line
60 location_ = TestUtil::GetLocation(sourceFile_.c_str(), 27, 0, 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
GetEntryPoint()91 std::pair<std::string, std::string> GetEntryPoint() override
92 {
93 return {pandaFile_, entryPoint_};
94 }
95 ~JsSyntaxExceptionTest() = default;
96
97 private:
98 const std::string testFile = "syntax_exception";
99 std::string pandaFile_ = DEBUGGER_ABC_DIR + testFile + ".abc";
100 std::string sourceFile_ = DEBUGGER_JS_DIR + testFile + ".js";
101 std::string entryPoint_ = "_GLOBAL::func_main_0";
102 JSPtLocation location_ {nullptr, JSPtLocation::EntityId(0), 0};
103 size_t breakpointCounter_ = 0;
104 size_t exceptionCounter_ = 0;
105 };
106
GetJsSyntaxExceptionTest()107 std::unique_ptr<TestEvents> GetJsSyntaxExceptionTest()
108 {
109 return std::make_unique<JsSyntaxExceptionTest>();
110 }
111 } // namespace panda::ecmascript::tooling::test
112
113 #endif // ECMASCRIPT_TOOLING_TEST_UTILS_TESTCASES_JS_SYNTAX_EXCEPTION_TEST_H
114