OpenTTD Source 20260109-master-g241b5fcdfe
test_script_admin.cpp File Reference

Tests for the Squirrel -> JSON conversion in ScriptAdmin. More...

#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>
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

class  TestScriptController
 A controller to start enough so we can use Squirrel for testing. More...
 

Functions

bool ScriptAdminMakeJSON (nlohmann::json &json, HSQUIRRELVM vm, SQInteger index, int depth=0)
 
static std::optional< std::string > TestScriptAdminMakeJSON (std::string_view squirrel)
 Small wrapper around ScriptAdmin's MakeJSON that prepares the Squirrel engine if it was called from actual scripting.
 
static std::optional< std::string > TestScriptEventAdminPort (const std::string &json)
 Validate ScriptEventAdminPort can convert JSON to Squirrel.
 
 TEST_CASE ("Squirrel -> JSON conversion")
 
 TEST_CASE ("JSON -> Squirrel conversion")
 

Detailed Description

Tests for the Squirrel -> JSON conversion in ScriptAdmin.

Definition in file test_script_admin.cpp.

Function Documentation

◆ TEST_CASE() [1/2]

TEST_CASE ( "JSON -> Squirrel conversion"  )

Definition at line 144 of file test_script_admin.cpp.

◆ TEST_CASE() [2/2]

TEST_CASE ( "Squirrel -> JSON conversion"  )

Definition at line 121 of file test_script_admin.cpp.

◆ TestScriptAdminMakeJSON()

static std::optional< std::string > TestScriptAdminMakeJSON ( std::string_view  squirrel)
static

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

Definition at line 52 of file test_script_admin.cpp.

◆ TestScriptEventAdminPort()

static std::optional< std::string > TestScriptEventAdminPort ( const std::string &  json)
static

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.

Parameters
jsonThe JSON-string to convert to Squirrel
Returns
The Squirrel table converted to a JSON-string.

Definition at line 102 of file test_script_admin.cpp.