MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / TestScriptAdminMakeJSON

Function TestScriptAdminMakeJSON

src/tests/test_script_admin.cpp:52–83  ·  view source on GitHub ↗

* Small wrapper around ScriptAdmin's MakeJSON that prepares the Squirrel * engine if it was called from actual scripting.. */

Source from the content-addressed store, hash-verified

50 * engine if it was called from actual scripting..
51 */
52static std::optional<std::string> TestScriptAdminMakeJSON(std::string_view squirrel)
53{
54 auto vm = sq_open(1024);
55 /* sq_compile creates a closure with our snippet, which is a table.
56 * Add "return " to get the table on the stack. */
57 std::string buffer = fmt::format("return {}", squirrel);
58
59 /* Insert an (empty) class for testing. */
60 sq_pushroottable(vm);
61 sq_pushstring(vm, "DummyClass");
62 sq_newclass(vm, SQFalse);
63 sq_newslot(vm, -3, SQFalse);
64 sq_pop(vm, 1);
65
66 /* Compile the snippet. */
67 REQUIRE(sq_compilebuffer(vm, buffer, "test", SQTrue) == SQ_OK);
68 /* Execute the snippet, capturing the return value. */
69 sq_pushroottable(vm);
70 REQUIRE(sq_call(vm, 1, SQTrue, SQTrue) == SQ_OK);
71 /* Ensure the snippet pushed a table on the stack. */
72 REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
73
74 /* Feed the snippet into the MakeJSON function. */
75 nlohmann::json json;
76 if (!ScriptAdminMakeJSON(json, vm, -1)) {
77 sq_close(vm);
78 return std::nullopt;
79 }
80
81 sq_close(vm);
82 return json.dump();
83}
84
85/**
86 * Validate ScriptEventAdminPort can convert JSON to Squirrel.

Callers 1

Calls 13

sq_openFunction · 0.85
sq_pushroottableFunction · 0.85
sq_pushstringFunction · 0.85
sq_newclassFunction · 0.85
sq_newslotFunction · 0.85
sq_popFunction · 0.85
sq_compilebufferFunction · 0.85
sq_callFunction · 0.85
sq_gettypeFunction · 0.85
ScriptAdminMakeJSONFunction · 0.85
sq_closeFunction · 0.85
dumpMethod · 0.80

Tested by

no test coverage detected