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_SOURCE_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_SOURCE_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsSourceTest : public TestActions {
23public:
24    JsSourceTest()
25    {
26        testAction = {
27            {SocketAction::SEND, "enable"},
28            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29            {SocketAction::SEND, "runtime-enable"},
30            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31            {SocketAction::SEND, "run"},
32            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33            // load sample.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::CUSTOM_RULE, [] (auto, auto, auto) -> bool {
35                return true;
36            }, TestCase::SOURCE},
37            // break on start
38            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
39            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
40                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
41                Result ret;
42                int id;
43                ret = json->GetInt("id", &id);
44                if (ret != Result::SUCCESS) {
45                    return false;
46                }
47
48                std::unique_ptr<PtJson> result = nullptr;
49                ret = json->GetObject("result", &result);
50                if (ret != Result::SUCCESS) {
51                    return false;
52                }
53
54                std::string scriptSource;
55                ret = result->GetString("scriptSource", &scriptSource);
56                if (ret != Result::SUCCESS) {
57                    return false;
58                }
59                return true;
60            }},
61            // reply success and run
62            {SocketAction::SEND, "success"},
63            {SocketAction::SEND, "resume"},
64            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
65            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66        };
67    }
68
69    std::pair<std::string, std::string> GetEntryPoint() override
70    {
71        return {pandaFile_, entryPoint_};
72    }
73    ~JsSourceTest() = default;
74
75private:
76    std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc";
77    std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js";
78    std::string entryPoint_ = "_GLOBAL::func_main_0";
79};
80
81std::unique_ptr<TestActions> GetJsSourceTest()
82{
83    return std::make_unique<JsSourceTest>();
84}
85}  // namespace panda::ecmascript::tooling::test
86
87#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_SOURCE_TEST_H
88