/
ImplementationWriterTest.js
171 lines (143 loc) · 5.04 KB
/
ImplementationWriterTest.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {expectCodeIsEqual, FakeWritable} from '../src/TestHelpers';
import {
emitNotificationDef,
emitRequestDef,
emitResponseDef,
emitTypeDef,
} from '../src/ImplementationWriter';
import { Event } from '../src/Event';
import { Command } from '../src/Command';
import { Type } from '../src/Type';
let stream = null;
beforeEach(() => {
stream = new FakeWritable();
});
test('emits type def', () => {
let obj = {
'id': 'Location',
'type': 'object',
'properties': [
{ 'name': 'scriptId', '$ref': 'Runtime.ScriptId', 'description': 'Script identifier as reported in the <code>Debugger.scriptParsed</code>.' },
{ 'name': 'lineNumber', 'type': 'integer', 'description': 'Line number in the script (0-based).' },
{ 'name': 'columnNumber', 'type': 'integer', 'optional': true, 'description': 'Column number in the script (0-based).' },
],
'description': 'Location in the source code.',
};
let type = Type.create('Debugger', obj);
emitTypeDef(stream, type);
expectCodeIsEqual(stream.get(), `
debugger::Location::Location(const dynamic &obj) {
assign(scriptId, obj, "scriptId");
assign(lineNumber, obj, "lineNumber");
assign(columnNumber, obj, "columnNumber");
}
dynamic debugger::Location::toDynamic() const {
dynamic obj = dynamic::object;
put(obj, "scriptId", scriptId);
put(obj, "lineNumber", lineNumber);
put(obj, "columnNumber", columnNumber);
return obj;
}
`);
});
test('emits request def', () => {
let obj = {
'name': 'getScriptSource',
'parameters': [
{ 'name': 'scriptId', '$ref': 'Runtime.ScriptId', 'description': 'Id of the script to get source for.' },
],
'returns': [
{ 'name': 'scriptSource', 'type': 'string', 'description': 'Script source.' },
],
'description': 'Returns source for the script with given id.',
};
let command = Command.create('Debugger', obj);
emitRequestDef(stream, command);
expectCodeIsEqual(stream.get(), `
debugger::GetScriptSourceRequest::GetScriptSourceRequest()
: Request("Debugger.getScriptSource") {}
debugger::GetScriptSourceRequest::GetScriptSourceRequest(const dynamic &obj)
: Request("Debugger.getScriptSource") {
assign(id, obj, "id");
assign(method, obj, "method");
dynamic params = obj.at("params");
assign(scriptId, params, "scriptId");
}
dynamic debugger::GetScriptSourceRequest::toDynamic() const {
dynamic params = dynamic::object;
put(params, "scriptId", scriptId);
dynamic obj = dynamic::object;
put(obj, "id", id);
put(obj, "method", method);
put(obj, "params", std::move(params));
return obj;
}
void debugger::GetScriptSourceRequest::accept(RequestHandler &handler) const {
handler.handle(*this);
}
`);
});
test('emits response def', () => {
let obj = {
'name': 'getScriptSource',
'parameters': [
{ 'name': 'scriptId', '$ref': 'Runtime.ScriptId', 'description': 'Id of the script to get source for.' },
],
'returns': [
{ 'name': 'scriptSource', 'type': 'string', 'description': 'Script source.' },
],
'description': 'Returns source for the script with given id.',
};
let command = Command.create('Debugger', obj);
emitResponseDef(stream, command);
expectCodeIsEqual(stream.get(), `
debugger::GetScriptSourceResponse::GetScriptSourceResponse(const dynamic &obj) {
assign(id, obj, "id");
dynamic res = obj.at("result");
assign(scriptSource, res, "scriptSource");
}
dynamic debugger::GetScriptSourceResponse::toDynamic() const {
dynamic res = dynamic::object;
put(res, "scriptSource", scriptSource);
dynamic obj = dynamic::object;
put(obj, "id", id);
put(obj, "result", std::move(res));
return obj;
}
`);
});
test('emits notification def', () => {
let obj = {
'name': 'messageAdded',
'parameters': [
{ 'name': 'message', '$ref': 'ConsoleMessage', 'description': 'Console message that has been added.' },
],
'description': 'Issued when new console message is added.',
};
let event = Event.create('Console', obj);
emitNotificationDef(stream, event);
expectCodeIsEqual(stream.get(), `
console::MessageAddedNotification::MessageAddedNotification()
: Notification("Console.messageAdded") {}
console::MessageAddedNotification::MessageAddedNotification(const dynamic &obj)
: Notification("Console.messageAdded") {
assign(method, obj, "method");
dynamic params = obj.at("params");
assign(message, params, "message");
}
dynamic console::MessageAddedNotification::toDynamic() const {
dynamic params = dynamic::object;
put(params, "message", message);
dynamic obj = dynamic::object;
put(obj, "method", method);
put(obj, "params", std::move(params));
return obj;
}
`);
});