* Small wrapper around ScriptAdmin's MakeJSON that prepares the Squirrel * engine if it was called from actual scripting.. */
| 50 | * engine if it was called from actual scripting.. |
| 51 | */ |
| 52 | static 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. |
no test coverage detected