forked from Dushistov/qt_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtmonkey_app_api.cpp
153 lines (136 loc) · 5.32 KB
/
qtmonkey_app_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "qtmonkey_app_api.hpp"
#include <cassert>
#include "common.hpp"
#include "json11.hpp"
namespace qt_monkey_app
{
using json11::Json;
namespace
{
struct QStringJsonTrait final {
explicit QStringJsonTrait(const QString &s) : str_(s) {}
std::string to_json() const { return str_.toUtf8().data(); }
private:
const QString &str_;
};
} // namespace
std::string createPacketFromUserAppEvent(const QString &scriptLines)
{
auto json = Json::object{
{"event", Json::object{{"script", QStringJsonTrait{scriptLines}}}}};
return Json{json}.dump();
}
std::string createPacketFromUserAppOutput(const QString &stdOutLines)
{
auto json = Json::object{{"app output", QStringJsonTrait{stdOutLines}}};
return Json{json}.dump();
}
std::string createPacketFromUserAppErrors(const QString &errMsg)
{
auto json = Json::object{{"app errors", QStringJsonTrait{errMsg}}};
return Json{json}.dump();
}
std::string createPacketFromScriptEnd()
{
auto json = Json{"script end"};
return Json{json}.dump();
}
std::string createPacketFromUserAppScriptLog(const QString &logMsg)
{
auto json = Json::object{{"script logs", QStringJsonTrait{logMsg}}};
return Json{json}.dump();
}
std::string createPacketFromRunScript(const QString &script,
const QString &scriptFileName)
{
auto json = Json::object{
{"run script",
Json::object{{"script", QStringJsonTrait{script}},
{"file", QStringJsonTrait{scriptFileName}}}}};
return Json{json}.dump();
}
void parseOutputFromMonkeyApp(
const json11::string_view &data, size_t &stopPos,
const std::function<void(QString)> &onNewUserAppEvent,
const std::function<void(QString)> &onUserAppError,
const std::function<void()> &onScriptEnd,
const std::function<void(QString)> &onScriptLog,
const std::function<void(QString)> &onParseError)
{
assert(data.size() > 0);
stopPos = 0;
std::string::size_type parserStopPos;
std::string err;
auto jsonArr = Json::parse_multi(data, parserStopPos, err);
stopPos = parserStopPos;
for (const Json &elm : jsonArr) {
if (elm.is_null())
continue;
if (elm.is_object() && elm.object_items().size() == 1u
&& elm.object_items().begin()->first == "event") {
const Json &eventJson = elm.object_items().begin()->second;
if (!eventJson.is_object() || eventJson.object_items().size() != 1u
|| eventJson.object_items().begin()->first != "script"
|| !eventJson.object_items().begin()->second.is_string()) {
onParseError(QStringLiteral("event"));
return;
}
onNewUserAppEvent(QString::fromUtf8(eventJson.object_items()
.begin()
->second.string_value()
.c_str()));
} else if (elm.is_object() && elm.object_items().size() == 1u
&& elm.object_items().begin()->first == "app errors") {
auto it = elm.object_items().begin();
if (!it->second.is_string()) {
onParseError(QStringLiteral("app errors"));
return;
}
onUserAppError(
QString::fromUtf8(it->second.string_value().c_str()));
} else if (elm.is_object() && elm.object_items().size() == 1u
&& elm.object_items().begin()->first == "script logs") {
auto it = elm.object_items().begin();
if (!it->second.is_string()) {
onParseError(QStringLiteral("script logs"));
return;
}
onScriptLog(QString::fromUtf8(it->second.string_value().c_str()));
} else if (elm.is_string() && elm.string_value() == "script end") {
onScriptEnd();
}
}
}
void parseOutputFromGui(
const json11::string_view &data, size_t &parserStopPos,
const std::function<void(QString, QString)> &onRunScript,
const std::function<void(QString)> &onParseError)
{
std::string err;
auto jsonArr = Json::parse_multi(data, parserStopPos, err);
for (const Json &elm : jsonArr) {
if (elm.is_null())
continue;
if (elm.is_object() && elm.object_items().size() == 1u
&& elm.object_items().begin()->first == "run script") {
const Json &scriptJson = elm.object_items().begin()->second;
Json::object::const_iterator scriptIt, scriptFNameIt;
if (!scriptJson.is_object()
|| scriptJson.object_items().size() != 2u
|| (scriptIt = scriptJson.object_items().find("script"))
== scriptJson.object_items().end()
|| !scriptIt->second.is_string()
|| (scriptFNameIt = scriptJson.object_items().find("file"))
== scriptJson.object_items().end()
|| !scriptFNameIt->second.is_string()) {
onParseError(QStringLiteral("run script"));
return;
}
onRunScript(
QString::fromUtf8(scriptIt->second.string_value().c_str()),
QString::fromUtf8(
scriptFNameIt->second.string_value().c_str()));
}
}
}
} // namespace qt_monkey_app