12#include "../3rdparty/catch2/catch.hpp"
14#include "../game/game_instance.hpp"
15#include "../script/api/script_admin.hpp"
16#include "../script/api/script_event_types.hpp"
17#include "../script/script_instance.hpp"
18#include "../script/squirrel.hpp"
20#include "../3rdparty/fmt/format.h"
21#include "../3rdparty/nlohmann/json.hpp"
25#include "../safeguards.h"
40 ScriptObject::ActiveInstance active{game};
46extern bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index,
int depth = 0);
52static std::optional<std::string> TestScriptAdminMakeJSON(std::string_view squirrel)
54 auto vm = sq_open(1024);
57 std::string buffer = fmt::format(
"return {}", squirrel);
61 sq_pushstring(vm,
"DummyClass");
62 sq_newclass(vm, SQFalse);
63 sq_newslot(vm, -3, SQFalse);
67 REQUIRE(sq_compilebuffer(vm, buffer,
"test", SQTrue) == SQ_OK);
70 REQUIRE(sq_call(vm, 1, SQTrue, SQTrue) == SQ_OK);
72 REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
76 if (!ScriptAdminMakeJSON(json, vm, -1)) {
102static std::optional<std::string> TestScriptEventAdminPort(
const std::string &json)
104 auto vm = sq_open(1024);
107 ScriptEventAdminPort(json).GetObject(vm);
108 if (sq_gettype(vm, -1) == OT_NULL) {
112 REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
114 nlohmann::json squirrel_json;
115 REQUIRE(ScriptAdminMakeJSON(squirrel_json, vm, -1) ==
true);
118 return squirrel_json.dump();
121TEST_CASE(
"Squirrel -> JSON conversion")
125 CHECK(TestScriptAdminMakeJSON(R
"sq({ test = null })sq") == R"json({"test":null})json");
126 CHECK(TestScriptAdminMakeJSON(R"sq({ test = 1 })sq") == R"json({"test":1})json");
127 CHECK(TestScriptAdminMakeJSON(R"sq({ test = -1 })sq") == R"json({"test":-1})json");
128 CHECK(TestScriptAdminMakeJSON(R"sq({ test = true })sq") == R"json({"test":true})json");
129 CHECK(TestScriptAdminMakeJSON(R"sq({ test = "a" })sq") == R"json({"test":"a"})json");
130 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ ] })sq") == R"json({"test":[]})json");
131 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1 ] })sq") == R"json({"test":[1]})json");
132 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, "a", true, { test = 1 }, [], null ] })sq") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
133 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { } })sq") == R"json({"test":{}})json");
134 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1 } })sq") == R"json({"test":{"test":1}})json");
135 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test = 2 } })sq") == R"json({"test":{"test":2}})json");
136 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = [ 2 ] } })sq") == R"json({"test":{"test":1,"test2":[2]}})json");
139 CHECK(TestScriptAdminMakeJSON(R
"sq({ test = DummyClass })sq") == std::nullopt);
140 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, DummyClass ] })sq") == std::nullopt);
141 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = DummyClass } })sq") == std::nullopt);
144TEST_CASE("JSON -> Squirrel conversion")
148 CHECK(TestScriptEventAdminPort(R
"json({ "test": null })json") == R"json({"test":null})json");
149 CHECK(TestScriptEventAdminPort(R"json({ "test": 1 })json") == R"json({"test":1})json");
150 CHECK(TestScriptEventAdminPort(R"json({ "test": -1 })json") == R"json({"test":-1})json");
151 CHECK(TestScriptEventAdminPort(R"json({ "test": true })json") == R"json({"test":true})json");
152 CHECK(TestScriptEventAdminPort(R"json({ "test": "a" })json") == R"json({"test":"a"})json");
153 CHECK(TestScriptEventAdminPort(R"json({ "test": [] })json") == R"json({"test":[]})json");
154 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1 ] })json") == R"json({"test":[1]})json");
155 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, "a", true, { "test": 1 }, [], null ] })json") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
156 CHECK(TestScriptEventAdminPort(R"json({ "test": {} })json") == R"json({"test":{}})json");
157 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1 } })json") == R"json({"test":{"test":1}})json");
158 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 2 } })json") == R"json({"test":{"test":2}})json");
159 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1, "test2": [ 2 ] } })json") == R"json({"test":{"test":1,"test2":[2]}})json");
162 CHECK(TestScriptEventAdminPort(R
"json({"test":1})json") == R"json({"test":1})json");
163 CHECK(TestScriptEventAdminPort(R"json({"test": 1})json") == R"json({"test":1})json");
166 CHECK(TestScriptEventAdminPort(R
"json({ "test": 1.1 })json") == std::nullopt);
167 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, 3, 1.1 ] })json") == std::nullopt);
170 CHECK(TestScriptEventAdminPort(R
"json( 1 )json") == std::nullopt);
171 CHECK(TestScriptEventAdminPort(R"json( "a" )json") == std::nullopt);
172 CHECK(TestScriptEventAdminPort(R"json( [ 1 ] )json") == std::nullopt);
173 CHECK(TestScriptEventAdminPort(R"json( null )json") == std::nullopt);
174 CHECK(TestScriptEventAdminPort(R"json( true )json") == std::nullopt);
177 CHECK(TestScriptEventAdminPort(R
"json({"test":test})json") == std::nullopt);
178 CHECK(TestScriptEventAdminPort(R"json({ "test": 1 )json") == std::nullopt);
179 CHECK(TestScriptEventAdminPort(R
"json( "test": 1})json") == std::nullopt);
180 CHECK(TestScriptEventAdminPort(R
"json({ "test" = 1})json") == std::nullopt);
181 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1 })json") == std::nullopt);
182 CHECK(TestScriptEventAdminPort(R
"json({ "test": 1 ] })json") == std::nullopt);
Runtime information about a game script like a pointer to the squirrel vm and the current state.
A controller to start enough so we can use Squirrel for testing.