1 /*
2 * Copyright (c) 2023 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_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
18
19 #include <map>
20 #include "tooling/test/client_utils/test_util.h"
21
22 namespace panda::ecmascript::tooling::test {
23 class JsLocalVariableScopeTest : public TestActions {
24 public:
JsLocalVariableScopeTest()25 JsLocalVariableScopeTest()
26 {
27 testAction = {
28 {SocketAction::SEND, "enable"},
29 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
30 {SocketAction::SEND, "runtime-enable"},
31 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
32 {SocketAction::SEND, "run"},
33 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
34 // load closure_scope.js
35 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
36 // break on start
37 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
38 // set first breakpoint
39 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "local_variable_scope.js 38"},
40 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
41
42 // hit breakpoint after resume first time
43 {SocketAction::SEND, "resume"},
44 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
45 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
47 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
48 Result ret;
49 std::string method;
50 ret = json->GetString("method", &method);
51 if (ret != Result::SUCCESS || method != "Debugger.paused") {
52 return false;
53 }
54 DebuggerClient debuggerClient(0);
55 debuggerClient.PausedReply(std::move(json));
56 return true;
57 }},
58 {SocketAction::SEND, "print"},
59 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool {
60 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
61 int32_t id = 0;
62 Result ret = json->GetInt("id", &id);
63 if (ret != Result::SUCCESS) {
64 return false;
65 }
66
67 std::unique_ptr<PtJson> result = nullptr;
68 ret = json->GetObject("result", &result);
69 if (ret != Result::SUCCESS) {
70 return false;
71 }
72
73 std::unique_ptr<PtJson> innerResult;
74 ret = result->GetArray("result", &innerResult);
75 if (ret != Result::SUCCESS) {
76 return false;
77 }
78
79 std::string name;
80 std::unique_ptr<PtJson> value;
81 std::string valueDes;
82 std::string type;
83 for (int32_t i = 0; i < innerResult->GetSize(); i++) {
84 ret = innerResult->Get(i)->GetString("name", &name);
85 if (ret != Result::SUCCESS) {
86 return false;
87 }
88
89 ret = innerResult->Get(i)->GetObject("value", &value);
90 if (ret != Result::SUCCESS) {
91 return false;
92 }
93
94 ret = value->GetString("type", &type);
95 if (ret != Result::SUCCESS) {
96 return false;
97 }
98
99 if (type == "undefined") {
100 continue;
101 }
102
103 ret = value->GetString("description", &valueDes);
104 if (ret != Result::SUCCESS) {
105 return false;
106 }
107
108 if (this->truthGroundMap_.count(name) == 0) {
109 return false;
110 }
111
112 if (this->truthGroundMap_[name] != valueDes) {
113 return false;
114 }
115 }
116 return true;
117 }},
118
119 // // reply success and run
120 {SocketAction::SEND, "success"},
121 {SocketAction::SEND, "resume"},
122 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
123 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
124 };
125 }
126
127 std::pair<std::string, std::string> GetEntryPoint() override
128 {
129 return {pandaFile_, entryPoint_};
130 }
131 ~JsLocalVariableScopeTest() = default;
132
133 private:
134 std::string pandaFile_ = DEBUGGER_ABC_DIR "local_variable_scope.abc";
135 std::string sourceFile_ = DEBUGGER_JS_DIR "local_variable_scope.js";
136 std::string entryPoint_ = "_GLOBAL::func_main_0";
137
138 std::map<std::string, std::string> truthGroundMap_ = {
139 {"a", "1"},
140 {"b", "2"},
141 {"e", "6"},
142 {"f", "7"},
143 {"innerTest", "function innerTest( { [js code] }"},
144 {"j", "12"},
145 {"scopeTest", "function scopeTest( { [js code] }"},
146 {"n", "undefined"},
147 {"p", "undefined"},
148 {"q", "undefined"}
149 };
150 };
151
GetJsLocalVariableScopeTest()152 std::unique_ptr<TestActions> GetJsLocalVariableScopeTest()
153 {
154 return std::make_unique<JsLocalVariableScopeTest>();
155 }
156 } // namespace panda::ecmascript::tooling::test
157
158 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_LOCAL_VARIABLE_SCOPE_TEST_H
159