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"
38 ScriptObject::ActiveInstance active{&game};
44extern bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index,
int depth = 0);
50static std::optional<std::string> TestScriptAdminMakeJSON(std::string_view squirrel)
52 auto vm = sq_open(1024);
55 std::string buffer = fmt::format(
"return {}", squirrel);
59 sq_pushstring(vm,
"DummyClass", -1);
60 sq_newclass(vm, SQFalse);
61 sq_newslot(vm, -3, SQFalse);
65 REQUIRE(sq_compilebuffer(vm, buffer.c_str(), buffer.size(),
"test", SQTrue) == SQ_OK);
68 REQUIRE(sq_call(vm, 1, SQTrue, SQTrue) == SQ_OK);
70 REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
74 if (!ScriptAdminMakeJSON(json, vm, -1)) {
100static std::optional<std::string> TestScriptEventAdminPort(
const std::string &json)
102 auto vm = sq_open(1024);
105 ScriptEventAdminPort(json).GetObject(vm);
106 if (sq_gettype(vm, -1) == OT_NULL) {
110 REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
112 nlohmann::json squirrel_json;
113 REQUIRE(ScriptAdminMakeJSON(squirrel_json, vm, -1) ==
true);
116 return squirrel_json.dump();
119TEST_CASE(
"Squirrel -> JSON conversion")
123 CHECK(TestScriptAdminMakeJSON(R
"sq({ test = null })sq") == R"json({"test":null})json");
124 CHECK(TestScriptAdminMakeJSON(R"sq({ test = 1 })sq") == R"json({"test":1})json");
125 CHECK(TestScriptAdminMakeJSON(R"sq({ test = -1 })sq") == R"json({"test":-1})json");
126 CHECK(TestScriptAdminMakeJSON(R"sq({ test = true })sq") == R"json({"test":true})json");
127 CHECK(TestScriptAdminMakeJSON(R"sq({ test = "a" })sq") == R"json({"test":"a"})json");
128 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ ] })sq") == R"json({"test":[]})json");
129 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1 ] })sq") == R"json({"test":[1]})json");
130 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, "a", true, { test = 1 }, [], null ] })sq") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
131 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { } })sq") == R"json({"test":{}})json");
132 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1 } })sq") == R"json({"test":{"test":1}})json");
133 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test = 2 } })sq") == R"json({"test":{"test":2}})json");
134 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = [ 2 ] } })sq") == R"json({"test":{"test":1,"test2":[2]}})json");
137 CHECK(TestScriptAdminMakeJSON(R
"sq({ test = DummyClass })sq") == std::nullopt);
138 CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, DummyClass ] })sq") == std::nullopt);
139 CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = DummyClass } })sq") == std::nullopt);
142TEST_CASE("JSON -> Squirrel conversion")
146 CHECK(TestScriptEventAdminPort(R
"json({ "test": null })json") == R"json({"test":null})json");
147 CHECK(TestScriptEventAdminPort(R"json({ "test": 1 })json") == R"json({"test":1})json");
148 CHECK(TestScriptEventAdminPort(R"json({ "test": -1 })json") == R"json({"test":-1})json");
149 CHECK(TestScriptEventAdminPort(R"json({ "test": true })json") == R"json({"test":true})json");
150 CHECK(TestScriptEventAdminPort(R"json({ "test": "a" })json") == R"json({"test":"a"})json");
151 CHECK(TestScriptEventAdminPort(R"json({ "test": [] })json") == R"json({"test":[]})json");
152 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1 ] })json") == R"json({"test":[1]})json");
153 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, "a", true, { "test": 1 }, [], null ] })json") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
154 CHECK(TestScriptEventAdminPort(R"json({ "test": {} })json") == R"json({"test":{}})json");
155 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1 } })json") == R"json({"test":{"test":1}})json");
156 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 2 } })json") == R"json({"test":{"test":2}})json");
157 CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1, "test2": [ 2 ] } })json") == R"json({"test":{"test":1,"test2":[2]}})json");
160 CHECK(TestScriptEventAdminPort(R
"json({"test":1})json") == R"json({"test":1})json");
161 CHECK(TestScriptEventAdminPort(R"json({"test": 1})json") == R"json({"test":1})json");
164 CHECK(TestScriptEventAdminPort(R
"json({ "test": 1.1 })json") == std::nullopt);
165 CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, 3, 1.1 ] })json") == std::nullopt);
168 CHECK(TestScriptEventAdminPort(R
"json( 1 )json") == std::nullopt);
169 CHECK(TestScriptEventAdminPort(R"json( "a" )json") == std::nullopt);
170 CHECK(TestScriptEventAdminPort(R"json( [ 1 ] )json") == std::nullopt);
171 CHECK(TestScriptEventAdminPort(R"json( null )json") == std::nullopt);
172 CHECK(TestScriptEventAdminPort(R"json( true )json") == std::nullopt);
175 CHECK(TestScriptEventAdminPort(R
"json({"test":test})json") == std::nullopt);
176 CHECK(TestScriptEventAdminPort(R"json({ "test": 1 )json") == std::nullopt);
177 CHECK(TestScriptEventAdminPort(R
"json( "test": 1})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);
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.