Files
@ r28520:f9aebe299cae
Branch filter:
Location: cpp/openttd-patchpack/source/src/tests/test_script_admin.cpp
r28520:f9aebe299cae
8.3 KiB
text/x-c
Codechange: MacOS already has MIN/MAX macros defined
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
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 172 173 174 175 176 177 178 179 180 181 | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file script_admin_json.cpp Tests for the Squirrel -> JSON conversion in ScriptAdmin. */
#include "../stdafx.h"
#include "../3rdparty/catch2/catch.hpp"
#include "../game/game_instance.hpp"
#include "../script/api/script_admin.hpp"
#include "../script/api/script_event_types.hpp"
#include "../script/script_instance.hpp"
#include "../script/squirrel.hpp"
#include "../3rdparty/fmt/format.h"
#include "../3rdparty/nlohmann/json.hpp"
#include <squirrel.h>
/**
* A controller to start enough so we can use Squirrel for testing.
*
* To run Squirrel, we need an Allocator, so malloc/free works.
* For functions that log, we need an ActiveInstance, so the logger knows where
* to send the logs to.
*
* By instantiating this class, both are set correctly. After that you can
* use Squirrel without issues.
*/
class TestScriptController {
public:
GameInstance game{};
ScriptObject::ActiveInstance active{&game};
Squirrel engine{"test"};
ScriptAllocatorScope scope{&engine};
};
extern bool ScriptAdminMakeJSON(nlohmann::json &json, HSQUIRRELVM vm, SQInteger index, int depth = 0);
/**
* Small wrapper around ScriptAdmin's MakeJSON that prepares the Squirrel
* engine if it was called from actual scripting..
*/
static std::optional<std::string> TestScriptAdminMakeJSON(std::string_view squirrel)
{
auto vm = sq_open(1024);
/* sq_compile creates a closure with our snipper, which is a table.
* Add "return " to get the table on the stack. */
std::string buffer = fmt::format("return {}", squirrel);
/* Insert an (empty) class for testing. */
sq_pushroottable(vm);
sq_pushstring(vm, "DummyClass", -1);
sq_newclass(vm, SQFalse);
sq_newslot(vm, -3, SQFalse);
sq_pop(vm, 1);
/* Compile the snippet. */
REQUIRE(sq_compilebuffer(vm, buffer.c_str(), buffer.size(), "test", SQTrue) == SQ_OK);
/* Execute the snippet, capturing the return value. */
sq_pushroottable(vm);
REQUIRE(sq_call(vm, 1, SQTrue, SQTrue) == SQ_OK);
/* Ensure the snippet pushed a table on the stack. */
REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
/* Feed the snippet into the MakeJSON function. */
nlohmann::json json;
if (!ScriptAdminMakeJSON(json, vm, -1)) {
sq_close(vm);
return std::nullopt;
}
sq_close(vm);
return json.dump();
}
/**
* Validate ScriptEventAdminPort can convert JSON to Squirrel.
*
* This function is not actually part of ScriptAdmin, but we will use MakeJSON,
* and as such need to be inside this class.
*
* The easiest way to do validate, is to first use ScriptEventAdminPort (the function
* we are testing) to convert the JSON to a Squirrel table. Then to use MakeJSON
* to convert it back to JSON.
*
* Sadly, Squirrel has no way to easily compare if two tables are identical, so we
* use the JSON -> Squirrel -> JSON method to validate the conversion. But mind you,
* a failure in the final JSON might also mean a bug in MakeJSON.
*
* @param json The JSON-string to convert to Squirrel
* @return The Squirrel table converted to a JSON-string.
*/
static std::optional<std::string> TestScriptEventAdminPort(const std::string &json)
{
auto vm = sq_open(1024);
/* Run the conversion JSON -> Squirrel (this will now be on top of the stack). */
ScriptEventAdminPort(json).GetObject(vm);
if (sq_gettype(vm, -1) == OT_NULL) {
sq_close(vm);
return std::nullopt;
}
REQUIRE(sq_gettype(vm, -1) == OT_TABLE);
nlohmann::json squirrel_json;
REQUIRE(ScriptAdminMakeJSON(squirrel_json, vm, -1) == true);
sq_close(vm);
return squirrel_json.dump();
}
TEST_CASE("Squirrel -> JSON conversion")
{
TestScriptController controller;
CHECK(TestScriptAdminMakeJSON(R"sq({ test = null })sq") == R"json({"test":null})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = 1 })sq") == R"json({"test":1})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = -1 })sq") == R"json({"test":-1})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = true })sq") == R"json({"test":true})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = "a" })sq") == R"json({"test":"a"})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ ] })sq") == R"json({"test":[]})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1 ] })sq") == R"json({"test":[1]})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, "a", true, { test = 1 }, [], null ] })sq") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = { } })sq") == R"json({"test":{}})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1 } })sq") == R"json({"test":{"test":1}})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test = 2 } })sq") == R"json({"test":{"test":2}})json");
CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = [ 2 ] } })sq") == R"json({"test":{"test":1,"test2":[2]}})json");
/* Cases that should fail, as we cannot convert a class to JSON. */
CHECK(TestScriptAdminMakeJSON(R"sq({ test = DummyClass })sq") == std::nullopt);
CHECK(TestScriptAdminMakeJSON(R"sq({ test = [ 1, DummyClass ] })sq") == std::nullopt);
CHECK(TestScriptAdminMakeJSON(R"sq({ test = { test = 1, test2 = DummyClass } })sq") == std::nullopt);
}
TEST_CASE("JSON -> Squirrel conversion")
{
TestScriptController controller;
CHECK(TestScriptEventAdminPort(R"json({ "test": null })json") == R"json({"test":null})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": 1 })json") == R"json({"test":1})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": -1 })json") == R"json({"test":-1})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": true })json") == R"json({"test":true})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": "a" })json") == R"json({"test":"a"})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": [] })json") == R"json({"test":[]})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1 ] })json") == R"json({"test":[1]})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, "a", true, { "test": 1 }, [], null ] })json") == R"json({"test":[1,"a",true,{"test":1},[],null]})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": {} })json") == R"json({"test":{}})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1 } })json") == R"json({"test":{"test":1}})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 2 } })json") == R"json({"test":{"test":2}})json");
CHECK(TestScriptEventAdminPort(R"json({ "test": { "test": 1, "test2": [ 2 ] } })json") == R"json({"test":{"test":1,"test2":[2]}})json");
/* Check if spaces are properly ignored. */
CHECK(TestScriptEventAdminPort(R"json({"test":1})json") == R"json({"test":1})json");
CHECK(TestScriptEventAdminPort(R"json({"test": 1})json") == R"json({"test":1})json");
/* Valid JSON but invalid Squirrel (read: floats). */
CHECK(TestScriptEventAdminPort(R"json({ "test": 1.1 })json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1, 3, 1.1 ] })json") == std::nullopt);
/* Root element has to be an object. */
CHECK(TestScriptEventAdminPort(R"json( 1 )json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json( "a" )json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json( [ 1 ] )json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json( null )json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json( true )json") == std::nullopt);
/* Cases that should fail, as it is invalid JSON. */
CHECK(TestScriptEventAdminPort(R"json({"test":test})json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json({ "test": 1 )json") == std::nullopt); // Missing closing }
CHECK(TestScriptEventAdminPort(R"json( "test": 1})json") == std::nullopt); // Missing opening {
CHECK(TestScriptEventAdminPort(R"json({ "test" = 1})json") == std::nullopt);
CHECK(TestScriptEventAdminPort(R"json({ "test": [ 1 })json") == std::nullopt); // Missing closing ]
CHECK(TestScriptEventAdminPort(R"json({ "test": 1 ] })json") == std::nullopt); // Missing opening [
}
|