From a2c99ad37d96900172d08b82e744fce3ed33b5be Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 12 Nov 2023 17:03:31 +0100 Subject: [PATCH] Added the monster example to the JSON tests --- tests/json/test_monster_example.hpp | 63 +++++++++++++++++++++++++++++ tests/json/tests.cpp | 2 + 2 files changed, 65 insertions(+) create mode 100644 tests/json/test_monster_example.hpp diff --git a/tests/json/test_monster_example.hpp b/tests/json/test_monster_example.hpp new file mode 100644 index 00000000..a4bdabdc --- /dev/null +++ b/tests/json/test_monster_example.hpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_monster_example() { + std::cout << "test_monster_example" << std::endl; + + using Color = rfl::Literal<"Red", "Green", "Blue">; + + struct Weapon { + std::string name; + short damage; + }; + + using Equipment = rfl::Variant>; + + struct Vec3 { + float x; + float y; + float z; + }; + + struct Monster { + Vec3 pos; + short mana = 150; + short hp = 100; + std::string name; + bool friendly = false; + std::vector inventory; + Color color = Color::make<"Blue">(); + std::vector weapons; + Equipment equipped; + std::vector path; + }; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = + std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe)}; + + write_and_read( + orc, + R"([[1.0,2.0,3.0],150,80,"MyMonster",false,[0,1,2,3,4,5,6,7,8,9],"Red",[["Sword",3],["Axe",5]],{"weapon":["Axe",5]},[]])"); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index bc7cad46..89c7ea04 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -20,6 +20,7 @@ #include "test_list.hpp" #include "test_literal.hpp" #include "test_map.hpp" +#include "test_monster_example.hpp" #include "test_multimap.hpp" #include "test_multiset.hpp" #include "test_one_of.hpp" @@ -75,6 +76,7 @@ int main() { test_error_messages(); test_result(); test_anonymous_fields(); + test_monster_example(); test_custom_class1(); test_custom_class2();