From 3cfee3f62ddf64f8066984d88a7e16113f29dbe2 Mon Sep 17 00:00:00 2001 From: stu Date: Thu, 21 Mar 2024 12:11:06 +0000 Subject: [PATCH 01/33] revert change of F&& to const F& --- include/rfl/NamedTuple.hpp | 4 ++-- tests/json/test_apply.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 1edd4d73..c2d38d73 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -199,7 +199,7 @@ class NamedTuple { /// Invokes a callable object once for each field in order. template - void apply(const F& _f) { + void apply(F&& _f) { const auto apply_to_field = [&_f](AFields&&... fields) { ((_f(std::forward(fields))), ...); @@ -209,7 +209,7 @@ class NamedTuple { /// Invokes a callable object once for each field in order. template - void apply(const F& _f) const { + void apply(F&& _f) const { const auto apply_to_field = [&_f](const auto&... fields) { ((_f(fields)), ...); }; diff --git a/tests/json/test_apply.cpp b/tests/json/test_apply.cpp index 3f80e843..bcb97db7 100644 --- a/tests/json/test_apply.cpp +++ b/tests/json/test_apply.cpp @@ -38,7 +38,7 @@ void test() { } }); - rfl::to_view(lisa).apply([](auto field) { + rfl::to_view(lisa).apply([](auto field) mutable { if constexpr (decltype(field)::name() == "first_name") { *field.value() = "Bart"; } From 270d27d3967ad0f49582cb09a16926f5d9f28850 Mon Sep 17 00:00:00 2001 From: stu Date: Thu, 21 Mar 2024 12:42:53 +0000 Subject: [PATCH 02/33] also for empty named tuple --- include/rfl/NamedTuple.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index c2d38d73..ba9619be 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -602,7 +602,7 @@ class NamedTuple<> { /// Does nothing at all. template - void apply(const F& _f) const {} + void apply(F&& _f) const {} /// Returns an empty tuple. auto fields() const { return std::tuple(); } From d21dc1c0c93920d90b3731931a876edaabdf857a Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Fri, 29 Mar 2024 11:57:31 +0100 Subject: [PATCH 03/33] Added documentation for the JSON schema --- README.md | 32 +++++++- docs/README.md | 2 + docs/json_schema.md | 191 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 docs/json_schema.md diff --git a/README.md b/README.md index 402705e7..0c2d73f7 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ age: 45 ``` This will work for just about any example in the entire documentation -and any supported format: +and any supported format, except where explicitly noted otherwise: ```cpp rfl::bson::write(homer); @@ -197,6 +197,35 @@ Found 5 errors: 5) Field named 'children' not found. ``` +## JSON schema + +reflect-cpp also supports generating JSON schemata: + +```cpp +struct Person { + std::string first_name; + std::string last_name; + rfl::Description<"Must be a proper email in the form xxx@xxx.xxx.", + rfl::Email> + email; + rfl::Description< + "The person's children. Pass an empty array for no children.", + std::vector> + children; + float salary; +}; + +const std::string json_schema = rfl::json::to_schema(); +``` + +The resulting JSON schema looks like this: + +```json +{"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/Person","definitions":{"Person":{"type":"object","properties":{"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/Person"}},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"first_name":{"type":"string"},"last_name":{"type":"string"},"salary":{"type":"number"}},"required":["children","email","first_name","last_name","salary"]}}} +``` + +Note that this is currently supported for JSON only, since most other formats do not support schemata in the first place. + ## Enums reflect-cpp supports scoped enumerations: @@ -388,6 +417,7 @@ reflect-cpp supports the following containers from the C++ standard library: - `std::unordered_set` - `std::variant` - `std::vector` +- `std::wstring` ### Additional containers diff --git a/docs/README.md b/docs/README.md index 30465617..158e5ee0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -36,6 +36,8 @@ 2.4) [Size validation](https://github.com/getml/reflect-cpp/blob/main/docs/size_validation.md) - For imposing size constraints on containers such as `std::vector` or `std::string`. +2.5) [JSON schema](https://github.com/getml/reflect-cpp/blob/main/docs/json_schema.md) - For validating your schema before you even send it to your C++ backend. + ## 3) Custom classes 3.1) [Custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) - For custom classes with private fields. diff --git a/docs/json_schema.md b/docs/json_schema.md new file mode 100644 index 00000000..110c3da7 --- /dev/null +++ b/docs/json_schema.md @@ -0,0 +1,191 @@ +# JSON schema + +JSON schemata are a powerful tool for expressing the expected structure of your input. You can use it to validate your input before you even send it to your C++ backend, which will result in better UX. + +It can also be used for code generation. For instance, tools such as https://app.quicktype.io/ allow you to generate code in multiple programming languages from the JSON schema (even though the validations are usually omitted). + +If you are interacting with Python, we warmly recommend https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/. This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema. + +Note that this is only supported for JSON, not for other formats. + +## Basic idea + +Suppose you have a struct like this: + +```cpp +struct Person { + std::string first_name; + std::string last_name; + rfl::Email email; + std::vector children; + float salary; +}; +``` + +You can generate a JSON schema like this: + +```cpp +const std::string json_schema = rfl::json::to_schema(rfl::json::pretty); +``` + +You do not have to pass `rfl::json::pretty`, but for the purposes of this documentation it is better to do so. + +This will result in the following JSON schema: + +```json +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$ref": "#/definitions/Person", + "definitions": { + "Person": { + "type": "object", + "properties": { + "children": { + "type": "array", + "items": { + "$ref": "#/definitions/Person" + } + }, + "email": { + "type": "string", + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" + }, + "first_name": { + "type": "string" + }, + "last_name": { + "type": "string" + }, + "salary": { + "type": "number" + } + }, + "required": [ + "children", + "email", + "first_name", + "last_name", + "salary" + ] + } + } +}``` + +You can insert this into the tools mentioned above and see the generated code. + +## Adding descriptions + +JSON schemata also often contain descriptions. reflect-cpp supports this as well. + +```cpp +struct Person { + std::string first_name; + std::string last_name; + rfl::Description<"Must be a proper email in the form xxx@xxx.xxx.", + rfl::Email> + email; + rfl::Description< + "The person's children. Pass an empty array for no children.", + std::vector> + children; + float salary; +}; +``` + +```cpp +const std::string json_schema = rfl::json::to_schema(rfl::json::pretty); +``` + +```json +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$ref": "#/definitions/Person", + "definitions": { + "Person": { + "type": "object", + "properties": { + "children": { + "type": "array", + "description": "The person's children. Pass an empty array for no children.", + "items": { + "$ref": "#/definitions/Person" + } + }, + "email": { + "type": "string", + "description": "Must be a proper email in the form xxx@xxx.xxx.", + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" + }, + "first_name": { + "type": "string" + }, + "last_name": { + "type": "string" + }, + "salary": { + "type": "number" + } + }, + "required": [ + "children", + "email", + "first_name", + "last_name", + "salary" + ] + } + } +} +``` + +You also add a description to the entire JSON schema: + +```cpp +const std::string json_schema = rfl::json::to_schema< + rfl::Description<"JSON schema that describes the required " + "attributes for the person class.", + Person>>(rfl::json::pretty); +``` + +```json +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$ref": "#/definitions/Person", + "description": "JSON schema that describes the required attributes for the person class.", + "definitions": { + "Person": { + "type": "object", + "properties": { + "children": { + "type": "array", + "description": "The person's children. Pass an empty array for no children.", + "items": { + "$ref": "#/definitions/Person" + } + }, + "email": { + "type": "string", + "description": "Must be a proper email in the form xxx@xxx.xxx.", + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" + }, + "first_name": { + "type": "string" + }, + "last_name": { + "type": "string" + }, + "salary": { + "type": "number" + } + }, + "required": [ + "children", + "email", + "first_name", + "last_name", + "salary" + ] + } + } +} +``` From 1f2e216c7544805ab36e52cc366b50ca74f78f57 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Fri, 29 Mar 2024 11:59:09 +0100 Subject: [PATCH 04/33] Fixed typo --- docs/json_schema.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/json_schema.md b/docs/json_schema.md index 110c3da7..6c995155 100644 --- a/docs/json_schema.md +++ b/docs/json_schema.md @@ -69,7 +69,8 @@ This will result in the following JSON schema: ] } } -}``` +} +``` You can insert this into the tools mentioned above and see the generated code. From 1f0dcf591e17ee40ad714bf29ff98adaa352ba23 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Fri, 29 Mar 2024 12:24:43 +0100 Subject: [PATCH 05/33] Added TOML support --- .github/workflows/test.yaml | 6 +- .gitignore | 1 + CMakeLists.txt | 9 +- README.md | 7 +- docs/README.md | 6 +- docs/toml.md | 98 ++++++++++++++++ docs/yaml.md | 2 +- include/rfl/toml.hpp | 12 ++ include/rfl/toml/Parser.hpp | 15 +++ include/rfl/toml/Reader.hpp | 124 ++++++++++++++++++++ include/rfl/toml/Writer.hpp | 124 ++++++++++++++++++++ include/rfl/toml/load.hpp | 18 +++ include/rfl/toml/read.hpp | 40 +++++++ include/rfl/toml/save.hpp | 26 ++++ include/rfl/toml/write.hpp | 40 +++++++ tests/CMakeLists.txt | 4 + tests/toml/CMakeLists.txt | 8 ++ tests/toml/test_array.cpp | 40 +++++++ tests/toml/test_array.hpp | 4 + tests/toml/test_box.cpp | 50 ++++++++ tests/toml/test_box.hpp | 4 + tests/toml/test_custom_class1.cpp | 42 +++++++ tests/toml/test_custom_class1.hpp | 4 + tests/toml/test_custom_class3.cpp | 68 +++++++++++ tests/toml/test_custom_class3.hpp | 4 + tests/toml/test_custom_class4.cpp | 69 +++++++++++ tests/toml/test_custom_class4.hpp | 4 + tests/toml/test_custom_constructor.cpp | 57 +++++++++ tests/toml/test_custom_constructor.hpp | 4 + tests/toml/test_default_values.cpp | 32 +++++ tests/toml/test_default_values.hpp | 4 + tests/toml/test_deque.cpp | 33 ++++++ tests/toml/test_deque.hpp | 4 + tests/toml/test_enum.cpp | 29 +++++ tests/toml/test_enum.hpp | 4 + tests/toml/test_field_variant.cpp | 39 ++++++ tests/toml/test_field_variant.hpp | 4 + tests/toml/test_flag_enum.cpp | 39 ++++++ tests/toml/test_flag_enum.hpp | 4 + tests/toml/test_flag_enum_with_int.cpp | 38 ++++++ tests/toml/test_flag_enum_with_int.hpp | 4 + tests/toml/test_flatten.cpp | 38 ++++++ tests/toml/test_flatten.hpp | 4 + tests/toml/test_flatten_anonymous.cpp | 39 ++++++ tests/toml/test_flatten_anonymous.hpp | 4 + tests/toml/test_forward_list.cpp | 33 ++++++ tests/toml/test_forward_list.hpp | 4 + tests/toml/test_literal.cpp | 30 +++++ tests/toml/test_literal.hpp | 4 + tests/toml/test_literal_map.cpp | 28 +++++ tests/toml/test_literal_map.hpp | 4 + tests/toml/test_map.cpp | 32 +++++ tests/toml/test_map.hpp | 4 + tests/toml/test_map_with_key_validation.cpp | 33 ++++++ tests/toml/test_map_with_key_validation.hpp | 4 + tests/toml/test_monster_example.cpp | 64 ++++++++++ tests/toml/test_monster_example.hpp | 4 + tests/toml/test_readme_example.cpp | 51 ++++++++ tests/toml/test_readme_example.hpp | 4 + tests/toml/test_readme_example2.cpp | 26 ++++ tests/toml/test_readme_example2.hpp | 4 + tests/toml/test_ref.cpp | 49 ++++++++ tests/toml/test_ref.hpp | 4 + tests/toml/test_save_load.cpp | 70 +++++++++++ tests/toml/test_save_load.hpp | 4 + tests/toml/test_set.cpp | 30 +++++ tests/toml/test_set.hpp | 4 + tests/toml/test_size.cpp | 42 +++++++ tests/toml/test_size.hpp | 4 + tests/toml/test_string_map.cpp | 24 ++++ tests/toml/test_string_map.hpp | 4 + tests/toml/test_tagged_union.cpp | 36 ++++++ tests/toml/test_tagged_union.hpp | 4 + tests/toml/test_timestamp.cpp | 36 ++++++ tests/toml/test_timestamp.hpp | 4 + tests/toml/test_unique_ptr.cpp | 33 ++++++ tests/toml/test_unique_ptr.hpp | 4 + tests/toml/test_unique_ptr2.cpp | 48 ++++++++ tests/toml/test_unique_ptr2.hpp | 4 + tests/toml/test_variant.cpp | 36 ++++++ tests/toml/test_variant.hpp | 4 + tests/toml/test_wstring.cpp | 26 ++++ tests/toml/test_wstring.hpp | 3 + tests/toml/tests.cpp | 69 +++++++++++ tests/toml/write_and_read.hpp | 32 +++++ vcpkg.json | 4 + 86 files changed, 2109 insertions(+), 7 deletions(-) create mode 100644 docs/toml.md create mode 100644 include/rfl/toml.hpp create mode 100644 include/rfl/toml/Parser.hpp create mode 100644 include/rfl/toml/Reader.hpp create mode 100644 include/rfl/toml/Writer.hpp create mode 100644 include/rfl/toml/load.hpp create mode 100644 include/rfl/toml/read.hpp create mode 100644 include/rfl/toml/save.hpp create mode 100644 include/rfl/toml/write.hpp create mode 100644 tests/toml/CMakeLists.txt create mode 100644 tests/toml/test_array.cpp create mode 100644 tests/toml/test_array.hpp create mode 100644 tests/toml/test_box.cpp create mode 100644 tests/toml/test_box.hpp create mode 100644 tests/toml/test_custom_class1.cpp create mode 100644 tests/toml/test_custom_class1.hpp create mode 100644 tests/toml/test_custom_class3.cpp create mode 100644 tests/toml/test_custom_class3.hpp create mode 100644 tests/toml/test_custom_class4.cpp create mode 100644 tests/toml/test_custom_class4.hpp create mode 100644 tests/toml/test_custom_constructor.cpp create mode 100644 tests/toml/test_custom_constructor.hpp create mode 100644 tests/toml/test_default_values.cpp create mode 100644 tests/toml/test_default_values.hpp create mode 100644 tests/toml/test_deque.cpp create mode 100644 tests/toml/test_deque.hpp create mode 100644 tests/toml/test_enum.cpp create mode 100644 tests/toml/test_enum.hpp create mode 100644 tests/toml/test_field_variant.cpp create mode 100644 tests/toml/test_field_variant.hpp create mode 100644 tests/toml/test_flag_enum.cpp create mode 100644 tests/toml/test_flag_enum.hpp create mode 100644 tests/toml/test_flag_enum_with_int.cpp create mode 100644 tests/toml/test_flag_enum_with_int.hpp create mode 100644 tests/toml/test_flatten.cpp create mode 100644 tests/toml/test_flatten.hpp create mode 100644 tests/toml/test_flatten_anonymous.cpp create mode 100644 tests/toml/test_flatten_anonymous.hpp create mode 100644 tests/toml/test_forward_list.cpp create mode 100644 tests/toml/test_forward_list.hpp create mode 100644 tests/toml/test_literal.cpp create mode 100644 tests/toml/test_literal.hpp create mode 100644 tests/toml/test_literal_map.cpp create mode 100644 tests/toml/test_literal_map.hpp create mode 100644 tests/toml/test_map.cpp create mode 100644 tests/toml/test_map.hpp create mode 100644 tests/toml/test_map_with_key_validation.cpp create mode 100644 tests/toml/test_map_with_key_validation.hpp create mode 100644 tests/toml/test_monster_example.cpp create mode 100644 tests/toml/test_monster_example.hpp create mode 100644 tests/toml/test_readme_example.cpp create mode 100644 tests/toml/test_readme_example.hpp create mode 100644 tests/toml/test_readme_example2.cpp create mode 100644 tests/toml/test_readme_example2.hpp create mode 100644 tests/toml/test_ref.cpp create mode 100644 tests/toml/test_ref.hpp create mode 100644 tests/toml/test_save_load.cpp create mode 100644 tests/toml/test_save_load.hpp create mode 100644 tests/toml/test_set.cpp create mode 100644 tests/toml/test_set.hpp create mode 100644 tests/toml/test_size.cpp create mode 100644 tests/toml/test_size.hpp create mode 100644 tests/toml/test_string_map.cpp create mode 100644 tests/toml/test_string_map.hpp create mode 100644 tests/toml/test_tagged_union.cpp create mode 100644 tests/toml/test_tagged_union.hpp create mode 100644 tests/toml/test_timestamp.cpp create mode 100644 tests/toml/test_timestamp.hpp create mode 100644 tests/toml/test_unique_ptr.cpp create mode 100644 tests/toml/test_unique_ptr.hpp create mode 100644 tests/toml/test_unique_ptr2.cpp create mode 100644 tests/toml/test_unique_ptr2.hpp create mode 100644 tests/toml/test_variant.cpp create mode 100644 tests/toml/test_variant.hpp create mode 100644 tests/toml/test_wstring.cpp create mode 100644 tests/toml/test_wstring.hpp create mode 100644 tests/toml/tests.cpp create mode 100644 tests/toml/write_and_read.hpp diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5e53c216..e9851d07 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,12 +20,13 @@ jobs: - name: Run test run: | g++ --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests + ./build/tests/toml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/yaml/reflect-cpp-yaml-tests @@ -53,12 +54,13 @@ jobs: CXX: clang++ run: | clang --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests + ./build/tests/toml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/yaml/reflect-cpp-yaml-tests diff --git a/.gitignore b/.gitignore index 60e0ad5a..f05d838f 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ *.cbor *.json *.fb +*.toml *.xml *.yml *.yaml diff --git a/CMakeLists.txt b/CMakeLists.txt index 964b7f75..65a58e00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,12 +5,13 @@ option(REFLECTCPP_BSON "Enable BSON support" OFF) option(REFLECTCPP_CBOR "Enable CBOR support" OFF) option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) option(REFLECTCPP_XML "Enable XML support" OFF) +option(REFLECTCPP_TOML "Enable TOML support" OFF) option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) # enable vcpkg if require features other than JSON -if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML) +if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") endif () @@ -44,6 +45,12 @@ if (REFLECTCPP_FLEXBUFFERS) target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers) endif () +if (REFLECTCPP_TOML) + find_package(PkgConfig REQUIRED) + pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) + target_link_libraries(reflectcpp INTERFACE PkgConfig::tomlplusplus) +endif() + if (REFLECTCPP_XML) find_package(pugixml CONFIG REQUIRED) target_link_libraries(reflectcpp INTERFACE pugixml::pugixml) diff --git a/README.md b/README.md index 0c2d73f7..cc5ce0f9 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ The following table lists the serialization formats currently supported by refle | BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | JSON-like binary format | | CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format | | flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format | +| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability | | XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects | | YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability | @@ -95,11 +96,13 @@ and any supported format, except where explicitly noted otherwise: rfl::bson::write(homer); rfl::cbor::write(homer); rfl::flexbuf::write(homer); +rfl::toml::write(homer); rfl::xml::write(homer); rfl::bson::read(bson_bytes); rfl::cbor::read(cbor_bytes); rfl::flexbuf::read(flexbuf_bytes); +rfl::toml::read(toml_string); rfl::xml::read(xml_string); ``` @@ -504,6 +507,7 @@ add_subdirectory(reflect-cpp) # Add this project as a subdirectory set(REFLECTCPP_BSON ON) # Optional set(REFLECTCPP_CBOR ON) # Optional set(REFLECTCPP_FLEXBUFFERS ON) # Optional +set(REFLECTCPP_TOML ON) # Optional set(REFLECTCPP_XML ON) # Optional set(REFLECTCPP_YAML ON) # Optional @@ -549,7 +553,7 @@ git submodule update --init ./vcpkg/bootstrap-vcpkg.bat # Windows # You may be prompted to install additional dependencies. -cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release +cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 # gcc, clang cmake --build build --config Release -j 4 # MSVC ``` @@ -561,6 +565,7 @@ To run the tests, do the following: ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests +./build/tests/xml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/xml/reflect-cpp-yaml-tests ``` diff --git a/docs/README.md b/docs/README.md index 158e5ee0..de570bb9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -64,9 +64,11 @@ 5.4) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md) -5.5) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) +5.5) [TOML](https://github.com/getml/reflect-cpp/blob/main/docs/toml.md) -5.6) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) +5.6) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) + +5.7) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) ## 6) Advanced topics diff --git a/docs/toml.md b/docs/toml.md new file mode 100644 index 00000000..52d05ff8 --- /dev/null +++ b/docs/toml.md @@ -0,0 +1,98 @@ +# TOML + +For TOML support, you must also include the header `` and include the toml++ library (https://github.com/marzer/tomlplusplus) in your project. + +## Reading and writing + +Suppose you have a struct like this: + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + std::vector children; +}; +``` + +You can parse TOML strings like this: + +```cpp +const rfl::Result result = rfl::toml::read(toml_string); +``` + +A `person` can be serialized like this: + +```cpp +const auto person = Person{...}; +const std::string toml_string = rfl::toml::write(person); +``` + +## Loading and saving + +You can also load and save to disc using a very similar syntax: + +```cpp +const rfl::Result result = rfl::toml::load("/path/to/file.toml"); + +const auto person = Person{...}; +rfl::toml::save("/path/to/file.toml", person); +``` + +## Reading from and writing into streams + +You can also read from and write into any `std::istream` and `std::ostream` respectively. + +```cpp +const rfl::Result result = rfl::toml::read(my_istream); + +const auto person = Person{...}; +rfl::toml::write(person, my_ostream); +``` + +Note that `std::cout` is also an ostream, so this works as well: + +```cpp +rfl::toml::write(person, std::cout) << std::endl; +``` + +## Custom constructors + +One of the great things about C++ is that it gives you control over +when and how you code is compiled. + +For large and complex systems of structs, it is often a good idea to split up +your code into smaller compilation units. You can do so using custom constructors. + +For the TOML format, these must be a static function on your struct or class called +`from_toml_obj` that take a `rfl::toml::Reader::InputVarType` as input and return +the class or the class wrapped in `rfl::Result`. + +In your header file you can write something like this: + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + + using TOMLVar = typename rfl::toml::Reader::InputVarType; + static rfl::Result from_toml_obj(const TOMLVar& _obj); +}; +``` + +And in your source file, you implement `from_toml_obj` as follows: + +```cpp +rfl::Result Person::from_toml_obj(const TOMLVar& _obj) { + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::toml::read>(_obj) + .transform(from_nt); +} +``` + +This will force the compiler to only compile the TOML parsing when the +source file is compiled. + diff --git a/docs/yaml.md b/docs/yaml.md index c180d681..189caa88 100644 --- a/docs/yaml.md +++ b/docs/yaml.md @@ -21,7 +21,7 @@ You can parse YAML strings like this: const rfl::Result result = rfl::yaml::read(yaml_string); ``` -A person can be turned into a YAML string like this: +A `person` can be serialized like this: ```cpp const auto person = Person{...}; diff --git a/include/rfl/toml.hpp b/include/rfl/toml.hpp new file mode 100644 index 00000000..15b1451e --- /dev/null +++ b/include/rfl/toml.hpp @@ -0,0 +1,12 @@ +#ifndef RFL_TOML_HPP_ +#define RFL_TOML_HPP_ + +#include "toml/Parser.hpp" +#include "toml/Reader.hpp" +#include "toml/Writer.hpp" +#include "toml/load.hpp" +#include "toml/read.hpp" +#include "toml/save.hpp" +#include "toml/write.hpp" + +#endif diff --git a/include/rfl/toml/Parser.hpp b/include/rfl/toml/Parser.hpp new file mode 100644 index 00000000..d550fe5c --- /dev/null +++ b/include/rfl/toml/Parser.hpp @@ -0,0 +1,15 @@ +#ifndef RFL_TOML_PARSER_HPP_ +#define RFL_TOML_PARSER_HPP_ + +#include "../parsing/Parser.hpp" +#include "Reader.hpp" +#include "Writer.hpp" + +namespace rfl::toml { + +template +using Parser = parsing::Parser; + +} // namespace rfl::toml + +#endif diff --git a/include/rfl/toml/Reader.hpp b/include/rfl/toml/Reader.hpp new file mode 100644 index 00000000..d8002087 --- /dev/null +++ b/include/rfl/toml/Reader.hpp @@ -0,0 +1,124 @@ +#ifndef RFL_TOML_READER_HPP_ +#define RFL_TOML_READER_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl::toml { + +struct Reader { + using InputArrayType = ::toml::array*; + using InputObjectType = ::toml::table*; + using InputVarType = ::toml::node*; + + template + static constexpr bool has_custom_constructor = (requires(InputVarType var) { + T::from_toml_obj(var); + }); + + rfl::Result get_field( + const std::string& _name, const InputObjectType& _obj) const noexcept { + auto var = (*_obj)[_name]; + if (!var) { + return rfl::Error("Object contains no field named '" + _name + "'."); + } + return var.node(); + } + + bool is_empty(const InputVarType& _var) const noexcept { + return !_var && true; + } + + template + rfl::Result to_basic_type(const InputVarType& _var) const noexcept { + if constexpr (std::is_same, std::string>()) { + const auto ptr = _var->as(); + if (!ptr) { + return Error("Could not cast the node to std::string!"); + } + return **ptr; + } else if constexpr (std::is_same, bool>()) { + const auto ptr = _var->as(); + if (!ptr) { + return Error("Could not cast the node to bool!"); + } + return **ptr; + } else if constexpr (std::is_floating_point>()) { + const auto ptr = _var->as(); + if (!ptr) { + return Error("Could not cast the node to double!"); + } + return static_cast>(**ptr); + } else if constexpr (std::is_integral>()) { + const auto ptr = _var->as(); + if (!ptr) { + return Error("Could not cast the node to int64_t!"); + } + return static_cast>(**ptr); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + } + + rfl::Result to_array( + const InputVarType& _var) const noexcept { + const auto ptr = _var->as_array(); + if (!ptr) { + return rfl::Error("Could not cast to an array!"); + } + return ptr; + } + + template + std::optional read_object(const ObjectReader& _object_reader, + InputObjectType _obj) const noexcept { + for (auto& [k, v] : *_obj) { + _object_reader.read(std::string_view(k), &v); + } + return std::nullopt; + } + + rfl::Result to_object( + const InputVarType& _var) const noexcept { + const auto ptr = _var->as_table(); + if (!ptr) { + return rfl::Error("Could not cast to a table!"); + } + return ptr; + } + + std::vector to_vec(const InputArrayType& _arr) const noexcept { + std::vector vec; + for (auto& node : *_arr) { + vec.push_back(&node); + } + return vec; + } + + template + rfl::Result use_custom_constructor( + const InputVarType _var) const noexcept { + try { + return T::from_toml_obj(_var); + } catch (std::exception& e) { + return rfl::Error(e.what()); + } + } +}; + +} // namespace rfl::toml + +#endif diff --git a/include/rfl/toml/Writer.hpp b/include/rfl/toml/Writer.hpp new file mode 100644 index 00000000..d43b155e --- /dev/null +++ b/include/rfl/toml/Writer.hpp @@ -0,0 +1,124 @@ +#ifndef RFL_TOML_WRITER_HPP_ +#define RFL_TOML_WRITER_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Ref.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl::toml { + +class Writer { + public: + struct TOMLArray { + ::toml::array* val_; + }; + + struct TOMLObject { + ::toml::table* val_; + }; + + struct TOMLVar {}; + + using OutputArrayType = TOMLArray; + using OutputObjectType = TOMLObject; + using OutputVarType = TOMLVar; + + Writer(::toml::table* _root) : root_(_root) {} + + ~Writer() = default; + + template + OutputArrayType array_as_root(const T _size) const noexcept { + static_assert(rfl::always_false_v, + "TOML only allows tables as the root element."); + return OutputArrayType{nullptr}; + } + + OutputObjectType object_as_root(const size_t _size) const noexcept { + return OutputObjectType{root_}; + } + + OutputVarType null_as_root() const noexcept { return OutputVarType{}; } + + template + OutputVarType value_as_root(const T& _var) const noexcept { + static_assert(rfl::always_false_v, + "TOML only allows tables as the root element."); + return OutputVarType{}; + } + + OutputArrayType add_array_to_array(const size_t _size, + OutputArrayType* _parent) const noexcept { + const auto i = _parent->val_->size(); + _parent->val_->push_back(::toml::array()); + return OutputArrayType{_parent->val_->at(i).as_array()}; + } + + OutputArrayType add_array_to_object( + const std::string_view& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + _parent->val_->emplace(_name, ::toml::array()); + return OutputArrayType{_parent->val_->at_path(_name).as_array()}; + } + + OutputObjectType add_object_to_array( + const size_t _size, OutputArrayType* _parent) const noexcept { + const auto i = _parent->val_->size(); + _parent->val_->push_back(::toml::table()); + return OutputObjectType{_parent->val_->at(i).as_table()}; + } + + OutputObjectType add_object_to_object( + const std::string_view& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + _parent->val_->emplace(_name, ::toml::table()); + return OutputObjectType{_parent->val_->at_path(_name).as_table()}; + } + + template + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + _parent->val_->push_back(::toml::value(_var)); + return OutputVarType{}; + } + + template + OutputVarType add_value_to_object(const std::string_view& _name, + const T& _var, + OutputObjectType* _parent) const noexcept { + _parent->val_->emplace(_name, ::toml::value(_var)); + return OutputVarType{}; + } + + OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept { + _parent->val_->push_back(std::string("")); + return OutputVarType{}; + } + + OutputVarType add_null_to_object(const std::string_view& _name, + OutputObjectType* _parent) const noexcept { + _parent->val_->emplace(_name, ::toml::value(std::string(""))); + return OutputVarType{}; + } + + void end_array(OutputArrayType* _arr) const noexcept {} + + void end_object(OutputObjectType* _obj) const noexcept {} + + private: + ::toml::table* root_; +}; + +} // namespace rfl::toml + +#endif // JSON_PARSER_HPP_ diff --git a/include/rfl/toml/load.hpp b/include/rfl/toml/load.hpp new file mode 100644 index 00000000..f620c87b --- /dev/null +++ b/include/rfl/toml/load.hpp @@ -0,0 +1,18 @@ +#ifndef RFL_TOML_LOAD_HPP_ +#define RFL_TOML_LOAD_HPP_ + +#include "../Result.hpp" +#include "../io/load_string.hpp" +#include "read.hpp" + +namespace rfl::toml { + +template +Result load(const std::string& _fname) { + const auto read_string = [](const auto& _str) { return read(_str); }; + return rfl::io::load_string(_fname).and_then(read_string); +} + +} // namespace rfl::toml + +#endif diff --git a/include/rfl/toml/read.hpp b/include/rfl/toml/read.hpp new file mode 100644 index 00000000..d92617b4 --- /dev/null +++ b/include/rfl/toml/read.hpp @@ -0,0 +1,40 @@ +#ifndef RFL_TOML_READ_HPP_ +#define RFL_TOML_READ_HPP_ + +#include +#include +#include + +#include "../internal/wrap_in_rfl_array_t.hpp" +#include "Parser.hpp" +#include "Reader.hpp" + +namespace rfl::toml { + +using InputVarType = typename Reader::InputVarType; + +/// Parses an object from a TOML var. +template +auto read(InputVarType _var) { + const auto r = Reader(); + return Parser::read(r, _var); +} + +/// Parses an object from TOML using reflection. +template +Result> read(const std::string& _toml_str) { + auto table = ::toml::parse(_toml_str); + return read(&table); +} + +/// Parses an object from a stringstream. +template +auto read(std::istream& _stream) { + const auto toml_str = std::string(std::istreambuf_iterator(_stream), + std::istreambuf_iterator()); + return read(toml_str); +} + +} // namespace rfl::toml + +#endif diff --git a/include/rfl/toml/save.hpp b/include/rfl/toml/save.hpp new file mode 100644 index 00000000..0b77d9aa --- /dev/null +++ b/include/rfl/toml/save.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_TOML_SAVE_HPP_ +#define RFL_TOML_SAVE_HPP_ + +#include +#include +#include + +#include "../Result.hpp" +#include "../io/save_string.hpp" +#include "write.hpp" + +namespace rfl { +namespace toml { + +template +Result save(const std::string& _fname, const T& _obj) { + const auto write_func = [](const auto& _obj, auto& _stream) -> auto& { + return write(_obj, _stream); + }; + return rfl::io::save_string(_fname, _obj, write_func); +} + +} // namespace toml +} // namespace rfl + +#endif diff --git a/include/rfl/toml/write.hpp b/include/rfl/toml/write.hpp new file mode 100644 index 00000000..3a684f78 --- /dev/null +++ b/include/rfl/toml/write.hpp @@ -0,0 +1,40 @@ +#ifndef RFL_TOML_WRITE_HPP_ +#define RFL_TOML_WRITE_HPP_ + +#include +#include +#include +#include +#include + +#include "../parsing/Parent.hpp" +#include "Parser.hpp" + +namespace rfl::toml { + +/// Writes a TOML into an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) { + using ParentType = parsing::Parent; + ::toml::table root; + auto w = Writer(&root); + Parser::write(w, _obj, typename ParentType::Root{}); + _stream << root; + return _stream; +} + +/// Returns a TOML string. +template +std::string write(const T& _obj) { + using ParentType = parsing::Parent; + std::stringstream sstream; + ::toml::table root; + auto w = Writer(&root); + Parser::write(w, _obj, typename ParentType::Root{}); + sstream << root; + return sstream.str(); +} + +} // namespace rfl::toml + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 152894e0..905ffead 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,6 +24,10 @@ if (REFLECTCPP_XML) add_subdirectory(xml) endif() +if (REFLECTCPP_TOML) + add_subdirectory(toml) +endif() + if (REFLECTCPP_YAML) add_subdirectory(yaml) endif() diff --git a/tests/toml/CMakeLists.txt b/tests/toml/CMakeLists.txt new file mode 100644 index 00000000..8e85ac23 --- /dev/null +++ b/tests/toml/CMakeLists.txt @@ -0,0 +1,8 @@ +project(reflect-cpp-toml-tests) + +file(GLOB_RECURSE SOURCES "*.cpp") + +add_executable(reflect-cpp-toml-tests ${SOURCES}) + +target_link_libraries(reflect-cpp-toml-tests PRIVATE reflectcpp) + diff --git a/tests/toml/test_array.cpp b/tests/toml/test_array.cpp new file mode 100644 index 00000000..f3f10206 --- /dev/null +++ b/tests/toml/test_array.cpp @@ -0,0 +1,40 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/yaml.hpp. +#include + +#include "write_and_read.hpp" + +namespace test_array { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = Person{ + .first_name = "Homer", + .children = std::make_unique>(std::array{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read(homer); +} +} // namespace test_array diff --git a/tests/toml/test_array.hpp b/tests/toml/test_array.hpp new file mode 100644 index 00000000..502c3388 --- /dev/null +++ b/tests/toml/test_array.hpp @@ -0,0 +1,4 @@ +namespace test_array{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_box.cpp b/tests/toml/test_box.cpp new file mode 100644 index 00000000..04d88641 --- /dev/null +++ b/tests/toml/test_box.cpp @@ -0,0 +1,50 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_box { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Box lesser; + rfl::Box greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_box diff --git a/tests/toml/test_box.hpp b/tests/toml/test_box.hpp new file mode 100644 index 00000000..a564b9e1 --- /dev/null +++ b/tests/toml/test_box.hpp @@ -0,0 +1,4 @@ +namespace test_box{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_custom_class1.cpp b/tests/toml/test_custom_class1.cpp new file mode 100644 index 00000000..35f4a1da --- /dev/null +++ b/tests/toml/test_custom_class1.cpp @@ -0,0 +1,42 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class1 { + +struct Person { + struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart"); + + write_and_read(bart); +} +} // namespace test_custom_class1 diff --git a/tests/toml/test_custom_class1.hpp b/tests/toml/test_custom_class1.hpp new file mode 100644 index 00000000..eafe6cd0 --- /dev/null +++ b/tests/toml/test_custom_class1.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class1{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_custom_class3.cpp b/tests/toml/test_custom_class3.cpp new file mode 100644 index 00000000..2ea105e4 --- /dev/null +++ b/tests/toml/test_custom_class3.cpp @@ -0,0 +1,68 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class3 { + +struct Person { + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + std::string last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + int age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = _p.last_name(), + .age = _p.age()}; + } + + Person to_class() const { return Person(first_name(), last_name(), age); } +}; +} // namespace test_custom_class3 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class3 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart", "Simpson", 10); + + write_and_read(bart); +} + +} // namespace test_custom_class3 diff --git a/tests/toml/test_custom_class3.hpp b/tests/toml/test_custom_class3.hpp new file mode 100644 index 00000000..9a6fdab4 --- /dev/null +++ b/tests/toml/test_custom_class3.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class3{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_custom_class4.cpp b/tests/toml/test_custom_class4.cpp new file mode 100644 index 00000000..e2d79a94 --- /dev/null +++ b/tests/toml/test_custom_class4.cpp @@ -0,0 +1,69 @@ +#include "test_custom_class4.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class4 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class4::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart); +} +} // namespace test_custom_class4 diff --git a/tests/toml/test_custom_class4.hpp b/tests/toml/test_custom_class4.hpp new file mode 100644 index 00000000..2d3b151a --- /dev/null +++ b/tests/toml/test_custom_class4.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class4{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_custom_constructor.cpp b/tests/toml/test_custom_constructor.cpp new file mode 100644 index 00000000..03123d2a --- /dev/null +++ b/tests/toml/test_custom_constructor.cpp @@ -0,0 +1,57 @@ +#include "test_custom_constructor.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_constructor { + +struct Person { + static rfl::Result from_toml_obj( + typename rfl::toml::Reader::InputVarType _obj) { + /// This only exists for the purpose of the test. + const auto change_first_name = [](auto&& _person) { + return rfl::replace(std::move(_person), + rfl::Field<"firstName", std::string>("Bart")); + }; + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::toml::read>(_obj) + .transform(from_nt) + .transform(change_first_name); + } + + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto res = rfl::toml::read( + R"(firstName = 'Homer' +lastName = 'Simpson' +birthday = '1987-04-19')"); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl; + return; + } + + if (res.value().first_name() != "Bart") { + std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" + << std::endl + << std::endl; + } else { + std::cout << "OK" << std::endl << std::endl; + } +} +} // namespace test_custom_constructor diff --git a/tests/toml/test_custom_constructor.hpp b/tests/toml/test_custom_constructor.hpp new file mode 100644 index 00000000..d3b5b950 --- /dev/null +++ b/tests/toml/test_custom_constructor.hpp @@ -0,0 +1,4 @@ +namespace test_custom_constructor { +void test(); +} + diff --git a/tests/toml/test_default_values.cpp b/tests/toml/test_default_values.cpp new file mode 100644 index 00000000..f6af2352 --- /dev/null +++ b/tests/toml/test_default_values.cpp @@ -0,0 +1,32 @@ +#include "test_default_values.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_default_values { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart"}; + const auto lisa = Person{.first_name = "Lisa"}; + const auto maggie = Person{.first_name = "Maggie"}; + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_default_values diff --git a/tests/toml/test_default_values.hpp b/tests/toml/test_default_values.hpp new file mode 100644 index 00000000..c8f8360e --- /dev/null +++ b/tests/toml/test_default_values.hpp @@ -0,0 +1,4 @@ +namespace test_default_values{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_deque.cpp b/tests/toml/test_deque.cpp new file mode 100644 index 00000000..6dfa5803 --- /dev/null +++ b/tests/toml/test_deque.cpp @@ -0,0 +1,33 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deque { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_deque diff --git a/tests/toml/test_deque.hpp b/tests/toml/test_deque.hpp new file mode 100644 index 00000000..6781e880 --- /dev/null +++ b/tests/toml/test_deque.hpp @@ -0,0 +1,4 @@ +namespace test_deque{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_enum.cpp b/tests/toml/test_enum.cpp new file mode 100644 index 00000000..d4d56a8b --- /dev/null +++ b/tests/toml/test_enum.cpp @@ -0,0 +1,29 @@ +#include "test_enum.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle); +} + +} // namespace test_enum diff --git a/tests/toml/test_enum.hpp b/tests/toml/test_enum.hpp new file mode 100644 index 00000000..2e2e0b3d --- /dev/null +++ b/tests/toml/test_enum.hpp @@ -0,0 +1,4 @@ +namespace test_enum { +void test(); +} + diff --git a/tests/toml/test_field_variant.cpp b/tests/toml/test_field_variant.cpp new file mode 100644 index 00000000..8b17665f --- /dev/null +++ b/tests/toml/test_field_variant.cpp @@ -0,0 +1,39 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_field_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::Variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r); +} +} // namespace test_field_variant diff --git a/tests/toml/test_field_variant.hpp b/tests/toml/test_field_variant.hpp new file mode 100644 index 00000000..ba93e732 --- /dev/null +++ b/tests/toml/test_field_variant.hpp @@ -0,0 +1,4 @@ +namespace test_field_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_flag_enum.cpp b/tests/toml/test_flag_enum.cpp new file mode 100644 index 00000000..1d3f76af --- /dev/null +++ b/tests/toml/test_flag_enum.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = + Circle{.radius = 2.0, .color = Color::blue | Color::orange}; + + write_and_read(circle); +} + +} // namespace test_flag_enum diff --git a/tests/toml/test_flag_enum.hpp b/tests/toml/test_flag_enum.hpp new file mode 100644 index 00000000..2f4dc7a0 --- /dev/null +++ b/tests/toml/test_flag_enum.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum { +void test(); +} + diff --git a/tests/toml/test_flag_enum_with_int.cpp b/tests/toml/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..4c7deefe --- /dev/null +++ b/tests/toml/test_flag_enum_with_int.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum_with_int { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/toml/test_flag_enum_with_int.hpp b/tests/toml/test_flag_enum_with_int.hpp new file mode 100644 index 00000000..a7512b60 --- /dev/null +++ b/tests/toml/test_flag_enum_with_int.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum_with_int { +void test(); +} + diff --git a/tests/toml/test_flatten.cpp b/tests/toml/test_flatten.cpp new file mode 100644 index 00000000..c7f1d9be --- /dev/null +++ b/tests/toml/test_flatten.cpp @@ -0,0 +1,38 @@ +#include "test_flatten.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten diff --git a/tests/toml/test_flatten.hpp b/tests/toml/test_flatten.hpp new file mode 100644 index 00000000..24d60e11 --- /dev/null +++ b/tests/toml/test_flatten.hpp @@ -0,0 +1,4 @@ +namespace test_flatten{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_flatten_anonymous.cpp b/tests/toml/test_flatten_anonymous.cpp new file mode 100644 index 00000000..05e61464 --- /dev/null +++ b/tests/toml/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box employer; + float salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} + +} // namespace test_flatten_anonymous diff --git a/tests/toml/test_flatten_anonymous.hpp b/tests/toml/test_flatten_anonymous.hpp new file mode 100644 index 00000000..7ffa2785 --- /dev/null +++ b/tests/toml/test_flatten_anonymous.hpp @@ -0,0 +1,4 @@ +namespace test_flatten_anonymous{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_forward_list.cpp b/tests/toml/test_forward_list.cpp new file mode 100644 index 00000000..dd8d9e56 --- /dev/null +++ b/tests/toml/test_forward_list.cpp @@ -0,0 +1,33 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_forward_list { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_front(Person{.first_name = "Maggie"}); + children->emplace_front(Person{.first_name = "Lisa"}); + children->emplace_front(Person{.first_name = "Bart"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_forward_list diff --git a/tests/toml/test_forward_list.hpp b/tests/toml/test_forward_list.hpp new file mode 100644 index 00000000..9784a0c4 --- /dev/null +++ b/tests/toml/test_forward_list.hpp @@ -0,0 +1,4 @@ +namespace test_forward_list{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_literal.cpp b/tests/toml/test_literal.cpp new file mode 100644 index 00000000..00578178 --- /dev/null +++ b/tests/toml/test_literal.cpp @@ -0,0 +1,30 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal { + +using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; +using LastName = rfl::Literal<"Simpson">; + +struct Person { + rfl::Rename<"firstName", FirstName> first_name; + rfl::Rename<"lastName", LastName> last_name; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart); +} +} // namespace test_literal diff --git a/tests/toml/test_literal.hpp b/tests/toml/test_literal.hpp new file mode 100644 index 00000000..ccd500ef --- /dev/null +++ b/tests/toml/test_literal.hpp @@ -0,0 +1,4 @@ +namespace test_literal{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_literal_map.cpp b/tests/toml/test_literal_map.cpp new file mode 100644 index 00000000..9bbebafb --- /dev/null +++ b/tests/toml/test_literal_map.cpp @@ -0,0 +1,28 @@ +#include "test_literal_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_literal_map diff --git a/tests/toml/test_literal_map.hpp b/tests/toml/test_literal_map.hpp new file mode 100644 index 00000000..cc05d0c8 --- /dev/null +++ b/tests/toml/test_literal_map.hpp @@ -0,0 +1,4 @@ +namespace test_literal_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_map.cpp b/tests/toml/test_map.cpp new file mode 100644 index 00000000..e37ce564 --- /dev/null +++ b/tests/toml/test_map.cpp @@ -0,0 +1,32 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::map children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map diff --git a/tests/toml/test_map.hpp b/tests/toml/test_map.hpp new file mode 100644 index 00000000..9ae49728 --- /dev/null +++ b/tests/toml/test_map.hpp @@ -0,0 +1,4 @@ +namespace test_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_map_with_key_validation.cpp b/tests/toml/test_map_with_key_validation.cpp new file mode 100644 index 00000000..e758219d --- /dev/null +++ b/tests/toml/test_map_with_key_validation.cpp @@ -0,0 +1,33 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + + children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/toml/test_map_with_key_validation.hpp b/tests/toml/test_map_with_key_validation.hpp new file mode 100644 index 00000000..1372f926 --- /dev/null +++ b/tests/toml/test_map_with_key_validation.hpp @@ -0,0 +1,4 @@ +namespace test_map_with_key_validation{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_monster_example.cpp b/tests/toml/test_monster_example.cpp new file mode 100644 index 00000000..052d8390 --- /dev/null +++ b/tests/toml/test_monster_example.cpp @@ -0,0 +1,64 @@ +#include "test_monster_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_monster_example diff --git a/tests/toml/test_monster_example.hpp b/tests/toml/test_monster_example.hpp new file mode 100644 index 00000000..f2d959fc --- /dev/null +++ b/tests/toml/test_monster_example.hpp @@ -0,0 +1,4 @@ +namespace test_monster_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_readme_example.cpp b/tests/toml/test_readme_example.cpp new file mode 100644 index 00000000..4e6f8ee0 --- /dev/null +++ b/tests/toml/test_readme_example.cpp @@ -0,0 +1,51 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector child; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/toml/test_readme_example.hpp b/tests/toml/test_readme_example.hpp new file mode 100644 index 00000000..68c6cf81 --- /dev/null +++ b/tests/toml/test_readme_example.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_readme_example2.cpp b/tests/toml/test_readme_example2.cpp new file mode 100644 index 00000000..a2d212a4 --- /dev/null +++ b/tests/toml/test_readme_example2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#include "test_readme_example.hpp" +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/toml/test_readme_example2.hpp b/tests/toml/test_readme_example2.hpp new file mode 100644 index 00000000..5c6b011c --- /dev/null +++ b/tests/toml/test_readme_example2.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example2 { +void test(); +} + diff --git a/tests/toml/test_ref.cpp b/tests/toml/test_ref.cpp new file mode 100644 index 00000000..32a32e6c --- /dev/null +++ b/tests/toml/test_ref.cpp @@ -0,0 +1,49 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Ref lesser; + rfl::Ref greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_ref diff --git a/tests/toml/test_ref.hpp b/tests/toml/test_ref.hpp new file mode 100644 index 00000000..d289ba09 --- /dev/null +++ b/tests/toml/test_ref.hpp @@ -0,0 +1,4 @@ +namespace test_ref{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_save_load.cpp b/tests/toml/test_save_load.cpp new file mode 100644 index 00000000..fb14c76f --- /dev/null +++ b/tests/toml/test_save_load.cpp @@ -0,0 +1,70 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::toml::save("homer.toml", homer1); + + const auto homer2 = rfl::toml::load("homer.toml").value(); + + const auto string1 = rfl::toml::write(homer1); + const auto string2 = rfl::toml::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/toml/test_save_load.hpp b/tests/toml/test_save_load.hpp new file mode 100644 index 00000000..7bf10359 --- /dev/null +++ b/tests/toml/test_save_load.hpp @@ -0,0 +1,4 @@ +namespace test_save_load { +void test(); +} + diff --git a/tests/toml/test_set.cpp b/tests/toml/test_set.cpp new file mode 100644 index 00000000..9f2516ce --- /dev/null +++ b/tests/toml/test_set.cpp @@ -0,0 +1,30 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_set diff --git a/tests/toml/test_set.hpp b/tests/toml/test_set.hpp new file mode 100644 index 00000000..142a663b --- /dev/null +++ b/tests/toml/test_set.hpp @@ -0,0 +1,4 @@ +namespace test_set{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_size.cpp b/tests/toml/test_size.cpp new file mode 100644 index 00000000..c37b9bd8 --- /dev/null +++ b/tests/toml/test_size.cpp @@ -0,0 +1,42 @@ +#include "test_size.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_size diff --git a/tests/toml/test_size.hpp b/tests/toml/test_size.hpp new file mode 100644 index 00000000..be330df0 --- /dev/null +++ b/tests/toml/test_size.hpp @@ -0,0 +1,4 @@ +namespace test_size{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_string_map.cpp b/tests/toml/test_string_map.cpp new file mode 100644 index 00000000..cb4fb903 --- /dev/null +++ b/tests/toml/test_string_map.cpp @@ -0,0 +1,24 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_string_map diff --git a/tests/toml/test_string_map.hpp b/tests/toml/test_string_map.hpp new file mode 100644 index 00000000..94cb975a --- /dev/null +++ b/tests/toml/test_string_map.hpp @@ -0,0 +1,4 @@ +namespace test_string_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_tagged_union.cpp b/tests/toml/test_tagged_union.cpp new file mode 100644 index 00000000..a9dd1abc --- /dev/null +++ b/tests/toml/test_tagged_union.cpp @@ -0,0 +1,36 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_tagged_union diff --git a/tests/toml/test_tagged_union.hpp b/tests/toml/test_tagged_union.hpp new file mode 100644 index 00000000..5d522ff9 --- /dev/null +++ b/tests/toml/test_tagged_union.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_timestamp.cpp b/tests/toml/test_timestamp.cpp new file mode 100644 index 00000000..8af261d1 --- /dev/null +++ b/tests/toml/test_timestamp.cpp @@ -0,0 +1,36 @@ +#include "test_timestamp.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart); +} +} // namespace test_timestamp diff --git a/tests/toml/test_timestamp.hpp b/tests/toml/test_timestamp.hpp new file mode 100644 index 00000000..891d89b9 --- /dev/null +++ b/tests/toml/test_timestamp.hpp @@ -0,0 +1,4 @@ +namespace test_timestamp{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_unique_ptr.cpp b/tests/toml/test_unique_ptr.cpp new file mode 100644 index 00000000..b1833533 --- /dev/null +++ b/tests/toml/test_unique_ptr.cpp @@ -0,0 +1,33 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_unique_ptr diff --git a/tests/toml/test_unique_ptr.hpp b/tests/toml/test_unique_ptr.hpp new file mode 100644 index 00000000..428ea2a2 --- /dev/null +++ b/tests/toml/test_unique_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_unique_ptr2.cpp b/tests/toml/test_unique_ptr2.cpp new file mode 100644 index 00000000..e3007267 --- /dev/null +++ b/tests/toml/test_unique_ptr2.cpp @@ -0,0 +1,48 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_unique_ptr2 diff --git a/tests/toml/test_unique_ptr2.hpp b/tests/toml/test_unique_ptr2.hpp new file mode 100644 index 00000000..74adc170 --- /dev/null +++ b/tests/toml/test_unique_ptr2.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr2{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_variant.cpp b/tests/toml/test_variant.cpp new file mode 100644 index 00000000..cc3d6cbe --- /dev/null +++ b/tests/toml/test_variant.cpp @@ -0,0 +1,36 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = std::variant>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_variant diff --git a/tests/toml/test_variant.hpp b/tests/toml/test_variant.hpp new file mode 100644 index 00000000..0e58ce71 --- /dev/null +++ b/tests/toml/test_variant.hpp @@ -0,0 +1,4 @@ +namespace test_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/toml/test_wstring.cpp b/tests/toml/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/toml/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/toml/test_wstring.hpp b/tests/toml/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/toml/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/toml/tests.cpp b/tests/toml/tests.cpp new file mode 100644 index 00000000..7a38771d --- /dev/null +++ b/tests/toml/tests.cpp @@ -0,0 +1,69 @@ +#include "test_array.hpp" +#include "test_box.hpp" +#include "test_custom_class1.hpp" +#include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" +#include "test_custom_constructor.hpp" +#include "test_default_values.hpp" +#include "test_deque.hpp" +#include "test_enum.hpp" +#include "test_field_variant.hpp" +#include "test_flag_enum.hpp" +#include "test_flag_enum_with_int.hpp" +#include "test_flatten.hpp" +#include "test_flatten_anonymous.hpp" +#include "test_forward_list.hpp" +#include "test_literal.hpp" +#include "test_literal_map.hpp" +#include "test_map.hpp" +#include "test_map_with_key_validation.hpp" +#include "test_monster_example.hpp" +#include "test_readme_example.hpp" +#include "test_readme_example2.hpp" +#include "test_ref.hpp" +#include "test_save_load.hpp" +#include "test_set.hpp" +#include "test_size.hpp" +#include "test_tagged_union.hpp" +#include "test_timestamp.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" +#include "test_wstring.hpp" + +int main() { + test_readme_example::test(); + test_readme_example2::test(); + test_flatten::test(); + test_flatten_anonymous::test(); + test_enum::test(); + test_flag_enum::test(); + test_flag_enum_with_int::test(); + test_map::test(); + test_map_with_key_validation::test(); + test_variant::test(); + test_field_variant::test(); + test_tagged_union::test(); + test_deque::test(); + test_forward_list::test(); + test_literal_map::test(); + test_literal::test(); + test_monster_example::test(); + test_ref::test(); + test_set::test(); + test_size::test(); + test_timestamp::test(); + test_unique_ptr::test(); + test_unique_ptr2::test(); + test_array::test(); + test_box::test(); + test_custom_class1::test(); + test_custom_class3::test(); + test_custom_class4::test(); + test_default_values::test(); + test_custom_constructor::test(); + test_save_load::test(); + test_wstring::test(); + + return 0; +} diff --git a/tests/toml/write_and_read.hpp b/tests/toml/write_and_read.hpp new file mode 100644 index 00000000..8b0d7cac --- /dev/null +++ b/tests/toml/write_and_read.hpp @@ -0,0 +1,32 @@ +#ifndef WRITE_AND_READ_ +#define WRITE_AND_READ_ + +#include +#include +#include + +template +void write_and_read(const T& _struct) { + const auto toml_string1 = rfl::toml::write(_struct); + const auto res = rfl::toml::read(toml_string1); + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl + << "Original string: " << toml_string1 << std::endl + << std::endl; + return; + } + const auto toml_string2 = rfl::toml::write(res.value()); + if (toml_string2 != toml_string1) { + std::cout << "Test failed on read. Expected:" << std::endl + << toml_string1 << std::endl + << "Got: " << std::endl + << toml_string2 << std::endl + << std::endl; + return; + } + // std::cout << toml_string1 << std::endl; + std::cout << "OK" << std::endl << std::endl; +} + +#endif diff --git a/vcpkg.json b/vcpkg.json index 3a4cfbac..da724077 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -19,6 +19,10 @@ "name": "tinycbor", "version>=": "0.6.0" }, + { + "name": "tomlplusplus", + "version>=": "3.4.0" + }, { "name": "yaml-cpp", "version>=": "0.8.0#1" From 4fa84ffd30976156d7bfda778162b4e27a391720 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 11:14:58 +0200 Subject: [PATCH 06/33] Bugfix and added test for shared_ptr --- include/rfl/parsing/Parser_shared_ptr.hpp | 8 ++---- tests/json/test_shared_ptr.cpp | 35 +++++++++++++++++++++++ tests/json/test_shared_ptr.hpp | 4 +++ tests/json/tests.cpp | 2 ++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/json/test_shared_ptr.cpp create mode 100644 tests/json/test_shared_ptr.hpp diff --git a/include/rfl/parsing/Parser_shared_ptr.hpp b/include/rfl/parsing/Parser_shared_ptr.hpp index 60bb2e3a..cd29da63 100644 --- a/include/rfl/parsing/Parser_shared_ptr.hpp +++ b/include/rfl/parsing/Parser_shared_ptr.hpp @@ -12,8 +12,7 @@ #include "Parser_base.hpp" #include "schema/Type.hpp" -namespace rfl { -namespace parsing { +namespace rfl::parsing { template requires AreReaderAndWriter> @@ -25,7 +24,7 @@ struct Parser> { static Result> read(const R& _r, const InputVarType& _var) noexcept { - if (_r.is_empty(*_var)) { + if (_r.is_empty(_var)) { return std::shared_ptr(); } const auto to_ptr = [](auto&& _t) { @@ -53,7 +52,6 @@ struct Parser> { } }; -} // namespace parsing -} // namespace rfl +} // namespace rfl::parsing #endif diff --git a/tests/json/test_shared_ptr.cpp b/tests/json/test_shared_ptr.cpp new file mode 100644 index 00000000..71c44d18 --- /dev/null +++ b/tests/json/test_shared_ptr.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "test_unique_ptr.hpp" +#include "write_and_read.hpp" + +namespace test_shared_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::shared_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_shared>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_shared_ptr diff --git a/tests/json/test_shared_ptr.hpp b/tests/json/test_shared_ptr.hpp new file mode 100644 index 00000000..ac05afcf --- /dev/null +++ b/tests/json/test_shared_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_shared_ptr { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 38cf2a4f..bd82b48c 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -64,6 +64,7 @@ #include "test_result.hpp" #include "test_save_load.hpp" #include "test_set.hpp" +#include "test_shared_ptr.hpp" #include "test_size.hpp" #include "test_std_ref.hpp" #include "test_string_map.hpp" @@ -89,6 +90,7 @@ int main() { test_optional_fields::test(); test_unique_ptr::test(); test_unique_ptr2::test(); + test_shared_ptr::test(); test_literal::test(); test_variant::test(); test_tagged_union::test(); From 86f73123d2c5f04e16f2adbc71fef06bd8c42f00 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 11:40:22 +0200 Subject: [PATCH 07/33] Allow creation of box and ref through std::unique_ptr and std::shared_ptr respectively --- docs/rfl_ref.md | 10 ++++++++++ include/rfl/Box.hpp | 13 ++++++++++++- include/rfl/Ref.hpp | 22 +++++++++++++++++++++- tests/json/test_box2.cpp | 27 +++++++++++++++++++++++++++ tests/json/test_box2.hpp | 4 ++++ tests/json/tests.cpp | 2 ++ 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/json/test_box2.cpp create mode 100644 tests/json/test_box2.hpp diff --git a/docs/rfl_ref.md b/docs/rfl_ref.md index 60bc7a93..02ea28f5 100644 --- a/docs/rfl_ref.md +++ b/docs/rfl_ref.md @@ -138,6 +138,16 @@ This will result in the following JSON string: {"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}} ``` +You can also initialize `rfl::Box` from a `std::unique_ptr`: + +```cpp +auto ptr = std::make_unique("Hello World!"); +const rfl::Result> box = rfl::make_box(std::move(ptr)); +``` + +Note that `box` is wrapped in a `Result`. That is, because we cannot guarantee at compile time +that `ptr` is not `nullptr`, therefore we need to account for that. + If you want to use reference-counted pointers, instead of unique pointers, you can use `rfl::Ref`. `rfl::Ref` is the same concept as `rfl::Box`, but using `std::shared_ptr` under-the-hood. diff --git a/include/rfl/Box.hpp b/include/rfl/Box.hpp index c6eb806d..e3063246 100644 --- a/include/rfl/Box.hpp +++ b/include/rfl/Box.hpp @@ -4,6 +4,8 @@ #include #include +#include "Result.hpp" + namespace rfl { /// The Box class behaves very similarly to the unique_ptr, but unlike the @@ -20,6 +22,15 @@ class Box { return Box(std::make_unique(std::forward(_args)...)); } + /// You can generate them from unique_ptrs as well, in which case it will + /// return an Error, if the unique_ptr is not set. + static Result> make(std::unique_ptr&& _ptr) { + if (!_ptr) { + return Error("std::unique_ptr was a nullptr."); + } + return Box(std::move(_ptr)); + } + Box() : ptr_(std::make_unique()) {} Box(const Box& _other) = delete; @@ -77,7 +88,7 @@ class Box { /// Generates a new Ref. template -Box make_box(Args&&... _args) { +auto make_box(Args&&... _args) { return Box::make(std::forward(_args)...); } diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index e65217a7..e59379af 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -4,6 +4,8 @@ #include #include +#include "Result.hpp" + namespace rfl { /// The Ref class behaves very similarly to the shared_ptr, but unlike the @@ -20,6 +22,24 @@ class Ref { return Ref(std::make_shared(std::forward(_args)...)); } + /// You can generate them from shared_ptrs as well, in which case it will + /// return an Error, if the shared_ptr is not set. + static Result> make(std::shared_ptr&& _ptr) { + if (!_ptr) { + return Error("std::shared_ptr was a nullptr."); + } + return Ref(std::move(_ptr)); + } + + /// You can generate them from shared_ptrs as well, in which case it will + /// return an Error, if the shared_ptr is not set. + static Result> make(const std::shared_ptr& _ptr) { + if (!_ptr) { + return Error("std::shared_ptr was a nullptr."); + } + return Ref(_ptr); + } + Ref() : ptr_(std::make_shared()) {} Ref(const Ref& _other) = default; @@ -87,7 +107,7 @@ class Ref { /// Generates a new Ref. template -Ref make_ref(Args&&... _args) { +auto make_ref(Args&&... _args) { return Ref::make(std::forward(_args)...); } diff --git a/tests/json/test_box2.cpp b/tests/json/test_box2.cpp new file mode 100644 index 00000000..5733948f --- /dev/null +++ b/tests/json/test_box2.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "test_box.hpp" + +namespace test_box2 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto ptr = std::make_unique("Hello World!"); + const rfl::Result> box = + rfl::make_box(std::move(ptr)); + + if (box) { + std::cout << "OK" << std::endl << std::endl; + } else { + std::cout << "Expected the result to be successful." << std::endl + << std::endl; + } +} +} // namespace test_box2 diff --git a/tests/json/test_box2.hpp b/tests/json/test_box2.hpp new file mode 100644 index 00000000..63c3c303 --- /dev/null +++ b/tests/json/test_box2.hpp @@ -0,0 +1,4 @@ +namespace test_box2 { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index bd82b48c..3dca4d97 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -10,6 +10,7 @@ #include "test_as2.hpp" #include "test_as_flatten.hpp" #include "test_box.hpp" +#include "test_box2.hpp" #include "test_c_array_class1.hpp" #include "test_c_array_class2.hpp" #include "test_c_array_class3.hpp" @@ -98,6 +99,7 @@ int main() { test_field_variant::test(); test_ref::test(); test_box::test(); + test_box2::test(); test_array::test(); test_timestamp::test(); test_flatten::test(); From 12a294af7183e470f9eb4fd7b1c5039ee65eed7e Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 11:42:20 +0200 Subject: [PATCH 08/33] Allow writing from pointers --- include/rfl/internal/to_ptr_field.hpp | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/include/rfl/internal/to_ptr_field.hpp b/include/rfl/internal/to_ptr_field.hpp index 38249dad..0db97cc3 100644 --- a/include/rfl/internal/to_ptr_field.hpp +++ b/include/rfl/internal/to_ptr_field.hpp @@ -13,39 +13,18 @@ namespace internal { template inline auto to_ptr_field(Field<_name, Type>& _field) { - if constexpr (std::is_pointer_v) { - static_assert(always_false_v, - "Writing from raw pointers is dangerous and " - "therefore unsupported. " - "Please consider using std::unique_ptr, rfl::Box, " - "std::shared_ptr, rfl::Ref or std::optional instead."); - } using T = std::remove_reference_t; return Field<_name, T*>(&_field.value_); } template inline auto to_ptr_field(const Field<_name, Type>& _field) { - if constexpr (std::is_pointer_v) { - static_assert(always_false_v, - "Writing from raw pointers is dangerous and " - "therefore unsupported. " - "Please consider using std::unique_ptr, rfl::Box, " - "std::shared_ptr, rfl::Ref or std::optional instead."); - } using T = std::remove_cvref_t; return Field<_name, const T*>(&_field.value_); } template inline auto to_ptr_field(Flatten& _field) { - if constexpr (std::is_pointer_v) { - static_assert(always_false_v, - "Writing from raw pointers is dangerous and " - "therefore unsupported. " - "Please consider using std::unique_ptr, rfl::Box, " - "std::shared_ptr, rfl::Ref or std::optional instead."); - } using T = std::remove_reference_t; return Flatten(&_field.value_); } From 6f01c3fccb2b8aa453f72257cec4d263c9491aa3 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 11:44:34 +0200 Subject: [PATCH 09/33] Minor bugfix in copy_from_named_tuple --- include/rfl/internal/copy_from_named_tuple.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/internal/copy_from_named_tuple.hpp b/include/rfl/internal/copy_from_named_tuple.hpp index c2aea13a..b9b9ba43 100644 --- a/include/rfl/internal/copy_from_named_tuple.hpp +++ b/include/rfl/internal/copy_from_named_tuple.hpp @@ -11,7 +11,7 @@ namespace internal { template T copy_from_named_tuple(const NamedTupleType& _n) { auto n = _n; - return move_from_named_tuple(std::move(n)); + return move_from_named_tuple(std::move(n)); } } // namespace internal From 32ac265293784d99fc43462fc571b35cb64e91ab Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 12:33:54 +0200 Subject: [PATCH 10/33] Made sure we can serialize and deserialize empty structs --- include/rfl/internal/is_empty.hpp | 23 +++++++ include/rfl/internal/move_to_field_tuple.hpp | 3 + include/rfl/internal/to_ptr_field_tuple.hpp | 3 + include/rfl/internal/to_ptr_named_tuple.hpp | 3 + include/rfl/parsing/NamedTupleParser.hpp | 68 ++++++++++---------- include/rfl/parsing/ViewReader.hpp | 34 +++++----- tests/json/test_empty_object.cpp | 23 +++++++ tests/json/test_empty_object.hpp | 4 ++ tests/json/tests.cpp | 2 + 9 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 include/rfl/internal/is_empty.hpp create mode 100644 tests/json/test_empty_object.cpp create mode 100644 tests/json/test_empty_object.hpp diff --git a/include/rfl/internal/is_empty.hpp b/include/rfl/internal/is_empty.hpp new file mode 100644 index 00000000..5c721d01 --- /dev/null +++ b/include/rfl/internal/is_empty.hpp @@ -0,0 +1,23 @@ +#ifndef RFL_INTERNAL_ISEMPTY_HPP_ +#define RFL_INTERNAL_ISEMPTY_HPP_ + +#include +#include + +#include "is_named_tuple.hpp" + +namespace rfl::internal { + +template +constexpr bool is_empty() { + if constexpr (is_named_tuple_v) { + return std::remove_cvref_t>::size() == 0; + } else { + using TupleType = ptr_tuple_t; + return std::tuple_size_v == 0; + } +} + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/internal/move_to_field_tuple.hpp b/include/rfl/internal/move_to_field_tuple.hpp index b7d10a40..cf860ce1 100644 --- a/include/rfl/internal/move_to_field_tuple.hpp +++ b/include/rfl/internal/move_to_field_tuple.hpp @@ -8,6 +8,7 @@ #include "Array.hpp" #include "bind_to_tuple.hpp" #include "has_fields.hpp" +#include "is_empty.hpp" #include "is_named_tuple.hpp" #include "wrap_in_fields.hpp" @@ -21,6 +22,8 @@ auto move_to_field_tuple(OriginalStruct&& _t) { return _t.fields(); } else if constexpr (has_fields()) { return bind_to_tuple(_t, [](auto& x) { return std::move(x); }); + } else if constexpr (is_empty()) { + return std::tuple(); } else { using FieldNames = field_names_t; const auto fct = [](T& _v) { diff --git a/include/rfl/internal/to_ptr_field_tuple.hpp b/include/rfl/internal/to_ptr_field_tuple.hpp index fee5ba6b..42d5c4b4 100644 --- a/include/rfl/internal/to_ptr_field_tuple.hpp +++ b/include/rfl/internal/to_ptr_field_tuple.hpp @@ -8,6 +8,7 @@ #include "../field_names_t.hpp" #include "bind_to_tuple.hpp" #include "has_fields.hpp" +#include "is_empty.hpp" #include "is_named_tuple.hpp" #include "nt_to_ptr_named_tuple.hpp" #include "to_ptr_field.hpp" @@ -24,6 +25,8 @@ auto to_ptr_field_tuple(T& _t) { return nt_to_ptr_named_tuple(_t).fields(); } else if constexpr (has_fields()) { return bind_to_tuple(_t, [](auto& x) { return to_ptr_field(x); }); + } else if constexpr (is_empty()) { + return std::tuple(); } else { using FieldNames = field_names_t; auto tup = bind_to_tuple(_t, [](auto& x) { return to_ptr_field(x); }); diff --git a/include/rfl/internal/to_ptr_named_tuple.hpp b/include/rfl/internal/to_ptr_named_tuple.hpp index 85de26f6..c6fab6eb 100644 --- a/include/rfl/internal/to_ptr_named_tuple.hpp +++ b/include/rfl/internal/to_ptr_named_tuple.hpp @@ -10,6 +10,7 @@ #include "copy_flattened_tuple_to_named_tuple.hpp" #include "has_fields.hpp" #include "has_flatten_fields.hpp" +#include "is_empty.hpp" #include "is_field.hpp" #include "is_named_tuple.hpp" #include "to_flattened_ptr_tuple.hpp" @@ -63,6 +64,8 @@ auto to_ptr_named_tuple(T&& _t) { auto ptr_field_tuple = to_ptr_field_tuple(_t); return field_tuple_to_named_tuple(ptr_field_tuple); } + } else if constexpr (is_empty()) { + return rfl::NamedTuple<>(); } else { using FieldNames = rfl::field_names_t; auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t); diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index e00709c2..cfc19ae1 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -155,21 +155,22 @@ struct NamedTupleParser { template static void call_destructors_where_necessary( const NamedTupleType& _view, const std::array& _set) { - using FieldType = std::tuple_element_t<_i, typename NamedTupleType::Fields>; - using ValueType = - std::remove_cvref_t>; - if constexpr (!std::is_array_v && - std::is_destructible_v) { - if (std::get<_i>(_set)) { - rfl::get<_i>(_view)->~ValueType(); - } - } else if constexpr (std::is_array_v) { - if (std::get<_i>(_set)) { - auto ptr = rfl::get<_i>(_view); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + if constexpr (_i < sizeof...(FieldTypes)) { + using FieldType = + std::tuple_element_t<_i, typename NamedTupleType::Fields>; + using ValueType = + std::remove_cvref_t>; + if constexpr (!std::is_array_v && + std::is_destructible_v) { + if (std::get<_i>(_set)) { + rfl::get<_i>(_view)->~ValueType(); + } + } else if constexpr (std::is_array_v) { + if (std::get<_i>(_set)) { + auto ptr = rfl::get<_i>(_view); + call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } } - } - if constexpr (_i + 1 < size_) { call_destructors_where_necessary<_i + 1>(_view, _set); } } @@ -180,28 +181,29 @@ struct NamedTupleParser { const NamedTupleType& _view, std::array* _set, std::vector* _errors) noexcept { - using FieldType = std::tuple_element_t<_i, typename NamedTupleType::Fields>; - using ValueType = std::remove_reference_t< - std::remove_pointer_t>; - - if (!std::get<_i>(_found)) { - if constexpr (_all_required || - is_required()) { - constexpr auto current_name = - std::tuple_element_t<_i, typename NamedTupleType::Fields>::name(); - _errors->push_back("Field named '" + std::string(current_name) + - "' not found."); - } else { - if constexpr (!std::is_const_v) { - ::new (rfl::get<_i>(_view)) ValueType(); + if constexpr (_i < sizeof...(FieldTypes)) { + using FieldType = + std::tuple_element_t<_i, typename NamedTupleType::Fields>; + using ValueType = std::remove_reference_t< + std::remove_pointer_t>; + + if (!std::get<_i>(_found)) { + if constexpr (_all_required || + is_required()) { + constexpr auto current_name = + std::tuple_element_t<_i, typename NamedTupleType::Fields>::name(); + _errors->push_back("Field named '" + std::string(current_name) + + "' not found."); } else { - using NonConstT = std::remove_const_t; - ::new (const_cast(rfl::get<_i>(_view))) NonConstT(); + if constexpr (!std::is_const_v) { + ::new (rfl::get<_i>(_view)) ValueType(); + } else { + using NonConstT = std::remove_const_t; + ::new (const_cast(rfl::get<_i>(_view))) NonConstT(); + } + std::get<_i>(*_set) = true; } - std::get<_i>(*_set) = true; } - } - if constexpr (_i + 1 < size_) { handle_missing_fields<_i + 1>(_found, _view, _set, _errors); } } diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index ccbe2327..04f20b69 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -27,24 +27,24 @@ class ViewReader { template void read(const std::string_view& _name, const InputVarType& _var) const { - constexpr auto current_name = - std::tuple_element_t<_i, typename ViewType::Fields>::name(); - using CurrentType = std::remove_cvref_t::Type>>; - if (!std::get<_i>(*found_) && _name == current_name) { - auto res = Parser::read(*r_, _var); - if (res) { - move_to(rfl::get<_i>(*view_), &(*res)); - std::get<_i>(*set_) = true; - } else { - errors_->push_back(Error("Failed to parse field '" + - std::string(current_name) + - "': " + res.error()->what())); + if constexpr (_i < size_) { + constexpr auto current_name = + std::tuple_element_t<_i, typename ViewType::Fields>::name(); + using CurrentType = std::remove_cvref_t::Type>>; + if (!std::get<_i>(*found_) && _name == current_name) { + auto res = Parser::read(*r_, _var); + if (res) { + move_to(rfl::get<_i>(*view_), &(*res)); + std::get<_i>(*set_) = true; + } else { + errors_->push_back(Error("Failed to parse field '" + + std::string(current_name) + + "': " + res.error()->what())); + } + std::get<_i>(*found_) = true; + return; } - std::get<_i>(*found_) = true; - return; - } - if constexpr (_i + 1 < size_) { read<_i + 1>(_name, _var); } } diff --git a/tests/json/test_empty_object.cpp b/tests/json/test_empty_object.cpp new file mode 100644 index 00000000..7e294224 --- /dev/null +++ b/tests/json/test_empty_object.cpp @@ -0,0 +1,23 @@ +#include "test_empty_object.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_empty_object { + +struct Empty {}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto empty = Empty{}; + + write_and_read(empty, R"({})"); +} +} // namespace test_empty_object diff --git a/tests/json/test_empty_object.hpp b/tests/json/test_empty_object.hpp new file mode 100644 index 00000000..8eea4ddd --- /dev/null +++ b/tests/json/test_empty_object.hpp @@ -0,0 +1,4 @@ +namespace test_empty_object { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 3dca4d97..ed5ab8de 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -25,6 +25,7 @@ #include "test_default_values.hpp" #include "test_deque.hpp" #include "test_email.hpp" +#include "test_empty_object.hpp" #include "test_enum1.hpp" #include "test_enum2.hpp" #include "test_enum3.hpp" @@ -130,6 +131,7 @@ int main() { test_unnamed_namespace::test(); test_inside_function::test(); test_const_fields::test(); + test_empty_object::test(); test_enum1::test(); test_enum2::test(); From 9af2fed46f110b01987bc300d8c7685ddd199ab9 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 14:36:59 +0200 Subject: [PATCH 11/33] Allow empty Literals --- include/rfl/Literal.hpp | 45 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index e772a1b7..bc99fa1f 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -47,7 +47,7 @@ class Literal { /// A single-field literal is special because it /// can also have a default constructor. template > + typename = std::enable_if_t> Literal() : value_(0) {} ~Literal() = default; @@ -81,7 +81,11 @@ class Literal { } /// Determines whether the literal contains the string. - static bool contains(const std::string& _str) { return has_value(_str); } + static bool contains(const std::string& _str) { + bool found = false; + has_value(_str, &found); + return found; + } /// Determines whether the literal contains the string at compile time. template @@ -280,37 +284,38 @@ class Literal { /// Finds the value of a string literal at compile time. template static constexpr int find_value_of() { - using FieldType = typename std::tuple_element<_i, FieldsType>::type; - if constexpr (FieldType::field_ == _name) { - return _i; - } else if constexpr (_i + 1 < num_fields_) { - return find_value_of<_name, _i + 1>(); - } else { + if constexpr (_i == num_fields_) { return -1; + } else { + using FieldType = typename std::tuple_element<_i, FieldsType>::type; + if constexpr (FieldType::field_ == _name) { + return _i; + } else { + return find_value_of<_name, _i + 1>(); + } } } /// Whether the literal contains this string. template - static bool has_value(const std::string& _str) { - using FieldType = typename std::tuple_element<_i, FieldsType>::type; - if (FieldType::field_.str() == _str) { - return true; - } - if constexpr (_i + 1 == num_fields_) { - return false; + static void has_value(const std::string& _str, bool* _found) { + if constexpr (_i == num_fields_) { + *_found = false; + return; } else { - return has_value<_i + 1>(_str); + using FieldType = typename std::tuple_element<_i, FieldsType>::type; + if (FieldType::field_.str() == _str) { + *_found = true; + return; + } + return has_value<_i + 1>(_str, _found); } } - static_assert(sizeof...(fields_) > 0, - "There must be at least one field in a Literal."); - static_assert(sizeof...(fields_) <= std::numeric_limits::max(), "Too many fields."); - static_assert(!has_duplicates(), + static_assert(sizeof...(fields_) <= 1 || !has_duplicates(), "Duplicate strings are not allowed in a Literal."); private: From 63e7bf3d815628a329ba503627ed93d6766af69e Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 14:37:34 +0200 Subject: [PATCH 12/33] Allow tagged unions with explicit tags --- include/rfl/TaggedUnion.hpp | 60 +++++++++++++++++- include/rfl/field_type.hpp | 5 +- include/rfl/internal/Getter.hpp | 62 +------------------ .../internal/bind_fake_object_to_tuple.hpp | 1 - include/rfl/internal/get_field_names.hpp | 8 +-- include/rfl/internal/is_empty.hpp | 7 ++- include/rfl/internal/make_tag.hpp | 15 +++-- include/rfl/internal/tag_t.hpp | 12 ++-- include/rfl/parsing/Parser_tagged_union.hpp | 35 ++++++----- tests/json/test_tagged_union3.cpp | 45 ++++++++++++++ tests/json/test_tagged_union3.hpp | 4 ++ tests/json/tests.cpp | 2 + 12 files changed, 156 insertions(+), 100 deletions(-) create mode 100644 tests/json/test_tagged_union3.cpp create mode 100644 tests/json/test_tagged_union3.hpp diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index a44ee159..aa50600e 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -4,6 +4,7 @@ #include #include "define_literal.hpp" +#include "internal/Getter.hpp" #include "internal/StringLiteral.hpp" #include "internal/tag_t.hpp" @@ -84,7 +85,8 @@ struct TaggedUnion { /// Returns the underlying variant. const VariantType& variant() const { return variant_; } - static_assert(!define_literal_t...>::has_duplicates(), + static_assert(!define_literal_t< + internal::tag_t<_discriminator, Ts>...>::has_duplicates(), "Duplicate tags are not allowed inside tagged unions."); /// The underlying variant - a TaggedUnion is a thin wrapper @@ -92,6 +94,62 @@ struct TaggedUnion { VariantType variant_; }; +namespace internal { + +template +struct Getter> { + public: + /// Retrieves the indicated value from the tuple. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get<_index>( + _tu.variant_); + } + + /// Gets a field by name. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get<_field_name>( + _tu.variant_); + } + + /// Gets a field by the field type. + template + static inline auto& get( + TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get( + _tu.variant_); + } + + /// Retrieves the indicated value from the tuple. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const<_index>( + _tu.variant_); + } + + /// Gets a field by name. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const< + _field_name>(_tu.variant_); + } + + /// Gets a field by the field type. + template + static inline const auto& get_const( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { + return Getter>::template get_const( + _tu.variant_); + } +}; + +} // namespace internal + } // namespace rfl #endif // RFL_TAGGEDUNION_HPP_ diff --git a/include/rfl/field_type.hpp b/include/rfl/field_type.hpp index 1e97fca1..9d79778c 100644 --- a/include/rfl/field_type.hpp +++ b/include/rfl/field_type.hpp @@ -10,9 +10,8 @@ namespace rfl { -template -using field_type_t = - typename internal::FieldType<_field_name, NamedTupleType>::Type; +template +using field_type_t = typename internal::FieldType<_field_name, T>::Type; } // namespace rfl diff --git a/include/rfl/internal/Getter.hpp b/include/rfl/internal/Getter.hpp index cee1742a..03824624 100644 --- a/include/rfl/internal/Getter.hpp +++ b/include/rfl/internal/Getter.hpp @@ -4,12 +4,10 @@ #include #include -#include "../TaggedUnion.hpp" #include "StringLiteral.hpp" #include "find_index.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { // ---------------------------------------------------------------------------- @@ -153,62 +151,6 @@ struct Getter> { // ---------------------------------------------------------------------------- -/// For handling std::variant. -template -struct Getter> { - public: - /// Retrieves the indicated value from the tuple. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get<_index>( - _tu.variant_); - } - - /// Gets a field by name. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get<_field_name>( - _tu.variant_); - } - - /// Gets a field by the field type. - template - static inline auto& get( - TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get( - _tu.variant_); - } - - /// Retrieves the indicated value from the tuple. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const<_index>( - _tu.variant_); - } - - /// Gets a field by name. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const< - _field_name>(_tu.variant_); - } - - /// Gets a field by the field type. - template - static inline const auto& get_const( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) { - return Getter>::template get_const( - _tu.variant_); - } -}; - -// ---------------------------------------------------------------------------- - -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/bind_fake_object_to_tuple.hpp b/include/rfl/internal/bind_fake_object_to_tuple.hpp index 26d1652d..2929306c 100644 --- a/include/rfl/internal/bind_fake_object_to_tuple.hpp +++ b/include/rfl/internal/bind_fake_object_to_tuple.hpp @@ -9,7 +9,6 @@ #include "../always_false.hpp" #include "get_fake_object.hpp" -#include "is_named_tuple.hpp" #include "num_fields.hpp" namespace rfl { diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 01e743e2..8f5bd3c8 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -23,8 +23,7 @@ #endif #endif -namespace rfl { -namespace internal { +namespace rfl::internal { template struct Wrapper { @@ -112,6 +111,8 @@ auto concat_literals(const Head& _head, const Tail&... _tail) { } } +inline auto concat_literals() { return rfl::Literal<>(); } + #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundefined-var-template" @@ -150,7 +151,6 @@ auto get_field_names() { #pragma clang diagnostic pop #endif -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/is_empty.hpp b/include/rfl/internal/is_empty.hpp index 5c721d01..a671ef00 100644 --- a/include/rfl/internal/is_empty.hpp +++ b/include/rfl/internal/is_empty.hpp @@ -10,10 +10,11 @@ namespace rfl::internal { template constexpr bool is_empty() { - if constexpr (is_named_tuple_v) { - return std::remove_cvref_t>::size() == 0; + using U = std::remove_cvref_t>; + if constexpr (is_named_tuple_v) { + return U::size() == 0; } else { - using TupleType = ptr_tuple_t; + using TupleType = ptr_tuple_t; return std::tuple_size_v == 0; } } diff --git a/include/rfl/internal/make_tag.hpp b/include/rfl/internal/make_tag.hpp index ce747c19..4004a3dd 100644 --- a/include/rfl/internal/make_tag.hpp +++ b/include/rfl/internal/make_tag.hpp @@ -2,18 +2,22 @@ #define RFL_INTERNAL_MAKE_TAG_HPP_ #include "../Literal.hpp" +#include "../field_names_t.hpp" +#include "../to_view.hpp" +#include "StringLiteral.hpp" #include "get_type_name.hpp" #include "has_reflection_type_v.hpp" #include "has_tag_v.hpp" #include "remove_namespaces.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -static inline auto make_tag() noexcept { +template +static inline auto make_tag(const T& _t) noexcept { if constexpr (internal::has_reflection_type_v) { return make_tag(); + } else if constexpr (field_names_t::template contains<_discriminator>()) { + return *to_view(_t).template get<_discriminator>(); } else if constexpr (internal::has_tag_v) { using LiteralType = typename T::Tag; return LiteralType::template name_of<0>(); @@ -23,7 +27,6 @@ static inline auto make_tag() noexcept { } } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/tag_t.hpp b/include/rfl/internal/tag_t.hpp index 927b0743..02278c82 100644 --- a/include/rfl/internal/tag_t.hpp +++ b/include/rfl/internal/tag_t.hpp @@ -3,15 +3,15 @@ #include +#include "StringLiteral.hpp" #include "make_tag.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -using tag_t = typename std::invoke_result)>::type; +template +using tag_t = + typename std::invoke_result), T>::type; -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index fbc490aa..4efcfbb3 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -53,9 +53,8 @@ struct Parser> { static schema::Type to_schema( std::map* _definitions) { - using VariantType = - std::variant), - AlternativeTypes>...>; + using VariantType = std::variant), AlternativeTypes>...>; return Parser::to_schema(_definitions); } @@ -117,32 +116,36 @@ struct Parser> { template static inline bool contains_disc_value( const std::string& _disc_value) noexcept { - return internal::tag_t::contains(_disc_value); + return internal::tag_t<_discriminator, T>::contains(_disc_value); } /// Writes a wrapped version of the original object, which contains the tag. template static void write_wrapped(const W& _w, const T& _val, const P& _parent) noexcept { - const auto wrapped = wrap(_val); + const auto wrapped = wrap_if_necessary(_val); Parser>::write(_w, wrapped, _parent); } /// Generates a wrapped version of the original object, which contains the - /// tag. + /// tag, if the object doesn't already contain the wrap. template - static auto wrap(const T& _val) noexcept { - const auto tag = internal::make_tag(); - using TagType = std::remove_cvref_t; - if constexpr (internal::has_fields>()) { - using WrapperType = - TaggedUnionWrapperWithFields; - return WrapperType{.tag = tag, .fields = &_val}; + static auto wrap_if_necessary(const T& _val) noexcept { + if constexpr (field_names_t::template contains<_discriminator>()) { + return _val; } else { - using WrapperType = - TaggedUnionWrapperNoFields; - return WrapperType{.tag = tag, .fields = &_val}; + const auto tag = internal::make_tag<_discriminator, T>(_val); + using TagType = std::remove_cvref_t; + if constexpr (internal::has_fields>()) { + using WrapperType = + TaggedUnionWrapperWithFields; + return WrapperType{.tag = tag, .fields = &_val}; + } else { + using WrapperType = + TaggedUnionWrapperNoFields; + return WrapperType{.tag = tag, .fields = &_val}; + } } } }; diff --git a/tests/json/test_tagged_union3.cpp b/tests/json/test_tagged_union3.cpp new file mode 100644 index 00000000..82b14a04 --- /dev/null +++ b/tests/json/test_tagged_union3.cpp @@ -0,0 +1,45 @@ +#include "test_tagged_union3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union3 { + +struct Circle { + rfl::Literal<"circle", "Circle"> shape; + double radius; +}; + +struct Rectangle { + rfl::Literal<"rectangle", "Rectangle", "rect"> shape; + double height; + double width; +}; + +struct Square { + rfl::Literal<"square", "Square"> shape; + double width; +}; + +struct Empty {}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle, Empty>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{ + .shape = rfl::Literal<"rectangle", "Rectangle", "rect">::make<"rect">(), + .height = 10, + .width = 5}; + + write_and_read(r, R"({"shape":"rect","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union3 diff --git a/tests/json/test_tagged_union3.hpp b/tests/json/test_tagged_union3.hpp new file mode 100644 index 00000000..c40beebf --- /dev/null +++ b/tests/json/test_tagged_union3.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union3 { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index ed5ab8de..ebcffe3b 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -73,6 +73,7 @@ #include "test_string_unordered_map.hpp" #include "test_tagged_union.hpp" #include "test_tagged_union2.hpp" +#include "test_tagged_union3.hpp" #include "test_timestamp.hpp" #include "test_transform.hpp" #include "test_unique_ptr.hpp" @@ -97,6 +98,7 @@ int main() { test_variant::test(); test_tagged_union::test(); test_tagged_union2::test(); + test_tagged_union3::test(); test_field_variant::test(); test_ref::test(); test_box::test(); From de1de23d358513cef5b67fed53059a01804486a7 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 14:47:22 +0200 Subject: [PATCH 13/33] Added documentation for the new tagged unions --- docs/variants_and_tagged_unions.md | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index 8497e42b..70987e50 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -117,6 +117,44 @@ However, `rfl::json::read` would also accept this: {"shape":"Rectangle","height":10.0,"width":5.0} ``` +If the behavior of your program depends on the value the user has decided to pass, then +you can also set the tag as a field explicitly. + +For instance, if it somehow makes a difference +whether the JSON contains "Rectangle" or "rectangle", you can do the following: + +```cpp +struct Circle { + rfl::Literal<"circle", "Circle"> shape; + double radius; +}; + +struct Rectangle { + rfl::Literal<"rectangle", "Rectangle"> shape; + double height; + double width; +}; + +struct Square { + rfl::Literal<"square", "Square"> shape; + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +const Shapes r = Rectangle{ + .shape = rfl::Literal<"rectangle", "Rectangle">::make<"Rectangle">(), + .height = 10, + .width = 5}; + +const auto json_string = rfl::json::write(r); + +const auto r2 = rfl::json::read(json_string); +``` + +Note that in this case the type of the field `shape` MUST be `rfl::Literal`. +Also note that this is exactly how tagged unions work in Pydantic. + ## `std::variant` or `rfl::Variant` (externally tagged) Another approach is to add external tags. From 916323495c931f48aced6297d69887e325154f07 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 16:57:54 +0200 Subject: [PATCH 14/33] Added const std::shared_ptr --- include/rfl/Ref.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index e59379af..e26964ab 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -100,6 +100,9 @@ class Ref { /// Only make is allowed to use this constructor. explicit Ref(std::shared_ptr&& _ptr) : ptr_(std::move(_ptr)) {} + /// Only make is allowed to use this constructor. + explicit Ref(const std::shared_ptr& _ptr) : ptr_(_ptr) {} + private: /// The underlying shared_ptr_ std::shared_ptr ptr_; From 1dcd6af3736e9b3fb0be52884cc06194446788ff Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 31 Mar 2024 17:45:09 +0200 Subject: [PATCH 15/33] Added Names to NamedTuple --- include/rfl/NamedTuple.hpp | 6 ++++-- include/rfl/internal/make_tag.hpp | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index ba9619be..4498d838 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -8,6 +8,7 @@ #include #include "Field.hpp" +#include "Literal.hpp" #include "get.hpp" #include "internal/StringLiteral.hpp" #include "internal/find_index.hpp" @@ -29,8 +30,8 @@ template class NamedTuple { public: using Fields = std::tuple...>; - using Values = - std::tuple::type::Type...>; + using Names = Literal::name_...>; + using Values = std::tuple::Type...>; public: /// Construct from the values. @@ -555,6 +556,7 @@ template <> class NamedTuple<> { public: using Fields = std::tuple<>; + using Names = Literal<>; using Values = std::tuple<>; NamedTuple(){}; diff --git a/include/rfl/internal/make_tag.hpp b/include/rfl/internal/make_tag.hpp index 4004a3dd..38a44f0a 100644 --- a/include/rfl/internal/make_tag.hpp +++ b/include/rfl/internal/make_tag.hpp @@ -3,6 +3,7 @@ #include "../Literal.hpp" #include "../field_names_t.hpp" +#include "../named_tuple_t.hpp" #include "../to_view.hpp" #include "StringLiteral.hpp" #include "get_type_name.hpp" @@ -16,7 +17,8 @@ template static inline auto make_tag(const T& _t) noexcept { if constexpr (internal::has_reflection_type_v) { return make_tag(); - } else if constexpr (field_names_t::template contains<_discriminator>()) { + } else if constexpr (named_tuple_t::Names::template contains< + _discriminator>()) { return *to_view(_t).template get<_discriminator>(); } else if constexpr (internal::has_tag_v) { using LiteralType = typename T::Tag; From 78554708dc50fe0a91854862d7e940c5f5cb1bb6 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 1 Apr 2024 12:16:17 +0200 Subject: [PATCH 16/33] Use BUILD_SHARED_LIBS as default value for REFLECTCPP_BUILD_SHARED --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65a58e00..0192c9b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) -option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF) +option(REFLECTCPP_BUILD_SHARED "Build shared library" ${BUILD_SHARED_LIBS}) option(REFLECTCPP_BSON "Enable BSON support" OFF) option(REFLECTCPP_CBOR "Enable CBOR support" OFF) option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) From 23c5a603ae813012268e0159bfcf25aea3c2118a Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 1 Apr 2024 13:31:14 +0200 Subject: [PATCH 17/33] Added more reliable checks in Parser_tagged_union --- include/rfl/parsing/Parser_tagged_union.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index 4efcfbb3..d67f0953 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -132,7 +132,7 @@ struct Parser> { /// tag, if the object doesn't already contain the wrap. template static auto wrap_if_necessary(const T& _val) noexcept { - if constexpr (field_names_t::template contains<_discriminator>()) { + if constexpr (named_tuple_t::Names::template contains<_discriminator>()) { return _val; } else { const auto tag = internal::make_tag<_discriminator, T>(_val); From df488ec79e9a3d295d81f38fd0fc04386eece038 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 1 Apr 2024 13:31:31 +0200 Subject: [PATCH 18/33] Changed the semantics of .add and .replace in NamedTuple --- include/rfl/NamedTuple.hpp | 112 +++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 61 deletions(-) diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 4498d838..2fffbcaa 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -105,8 +105,9 @@ class NamedTuple { ~NamedTuple() = default; /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) { + template + auto add(Field<_name, FType>&& _head, Tail&&... _tail) { + using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( make_fields<1, Head>(std::forward(_head))) @@ -118,15 +119,16 @@ class NamedTuple { } /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) const { + template + auto add(Field<_name, FType> _head, const Tail&... _tail) const { + using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))) - .add(std::forward(_tail)...); + make_fields<1, Head>(_head)) + .add(_tail...); } else { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))); + make_fields<1, Head>(_head)); } } @@ -145,12 +147,11 @@ class NamedTuple { /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple&& _tuple, Tail&&... _tail) const { + auto add(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple)) - .add(std::forward(_tail)...); + return add_tuple(std::move(_tuple)).add(_tail...); } else { - return add_tuple(std::forward>(_tuple)); + return add_tuple(std::move(_tuple)); } } @@ -165,9 +166,8 @@ class NamedTuple { /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template - auto add(NamedTuple&& _named_tuple, Tail&&... _tail) const { - return add(std::forward>(_named_tuple.fields()), - std::forward(_tail)...); + auto add(NamedTuple _named_tuple, const Tail&... _tail) const { + return add(_named_tuple.fields(), _tail...); } /// Creates a new named tuple by applying the supplied function to @@ -273,11 +273,20 @@ class NamedTuple { NamedTuple& operator=( NamedTuple&& _other) noexcept = default; + /// Equality operator + inline auto operator==(const rfl::NamedTuple& _other) const { + return values() == _other.values(); + } + + /// Inequality operator + inline auto operator!=(const rfl::NamedTuple& _other) const { + return !(*this == _other); + } /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. - template - NamedTuple replace(RField&& _field, - OtherRFields&&... _other_fields) { + template + auto replace(Field<_name, FType>&& _field, OtherRFields&&... _other_fields) { + using RField = Field<_name, FType>; constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { return replace_value(_field.value_); @@ -289,15 +298,16 @@ class NamedTuple { /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. - template - NamedTuple replace(RField&& _field, - OtherRFields&&... _other_fields) const { + template + auto replace(Field<_name, FType> _field, + const OtherRFields&... _other_fields) const { + using RField = Field<_name, FType>; constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { - return replace_value(_field.value_); + return replace_value(std::move(_field.value_)); } else { - return replace_value(_field.value_) - .replace(std::forward(_other_fields)...); + return replace_value(std::move(_field.value_)) + .replace(_other_fields...); } } @@ -316,12 +326,11 @@ class NamedTuple { /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto replace(std::tuple&& _tuple, Tail&&... _tail) const { + auto replace(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return replace_tuple(std::forward>(_tuple)) - .replace(std::forward(_tail)...); + return replace_tuple(std::move(_tuple)).replace(_tail...); } else { - return replace_tuple(std::forward>(_tuple)); + return replace_tuple(std::move(_tuple)); } } @@ -337,11 +346,9 @@ class NamedTuple { /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template - auto replace(const NamedTuple& _named_tuple, - Tail&&... _tail) const { - return replace( - std::forward>(_named_tuple).fields(), - std::forward(_tail)...); + auto replace(NamedTuple _named_tuple, + const Tail&... _tail) const { + return replace(_named_tuple.fields(), _tail...); } /// Returns the size of the named tuple @@ -426,7 +433,7 @@ class NamedTuple { /// Generates the fields. template - auto make_fields(Args&&... _args) const { + auto make_fields(Args... _args) const { constexpr auto size = sizeof...(Args) - num_additional_fields; constexpr auto num_fields = std::tuple_size_v; constexpr auto i = num_fields - size - 1; @@ -434,13 +441,13 @@ class NamedTuple { constexpr bool retrieved_all_fields = size == num_fields; if constexpr (retrieved_all_fields) { - return std::make_tuple(std::forward(_args)...); + return std::make_tuple(std::move(_args)...); } else { // When we add additional fields, it is more intuitive to add // them to the end, that is why we do it like this. using FieldType = typename std::tuple_element::type; return make_fields(FieldType(std::get(values_)), - std::forward(_args)...); + std::move(_args)...); } } @@ -564,36 +571,31 @@ class NamedTuple<> { ~NamedTuple() = default; /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) const { + template + auto add(Field<_name, FType> _head, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return NamedTuple>(std::forward(_head)) - .add(std::forward(_tail)...); + return NamedTuple>(std::move(_head)).add(_tail...); } else { - return NamedTuple>(std::forward(_head)); + return NamedTuple>(std::move(_head)); } } /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple&& _tuple, Tail&&... _tail) const { + auto add(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return NamedTuple( - std::forward>(_tuple)) - .add(std::forward(_tail)...); + return NamedTuple(std::move(_tuple)).add(_tail...); } else { - return NamedTuple( - std::forward>(_tuple)); + return NamedTuple(std::move(_tuple)); } } /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template - auto add(NamedTuple&& _named_tuple, Tail&&... _tail) const { - return add(std::forward>(_named_tuple.fields()), - std::forward(_tail)...); + auto add(NamedTuple _named_tuple, const Tail&... _tail) const { + return add(_named_tuple.fields(), _tail...); } /// Returns an empty named tuple. @@ -624,18 +626,6 @@ class NamedTuple<> { // ---------------------------------------------------------------------------- -template -inline bool operator==(const rfl::NamedTuple& _nt1, - const rfl::NamedTuple& _nt2) { - return _nt1.values() == _nt2.values(); -} - -template -inline bool operator!=(const rfl::NamedTuple& _nt1, - const rfl::NamedTuple& _nt2) { - return _nt1.values() != _nt2.values(); -} - template inline auto operator*(const rfl::Field<_name1, Type1>& _f1, From 648d628de57211d2eda8fba07140b1d35503cfde Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 1 Apr 2024 13:31:45 +0200 Subject: [PATCH 19/33] Explicit equality operator in Literal --- include/rfl/Literal.hpp | 51 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index bc99fa1f..87ce4859 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -161,9 +161,32 @@ class Literal { return *this; } - /// Equality operator other Literals. - bool operator==(const Literal& _other) const { - return value() == _other.value(); + /// <=> for other Literals with the same fields. + auto operator<=>(const Literal& _other) const { + return value() <=> _other.value(); + } + + /// <=> for other Literals with different fields. + template + inline auto operator<=>(const Literal<_fields...>& _l2) const { + return name() <=> _l2.name(); + } + + /// <=> for strings. + inline auto operator<=>(const std::string& _str) const { + return name() <=> _str; + } + + /// <=> for const char*. + template + inline auto operator<=>(const char* _str) const { + return name() <=> _str; + } + + /// Equality operator. + template + bool operator==(const Other& _other) const { + return (*this <=> _other) == 0; } /// Alias for .name(). @@ -335,28 +358,6 @@ inline constexpr auto value_of() { return LiteralType::template value_of<_name>(); } -/// <=> for other Literals with the same fields. -template -inline auto operator<=>(const Literal& _l1, - const Literal& _l2) { - return _l1.value() <=> _l2.value(); -} - -/// <=> for other Literals with different fields. -template -inline auto operator<=>(const Literal& _l1, - const Literal& _l2) { - return _l1.name() <=> _l2.name(); -} - -/// <=> for strings. -template -inline auto operator<=>(const Literal& _l, - const std::string& _str) { - return _l <=> _str; -} - } // namespace rfl namespace std { From 8c1f9d1ee332775f7bf82dc24dd3e54cbaeab8da Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 1 Apr 2024 14:38:48 +0200 Subject: [PATCH 20/33] Add cmake config module --- CMakeLists.txt | 48 ++++++++++++++++++++++++++++++++++++-- reflectcpp-config.cmake.in | 38 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 reflectcpp-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 65a58e00..a5f2e36f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,15 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) -# enable vcpkg if require features other than JSON +set(REFLECTCPP_USE_VCPKG_DEFAULT OFF) if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) + # enable vcpkg per default if require features other than JSON + set(REFLECTCPP_USE_VCPKG_DEFAULT ON) +endif() + +option(REFLECTCPP_USE_VCPKG "Use VCPKG to download and build dependencies" ${REFLECTCPP_USE_VCPKG_DEFAULT}) + +if (REFLECTCPP_USE_VCPKG) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") endif () @@ -25,7 +32,9 @@ else () add_library(reflectcpp STATIC src/yyjson.c) endif () -target_include_directories(reflectcpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_compile_features(reflectcpp PUBLIC cxx_std_20) + +target_include_directories(reflectcpp PUBLIC $ $ ) if (REFLECTCPP_BSON) find_package(bson-1.0 CONFIG REQUIRED) @@ -66,3 +75,38 @@ target_compile_options(reflectcpp PRIVATE -Wall) if (REFLECTCPP_BUILD_TESTS) add_subdirectory(tests) endif () + + +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +configure_package_config_file(reflectcpp-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp + ) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/reflectcpp-config.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp" +) + +file(GLOB_RECURSE RFL_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/include/*" ) + +target_sources(reflectcpp + PUBLIC + FILE_SET reflectcpp_headers + TYPE HEADERS + BASE_DIRS $ $ + FILES ${RFL_HEADERS}) + +install( + TARGETS reflectcpp + EXPORT reflectcpp-exports + FILE_SET reflectcpp_headers DESTINATION ${INCLUDE_INSTALL_DIR} + ) + +install( + EXPORT reflectcpp-exports + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp + NAMESPACE reflectcpp:: +) \ No newline at end of file diff --git a/reflectcpp-config.cmake.in b/reflectcpp-config.cmake.in new file mode 100644 index 00000000..1d8d7c06 --- /dev/null +++ b/reflectcpp-config.cmake.in @@ -0,0 +1,38 @@ +@PACKAGE_INIT@ + +set(REFLECTCPP_BSON @REFLECTCPP_BSON@) +set(REFLECTCPP_FLEXBUFFERS @REFLECTCPP_FLEXBUFFERS@) +set(REFLECTCPP_TOML @REFLECTCPP_TOML@) +set(REFLECTCPP_XML @REFLECTCPP_XML@) +set(REFLECTCPP_YAML @REFLECTCPP_YAML@) + +if(REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML) + include(CMakeFindDependencyMacro) +endif() + +include(${CMAKE_CURRENT_LIST_DIR}/reflectcpp-exports.cmake) + + +if (REFLECTCPP_BSON) + find_dependency(bson-1.0) +endif () + +if (REFLECTCPP_FLEXBUFFERS) + find_dependency(flatbuffers) +endif () + +if (REFLECTCPP_TOML) + find_package(PkgConfig REQUIRED) + pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) +endif() + +if (REFLECTCPP_XML) + find_dependency(pugixml) +endif () + +if (REFLECTCPP_YAML) + find_dependency(yaml-cpp) +endif () + +check_required_components(reflect-cpp) + From 52cf3ed7d4a5d400db9fc4f38e1b33b989392047 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Fri, 5 Apr 2024 08:33:55 +0200 Subject: [PATCH 21/33] reflectcpp-config.cmake.in: Fix typo in check_required_components --- reflectcpp-config.cmake.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reflectcpp-config.cmake.in b/reflectcpp-config.cmake.in index 1d8d7c06..702cbc4a 100644 --- a/reflectcpp-config.cmake.in +++ b/reflectcpp-config.cmake.in @@ -34,5 +34,4 @@ if (REFLECTCPP_YAML) find_dependency(yaml-cpp) endif () -check_required_components(reflect-cpp) - +check_required_components(reflectcpp) From 0b33c92c9b0362cf2e1320aaf973a56db08720e0 Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 8 Apr 2024 10:42:42 +0200 Subject: [PATCH 22/33] Fix names of inherited members being prefixed with the base-class's name when using clang --- include/rfl/internal/get_field_names.hpp | 5 ++++- tests/json/CMakeLists.txt | 2 +- tests/json/test_inheritance.cpp | 26 ++++++++++++++++++++++++ tests/json/test_inheritance.hpp | 3 +++ tests/json/tests.cpp | 3 +++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/json/test_inheritance.cpp create mode 100644 tests/json/test_inheritance.hpp diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 8f5bd3c8..52efbd2e 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -52,7 +52,10 @@ consteval auto get_field_name_str_view() { #endif #if defined(__clang__) const auto split = func_name.substr(0, func_name.size() - 2); - return split.substr(split.find_last_of(".") + 1); + const auto possibly_prefixed = split.substr(split.find_last_of(".") + 1); + const auto separator_index = possibly_prefixed.find_last_of(":"); + if (separator_index == std::string_view::npos) return possibly_prefixed; + return possibly_prefixed.substr(separator_index + 1); #elif defined(__GNUC__) const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index 8a32cc38..1e077f6a 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -1,6 +1,6 @@ project(reflect-cpp-json-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") add_executable(reflect-cpp-json-tests ${SOURCES}) diff --git a/tests/json/test_inheritance.cpp b/tests/json/test_inheritance.cpp new file mode 100644 index 00000000..5f480970 --- /dev/null +++ b/tests/json/test_inheritance.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +namespace test_inheritance { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + struct S { + int x; + }; + + struct T : S {}; + + const auto name = get<0>(rfl::fields()).name(); + if (name == "x") { + std::cout << "OK" << std::endl; + } else { + std::cout << "FAIL\n" + << "Expected member name 'x', got '" << name << "'" << std::endl; + } +} + +} // namespace test_inheritance \ No newline at end of file diff --git a/tests/json/test_inheritance.hpp b/tests/json/test_inheritance.hpp new file mode 100644 index 00000000..31d51d1a --- /dev/null +++ b/tests/json/test_inheritance.hpp @@ -0,0 +1,3 @@ +namespace test_inheritance{ +void test(); +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index ebcffe3b..1246c887 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -86,6 +86,7 @@ #include "test_variant.hpp" #include "test_view.hpp" #include "test_wstring.hpp" +#include "test_inheritance.hpp" int main() { test_readme_example::test(); @@ -187,5 +188,7 @@ int main() { test_wstring::test(); test_json_schema::test(); + test_inheritance::test(); + return 0; } From 1146f76bded466f60a34918f3bf5be3bb65a848d Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Mon, 8 Apr 2024 16:02:16 +0200 Subject: [PATCH 23/33] More elegant formulation --- include/rfl/internal/get_field_names.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index 52efbd2e..ebb3e8ae 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -52,10 +52,7 @@ consteval auto get_field_name_str_view() { #endif #if defined(__clang__) const auto split = func_name.substr(0, func_name.size() - 2); - const auto possibly_prefixed = split.substr(split.find_last_of(".") + 1); - const auto separator_index = possibly_prefixed.find_last_of(":"); - if (separator_index == std::string_view::npos) return possibly_prefixed; - return possibly_prefixed.substr(separator_index + 1); + return split.substr(split.find_last_of(":.") + 1); #elif defined(__GNUC__) const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); From b38f12ecdf8893eaa6fbef6f9dfc1bf0a5c64f7f Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Wed, 10 Apr 2024 22:55:06 +0200 Subject: [PATCH 24/33] More efficient way of parsing variants --- include/rfl/parsing/Parser_variant.hpp | 64 +++++++++++-------- include/rfl/parsing/VectorParser.hpp | 2 +- .../rfl/parsing/to_single_error_message.hpp | 21 ++++-- 3 files changed, 54 insertions(+), 33 deletions(-) diff --git a/include/rfl/parsing/Parser_variant.hpp b/include/rfl/parsing/Parser_variant.hpp index 683e6da0..4caeddd8 100644 --- a/include/rfl/parsing/Parser_variant.hpp +++ b/include/rfl/parsing/Parser_variant.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_PARSER_VARIANT_HPP_ #include +#include #include #include @@ -11,43 +12,36 @@ #include "FieldVariantParser.hpp" #include "Parser_base.hpp" #include "schema/Type.hpp" +#include "to_single_error_message.hpp" namespace rfl { namespace parsing { template requires AreReaderAndWriter> -struct Parser> { +class Parser> { + public: using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; - template static Result> read( - const R& _r, const InputVarType& _var, - const std::string _errors = "") noexcept { - if constexpr (_i == 0 && - internal::all_fields>()) { + const R& _r, const InputVarType& _var) noexcept { + if constexpr (internal::all_fields>()) { return FieldVariantParser::read(_r, _var); - } else if constexpr (_i == sizeof...(FieldTypes)) { - return Error("Could not parse variant: " + _errors); } else { - const auto to_variant = [](auto&& _val) { - return std::variant(std::move(_val)); - }; - - const auto try_next = [&_r, _var, &_errors](const auto& _err) { - auto errors = _errors; - errors += std::string("\n" + std::to_string(_i + 1) + ") ") + - internal::strings::replace_all(_err.what(), "\n", "\n "); - return read<_i + 1>(_r, _var, errors); - }; - - using AltType = std::remove_cvref_t< - std::variant_alternative_t<_i, std::variant>>; - - return Parser::read(_r, _var) - .transform(to_variant) - .or_else(try_next); + std::optional> result; + std::vector errors; + read_variant(_r, _var, &result, &errors); + if (result) { + return std::move(*result); + } else { + return Error( + to_single_error_message(errors, + "Could not parse the variant. Each of the " + "possible alternatives failed " + "for the following reasons: ", + 100000)); + } } } @@ -84,6 +78,26 @@ struct Parser> { } } } + + private: + template + static void read_variant(const R& _r, const InputVarType& _var, + std::optional>* _result, + std::vector* _errors) noexcept { + constexpr size_t size = sizeof...(FieldTypes); + if constexpr (_i < size) { + using AltType = std::remove_cvref_t< + std::variant_alternative_t<_i, std::variant>>; + auto res = Parser::read(_r, _var); + if (res) { + *_result = std::move(*res); + return; + } else { + _errors->emplace_back(*res.error()); + return read_variant<_i + 1>(_r, _var, _result, _errors); + } + } + } }; } // namespace parsing diff --git a/include/rfl/parsing/VectorParser.hpp b/include/rfl/parsing/VectorParser.hpp index 53afe5ad..06aec32e 100644 --- a/include/rfl/parsing/VectorParser.hpp +++ b/include/rfl/parsing/VectorParser.hpp @@ -86,7 +86,7 @@ struct VectorParser { .value(); } - static Result to_container(const R& _r, InputArrayType&& _arr) { + static VecType to_container(const R& _r, InputArrayType&& _arr) { auto input_vars = _r.to_vec(_arr); VecType vec; if constexpr (is_forward_list()) { diff --git a/include/rfl/parsing/to_single_error_message.hpp b/include/rfl/parsing/to_single_error_message.hpp index 16591d78..e8601e09 100644 --- a/include/rfl/parsing/to_single_error_message.hpp +++ b/include/rfl/parsing/to_single_error_message.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_TOSINGLEERRORMESSAGE_HPP_ #define RFL_PARSING_TOSINGLEERRORMESSAGE_HPP_ +#include #include #include @@ -9,20 +10,26 @@ namespace rfl::parsing { /// Combines a set of errors to a single, readable error message. -inline Error to_single_error_message(std::vector _errors) { +inline Error to_single_error_message( + std::vector _errors, + std::optional _msg_prefix = std::nullopt, + size_t _err_limit = 10) { if (_errors.size() == 1) { return std::move(_errors[0]); } else { - std::string msg = "Found " + std::to_string(_errors.size()) + " errors:"; - for (size_t i = 0; i < _errors.size() && i < 10; ++i) { + std::string msg = + _msg_prefix ? *_msg_prefix + : "Found " + std::to_string(_errors.size()) + " errors:"; + for (size_t i = 0; i < _errors.size() && i < _err_limit; ++i) { msg += "\n" + std::to_string(i + 1) + ") " + internal::strings::replace_all(_errors.at(i).what(), "\n", "\n "); } - if (_errors.size() > 10) { - msg += - "\n...\nMore than 10 errors occurred, but I am only showing the " - "first 10."; + if (_errors.size() > _err_limit) { + msg += "\n...\nMore than " + std::to_string(_err_limit) + + " errors occurred, but I am only showing the " + "first " + + std::to_string(_err_limit) + "."; } return Error(msg); } From fbeca9b095fae31365b4d2fda48ea849c0d46aca Mon Sep 17 00:00:00 2001 From: Lixin Wei Date: Thu, 11 Apr 2024 22:54:17 +0800 Subject: [PATCH 25/33] Support one-level inheritance cases (#83) * Substract base class param from num of fields * Fix comments from @schaumb * Fix CI * fix comments from @liuzicheng1987 * fix some comments form @schaumb * rename some funcs * one-level inheritance fixed * fix CI --- .gitignore | 4 ++ include/rfl/internal/bind_to_tuple.hpp | 1 + include/rfl/internal/num_fields.hpp | 97 ++++++++++++++++++++++++-- tests/json/test_inheritance2.cpp | 52 ++++++++++++++ tests/json/test_inheritance2.hpp | 3 + tests/json/test_view.cpp | 1 + tests/json/tests.cpp | 2 + 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 tests/json/test_inheritance2.cpp create mode 100644 tests/json/test_inheritance2.hpp diff --git a/.gitignore b/.gitignore index f05d838f..d57c7e6f 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,7 @@ *.yaml !vcpkg.json + +# clangd +compile_flags.txt +compile_commands.json diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index c2ec8fc5..16eb6755 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -9,6 +9,7 @@ #include "../always_false.hpp" #include "is_named_tuple.hpp" +#include "num_fields.hpp" namespace rfl { namespace internal { diff --git a/include/rfl/internal/num_fields.hpp b/include/rfl/internal/num_fields.hpp index fb88454a..a70397b5 100644 --- a/include/rfl/internal/num_fields.hpp +++ b/include/rfl/internal/num_fields.hpp @@ -29,6 +29,7 @@ the potential array as we can without missing variables in subsequent fields. This is the purpose of get_nested_array_size(). */ +#include #include #include #include @@ -48,6 +49,29 @@ This is the purpose of get_nested_array_size(). namespace rfl { namespace internal { +template +struct any_empty_base { + any_empty_base(std::size_t); + template + requires( + std::is_empty_v> && + std::is_base_of_v, + std::remove_cv_t> && + !std::is_same_v, std::remove_cv_t>) + constexpr operator Base&() const noexcept; +}; + +template +struct any_base { + any_base(std::size_t); + template + requires( + std::is_base_of_v, + std::remove_cv_t> && + !std::is_same_v, std::remove_cv_t>) + constexpr operator Base&() const noexcept; +}; + struct any { any(std::size_t); template @@ -76,12 +100,12 @@ struct CountFieldsHelper { } template - static consteval std::size_t count_max_fields() { + static consteval std::size_t count_max_args_in_agg_init() { static_assert(n <= static_cast(sizeof(T))); if constexpr (constructible() && !constructible()) { return n; } else { - return count_max_fields(); + return count_max_args_in_agg_init(); } } @@ -97,20 +121,83 @@ struct CountFieldsHelper { } } + template + static consteval std::size_t find_the_sole_non_empty_base_index() { + static_assert(index < max_args); + constexpr auto check = []( + std::index_sequence, + std::index_sequence) { + return requires { + T{any_empty_base(l)..., any_base(0), any_empty_base(r)...}; + }; + }; + + if constexpr (check(std::make_index_sequence(), + std::make_index_sequence())) { + return index; + } else { + return find_the_sole_non_empty_base_index(); + } + } + + template + static consteval std::size_t get_nested_base_field_count() { + static_assert(size <= sizeof(T)); + if constexpr (constructible_with_nested() && + !constructible_with_nested()) { + return size; + } else { + return get_nested_base_field_count(); + } + } + + template + static consteval bool has_n_base_param() { + constexpr auto right_len = max_arg_num>=n ? max_arg_num-n : 0; + return [](std::index_sequence, + std::index_sequence) { + return requires { T{any_base(l)..., any(r)...}; }; + }(std::make_index_sequence(), std::make_index_sequence()); + } + + template + static consteval std::size_t base_param_num() { + if constexpr (!has_n_base_param()) { + return index; + } else { + return base_param_num(); + } + } + template - static consteval std::size_t count_fields_impl() { + static consteval std::size_t constructible_no_brace_elision() { static_assert(index <= max); if constexpr (index == max) { return 0; } else { return 1 + - count_fields_impl< + constructible_no_brace_elision< index + get_nested_array_size(), max>(); } } static consteval std::size_t count_fields() { - return count_fields_impl<0, count_max_fields()>(); + constexpr std::size_t max_agg_args = count_max_args_in_agg_init(); + constexpr std::size_t no_brace_ellison_args = + constructible_no_brace_elision<0, max_agg_args>(); + constexpr std::size_t base_args = base_param_num(); + if constexpr (no_brace_ellison_args == 0 && base_args == 0) { + // Empty struct + return 0; + } else if constexpr (base_args == no_brace_ellison_args) { + // Special case when the derived class is empty. + // In such cases the filed number is the fields in base class. + // Note that there should be only one base class in this case. + return get_nested_base_field_count< + find_the_sole_non_empty_base_index()>(); + } else { + return no_brace_ellison_args - base_args; + } } }; diff --git a/tests/json/test_inheritance2.cpp b/tests/json/test_inheritance2.cpp new file mode 100644 index 00000000..25e62b25 --- /dev/null +++ b/tests/json/test_inheritance2.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +#include "rfl/internal/num_fields.hpp" + +namespace test_inheritance2 { + +struct EmptyBase1 {}; +struct EmptyBase2 {}; +struct Derived1 : public EmptyBase1 { + int x; + int y; +}; +struct Derived2 : public EmptyBase1, public EmptyBase2 { + int x; + int y; + int z; +}; + +struct BaseX { + int x; + int y; +}; +struct EmptyDerived0 : BaseX, EmptyBase1 {}; +struct EmptyDerived1 : EmptyBase1, BaseX {}; +struct EmptyDerived2 : EmptyBase1, EmptyBase2, BaseX {}; + +void test() { + Derived1 derived1; + const auto derived1_view = rfl::to_view(derived1); + static_assert(derived1_view.size() == 2); + Derived2 derived2; + const auto derived2_view = rfl::to_view(derived2); + static_assert(derived2_view.size() == 3); + + EmptyDerived1 empty_derived0; + auto empty_derived0_view = rfl::to_view(empty_derived0); + static_assert(empty_derived0_view.size() == 2); + + EmptyDerived1 empty_derived1; + auto empty_derived1_view = rfl::to_view(empty_derived1); + static_assert(empty_derived1_view.size() == 2); + + EmptyDerived1 empty_derived2; + auto empty_derived2_view = rfl::to_view(empty_derived2); + static_assert(empty_derived0_view.size() == 2); + + std::cout << "OK\n"; +} + +} // namespace test_inheritance \ No newline at end of file diff --git a/tests/json/test_inheritance2.hpp b/tests/json/test_inheritance2.hpp new file mode 100644 index 00000000..c347aac6 --- /dev/null +++ b/tests/json/test_inheritance2.hpp @@ -0,0 +1,3 @@ +namespace test_inheritance2 { +void test(); +} diff --git a/tests/json/test_view.cpp b/tests/json/test_view.cpp index e1de18de..f4b4b042 100644 --- a/tests/json/test_view.cpp +++ b/tests/json/test_view.cpp @@ -7,6 +7,7 @@ #include #include +#include "rfl/internal/num_fields.hpp" #include "test_replace.hpp" #include "write_and_read.hpp" diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 1246c887..fd415c17 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -87,6 +87,7 @@ #include "test_view.hpp" #include "test_wstring.hpp" #include "test_inheritance.hpp" +#include "test_inheritance2.hpp" int main() { test_readme_example::test(); @@ -189,6 +190,7 @@ int main() { test_json_schema::test(); test_inheritance::test(); + test_inheritance2::test(); return 0; } From 62619d4a195faa3513bb306a880b483e34b9d6c6 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 16 Apr 2024 00:48:43 +0200 Subject: [PATCH 26/33] Better error messages for tagged unions --- include/rfl/TaggedUnion.hpp | 6 ++++-- include/rfl/parsing/Parser_tagged_union.hpp | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index aa50600e..09448837 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -18,6 +18,9 @@ struct TaggedUnion { /// The type of the underlying variant. using VariantType = std::variant; + /// A literal containing all the tags that are possible + using PossibleTags = define_literal_t...>; + TaggedUnion(const VariantType& _variant) : variant_(_variant) {} TaggedUnion(VariantType&& _variant) noexcept @@ -85,8 +88,7 @@ struct TaggedUnion { /// Returns the underlying variant. const VariantType& variant() const { return variant_; } - static_assert(!define_literal_t< - internal::tag_t<_discriminator, Ts>...>::has_duplicates(), + static_assert(!PossibleTags::has_duplicates(), "Duplicate tags are not allowed inside tagged unions."); /// The underlying variant - a TaggedUnion is a thin wrapper diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index d67f0953..4d7d9c17 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -7,6 +7,7 @@ #include "../Result.hpp" #include "../TaggedUnion.hpp" #include "../always_false.hpp" +#include "../internal/strings/join.hpp" #include "Parser_base.hpp" #include "TaggedUnionWrapper.hpp" #include "schema/Type.hpp" @@ -64,8 +65,13 @@ struct Parser> { const R& _r, const std::string& _disc_value, const InputVarType& _var) noexcept { if constexpr (_i == sizeof...(AlternativeTypes)) { + const auto names = + TaggedUnion<_discriminator, + AlternativeTypes...>::PossibleTags::names(); return Error("Could not parse tagged union, could not match " + - _discriminator.str() + " '" + _disc_value + "'."); + _discriminator.str() + " '" + _disc_value + + "'. The following tags are allowed: " + + internal::strings::join(",", names)); } else { using AlternativeType = std::remove_cvref_t< std::variant_alternative_t<_i, std::variant>>; @@ -132,7 +138,8 @@ struct Parser> { /// tag, if the object doesn't already contain the wrap. template static auto wrap_if_necessary(const T& _val) noexcept { - if constexpr (named_tuple_t::Names::template contains<_discriminator>()) { + if constexpr (named_tuple_t::Names::template contains< + _discriminator>()) { return _val; } else { const auto tag = internal::make_tag<_discriminator, T>(_val); From 3f4def672ee5b87f72f91971771bef92cb795457 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 18 Apr 2024 20:12:16 +0200 Subject: [PATCH 27/33] Made sure we can read larger integers, fixes #85 --- include/rfl/parsing/MapReader.hpp | 6 ++++-- tests/json/test_map.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/rfl/parsing/MapReader.hpp b/include/rfl/parsing/MapReader.hpp index 7f66f3b1..8e63fc8e 100644 --- a/include/rfl/parsing/MapReader.hpp +++ b/include/rfl/parsing/MapReader.hpp @@ -41,8 +41,10 @@ class MapReader { template Result key_to_numeric(auto& _pair) const noexcept { try { - if constexpr (std::is_integral_v) { - return static_cast(std::stoi(_pair.first)); + if constexpr (std::is_integral_v && std::is_signed_v) { + return static_cast(std::stoll(_pair.first)); + } else if constexpr (std::is_integral_v && std::is_unsigned_v) { + return static_cast(std::stoull(_pair.first)); } else if constexpr (std::is_floating_point_v) { return static_cast(std::stod(_pair.first)); } else { diff --git a/tests/json/test_map.cpp b/tests/json/test_map.cpp index 9f4a619d..20a227ea 100644 --- a/tests/json/test_map.cpp +++ b/tests/json/test_map.cpp @@ -14,22 +14,22 @@ namespace test_map { struct Person { rfl::Rename<"firstName", std::string> first_name; rfl::Rename<"lastName", std::string> last_name = "Simpson"; - std::unique_ptr> children; + std::unique_ptr> children; }; void test() { std::cout << std::source_location::current().function_name() << std::endl; - auto children = std::make_unique>(); + auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + children->insert(std::make_pair(2660778562, Person{.first_name = "Maggie"})); const auto homer = Person{.first_name = "Homer", .children = std::move(children)}; write_and_read( homer, - R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})"); + R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"2660778562":{"firstName":"Maggie","lastName":"Simpson"}}})"); } } // namespace test_map From 4d2116ad4cf1a8b46a85901693b3f297e909c8f5 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 18 Apr 2024 20:12:32 +0200 Subject: [PATCH 28/33] Beautified the inheritance tests --- tests/json/test_inheritance.cpp | 4 ++-- tests/json/test_inheritance2.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/json/test_inheritance.cpp b/tests/json/test_inheritance.cpp index 5f480970..4c131053 100644 --- a/tests/json/test_inheritance.cpp +++ b/tests/json/test_inheritance.cpp @@ -16,11 +16,11 @@ void test() { const auto name = get<0>(rfl::fields()).name(); if (name == "x") { - std::cout << "OK" << std::endl; + std::cout << "OK" << std::endl << std::endl; } else { std::cout << "FAIL\n" << "Expected member name 'x', got '" << name << "'" << std::endl; } } -} // namespace test_inheritance \ No newline at end of file +} // namespace test_inheritance diff --git a/tests/json/test_inheritance2.cpp b/tests/json/test_inheritance2.cpp index 25e62b25..21b8475a 100644 --- a/tests/json/test_inheritance2.cpp +++ b/tests/json/test_inheritance2.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "rfl/internal/num_fields.hpp" @@ -27,9 +28,12 @@ struct EmptyDerived1 : EmptyBase1, BaseX {}; struct EmptyDerived2 : EmptyBase1, EmptyBase2, BaseX {}; void test() { + std::cout << std::source_location::current().function_name() << std::endl; + Derived1 derived1; const auto derived1_view = rfl::to_view(derived1); static_assert(derived1_view.size() == 2); + Derived2 derived2; const auto derived2_view = rfl::to_view(derived2); static_assert(derived2_view.size() == 3); @@ -46,7 +50,7 @@ void test() { auto empty_derived2_view = rfl::to_view(empty_derived2); static_assert(empty_derived0_view.size() == 2); - std::cout << "OK\n"; + std::cout << "OK" << std::endl << std::endl; } -} // namespace test_inheritance \ No newline at end of file +} // namespace test_inheritance2 From 2f499714f878d09e9ccee7c4aa8eee63b1541940 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 18 Apr 2024 20:14:21 +0200 Subject: [PATCH 29/33] Updated the minimum required CMake version --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ac5739b..94643ce3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.23) option(REFLECTCPP_BUILD_SHARED "Build shared library" ${BUILD_SHARED_LIBS}) option(REFLECTCPP_BSON "Enable BSON support" OFF) @@ -109,4 +109,4 @@ install( EXPORT reflectcpp-exports DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/reflectcpp NAMESPACE reflectcpp:: -) \ No newline at end of file +) From 8a858d2cca4a6d8b3958e6375fe310400da45b99 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 21 Apr 2024 12:03:36 +0200 Subject: [PATCH 30/33] Add support for msgpack; resolves #26 (#87) --- .github/workflows/test.yaml | 6 +- .gitignore | 1 + CMakeLists.txt | 13 +- README.md | 9 +- docs/README.md | 8 +- docs/json.md | 2 +- docs/msgpack.md | 100 ++++++++++++ docs/yaml.md | 2 +- include/rfl/msgpack.hpp | 12 ++ include/rfl/msgpack/Parser.hpp | 43 +++++ include/rfl/msgpack/Reader.hpp | 144 ++++++++++++++++ include/rfl/msgpack/Writer.hpp | 154 ++++++++++++++++++ include/rfl/msgpack/load.hpp | 20 +++ include/rfl/msgpack/read.hpp | 56 +++++++ include/rfl/msgpack/save.hpp | 26 +++ include/rfl/msgpack/write.hpp | 42 +++++ tests/CMakeLists.txt | 4 + tests/msgpack/CMakeLists.txt | 8 + tests/msgpack/test_array.cpp | 40 +++++ tests/msgpack/test_array.hpp | 4 + tests/msgpack/test_box.cpp | 49 ++++++ tests/msgpack/test_box.hpp | 4 + tests/msgpack/test_custom_class1.cpp | 41 +++++ tests/msgpack/test_custom_class1.hpp | 4 + tests/msgpack/test_custom_class3.cpp | 67 ++++++++ tests/msgpack/test_custom_class3.hpp | 4 + tests/msgpack/test_custom_class4.cpp | 68 ++++++++ tests/msgpack/test_custom_class4.hpp | 4 + tests/msgpack/test_default_values.cpp | 32 ++++ tests/msgpack/test_default_values.hpp | 4 + tests/msgpack/test_deque.cpp | 33 ++++ tests/msgpack/test_deque.hpp | 4 + tests/msgpack/test_enum.cpp | 29 ++++ tests/msgpack/test_enum.hpp | 4 + tests/msgpack/test_field_variant.cpp | 39 +++++ tests/msgpack/test_field_variant.hpp | 4 + tests/msgpack/test_flag_enum.cpp | 39 +++++ tests/msgpack/test_flag_enum.hpp | 4 + tests/msgpack/test_flag_enum_with_int.cpp | 38 +++++ tests/msgpack/test_flag_enum_with_int.hpp | 4 + tests/msgpack/test_flatten.cpp | 38 +++++ tests/msgpack/test_flatten.hpp | 4 + tests/msgpack/test_flatten_anonymous.cpp | 39 +++++ tests/msgpack/test_flatten_anonymous.hpp | 4 + tests/msgpack/test_forward_list.cpp | 33 ++++ tests/msgpack/test_forward_list.hpp | 4 + tests/msgpack/test_literal.cpp | 30 ++++ tests/msgpack/test_literal.hpp | 4 + tests/msgpack/test_literal_map.cpp | 28 ++++ tests/msgpack/test_literal_map.hpp | 4 + tests/msgpack/test_map.cpp | 32 ++++ tests/msgpack/test_map.hpp | 4 + .../msgpack/test_map_with_key_validation.cpp | 33 ++++ .../msgpack/test_map_with_key_validation.hpp | 4 + tests/msgpack/test_monster_example.cpp | 64 ++++++++ tests/msgpack/test_monster_example.hpp | 4 + tests/msgpack/test_readme_example.cpp | 51 ++++++ tests/msgpack/test_readme_example.hpp | 4 + tests/msgpack/test_readme_example2.cpp | 26 +++ tests/msgpack/test_readme_example2.hpp | 4 + tests/msgpack/test_ref.cpp | 49 ++++++ tests/msgpack/test_ref.hpp | 4 + tests/msgpack/test_save_load.cpp | 70 ++++++++ tests/msgpack/test_save_load.hpp | 4 + tests/msgpack/test_set.cpp | 30 ++++ tests/msgpack/test_set.hpp | 4 + tests/msgpack/test_size.cpp | 42 +++++ tests/msgpack/test_size.hpp | 4 + tests/msgpack/test_string_map.cpp | 24 +++ tests/msgpack/test_string_map.hpp | 4 + tests/msgpack/test_tagged_union.cpp | 36 ++++ tests/msgpack/test_tagged_union.hpp | 4 + tests/msgpack/test_timestamp.cpp | 36 ++++ tests/msgpack/test_timestamp.hpp | 4 + tests/msgpack/test_unique_ptr.cpp | 33 ++++ tests/msgpack/test_unique_ptr.hpp | 4 + tests/msgpack/test_unique_ptr2.cpp | 48 ++++++ tests/msgpack/test_unique_ptr2.hpp | 4 + tests/msgpack/test_variant.cpp | 36 ++++ tests/msgpack/test_variant.hpp | 4 + tests/msgpack/test_wstring.cpp | 26 +++ tests/msgpack/test_wstring.hpp | 3 + tests/msgpack/tests.cpp | 67 ++++++++ tests/msgpack/write_and_read.hpp | 39 +++++ vcpkg.json | 6 +- 85 files changed, 2157 insertions(+), 11 deletions(-) create mode 100644 docs/msgpack.md create mode 100644 include/rfl/msgpack.hpp create mode 100644 include/rfl/msgpack/Parser.hpp create mode 100644 include/rfl/msgpack/Reader.hpp create mode 100644 include/rfl/msgpack/Writer.hpp create mode 100644 include/rfl/msgpack/load.hpp create mode 100644 include/rfl/msgpack/read.hpp create mode 100644 include/rfl/msgpack/save.hpp create mode 100644 include/rfl/msgpack/write.hpp create mode 100644 tests/msgpack/CMakeLists.txt create mode 100644 tests/msgpack/test_array.cpp create mode 100644 tests/msgpack/test_array.hpp create mode 100644 tests/msgpack/test_box.cpp create mode 100644 tests/msgpack/test_box.hpp create mode 100644 tests/msgpack/test_custom_class1.cpp create mode 100644 tests/msgpack/test_custom_class1.hpp create mode 100644 tests/msgpack/test_custom_class3.cpp create mode 100644 tests/msgpack/test_custom_class3.hpp create mode 100644 tests/msgpack/test_custom_class4.cpp create mode 100644 tests/msgpack/test_custom_class4.hpp create mode 100644 tests/msgpack/test_default_values.cpp create mode 100644 tests/msgpack/test_default_values.hpp create mode 100644 tests/msgpack/test_deque.cpp create mode 100644 tests/msgpack/test_deque.hpp create mode 100644 tests/msgpack/test_enum.cpp create mode 100644 tests/msgpack/test_enum.hpp create mode 100644 tests/msgpack/test_field_variant.cpp create mode 100644 tests/msgpack/test_field_variant.hpp create mode 100644 tests/msgpack/test_flag_enum.cpp create mode 100644 tests/msgpack/test_flag_enum.hpp create mode 100644 tests/msgpack/test_flag_enum_with_int.cpp create mode 100644 tests/msgpack/test_flag_enum_with_int.hpp create mode 100644 tests/msgpack/test_flatten.cpp create mode 100644 tests/msgpack/test_flatten.hpp create mode 100644 tests/msgpack/test_flatten_anonymous.cpp create mode 100644 tests/msgpack/test_flatten_anonymous.hpp create mode 100644 tests/msgpack/test_forward_list.cpp create mode 100644 tests/msgpack/test_forward_list.hpp create mode 100644 tests/msgpack/test_literal.cpp create mode 100644 tests/msgpack/test_literal.hpp create mode 100644 tests/msgpack/test_literal_map.cpp create mode 100644 tests/msgpack/test_literal_map.hpp create mode 100644 tests/msgpack/test_map.cpp create mode 100644 tests/msgpack/test_map.hpp create mode 100644 tests/msgpack/test_map_with_key_validation.cpp create mode 100644 tests/msgpack/test_map_with_key_validation.hpp create mode 100644 tests/msgpack/test_monster_example.cpp create mode 100644 tests/msgpack/test_monster_example.hpp create mode 100644 tests/msgpack/test_readme_example.cpp create mode 100644 tests/msgpack/test_readme_example.hpp create mode 100644 tests/msgpack/test_readme_example2.cpp create mode 100644 tests/msgpack/test_readme_example2.hpp create mode 100644 tests/msgpack/test_ref.cpp create mode 100644 tests/msgpack/test_ref.hpp create mode 100644 tests/msgpack/test_save_load.cpp create mode 100644 tests/msgpack/test_save_load.hpp create mode 100644 tests/msgpack/test_set.cpp create mode 100644 tests/msgpack/test_set.hpp create mode 100644 tests/msgpack/test_size.cpp create mode 100644 tests/msgpack/test_size.hpp create mode 100644 tests/msgpack/test_string_map.cpp create mode 100644 tests/msgpack/test_string_map.hpp create mode 100644 tests/msgpack/test_tagged_union.cpp create mode 100644 tests/msgpack/test_tagged_union.hpp create mode 100644 tests/msgpack/test_timestamp.cpp create mode 100644 tests/msgpack/test_timestamp.hpp create mode 100644 tests/msgpack/test_unique_ptr.cpp create mode 100644 tests/msgpack/test_unique_ptr.hpp create mode 100644 tests/msgpack/test_unique_ptr2.cpp create mode 100644 tests/msgpack/test_unique_ptr2.hpp create mode 100644 tests/msgpack/test_variant.cpp create mode 100644 tests/msgpack/test_variant.hpp create mode 100644 tests/msgpack/test_wstring.cpp create mode 100644 tests/msgpack/test_wstring.hpp create mode 100644 tests/msgpack/tests.cpp create mode 100644 tests/msgpack/write_and_read.hpp diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e9851d07..e86190b3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,12 +20,13 @@ jobs: - name: Run test run: | g++ --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests + ./build/tests/msgpack/reflect-cpp-msgpack-tests ./build/tests/toml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/yaml/reflect-cpp-yaml-tests @@ -54,12 +55,13 @@ jobs: CXX: clang++ run: | clang --version - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/json/reflect-cpp-json-tests + ./build/tests/msgpack/reflect-cpp-msgpack-tests ./build/tests/toml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/yaml/reflect-cpp-yaml-tests diff --git a/.gitignore b/.gitignore index d57c7e6f..36fa4976 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ *.cbor *.json *.fb +*.msgpack *.toml *.xml *.yml diff --git a/CMakeLists.txt b/CMakeLists.txt index 94643ce3..ad51489b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ option(REFLECTCPP_BUILD_SHARED "Build shared library" ${BUILD_SHARED_LIBS}) option(REFLECTCPP_BSON "Enable BSON support" OFF) option(REFLECTCPP_CBOR "Enable CBOR support" OFF) option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) +option(REFLECTCPP_MSGPACK "Enable msgpack support" OFF) option(REFLECTCPP_XML "Enable XML support" OFF) option(REFLECTCPP_TOML "Enable TOML support" OFF) option(REFLECTCPP_YAML "Enable YAML support" OFF) @@ -11,7 +12,7 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) set(REFLECTCPP_USE_VCPKG_DEFAULT OFF) -if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) +if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) # enable vcpkg per default if require features other than JSON set(REFLECTCPP_USE_VCPKG_DEFAULT ON) endif() @@ -54,6 +55,16 @@ if (REFLECTCPP_FLEXBUFFERS) target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers) endif () +if (REFLECTCPP_MSGPACK) + message(${VCPKG_INSTALLED_DIR}) + find_package(msgpack-c CONFIG REQUIRED) + if (MSVC) + target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/msgpack-c${CMAKE_STATIC_LIBRARY_SUFFIX}") + else () + target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libmsgpack-c${CMAKE_STATIC_LIBRARY_SUFFIX}") + endif () +endif () + if (REFLECTCPP_TOML) find_package(PkgConfig REQUIRED) pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) diff --git a/README.md b/README.md index cc5ce0f9..2a005924 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ The following table lists the serialization formats currently supported by refle | BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | JSON-like binary format | | CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format | | flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format | -| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability | +| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format | +| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability | | XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects | | YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability | @@ -96,12 +97,14 @@ and any supported format, except where explicitly noted otherwise: rfl::bson::write(homer); rfl::cbor::write(homer); rfl::flexbuf::write(homer); +rfl::msgpack::write(homer); rfl::toml::write(homer); rfl::xml::write(homer); rfl::bson::read(bson_bytes); rfl::cbor::read(cbor_bytes); rfl::flexbuf::read(flexbuf_bytes); +rfl::msgpack::read(msgpack_bytes); rfl::toml::read(toml_string); rfl::xml::read(xml_string); ``` @@ -507,6 +510,7 @@ add_subdirectory(reflect-cpp) # Add this project as a subdirectory set(REFLECTCPP_BSON ON) # Optional set(REFLECTCPP_CBOR ON) # Optional set(REFLECTCPP_FLEXBUFFERS ON) # Optional +set(REFLECTCPP_MSGPACK ON) # Optional set(REFLECTCPP_TOML ON) # Optional set(REFLECTCPP_XML ON) # Optional set(REFLECTCPP_YAML ON) # Optional @@ -553,7 +557,7 @@ git submodule update --init ./vcpkg/bootstrap-vcpkg.bat # Windows # You may be prompted to install additional dependencies. -cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release +cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 # gcc, clang cmake --build build --config Release -j 4 # MSVC ``` @@ -564,6 +568,7 @@ To run the tests, do the following: ./build/tests/bson/reflect-cpp-bson-tests ./build/tests/cbor/reflect-cpp-cbor-tests ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests +./build/tests/msgpack/reflect-cpp-msgpack-tests ./build/tests/json/reflect-cpp-json-tests ./build/tests/xml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests diff --git a/docs/README.md b/docs/README.md index de570bb9..d295a11a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -64,11 +64,13 @@ 5.4) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md) -5.5) [TOML](https://github.com/getml/reflect-cpp/blob/main/docs/toml.md) +5.5) [msgpack](https://github.com/getml/reflect-cpp/blob/main/docs/msgpack.md) -5.6) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) +5.6) [TOML](https://github.com/getml/reflect-cpp/blob/main/docs/toml.md) -5.7) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) +5.7) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md) + +5.8) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md) ## 6) Advanced topics diff --git a/docs/json.md b/docs/json.md index 45f6a5ff..ad0ca153 100644 --- a/docs/json.md +++ b/docs/json.md @@ -21,7 +21,7 @@ You can parse JSON strings like this: const rfl::Result result = rfl::json::read(json_string); ``` -A `Person` struct can be written into a JSON string like this: +A `Person` struct can be serialized like this: ```cpp const auto person = Person{...}; diff --git a/docs/msgpack.md b/docs/msgpack.md new file mode 100644 index 00000000..45524de0 --- /dev/null +++ b/docs/msgpack.md @@ -0,0 +1,100 @@ +# msgpack + +For msgpack support, you must also include the header `` and link to the msgpack-c library (https://github.com/msgpack/msgpack-c). + +msgpack is a JSON-like binary format with an emphasis on small binary sizes. + +## Reading and writing + +Suppose you have a struct like this: + +```cpp +struct Person { + std::string first_name; + std::string last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + std::vector children; +}; +``` + +A `Person` can be serialized like this: + +```cpp +const auto person = Person{...}; +const std::vector bytes = rfl::msgpack::write(person); +``` + +You can parse bytes like this: + +```cpp +const rfl::Result result = rfl::msgpack::read(bytes); +``` + +## Loading and saving + +You can also load and save to disc using a very similar syntax: + +```cpp +const rfl::Result result = rfl::msgpack::load("/path/to/file.msgpack"); + +const auto person = Person{...}; +rfl::msgpack::save("/path/to/file.msgpack", person); +``` + +## Reading from and writing into streams + +You can also read from and write into any `std::istream` and `std::ostream` respectively. + +```cpp +const rfl::Result result = rfl::msgpack::read(my_istream); + +const auto person = Person{...}; +rfl::msgpack::write(person, my_ostream); +``` + +Note that `std::cout` is also an ostream, so this works as well: + +```cpp +rfl::msgpack::write(person, std::cout) << std::endl; +``` + +(Since msgpack is a binary format, the readability of this will be limited, but it might be useful for debugging). + +## Custom constructors + +One of the great things about C++ is that it gives you control over +when and how you code is compiled. + +For large and complex systems of structs, it is often a good idea to split up +your code into smaller compilation units. You can do so using custom constructors. + +For the msgpack format, these must be a static function on your struct or class called +`from_msgpack` that take a `rfl::msgpack::Reader::InputVarType` as input and return +the class or the class wrapped in `rfl::Result`. + +In your header file you can write something like this: + +```cpp +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + + using InputVarType = typename rfl::msgpack::Reader::InputVarType; + static rfl::Result from_msgpack(const InputVarType& _obj); +}; +``` + +And in your source file, you implement `from_msgpack` as follows: + +```cpp +rfl::Result Person::from_msgpack(const InputVarType& _obj) { + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::msgpack::read>(_obj) + .transform(from_nt); +} +``` + +This will force the compiler to only compile the msgpack parsing when the source file is compiled. diff --git a/docs/yaml.md b/docs/yaml.md index 189caa88..12589e1f 100644 --- a/docs/yaml.md +++ b/docs/yaml.md @@ -21,7 +21,7 @@ You can parse YAML strings like this: const rfl::Result result = rfl::yaml::read(yaml_string); ``` -A `person` can be serialized like this: +A `Person` can be serialized like this: ```cpp const auto person = Person{...}; diff --git a/include/rfl/msgpack.hpp b/include/rfl/msgpack.hpp new file mode 100644 index 00000000..3e006d17 --- /dev/null +++ b/include/rfl/msgpack.hpp @@ -0,0 +1,12 @@ +#ifndef RFL_MSGPACK_HPP_ +#define RFL_MSGPACK_HPP_ + +#include "msgpack/Parser.hpp" +#include "msgpack/Reader.hpp" +#include "msgpack/Writer.hpp" +#include "msgpack/load.hpp" +#include "msgpack/read.hpp" +#include "msgpack/save.hpp" +#include "msgpack/write.hpp" + +#endif diff --git a/include/rfl/msgpack/Parser.hpp b/include/rfl/msgpack/Parser.hpp new file mode 100644 index 00000000..506cca0e --- /dev/null +++ b/include/rfl/msgpack/Parser.hpp @@ -0,0 +1,43 @@ +#ifndef RFL_MSGPACK_PARSER_HPP_ +#define RFL_MSGPACK_PARSER_HPP_ + +#include "../parsing/Parser.hpp" +#include "Reader.hpp" +#include "Writer.hpp" + +namespace rfl { +namespace parsing { + +/// msgpack-c requires us to explicitly set the number of fields in advance. +/// Because of that, we require all of the fields and then set them to nullptr, +/// if necessary. +template +requires AreReaderAndWriter> +struct Parser> + : public NamedTupleParser { +}; + +template +requires AreReaderAndWriter> +struct Parser> + : public TupleParser { +}; + +} // namespace parsing +} // namespace rfl + +namespace rfl { +namespace msgpack { + +template +using Parser = parsing::Parser; + +} +} // namespace rfl + +#endif diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp new file mode 100644 index 00000000..14064177 --- /dev/null +++ b/include/rfl/msgpack/Reader.hpp @@ -0,0 +1,144 @@ +#ifndef RFL_MSGPACK_READER_HPP_ +#define RFL_MSGPACK_READER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Box.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl { +namespace msgpack { + +struct Reader { + using InputArrayType = msgpack_object_array; + using InputObjectType = msgpack_object_map; + using InputVarType = msgpack_object; + + template + static constexpr bool has_custom_constructor = (requires(InputVarType var) { + T::from_msgpack_obj(var); + }); + + rfl::Result get_field( + const std::string& _name, const InputObjectType& _obj) const noexcept { + for (uint32_t i = 0; i < _obj.size; ++i) { + const auto& key = _obj.ptr[i].key; + if (key.type != MSGPACK_OBJECT_STR) { + return Error("Key in element " + std::to_string(i) + + " was not a string."); + } + const auto current_name = + std::string_view(key.via.str.ptr, key.via.str.size); + if (_name == current_name) { + return _obj.ptr[i].val; + } + } + return Error("No field named '" + _name + "' was found."); + } + + bool is_empty(const InputVarType& _var) const noexcept { + return _var.type == MSGPACK_OBJECT_NIL; + } + + template + rfl::Result to_basic_type(const InputVarType& _var) const noexcept { + const auto type = _var.type; + if constexpr (std::is_same, std::string>()) { + if (type != MSGPACK_OBJECT_STR) { + return Error("Could not cast to string."); + } + const auto str = _var.via.str; + return std::string(str.ptr, str.size); + } else if constexpr (std::is_same, bool>()) { + if (type != MSGPACK_OBJECT_BOOLEAN) { + return Error("Could not cast to boolean."); + } + return _var.via.boolean; + } else if constexpr (std::is_floating_point>() || + std::is_integral>()) { + if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 || + type == MSGPACK_OBJECT_FLOAT) { + return static_cast(_var.via.f64); + } else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) { + return static_cast(_var.via.u64); + } else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) { + return static_cast(_var.via.i64); + } + return rfl::Error( + "Could not cast to numeric value. The type must be integral, float " + "or double."); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + } + + rfl::Result to_array( + const InputVarType& _var) const noexcept { + if (_var.type != MSGPACK_OBJECT_ARRAY) { + return Error("Could not cast to an array."); + } + return _var.via.array; + } + + rfl::Result to_object( + const InputVarType& _var) const noexcept { + if (_var.type != MSGPACK_OBJECT_MAP) { + return Error("Could not cast to a map."); + } + return _var.via.map; + } + + std::vector to_vec(const InputArrayType& _arr) const noexcept { + std::vector vec; + for (uint32_t i = 0; i < _arr.size; ++i) { + vec.push_back(_arr.ptr[i]); + } + return vec; + } + + template + std::optional read_object(const ObjectReader& _object_reader, + const InputObjectType& _obj) const noexcept { + for (uint32_t i = 0; i < _obj.size; ++i) { + const auto& key = _obj.ptr[i].key; + const auto& val = _obj.ptr[i].val; + if (key.type != MSGPACK_OBJECT_STR) { + return Error("Key in element " + std::to_string(i) + + " was not a string."); + } + const auto name = std::string_view(key.via.str.ptr, key.via.str.size); + _object_reader.read(name, val); + } + return std::nullopt; + } + + template + rfl::Result use_custom_constructor( + const InputVarType& _var) const noexcept { + try { + return T::from_msgpack_obj(_var); + } catch (std::exception& e) { + return rfl::Error(e.what()); + } + } +}; + +} // namespace msgpack +} // namespace rfl + +#endif // JSON_PARSER_HPP_ diff --git a/include/rfl/msgpack/Writer.hpp b/include/rfl/msgpack/Writer.hpp new file mode 100644 index 00000000..0ee2796d --- /dev/null +++ b/include/rfl/msgpack/Writer.hpp @@ -0,0 +1,154 @@ +#ifndef RFL_MSGPACK_WRITER_HPP_ +#define RFL_MSGPACK_WRITER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Box.hpp" +#include "../Ref.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" + +namespace rfl::msgpack { + +class Writer { + public: + struct MsgpackOutputArray {}; + + struct MsgpackOutputObject {}; + + struct MsgpackOutputVar {}; + + using OutputArrayType = MsgpackOutputArray; + using OutputObjectType = MsgpackOutputObject; + using OutputVarType = MsgpackOutputVar; + + Writer(msgpack_packer* _pk) : pk_(_pk) {} + + ~Writer() = default; + + OutputArrayType array_as_root(const size_t _size) const noexcept { + return new_array(_size); + } + + OutputObjectType object_as_root(const size_t _size) const noexcept { + return new_object(_size); + } + + OutputVarType null_as_root() const noexcept { + msgpack_pack_nil(pk_); + return OutputVarType{}; + } + + template + OutputVarType value_as_root(const T& _var) const noexcept { + return new_value(_var); + } + + OutputArrayType add_array_to_array(const size_t _size, + OutputArrayType* _parent) const noexcept { + return new_array(_size); + } + + OutputArrayType add_array_to_object( + const std::string_view& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + msgpack_pack_str(pk_, _name.size()); + msgpack_pack_str_body(pk_, _name.data(), _name.size()); + return new_array(_size); + } + + OutputObjectType add_object_to_array( + const size_t _size, OutputArrayType* _parent) const noexcept { + return new_object(_size); + } + + OutputObjectType add_object_to_object( + const std::string_view& _name, const size_t _size, + OutputObjectType* _parent) const noexcept { + msgpack_pack_str(pk_, _name.size()); + msgpack_pack_str_body(pk_, _name.data(), _name.size()); + return new_object(_size); + } + + template + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + return new_value(_var); + } + + template + OutputVarType add_value_to_object(const std::string_view& _name, + const T& _var, + OutputObjectType* _parent) const noexcept { + msgpack_pack_str(pk_, _name.size()); + msgpack_pack_str_body(pk_, _name.data(), _name.size()); + return new_value(_var); + } + + OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept { + msgpack_pack_nil(pk_); + return OutputVarType{}; + } + + OutputVarType add_null_to_object(const std::string_view& _name, + OutputObjectType* _parent) const noexcept { + msgpack_pack_str(pk_, _name.size()); + msgpack_pack_str_body(pk_, _name.data(), _name.size()); + msgpack_pack_nil(pk_); + return OutputVarType{}; + } + + void end_array(OutputArrayType* _arr) const noexcept {} + + void end_object(OutputObjectType* _obj) const noexcept {} + + private: + OutputArrayType new_array(const size_t _size) const noexcept { + msgpack_pack_array(pk_, _size); + return OutputArrayType{}; + } + + OutputObjectType new_object(const size_t _size) const noexcept { + msgpack_pack_map(pk_, _size); + return OutputObjectType{}; + } + + template + OutputVarType new_value(const T& _var) const noexcept { + if constexpr (std::is_same, std::string>()) { + msgpack_pack_str(pk_, _var.size()); + msgpack_pack_str_body(pk_, _var.c_str(), _var.size()); + } else if constexpr (std::is_same, bool>()) { + if (_var) { + msgpack_pack_true(pk_); + } else { + msgpack_pack_false(pk_); + } + } else if constexpr (std::is_floating_point>()) { + msgpack_pack_double(pk_, static_cast(_var)); + } else if constexpr (std::is_integral>()) { + msgpack_pack_int64(pk_, static_cast(_var)); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + return OutputVarType{}; + } + + private: + /// The underlying packer. + msgpack_packer* pk_; +}; + +} // namespace rfl::msgpack + +#endif // MSGPACK_PARSER_HPP_ diff --git a/include/rfl/msgpack/load.hpp b/include/rfl/msgpack/load.hpp new file mode 100644 index 00000000..52d5b5b3 --- /dev/null +++ b/include/rfl/msgpack/load.hpp @@ -0,0 +1,20 @@ +#ifndef RFL_MSGPACK_LOAD_HPP_ +#define RFL_MSGPACK_LOAD_HPP_ + +#include "../Result.hpp" +#include "../io/load_bytes.hpp" +#include "read.hpp" + +namespace rfl { +namespace msgpack { + +template +Result load(const std::string& _fname) { + const auto read_bytes = [](const auto& _bytes) { return read(_bytes); }; + return rfl::io::load_bytes(_fname).and_then(read_bytes); +} + +} // namespace msgpack +} // namespace rfl + +#endif diff --git a/include/rfl/msgpack/read.hpp b/include/rfl/msgpack/read.hpp new file mode 100644 index 00000000..18da56ea --- /dev/null +++ b/include/rfl/msgpack/read.hpp @@ -0,0 +1,56 @@ +#ifndef RFL_MSGPACK_READ_HPP_ +#define RFL_MSGPACK_READ_HPP_ + +#include + +#include +#include + +#include "../internal/wrap_in_rfl_array_t.hpp" +#include "Parser.hpp" +#include "Reader.hpp" + +namespace rfl { +namespace msgpack { + +using InputObjectType = typename Reader::InputObjectType; +using InputVarType = typename Reader::InputVarType; + +/// Parses an object from a MSGPACK var. +template +auto read(const InputVarType& _obj) { + const auto r = Reader(); + return Parser::read(r, _obj); +} + +/// Parses an object from MSGPACK using reflection. +template +Result> read(const char* _bytes, + const size_t _size) { + msgpack_zone mempool; + msgpack_zone_init(&mempool, 2048); + msgpack_object deserialized; + msgpack_unpack(_bytes, _size, NULL, &mempool, &deserialized); + auto r = read(deserialized); + msgpack_zone_destroy(&mempool); + return r; +} + +/// Parses an object from MSGPACK using reflection. +template +auto read(const std::vector& _bytes) { + return read(_bytes.data(), _bytes.size()); +} + +/// Parses an object from a stream. +template +auto read(std::istream& _stream) { + std::istreambuf_iterator begin(_stream), end; + auto bytes = std::vector(begin, end); + return read(bytes.data(), bytes.size()); +} + +} // namespace msgpack +} // namespace rfl + +#endif diff --git a/include/rfl/msgpack/save.hpp b/include/rfl/msgpack/save.hpp new file mode 100644 index 00000000..aeb9d33d --- /dev/null +++ b/include/rfl/msgpack/save.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_MSGPACK_SAVE_HPP_ +#define RFL_MSGPACK_SAVE_HPP_ + +#include +#include +#include + +#include "../Result.hpp" +#include "../io/save_bytes.hpp" +#include "write.hpp" + +namespace rfl { +namespace msgpack { + +template +Result save(const std::string& _fname, const T& _obj) { + const auto write_func = [](const auto& _obj, auto& _stream) -> auto& { + return write(_obj, _stream); + }; + return rfl::io::save_bytes(_fname, _obj, write_func); +} + +} // namespace msgpack +} // namespace rfl + +#endif diff --git a/include/rfl/msgpack/write.hpp b/include/rfl/msgpack/write.hpp new file mode 100644 index 00000000..46315415 --- /dev/null +++ b/include/rfl/msgpack/write.hpp @@ -0,0 +1,42 @@ +#ifndef RFL_MSGPACK_WRITE_HPP_ +#define RFL_MSGPACK_WRITE_HPP_ + +#include + +#include +#include +#include +#include +#include + +#include "../parsing/Parent.hpp" +#include "Parser.hpp" + +namespace rfl::msgpack { + +/// Returns msgpack bytes. +template +std::vector write(const T& _obj) noexcept { + using ParentType = parsing::Parent; + msgpack_sbuffer sbuf; + msgpack_sbuffer_init(&sbuf); + msgpack_packer pk; + msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); + auto w = Writer(&pk); + Parser::write(w, _obj, typename ParentType::Root{}); + auto bytes = std::vector(sbuf.data, sbuf.data + sbuf.size); + msgpack_sbuffer_destroy(&sbuf); + return bytes; +} + +/// Writes a MSGPACK into an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) noexcept { + auto buffer = write(_obj); + _stream.write(buffer.data(), buffer.size()); + return _stream; +} + +} // namespace rfl::msgpack + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 905ffead..29133d7e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,6 +20,10 @@ if (REFLECTCPP_FLEXBUFFERS) add_subdirectory(flexbuffers) endif() +if (REFLECTCPP_MSGPACK) + add_subdirectory(msgpack) +endif() + if (REFLECTCPP_XML) add_subdirectory(xml) endif() diff --git a/tests/msgpack/CMakeLists.txt b/tests/msgpack/CMakeLists.txt new file mode 100644 index 00000000..222ea521 --- /dev/null +++ b/tests/msgpack/CMakeLists.txt @@ -0,0 +1,8 @@ +project(reflect-cpp-msgpack-tests) + +file(GLOB_RECURSE SOURCES "*.cpp") + +add_executable(reflect-cpp-msgpack-tests ${SOURCES}) + +target_include_directories(reflect-cpp-msgpack-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") +target_link_libraries(reflect-cpp-msgpack-tests PRIVATE reflectcpp) diff --git a/tests/msgpack/test_array.cpp b/tests/msgpack/test_array.cpp new file mode 100644 index 00000000..75f670d2 --- /dev/null +++ b/tests/msgpack/test_array.cpp @@ -0,0 +1,40 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/cbor.hpp. +#include + +#include "write_and_read.hpp" + +namespace test_array { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = Person{ + .first_name = "Homer", + .children = std::make_unique>(std::array{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read(homer); +} +} // namespace test_array diff --git a/tests/msgpack/test_array.hpp b/tests/msgpack/test_array.hpp new file mode 100644 index 00000000..502c3388 --- /dev/null +++ b/tests/msgpack/test_array.hpp @@ -0,0 +1,4 @@ +namespace test_array{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_box.cpp b/tests/msgpack/test_box.cpp new file mode 100644 index 00000000..fdb3516a --- /dev/null +++ b/tests/msgpack/test_box.cpp @@ -0,0 +1,49 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_box { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Box lesser; + rfl::Box greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_box diff --git a/tests/msgpack/test_box.hpp b/tests/msgpack/test_box.hpp new file mode 100644 index 00000000..a564b9e1 --- /dev/null +++ b/tests/msgpack/test_box.hpp @@ -0,0 +1,4 @@ +namespace test_box{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_custom_class1.cpp b/tests/msgpack/test_custom_class1.cpp new file mode 100644 index 00000000..fe8d0ea0 --- /dev/null +++ b/tests/msgpack/test_custom_class1.cpp @@ -0,0 +1,41 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class1 { + +struct Person { + struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart"); + + write_and_read(bart); +} +} // namespace test_custom_class1 diff --git a/tests/msgpack/test_custom_class1.hpp b/tests/msgpack/test_custom_class1.hpp new file mode 100644 index 00000000..eafe6cd0 --- /dev/null +++ b/tests/msgpack/test_custom_class1.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class1{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_custom_class3.cpp b/tests/msgpack/test_custom_class3.cpp new file mode 100644 index 00000000..598364b1 --- /dev/null +++ b/tests/msgpack/test_custom_class3.cpp @@ -0,0 +1,67 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class3 { + +struct Person { + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + std::string last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + int age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = _p.last_name(), + .age = _p.age()}; + } + + Person to_class() const { return Person(first_name(), last_name(), age); } +}; +} // namespace test_custom_class3 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class3 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart", "Simpson", 10); + + write_and_read(bart); +} + +} // namespace test_custom_class3 diff --git a/tests/msgpack/test_custom_class3.hpp b/tests/msgpack/test_custom_class3.hpp new file mode 100644 index 00000000..9a6fdab4 --- /dev/null +++ b/tests/msgpack/test_custom_class3.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class3{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_custom_class4.cpp b/tests/msgpack/test_custom_class4.cpp new file mode 100644 index 00000000..2b3b3bac --- /dev/null +++ b/tests/msgpack/test_custom_class4.cpp @@ -0,0 +1,68 @@ +#include "test_custom_class4.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class4 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class4::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart); +} +} // namespace test_custom_class4 diff --git a/tests/msgpack/test_custom_class4.hpp b/tests/msgpack/test_custom_class4.hpp new file mode 100644 index 00000000..2d3b151a --- /dev/null +++ b/tests/msgpack/test_custom_class4.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class4{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_default_values.cpp b/tests/msgpack/test_default_values.cpp new file mode 100644 index 00000000..f6af2352 --- /dev/null +++ b/tests/msgpack/test_default_values.cpp @@ -0,0 +1,32 @@ +#include "test_default_values.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_default_values { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart"}; + const auto lisa = Person{.first_name = "Lisa"}; + const auto maggie = Person{.first_name = "Maggie"}; + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_default_values diff --git a/tests/msgpack/test_default_values.hpp b/tests/msgpack/test_default_values.hpp new file mode 100644 index 00000000..c8f8360e --- /dev/null +++ b/tests/msgpack/test_default_values.hpp @@ -0,0 +1,4 @@ +namespace test_default_values{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_deque.cpp b/tests/msgpack/test_deque.cpp new file mode 100644 index 00000000..6dfa5803 --- /dev/null +++ b/tests/msgpack/test_deque.cpp @@ -0,0 +1,33 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deque { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_deque diff --git a/tests/msgpack/test_deque.hpp b/tests/msgpack/test_deque.hpp new file mode 100644 index 00000000..6781e880 --- /dev/null +++ b/tests/msgpack/test_deque.hpp @@ -0,0 +1,4 @@ +namespace test_deque{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_enum.cpp b/tests/msgpack/test_enum.cpp new file mode 100644 index 00000000..d4d56a8b --- /dev/null +++ b/tests/msgpack/test_enum.cpp @@ -0,0 +1,29 @@ +#include "test_enum.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle); +} + +} // namespace test_enum diff --git a/tests/msgpack/test_enum.hpp b/tests/msgpack/test_enum.hpp new file mode 100644 index 00000000..2e2e0b3d --- /dev/null +++ b/tests/msgpack/test_enum.hpp @@ -0,0 +1,4 @@ +namespace test_enum { +void test(); +} + diff --git a/tests/msgpack/test_field_variant.cpp b/tests/msgpack/test_field_variant.cpp new file mode 100644 index 00000000..8b17665f --- /dev/null +++ b/tests/msgpack/test_field_variant.cpp @@ -0,0 +1,39 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_field_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::Variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r); +} +} // namespace test_field_variant diff --git a/tests/msgpack/test_field_variant.hpp b/tests/msgpack/test_field_variant.hpp new file mode 100644 index 00000000..ba93e732 --- /dev/null +++ b/tests/msgpack/test_field_variant.hpp @@ -0,0 +1,4 @@ +namespace test_field_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_flag_enum.cpp b/tests/msgpack/test_flag_enum.cpp new file mode 100644 index 00000000..1d3f76af --- /dev/null +++ b/tests/msgpack/test_flag_enum.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = + Circle{.radius = 2.0, .color = Color::blue | Color::orange}; + + write_and_read(circle); +} + +} // namespace test_flag_enum diff --git a/tests/msgpack/test_flag_enum.hpp b/tests/msgpack/test_flag_enum.hpp new file mode 100644 index 00000000..2f4dc7a0 --- /dev/null +++ b/tests/msgpack/test_flag_enum.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum { +void test(); +} + diff --git a/tests/msgpack/test_flag_enum_with_int.cpp b/tests/msgpack/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..4c7deefe --- /dev/null +++ b/tests/msgpack/test_flag_enum_with_int.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "test_enum.hpp" +#include "write_and_read.hpp" + +namespace test_flag_enum_with_int { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/msgpack/test_flag_enum_with_int.hpp b/tests/msgpack/test_flag_enum_with_int.hpp new file mode 100644 index 00000000..a7512b60 --- /dev/null +++ b/tests/msgpack/test_flag_enum_with_int.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum_with_int { +void test(); +} + diff --git a/tests/msgpack/test_flatten.cpp b/tests/msgpack/test_flatten.cpp new file mode 100644 index 00000000..c7f1d9be --- /dev/null +++ b/tests/msgpack/test_flatten.cpp @@ -0,0 +1,38 @@ +#include "test_flatten.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten diff --git a/tests/msgpack/test_flatten.hpp b/tests/msgpack/test_flatten.hpp new file mode 100644 index 00000000..24d60e11 --- /dev/null +++ b/tests/msgpack/test_flatten.hpp @@ -0,0 +1,4 @@ +namespace test_flatten{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_flatten_anonymous.cpp b/tests/msgpack/test_flatten_anonymous.cpp new file mode 100644 index 00000000..05e61464 --- /dev/null +++ b/tests/msgpack/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box employer; + float salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} + +} // namespace test_flatten_anonymous diff --git a/tests/msgpack/test_flatten_anonymous.hpp b/tests/msgpack/test_flatten_anonymous.hpp new file mode 100644 index 00000000..7ffa2785 --- /dev/null +++ b/tests/msgpack/test_flatten_anonymous.hpp @@ -0,0 +1,4 @@ +namespace test_flatten_anonymous{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_forward_list.cpp b/tests/msgpack/test_forward_list.cpp new file mode 100644 index 00000000..dd8d9e56 --- /dev/null +++ b/tests/msgpack/test_forward_list.cpp @@ -0,0 +1,33 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_forward_list { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_front(Person{.first_name = "Maggie"}); + children->emplace_front(Person{.first_name = "Lisa"}); + children->emplace_front(Person{.first_name = "Bart"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); + +} +} // namespace test_forward_list diff --git a/tests/msgpack/test_forward_list.hpp b/tests/msgpack/test_forward_list.hpp new file mode 100644 index 00000000..9784a0c4 --- /dev/null +++ b/tests/msgpack/test_forward_list.hpp @@ -0,0 +1,4 @@ +namespace test_forward_list{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_literal.cpp b/tests/msgpack/test_literal.cpp new file mode 100644 index 00000000..00578178 --- /dev/null +++ b/tests/msgpack/test_literal.cpp @@ -0,0 +1,30 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal { + +using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; +using LastName = rfl::Literal<"Simpson">; + +struct Person { + rfl::Rename<"firstName", FirstName> first_name; + rfl::Rename<"lastName", LastName> last_name; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart); +} +} // namespace test_literal diff --git a/tests/msgpack/test_literal.hpp b/tests/msgpack/test_literal.hpp new file mode 100644 index 00000000..ccd500ef --- /dev/null +++ b/tests/msgpack/test_literal.hpp @@ -0,0 +1,4 @@ +namespace test_literal{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_literal_map.cpp b/tests/msgpack/test_literal_map.cpp new file mode 100644 index 00000000..9bbebafb --- /dev/null +++ b/tests/msgpack/test_literal_map.cpp @@ -0,0 +1,28 @@ +#include "test_literal_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_literal_map diff --git a/tests/msgpack/test_literal_map.hpp b/tests/msgpack/test_literal_map.hpp new file mode 100644 index 00000000..cc05d0c8 --- /dev/null +++ b/tests/msgpack/test_literal_map.hpp @@ -0,0 +1,4 @@ +namespace test_literal_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_map.cpp b/tests/msgpack/test_map.cpp new file mode 100644 index 00000000..e37ce564 --- /dev/null +++ b/tests/msgpack/test_map.cpp @@ -0,0 +1,32 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::map children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map diff --git a/tests/msgpack/test_map.hpp b/tests/msgpack/test_map.hpp new file mode 100644 index 00000000..9ae49728 --- /dev/null +++ b/tests/msgpack/test_map.hpp @@ -0,0 +1,4 @@ +namespace test_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_map_with_key_validation.cpp b/tests/msgpack/test_map_with_key_validation.cpp new file mode 100644 index 00000000..e758219d --- /dev/null +++ b/tests/msgpack/test_map_with_key_validation.cpp @@ -0,0 +1,33 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + + children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/msgpack/test_map_with_key_validation.hpp b/tests/msgpack/test_map_with_key_validation.hpp new file mode 100644 index 00000000..1372f926 --- /dev/null +++ b/tests/msgpack/test_map_with_key_validation.hpp @@ -0,0 +1,4 @@ +namespace test_map_with_key_validation{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_monster_example.cpp b/tests/msgpack/test_monster_example.cpp new file mode 100644 index 00000000..052d8390 --- /dev/null +++ b/tests/msgpack/test_monster_example.cpp @@ -0,0 +1,64 @@ +#include "test_monster_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_monster_example diff --git a/tests/msgpack/test_monster_example.hpp b/tests/msgpack/test_monster_example.hpp new file mode 100644 index 00000000..f2d959fc --- /dev/null +++ b/tests/msgpack/test_monster_example.hpp @@ -0,0 +1,4 @@ +namespace test_monster_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_readme_example.cpp b/tests/msgpack/test_readme_example.cpp new file mode 100644 index 00000000..4e6f8ee0 --- /dev/null +++ b/tests/msgpack/test_readme_example.cpp @@ -0,0 +1,51 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector child; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/msgpack/test_readme_example.hpp b/tests/msgpack/test_readme_example.hpp new file mode 100644 index 00000000..68c6cf81 --- /dev/null +++ b/tests/msgpack/test_readme_example.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_readme_example2.cpp b/tests/msgpack/test_readme_example2.cpp new file mode 100644 index 00000000..a2d212a4 --- /dev/null +++ b/tests/msgpack/test_readme_example2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#include "test_readme_example.hpp" +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/msgpack/test_readme_example2.hpp b/tests/msgpack/test_readme_example2.hpp new file mode 100644 index 00000000..5c6b011c --- /dev/null +++ b/tests/msgpack/test_readme_example2.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example2 { +void test(); +} + diff --git a/tests/msgpack/test_ref.cpp b/tests/msgpack/test_ref.cpp new file mode 100644 index 00000000..32a32e6c --- /dev/null +++ b/tests/msgpack/test_ref.cpp @@ -0,0 +1,49 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Ref lesser; + rfl::Ref greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_ref diff --git a/tests/msgpack/test_ref.hpp b/tests/msgpack/test_ref.hpp new file mode 100644 index 00000000..d289ba09 --- /dev/null +++ b/tests/msgpack/test_ref.hpp @@ -0,0 +1,4 @@ +namespace test_ref{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_save_load.cpp b/tests/msgpack/test_save_load.cpp new file mode 100644 index 00000000..3e8c4703 --- /dev/null +++ b/tests/msgpack/test_save_load.cpp @@ -0,0 +1,70 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::msgpack::save("homer.msgpack", homer1); + + const auto homer2 = rfl::msgpack::load("homer.msgpack").value(); + + const auto string1 = rfl::msgpack::write(homer1); + const auto string2 = rfl::msgpack::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/msgpack/test_save_load.hpp b/tests/msgpack/test_save_load.hpp new file mode 100644 index 00000000..7bf10359 --- /dev/null +++ b/tests/msgpack/test_save_load.hpp @@ -0,0 +1,4 @@ +namespace test_save_load { +void test(); +} + diff --git a/tests/msgpack/test_set.cpp b/tests/msgpack/test_set.cpp new file mode 100644 index 00000000..9f2516ce --- /dev/null +++ b/tests/msgpack/test_set.cpp @@ -0,0 +1,30 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_set diff --git a/tests/msgpack/test_set.hpp b/tests/msgpack/test_set.hpp new file mode 100644 index 00000000..142a663b --- /dev/null +++ b/tests/msgpack/test_set.hpp @@ -0,0 +1,4 @@ +namespace test_set{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_size.cpp b/tests/msgpack/test_size.cpp new file mode 100644 index 00000000..c37b9bd8 --- /dev/null +++ b/tests/msgpack/test_size.cpp @@ -0,0 +1,42 @@ +#include "test_size.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_size diff --git a/tests/msgpack/test_size.hpp b/tests/msgpack/test_size.hpp new file mode 100644 index 00000000..be330df0 --- /dev/null +++ b/tests/msgpack/test_size.hpp @@ -0,0 +1,4 @@ +namespace test_size{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_string_map.cpp b/tests/msgpack/test_string_map.cpp new file mode 100644 index 00000000..cb4fb903 --- /dev/null +++ b/tests/msgpack/test_string_map.cpp @@ -0,0 +1,24 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_string_map diff --git a/tests/msgpack/test_string_map.hpp b/tests/msgpack/test_string_map.hpp new file mode 100644 index 00000000..94cb975a --- /dev/null +++ b/tests/msgpack/test_string_map.hpp @@ -0,0 +1,4 @@ +namespace test_string_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_tagged_union.cpp b/tests/msgpack/test_tagged_union.cpp new file mode 100644 index 00000000..a9dd1abc --- /dev/null +++ b/tests/msgpack/test_tagged_union.cpp @@ -0,0 +1,36 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_tagged_union diff --git a/tests/msgpack/test_tagged_union.hpp b/tests/msgpack/test_tagged_union.hpp new file mode 100644 index 00000000..5d522ff9 --- /dev/null +++ b/tests/msgpack/test_tagged_union.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_timestamp.cpp b/tests/msgpack/test_timestamp.cpp new file mode 100644 index 00000000..8af261d1 --- /dev/null +++ b/tests/msgpack/test_timestamp.cpp @@ -0,0 +1,36 @@ +#include "test_timestamp.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart); +} +} // namespace test_timestamp diff --git a/tests/msgpack/test_timestamp.hpp b/tests/msgpack/test_timestamp.hpp new file mode 100644 index 00000000..891d89b9 --- /dev/null +++ b/tests/msgpack/test_timestamp.hpp @@ -0,0 +1,4 @@ +namespace test_timestamp{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_unique_ptr.cpp b/tests/msgpack/test_unique_ptr.cpp new file mode 100644 index 00000000..b1833533 --- /dev/null +++ b/tests/msgpack/test_unique_ptr.cpp @@ -0,0 +1,33 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_unique_ptr diff --git a/tests/msgpack/test_unique_ptr.hpp b/tests/msgpack/test_unique_ptr.hpp new file mode 100644 index 00000000..428ea2a2 --- /dev/null +++ b/tests/msgpack/test_unique_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_unique_ptr2.cpp b/tests/msgpack/test_unique_ptr2.cpp new file mode 100644 index 00000000..e3007267 --- /dev/null +++ b/tests/msgpack/test_unique_ptr2.cpp @@ -0,0 +1,48 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_unique_ptr2 diff --git a/tests/msgpack/test_unique_ptr2.hpp b/tests/msgpack/test_unique_ptr2.hpp new file mode 100644 index 00000000..74adc170 --- /dev/null +++ b/tests/msgpack/test_unique_ptr2.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr2{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_variant.cpp b/tests/msgpack/test_variant.cpp new file mode 100644 index 00000000..cc3d6cbe --- /dev/null +++ b/tests/msgpack/test_variant.cpp @@ -0,0 +1,36 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = std::variant>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_variant diff --git a/tests/msgpack/test_variant.hpp b/tests/msgpack/test_variant.hpp new file mode 100644 index 00000000..0e58ce71 --- /dev/null +++ b/tests/msgpack/test_variant.hpp @@ -0,0 +1,4 @@ +namespace test_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/msgpack/test_wstring.cpp b/tests/msgpack/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/msgpack/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/msgpack/test_wstring.hpp b/tests/msgpack/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/msgpack/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/msgpack/tests.cpp b/tests/msgpack/tests.cpp new file mode 100644 index 00000000..8a9fe0fb --- /dev/null +++ b/tests/msgpack/tests.cpp @@ -0,0 +1,67 @@ +#include "test_array.hpp" +#include "test_box.hpp" +#include "test_custom_class1.hpp" +#include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" +#include "test_default_values.hpp" +#include "test_deque.hpp" +#include "test_enum.hpp" +#include "test_field_variant.hpp" +#include "test_flag_enum.hpp" +#include "test_flag_enum_with_int.hpp" +#include "test_flatten.hpp" +#include "test_flatten_anonymous.hpp" +#include "test_forward_list.hpp" +#include "test_literal.hpp" +#include "test_literal_map.hpp" +#include "test_map.hpp" +#include "test_map_with_key_validation.hpp" +#include "test_monster_example.hpp" +#include "test_readme_example.hpp" +#include "test_readme_example2.hpp" +#include "test_ref.hpp" +#include "test_save_load.hpp" +#include "test_set.hpp" +#include "test_size.hpp" +#include "test_tagged_union.hpp" +#include "test_timestamp.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" +#include "test_wstring.hpp" + +int main() { + test_readme_example::test(); + test_readme_example2::test(); + test_flatten::test(); + test_flatten_anonymous::test(); + test_enum::test(); + test_flag_enum::test(); + test_flag_enum_with_int::test(); + test_map::test(); + test_map_with_key_validation::test(); + test_variant::test(); + test_field_variant::test(); + test_tagged_union::test(); + test_deque::test(); + test_forward_list::test(); + test_literal_map::test(); + test_literal::test(); + test_monster_example::test(); + test_ref::test(); + test_set::test(); + test_size::test(); + test_timestamp::test(); + test_unique_ptr::test(); + test_unique_ptr2::test(); + test_array::test(); + test_box::test(); + test_custom_class1::test(); + test_custom_class3::test(); + test_custom_class4::test(); + test_default_values::test(); + test_save_load::test(); + test_wstring::test(); + + return 0; +} diff --git a/tests/msgpack/write_and_read.hpp b/tests/msgpack/write_and_read.hpp new file mode 100644 index 00000000..9843c18f --- /dev/null +++ b/tests/msgpack/write_and_read.hpp @@ -0,0 +1,39 @@ +#ifndef WRITE_AND_READ_ +#define WRITE_AND_READ_ + +#include +#include +#include + +template +void write_and_read(const T& _struct) { + const auto bytes1 = rfl::msgpack::write(_struct); + + const auto res = rfl::msgpack::read(bytes1); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl + << std::endl; + return; + } + + const auto bytes2 = rfl::msgpack::write(res.value()); + + if (bytes1.size() != bytes2.size()) { + std::cout << "Test failed on write. Number of bytes was different." + << std::endl + << std::endl; + return; + } + + if (bytes1 != bytes2) { + std::cout << "Test failed on write. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} + +#endif diff --git a/vcpkg.json b/vcpkg.json index da724077..625ac896 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,6 @@ { "name": "reflectcpp", - "version-string": "0.7.0", + "version-string": "0.8.0", "builtin-baseline": "50bffcc62d7f6571eb32bc1a0b1807e77af1166c", "dependencies": [ { @@ -11,6 +11,10 @@ "name": "libbson", "version>=": "1.25.1" }, + { + "name": "msgpack-c", + "version>=": "6.0.0" + }, { "name": "pugixml", "version>=": "1.14" From bf1c9bea365ac12532677c803149a41c426ded23 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 21 Apr 2024 14:47:12 +0200 Subject: [PATCH 31/33] Made sure that Github Actions also work for MSVC (#88) --- .github/workflows/test.yaml | 40 +++++++++++++----------- CMakeLists.txt | 9 +++--- README.md | 4 +-- include/rfl/internal/get_field_names.hpp | 4 +-- tests/json/test_inheritance.cpp | 14 ++++----- tests/toml/CMakeLists.txt | 1 + 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e86190b3..88bab727 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -66,21 +66,25 @@ jobs: ./build/tests/xml/reflect-cpp-xml-tests ./build/tests/yaml/reflect-cpp-yaml-tests - -# The latest MSVC version on GitHub Actions has a bug, and it's difficult to switch to another version. -# Re-enable this when the bug is fixed. - -# windows-msvc: -# runs-on: windows-latest -# steps: -# - uses: actions/checkout@v3 -# with: -# submodules: true -# - uses: ilammy/msvc-dev-cmd@v1 -# - uses: lukka/run-vcpkg@v11 -# - name: Run test -# run: | -# cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -# cmake --build build -j 4 -# .\build\tests\flexbuffers\Release\reflect-cpp-flexbuffers-tests.exe -# .\build\tests\json\Release\reflect-cpp-json-tests.exe + windows-msvc: + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + submodules: recursive + fetch-depth: 0 + - uses: ilammy/msvc-dev-cmd@v1 + - uses: lukka/run-vcpkg@v11 + - name: Run test + run: | + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release + cmake --build build --config Release -j4 + .\build\tests\json\Release\reflect-cpp-json-tests.exe + .\build\tests\bson\Release\reflect-cpp-bson-tests.exe + .\build\tests\cbor\Release\reflect-cpp-cbor-tests.exe + .\build\tests\flexbuffers\Release\reflect-cpp-flexbuffers-tests.exe + .\build\tests\msgpack\Release\reflect-cpp-msgpack-tests.exe + .\build\tests\toml\Release\reflect-cpp-toml-tests.exe + .\build\tests\xml\Release\reflect-cpp-xml-tests.exe + .\build\tests\yaml\Release\reflect-cpp-yaml-tests.exe diff --git a/CMakeLists.txt b/CMakeLists.txt index ad51489b..3bdb1b33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,6 @@ if (REFLECTCPP_FLEXBUFFERS) endif () if (REFLECTCPP_MSGPACK) - message(${VCPKG_INSTALLED_DIR}) find_package(msgpack-c CONFIG REQUIRED) if (MSVC) target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/msgpack-c${CMAKE_STATIC_LIBRARY_SUFFIX}") @@ -66,9 +65,11 @@ if (REFLECTCPP_MSGPACK) endif () if (REFLECTCPP_TOML) - find_package(PkgConfig REQUIRED) - pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) - target_link_libraries(reflectcpp INTERFACE PkgConfig::tomlplusplus) + if (MSVC) + target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tomlplusplus${CMAKE_STATIC_LIBRARY_SUFFIX}") + else () + target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtomlplusplus${CMAKE_STATIC_LIBRARY_SUFFIX}") + endif () endif() if (REFLECTCPP_XML) diff --git a/README.md b/README.md index 2a005924..579dcbd5 100644 --- a/README.md +++ b/README.md @@ -570,9 +570,9 @@ To run the tests, do the following: ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests ./build/tests/msgpack/reflect-cpp-msgpack-tests ./build/tests/json/reflect-cpp-json-tests -./build/tests/xml/reflect-cpp-toml-tests +./build/tests/toml/reflect-cpp-toml-tests ./build/tests/xml/reflect-cpp-xml-tests -./build/tests/xml/reflect-cpp-yaml-tests +./build/tests/yaml/reflect-cpp-yaml-tests ``` ## How to contribute diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index ebb3e8ae..82505866 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -52,13 +52,13 @@ consteval auto get_field_name_str_view() { #endif #if defined(__clang__) const auto split = func_name.substr(0, func_name.size() - 2); - return split.substr(split.find_last_of(":.") + 1); + return split.substr(split.find_last_of(":.") + 1); #elif defined(__GNUC__) const auto split = func_name.substr(0, func_name.size() - 2); return split.substr(split.find_last_of(":") + 1); #elif defined(_MSC_VER) const auto split = func_name.substr(0, func_name.size() - 7); - return split.substr(split.find("value->") + 7); + return split.substr(split.rfind("->") + 2); #else static_assert(false, "You are using an unsupported compiler. Please use GCC, Clang " diff --git a/tests/json/test_inheritance.cpp b/tests/json/test_inheritance.cpp index 4c131053..1852f21b 100644 --- a/tests/json/test_inheritance.cpp +++ b/tests/json/test_inheritance.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace test_inheritance { @@ -14,13 +15,12 @@ void test() { struct T : S {}; - const auto name = get<0>(rfl::fields()).name(); - if (name == "x") { - std::cout << "OK" << std::endl << std::endl; - } else { - std::cout << "FAIL\n" - << "Expected member name 'x', got '" << name << "'" << std::endl; - } + constexpr auto name = + std::tuple_element_t<0, typename rfl::named_tuple_t::Fields>::name(); + + static_assert(name == "x"); + + std::cout << "OK" << std::endl << std::endl; } } // namespace test_inheritance diff --git a/tests/toml/CMakeLists.txt b/tests/toml/CMakeLists.txt index 8e85ac23..c80c52fa 100644 --- a/tests/toml/CMakeLists.txt +++ b/tests/toml/CMakeLists.txt @@ -4,5 +4,6 @@ file(GLOB_RECURSE SOURCES "*.cpp") add_executable(reflect-cpp-toml-tests ${SOURCES}) +target_include_directories(reflect-cpp-toml-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") target_link_libraries(reflect-cpp-toml-tests PRIVATE reflectcpp) From 325d163196d10bb46dab7afc756d09539a8b3b39 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Mon, 22 Apr 2024 23:12:35 +0200 Subject: [PATCH 32/33] Use GTest; resolves #34 (#90) --- .gitignore | 1 + CMakeLists.txt | 8 +- README.md | 17 +- tests/bson/CMakeLists.txt | 16 +- tests/bson/test_array.cpp | 8 +- tests/bson/test_array.hpp | 4 - tests/bson/test_box.cpp | 6 +- tests/bson/test_box.hpp | 4 - tests/bson/test_custom_class1.cpp | 6 +- tests/bson/test_custom_class1.hpp | 4 - tests/bson/test_custom_class3.cpp | 6 +- tests/bson/test_custom_class3.hpp | 4 - tests/bson/test_custom_class4.cpp | 6 +- tests/bson/test_custom_class4.hpp | 4 - tests/bson/test_default_values.cpp | 6 +- tests/bson/test_default_values.hpp | 4 - tests/bson/test_deque.cpp | 6 +- tests/bson/test_deque.hpp | 4 - tests/bson/test_enum.cpp | 6 +- tests/bson/test_enum.hpp | 4 - tests/bson/test_field_variant.cpp | 6 +- tests/bson/test_field_variant.hpp | 4 - tests/bson/test_flag_enum.cpp | 5 +- tests/bson/test_flag_enum.hpp | 4 - tests/bson/test_flag_enum_with_int.cpp | 5 +- tests/bson/test_flag_enum_with_int.hpp | 4 - tests/bson/test_flatten.cpp | 6 +- tests/bson/test_flatten.hpp | 4 - tests/bson/test_flatten_anonymous.cpp | 6 +- tests/bson/test_flatten_anonymous.hpp | 4 - tests/bson/test_forward_list.cpp | 6 +- tests/bson/test_forward_list.hpp | 4 - tests/bson/test_literal.cpp | 6 +- tests/bson/test_literal.hpp | 4 - tests/bson/test_literal_map.cpp | 6 +- tests/bson/test_literal_map.hpp | 4 - tests/bson/test_map.cpp | 6 +- tests/bson/test_map.hpp | 4 - tests/bson/test_map_with_key_validation.cpp | 6 +- tests/bson/test_map_with_key_validation.hpp | 4 - tests/bson/test_monster_example.cpp | 6 +- tests/bson/test_monster_example.hpp | 4 - tests/bson/test_readme_example.cpp | 6 +- tests/bson/test_readme_example.hpp | 4 - tests/bson/test_readme_example2.cpp | 5 +- tests/bson/test_readme_example2.hpp | 4 - tests/bson/test_ref.cpp | 6 +- tests/bson/test_ref.hpp | 4 - tests/bson/test_save_load.cpp | 16 +- tests/bson/test_save_load.hpp | 4 - tests/bson/test_set.cpp | 6 +- tests/bson/test_set.hpp | 4 - tests/bson/test_size.cpp | 6 +- tests/bson/test_size.hpp | 4 - tests/bson/test_string_map.cpp | 6 +- tests/bson/test_string_map.hpp | 4 - tests/bson/test_tagged_union.cpp | 7 +- tests/bson/test_tagged_union.hpp | 4 - tests/bson/test_timestamp.cpp | 6 +- tests/bson/test_timestamp.hpp | 4 - tests/bson/test_unique_ptr.cpp | 6 +- tests/bson/test_unique_ptr.hpp | 4 - tests/bson/test_unique_ptr2.cpp | 6 +- tests/bson/test_unique_ptr2.hpp | 4 - tests/bson/test_variant.cpp | 6 +- tests/bson/test_variant.hpp | 4 - tests/bson/test_wstring.cpp | 12 +- tests/bson/test_wstring.hpp | 3 - tests/bson/tests.cpp | 67 ------ tests/bson/write_and_read.hpp | 35 +--- tests/cbor/CMakeLists.txt | 17 +- tests/cbor/test_array.cpp | 6 +- tests/cbor/test_array.hpp | 4 - tests/cbor/test_box.cpp | 6 +- tests/cbor/test_box.hpp | 4 - tests/cbor/test_custom_class1.cpp | 6 +- tests/cbor/test_custom_class1.hpp | 4 - tests/cbor/test_custom_class3.cpp | 6 +- tests/cbor/test_custom_class3.hpp | 4 - tests/cbor/test_custom_class4.cpp | 6 +- tests/cbor/test_custom_class4.hpp | 4 - tests/cbor/test_default_values.cpp | 6 +- tests/cbor/test_default_values.hpp | 4 - tests/cbor/test_deque.cpp | 6 +- tests/cbor/test_deque.hpp | 4 - tests/cbor/test_enum.cpp | 6 +- tests/cbor/test_enum.hpp | 4 - tests/cbor/test_field_variant.cpp | 6 +- tests/cbor/test_field_variant.hpp | 4 - tests/cbor/test_flag_enum.cpp | 5 +- tests/cbor/test_flag_enum.hpp | 4 - tests/cbor/test_flag_enum_with_int.cpp | 5 +- tests/cbor/test_flag_enum_with_int.hpp | 4 - tests/cbor/test_flatten.cpp | 6 +- tests/cbor/test_flatten.hpp | 4 - tests/cbor/test_flatten_anonymous.cpp | 6 +- tests/cbor/test_flatten_anonymous.hpp | 4 - tests/cbor/test_forward_list.cpp | 6 +- tests/cbor/test_forward_list.hpp | 4 - tests/cbor/test_literal.cpp | 6 +- tests/cbor/test_literal.hpp | 4 - tests/cbor/test_literal_map.cpp | 6 +- tests/cbor/test_literal_map.hpp | 4 - tests/cbor/test_map.cpp | 6 +- tests/cbor/test_map.hpp | 4 - tests/cbor/test_map_with_key_validation.cpp | 6 +- tests/cbor/test_map_with_key_validation.hpp | 4 - tests/cbor/test_monster_example.cpp | 6 +- tests/cbor/test_monster_example.hpp | 4 - tests/cbor/test_readme_example.cpp | 6 +- tests/cbor/test_readme_example.hpp | 4 - tests/cbor/test_readme_example2.cpp | 5 +- tests/cbor/test_readme_example2.hpp | 4 - tests/cbor/test_ref.cpp | 6 +- tests/cbor/test_ref.hpp | 4 - tests/cbor/test_save_load.cpp | 16 +- tests/cbor/test_save_load.hpp | 4 - tests/cbor/test_set.cpp | 6 +- tests/cbor/test_set.hpp | 4 - tests/cbor/test_size.cpp | 6 +- tests/cbor/test_size.hpp | 4 - tests/cbor/test_string_map.cpp | 6 +- tests/cbor/test_string_map.hpp | 4 - tests/cbor/test_tagged_union.cpp | 7 +- tests/cbor/test_tagged_union.hpp | 4 - tests/cbor/test_timestamp.cpp | 6 +- tests/cbor/test_timestamp.hpp | 4 - tests/cbor/test_unique_ptr.cpp | 6 +- tests/cbor/test_unique_ptr.hpp | 4 - tests/cbor/test_unique_ptr2.cpp | 6 +- tests/cbor/test_unique_ptr2.hpp | 4 - tests/cbor/test_variant.cpp | 6 +- tests/cbor/test_variant.hpp | 4 - tests/cbor/test_wstring.cpp | 12 +- tests/cbor/test_wstring.hpp | 3 - tests/cbor/tests.cpp | 67 ------ tests/cbor/write_and_read.hpp | 35 +--- tests/flexbuffers/CMakeLists.txt | 19 +- tests/flexbuffers/README.md | 23 -- tests/flexbuffers/test_all_of.cpp | 34 --- tests/flexbuffers/test_all_of.hpp | 4 - tests/flexbuffers/test_anonymous_fields.cpp | 57 ----- tests/flexbuffers/test_anonymous_fields.hpp | 4 - tests/flexbuffers/test_array.cpp | 36 ++++ tests/flexbuffers/test_box.cpp | 18 +- tests/flexbuffers/test_box.hpp | 4 - tests/flexbuffers/test_custom_class1.cpp | 13 +- tests/flexbuffers/test_custom_class1.hpp | 4 - tests/flexbuffers/test_custom_class2.cpp | 69 ------ tests/flexbuffers/test_custom_class2.hpp | 4 - tests/flexbuffers/test_custom_class3.cpp | 31 ++- tests/flexbuffers/test_custom_class3.hpp | 4 - tests/flexbuffers/test_custom_class4.cpp | 64 ++++++ tests/flexbuffers/test_custom_constructor.cpp | 59 ------ tests/flexbuffers/test_custom_constructor.hpp | 4 - ...nal_fields.cpp => test_default_values.cpp} | 22 +- tests/flexbuffers/test_deque.cpp | 15 +- tests/flexbuffers/test_deque.hpp | 4 - tests/flexbuffers/test_enum.cpp | 25 +++ tests/flexbuffers/test_field_variant.cpp | 15 +- tests/flexbuffers/test_field_variant.hpp | 4 - tests/flexbuffers/test_flag_enum.cpp | 36 ++++ tests/flexbuffers/test_flag_enum_with_int.cpp | 35 ++++ tests/flexbuffers/test_flatten.cpp | 34 +++ tests/flexbuffers/test_flatten_anonymous.cpp | 8 +- tests/flexbuffers/test_flatten_anonymous.hpp | 4 - tests/flexbuffers/test_forward_list.cpp | 15 +- tests/flexbuffers/test_forward_list.hpp | 4 - tests/flexbuffers/test_list.cpp | 34 --- tests/flexbuffers/test_list.hpp | 4 - tests/flexbuffers/test_literal.cpp | 13 +- tests/flexbuffers/test_literal.hpp | 4 - tests/flexbuffers/test_literal_map.cpp | 24 +++ tests/flexbuffers/test_map.cpp | 22 +- tests/flexbuffers/test_map.hpp | 4 - .../test_map_with_key_validation.cpp | 29 +++ ..._example1.cpp => test_monster_example.cpp} | 23 +- tests/flexbuffers/test_monster_example1.hpp | 4 - tests/flexbuffers/test_monster_example2.cpp | 67 ------ tests/flexbuffers/test_monster_example2.hpp | 4 - tests/flexbuffers/test_multimap.cpp | 34 --- tests/flexbuffers/test_multimap.hpp | 4 - tests/flexbuffers/test_multiset.cpp | 32 --- tests/flexbuffers/test_multiset.hpp | 4 - tests/flexbuffers/test_optional_fields.hpp | 4 - tests/flexbuffers/test_readme_example.cpp | 65 +++--- tests/flexbuffers/test_readme_example.hpp | 4 - tests/flexbuffers/test_readme_example2.cpp | 23 ++ tests/flexbuffers/test_ref.cpp | 18 +- tests/flexbuffers/test_ref.hpp | 4 - tests/flexbuffers/test_save_load.cpp | 58 ++---- tests/flexbuffers/test_save_load.hpp | 4 - tests/flexbuffers/test_set.cpp | 14 +- tests/flexbuffers/test_set.hpp | 4 - tests/flexbuffers/test_size.cpp | 38 ++++ tests/flexbuffers/test_string_map.cpp | 7 +- tests/flexbuffers/test_string_map.hpp | 4 - .../flexbuffers/test_string_unordered_map.cpp | 30 --- .../flexbuffers/test_string_unordered_map.hpp | 4 - tests/flexbuffers/test_tagged_union.cpp | 10 +- tests/flexbuffers/test_tagged_union.hpp | 4 - tests/flexbuffers/test_timestamp.cpp | 32 +++ tests/flexbuffers/test_unique_ptr.cpp | 14 +- tests/flexbuffers/test_unique_ptr.hpp | 4 - tests/flexbuffers/test_unique_ptr2.cpp | 17 +- tests/flexbuffers/test_unique_ptr2.hpp | 4 - tests/flexbuffers/test_unordered_map.cpp | 38 ---- tests/flexbuffers/test_unordered_map.hpp | 4 - tests/flexbuffers/test_unordered_multimap.cpp | 38 ---- tests/flexbuffers/test_unordered_multimap.hpp | 4 - tests/flexbuffers/test_unordered_multiset.cpp | 36 ---- tests/flexbuffers/test_unordered_multiset.hpp | 4 - tests/flexbuffers/test_unordered_set.cpp | 36 ---- tests/flexbuffers/test_unordered_set.hpp | 4 - tests/flexbuffers/test_variant.cpp | 17 +- tests/flexbuffers/test_variant.hpp | 4 - tests/flexbuffers/test_wstring.cpp | 12 +- tests/flexbuffers/test_wstring.hpp | 3 - tests/flexbuffers/tests.cpp | 77 ------- tests/flexbuffers/write_and_read.hpp | 35 +--- tests/json/CMakeLists.txt | 15 +- tests/json/test_all_of.cpp | 6 +- tests/json/test_all_of.hpp | 4 - tests/json/test_alphanumeric_map.cpp | 6 +- tests/json/test_alphanumeric_map.hpp | 4 - .../json/test_alphanumeric_unordered_map.cpp | 7 +- .../json/test_alphanumeric_unordered_map.hpp | 4 - tests/json/test_and_then.cpp | 6 +- tests/json/test_and_then.hpp | 4 - tests/json/test_anonymous_fields.cpp | 6 +- tests/json/test_anonymous_fields.hpp | 4 - tests/json/test_any_of.cpp | 6 +- tests/json/test_any_of.hpp | 4 - tests/json/test_apply.cpp | 5 +- tests/json/test_apply.hpp | 4 - tests/json/test_array.cpp | 5 +- tests/json/test_array.hpp | 4 - tests/json/test_as.cpp | 5 +- tests/json/test_as.hpp | 4 - tests/json/test_as2.cpp | 6 +- tests/json/test_as2.hpp | 4 - tests/json/test_as_flatten.cpp | 6 +- tests/json/test_as_flatten.hpp | 4 - tests/json/test_box.cpp | 6 +- tests/json/test_box.hpp | 4 - tests/json/test_box2.cpp | 15 +- tests/json/test_box2.hpp | 4 - tests/json/test_c_array_class1.cpp | 6 +- tests/json/test_c_array_class1.hpp | 5 - tests/json/test_c_array_class2.cpp | 11 +- tests/json/test_c_array_class2.hpp | 5 - tests/json/test_c_array_class3.cpp | 10 +- tests/json/test_c_array_class3.hpp | 5 - tests/json/test_c_array_class4.cpp | 6 +- tests/json/test_c_array_class4.hpp | 5 - tests/json/test_c_array_class5.cpp | 6 +- tests/json/test_c_array_class5.hpp | 5 - tests/json/test_const_fields.cpp | 6 +- tests/json/test_const_fields.hpp | 5 - tests/json/test_custom_class1.cpp | 5 +- tests/json/test_custom_class1.hpp | 4 - tests/json/test_custom_class2.cpp | 11 +- tests/json/test_custom_class2.hpp | 4 - tests/json/test_custom_class3.cpp | 6 +- tests/json/test_custom_class3.hpp | 4 - tests/json/test_custom_class4.cpp | 6 +- tests/json/test_custom_class4.hpp | 4 - tests/json/test_custom_constructor.cpp | 20 +- tests/json/test_custom_constructor.hpp | 4 - tests/json/test_default_values.cpp | 6 +- tests/json/test_default_values.hpp | 4 - tests/json/test_deque.cpp | 6 +- tests/json/test_deque.hpp | 4 - tests/json/test_email.cpp | 6 +- tests/json/test_email.hpp | 4 - tests/json/test_empty_object.cpp | 6 +- tests/json/test_empty_object.hpp | 4 - tests/json/test_enum1.cpp | 6 +- tests/json/test_enum1.hpp | 4 - tests/json/test_enum2.cpp | 6 +- tests/json/test_enum2.hpp | 4 - tests/json/test_enum3.cpp | 6 +- tests/json/test_enum3.hpp | 4 - tests/json/test_enum4.cpp | 5 +- tests/json/test_enum4.hpp | 4 - tests/json/test_enum5.cpp | 6 +- tests/json/test_enum5.hpp | 4 - tests/json/test_enum6.cpp | 6 +- tests/json/test_enum6.hpp | 4 - tests/json/test_enum7.cpp | 10 +- tests/json/test_enum7.hpp | 4 - tests/json/test_error_messages.cpp | 22 +- tests/json/test_error_messages.hpp | 4 - tests/json/test_field_variant.cpp | 6 +- tests/json/test_field_variant.hpp | 4 - tests/json/test_flag_enum1.cpp | 6 +- tests/json/test_flag_enum1.hpp | 4 - tests/json/test_flag_enum2.cpp | 6 +- tests/json/test_flag_enum2.hpp | 4 - tests/json/test_flag_enum_with_int.cpp | 6 +- tests/json/test_flag_enum_with_int.hpp | 4 - tests/json/test_flatten.cpp | 6 +- tests/json/test_flatten.hpp | 4 - tests/json/test_flatten_anonymous.cpp | 6 +- tests/json/test_flatten_anonymous.hpp | 4 - tests/json/test_forward_list.cpp | 6 +- tests/json/test_forward_list.hpp | 4 - tests/json/test_inheritance.cpp | 8 +- tests/json/test_inheritance.hpp | 3 - tests/json/test_inheritance2.cpp | 8 +- tests/json/test_inheritance2.hpp | 3 - tests/json/test_inside_function.cpp | 6 +- tests/json/test_inside_function.hpp | 4 - tests/json/test_json_schema.cpp | 17 +- tests/json/test_json_schema.hpp | 4 - tests/json/test_list.cpp | 6 +- tests/json/test_list.hpp | 4 - tests/json/test_literal.cpp | 6 +- tests/json/test_literal.hpp | 4 - tests/json/test_literal_map.cpp | 6 +- tests/json/test_literal_map.hpp | 4 - tests/json/test_literal_unordered_map.cpp | 9 +- tests/json/test_literal_unordered_map.hpp | 4 - tests/json/test_map.cpp | 6 +- tests/json/test_map.hpp | 4 - tests/json/test_map_with_key_validation.cpp | 6 +- tests/json/test_map_with_key_validation.hpp | 4 - tests/json/test_meta_fields.cpp | 20 +- tests/json/test_meta_fields.hpp | 4 - tests/json/test_monster_example.cpp | 6 +- tests/json/test_monster_example.hpp | 4 - tests/json/test_move_replace.cpp | 6 +- tests/json/test_move_replace.hpp | 4 - tests/json/test_multimap.cpp | 6 +- tests/json/test_multimap.hpp | 4 - tests/json/test_multiset.cpp | 6 +- tests/json/test_multiset.hpp | 4 - tests/json/test_one_of.cpp | 6 +- tests/json/test_one_of.hpp | 4 - tests/json/test_optional_fields.cpp | 6 +- tests/json/test_optional_fields.hpp | 4 - tests/json/test_readme_example.cpp | 6 +- tests/json/test_readme_example.hpp | 4 - tests/json/test_ref.cpp | 6 +- tests/json/test_ref.hpp | 4 - tests/json/test_replace.cpp | 6 +- tests/json/test_replace.hpp | 4 - tests/json/test_replace2.cpp | 6 +- tests/json/test_replace2.hpp | 4 - tests/json/test_replace_flatten.cpp | 6 +- tests/json/test_replace_flatten.hpp | 4 - tests/json/test_replace_flatten2.cpp | 6 +- tests/json/test_replace_flatten2.hpp | 4 - tests/json/test_replace_with_other_struct.cpp | 6 +- tests/json/test_replace_with_other_struct.hpp | 4 - .../json/test_replace_with_other_struct2.cpp | 6 +- .../json/test_replace_with_other_struct2.hpp | 4 - tests/json/test_result.cpp | 6 +- tests/json/test_result.hpp | 4 - tests/json/test_save_load.cpp | 15 +- tests/json/test_save_load.hpp | 4 - tests/json/test_set.cpp | 6 +- tests/json/test_set.hpp | 4 - tests/json/test_shared_ptr.cpp | 5 +- tests/json/test_shared_ptr.hpp | 4 - tests/json/test_size.cpp | 6 +- tests/json/test_size.hpp | 4 - tests/json/test_std_ref.cpp | 15 +- tests/json/test_std_ref.hpp | 3 - tests/json/test_string_map.cpp | 6 +- tests/json/test_string_map.hpp | 4 - tests/json/test_string_unordered_map.cpp | 8 +- tests/json/test_string_unordered_map.hpp | 4 - tests/json/test_tagged_union.cpp | 6 +- tests/json/test_tagged_union.hpp | 4 - tests/json/test_tagged_union2.cpp | 6 +- tests/json/test_tagged_union2.hpp | 4 - tests/json/test_tagged_union3.cpp | 6 +- tests/json/test_tagged_union3.hpp | 4 - tests/json/test_timestamp.cpp | 11 +- tests/json/test_timestamp.hpp | 4 - tests/json/test_transform.cpp | 5 +- tests/json/test_transform.hpp | 4 - tests/json/test_unique_ptr.cpp | 6 +- tests/json/test_unique_ptr.hpp | 4 - tests/json/test_unique_ptr2.cpp | 6 +- tests/json/test_unique_ptr2.hpp | 4 - tests/json/test_unnamed_namespace.cpp | 7 +- tests/json/test_unnamed_namespace.hpp | 4 - tests/json/test_unordered_map.cpp | 9 +- tests/json/test_unordered_map.hpp | 4 - tests/json/test_unordered_multimap.cpp | 8 +- tests/json/test_unordered_multimap.hpp | 4 - tests/json/test_unordered_multiset.cpp | 8 +- tests/json/test_unordered_multiset.hpp | 4 - tests/json/test_unordered_set.cpp | 8 +- tests/json/test_unordered_set.hpp | 4 - tests/json/test_variant.cpp | 6 +- tests/json/test_variant.hpp | 4 - tests/json/test_view.cpp | 6 +- tests/json/test_view.hpp | 4 - tests/json/test_wstring.cpp | 6 +- tests/json/test_wstring.hpp | 3 - tests/json/tests.cpp | 196 ------------------ tests/json/write_and_read.hpp | 44 ++-- tests/msgpack/CMakeLists.txt | 16 +- tests/msgpack/test_array.cpp | 6 +- tests/msgpack/test_array.hpp | 4 - tests/msgpack/test_box.cpp | 6 +- tests/msgpack/test_box.hpp | 4 - tests/msgpack/test_custom_class1.cpp | 6 +- tests/msgpack/test_custom_class1.hpp | 4 - tests/msgpack/test_custom_class3.cpp | 6 +- tests/msgpack/test_custom_class3.hpp | 4 - tests/msgpack/test_custom_class4.cpp | 6 +- tests/msgpack/test_custom_class4.hpp | 4 - tests/msgpack/test_default_values.cpp | 6 +- tests/msgpack/test_default_values.hpp | 4 - tests/msgpack/test_deque.cpp | 6 +- tests/msgpack/test_deque.hpp | 4 - tests/msgpack/test_enum.cpp | 6 +- tests/msgpack/test_enum.hpp | 4 - tests/msgpack/test_field_variant.cpp | 6 +- tests/msgpack/test_field_variant.hpp | 4 - tests/msgpack/test_flag_enum.cpp | 5 +- tests/msgpack/test_flag_enum.hpp | 4 - tests/msgpack/test_flag_enum_with_int.cpp | 5 +- tests/msgpack/test_flag_enum_with_int.hpp | 4 - tests/msgpack/test_flatten.cpp | 6 +- tests/msgpack/test_flatten.hpp | 4 - tests/msgpack/test_flatten_anonymous.cpp | 6 +- tests/msgpack/test_flatten_anonymous.hpp | 4 - tests/msgpack/test_forward_list.cpp | 6 +- tests/msgpack/test_forward_list.hpp | 4 - tests/msgpack/test_literal.cpp | 6 +- tests/msgpack/test_literal.hpp | 4 - tests/msgpack/test_literal_map.cpp | 6 +- tests/msgpack/test_literal_map.hpp | 4 - tests/msgpack/test_map.cpp | 6 +- tests/msgpack/test_map.hpp | 4 - .../msgpack/test_map_with_key_validation.cpp | 6 +- .../msgpack/test_map_with_key_validation.hpp | 4 - tests/msgpack/test_monster_example.cpp | 6 +- tests/msgpack/test_monster_example.hpp | 4 - tests/msgpack/test_readme_example.cpp | 6 +- tests/msgpack/test_readme_example.hpp | 4 - tests/msgpack/test_readme_example2.cpp | 5 +- tests/msgpack/test_readme_example2.hpp | 4 - tests/msgpack/test_ref.cpp | 6 +- tests/msgpack/test_ref.hpp | 4 - tests/msgpack/test_save_load.cpp | 16 +- tests/msgpack/test_save_load.hpp | 4 - tests/msgpack/test_set.cpp | 6 +- tests/msgpack/test_set.hpp | 4 - tests/msgpack/test_size.cpp | 6 +- tests/msgpack/test_size.hpp | 4 - tests/msgpack/test_string_map.cpp | 6 +- tests/msgpack/test_string_map.hpp | 4 - tests/msgpack/test_tagged_union.cpp | 7 +- tests/msgpack/test_tagged_union.hpp | 4 - tests/msgpack/test_timestamp.cpp | 6 +- tests/msgpack/test_timestamp.hpp | 4 - tests/msgpack/test_unique_ptr.cpp | 6 +- tests/msgpack/test_unique_ptr.hpp | 4 - tests/msgpack/test_unique_ptr2.cpp | 6 +- tests/msgpack/test_unique_ptr2.hpp | 4 - tests/msgpack/test_variant.cpp | 6 +- tests/msgpack/test_variant.hpp | 4 - tests/msgpack/test_wstring.cpp | 12 +- tests/msgpack/test_wstring.hpp | 3 - tests/msgpack/tests.cpp | 67 ------ tests/msgpack/write_and_read.hpp | 35 +--- tests/toml/CMakeLists.txt | 15 +- tests/toml/test_array.cpp | 8 +- tests/toml/test_array.hpp | 4 - tests/toml/test_box.cpp | 7 +- tests/toml/test_box.hpp | 4 - tests/toml/test_custom_class1.cpp | 7 +- tests/toml/test_custom_class1.hpp | 4 - tests/toml/test_custom_class3.cpp | 7 +- tests/toml/test_custom_class3.hpp | 4 - tests/toml/test_custom_class4.cpp | 7 +- tests/toml/test_custom_class4.hpp | 4 - tests/toml/test_custom_constructor.cpp | 57 ----- tests/toml/test_custom_constructor.hpp | 4 - tests/toml/test_default_values.cpp | 6 +- tests/toml/test_default_values.hpp | 4 - tests/toml/test_deque.cpp | 6 +- tests/toml/test_deque.hpp | 4 - tests/toml/test_enum.cpp | 6 +- tests/toml/test_enum.hpp | 4 - tests/toml/test_field_variant.cpp | 6 +- tests/toml/test_field_variant.hpp | 4 - tests/toml/test_flag_enum.cpp | 5 +- tests/toml/test_flag_enum.hpp | 4 - tests/toml/test_flag_enum_with_int.cpp | 5 +- tests/toml/test_flag_enum_with_int.hpp | 4 - tests/toml/test_flatten.cpp | 6 +- tests/toml/test_flatten.hpp | 4 - tests/toml/test_flatten_anonymous.cpp | 6 +- tests/toml/test_flatten_anonymous.hpp | 4 - tests/toml/test_forward_list.cpp | 6 +- tests/toml/test_forward_list.hpp | 4 - tests/toml/test_literal.cpp | 6 +- tests/toml/test_literal.hpp | 4 - tests/toml/test_literal_map.cpp | 6 +- tests/toml/test_literal_map.hpp | 4 - tests/toml/test_map.cpp | 6 +- tests/toml/test_map.hpp | 4 - tests/toml/test_map_with_key_validation.cpp | 6 +- tests/toml/test_map_with_key_validation.hpp | 4 - tests/toml/test_monster_example.cpp | 6 +- tests/toml/test_monster_example.hpp | 4 - tests/toml/test_readme_example.cpp | 6 +- tests/toml/test_readme_example.hpp | 4 - tests/toml/test_readme_example2.cpp | 5 +- tests/toml/test_readme_example2.hpp | 4 - tests/toml/test_ref.cpp | 6 +- tests/toml/test_ref.hpp | 4 - tests/toml/test_save_load.cpp | 16 +- tests/toml/test_save_load.hpp | 4 - tests/toml/test_set.cpp | 6 +- tests/toml/test_set.hpp | 4 - tests/toml/test_size.cpp | 6 +- tests/toml/test_size.hpp | 4 - tests/toml/test_string_map.cpp | 6 +- tests/toml/test_string_map.hpp | 4 - tests/toml/test_tagged_union.cpp | 7 +- tests/toml/test_tagged_union.hpp | 4 - tests/toml/test_timestamp.cpp | 6 +- tests/toml/test_timestamp.hpp | 4 - tests/toml/test_unique_ptr.cpp | 6 +- tests/toml/test_unique_ptr.hpp | 4 - tests/toml/test_unique_ptr2.cpp | 6 +- tests/toml/test_unique_ptr2.hpp | 4 - tests/toml/test_variant.cpp | 6 +- tests/toml/test_variant.hpp | 4 - tests/toml/test_wstring.cpp | 12 +- tests/toml/test_wstring.hpp | 3 - tests/toml/tests.cpp | 69 ------ tests/toml/write_and_read.hpp | 28 +-- tests/xml/CMakeLists.txt | 16 +- tests/xml/get_pugixml.sh | 3 - tests/xml/test_array.cpp | 8 +- tests/xml/test_array.hpp | 4 - tests/xml/test_attributes.cpp | 52 ----- tests/xml/test_attributes.hpp | 4 - tests/xml/test_box.cpp | 7 +- tests/xml/test_box.hpp | 4 - tests/xml/test_custom_class1.cpp | 7 +- tests/xml/test_custom_class1.hpp | 4 - tests/xml/test_custom_class3.cpp | 7 +- tests/xml/test_custom_class3.hpp | 4 - tests/xml/test_custom_class4.cpp | 7 +- tests/xml/test_custom_class4.hpp | 4 - tests/xml/test_default_values.cpp | 7 +- tests/xml/test_default_values.hpp | 4 - tests/xml/test_deque.cpp | 7 +- tests/xml/test_deque.hpp | 4 - tests/xml/test_enum.cpp | 7 +- tests/xml/test_enum.hpp | 4 - tests/xml/test_field_variant.cpp | 9 +- tests/xml/test_field_variant.hpp | 4 - tests/xml/test_flag_enum.cpp | 6 +- tests/xml/test_flag_enum.hpp | 4 - tests/xml/test_flag_enum_with_int.cpp | 6 +- tests/xml/test_flag_enum_with_int.hpp | 4 - tests/xml/test_flatten.cpp | 7 +- tests/xml/test_flatten.hpp | 4 - tests/xml/test_flatten_anonymous.cpp | 7 +- tests/xml/test_flatten_anonymous.hpp | 4 - tests/xml/test_forward_list.cpp | 7 +- tests/xml/test_forward_list.hpp | 4 - tests/xml/test_literal.cpp | 7 +- tests/xml/test_literal.hpp | 4 - tests/xml/test_literal_map.cpp | 9 +- tests/xml/test_literal_map.hpp | 4 - tests/xml/test_map.cpp | 7 +- tests/xml/test_map.hpp | 4 - tests/xml/test_map_with_key_validation.cpp | 7 +- tests/xml/test_map_with_key_validation.hpp | 4 - tests/xml/test_monster_example.cpp | 13 +- tests/xml/test_monster_example.hpp | 4 - tests/xml/test_readme_example.cpp | 7 +- tests/xml/test_readme_example.hpp | 4 - tests/xml/test_readme_example2.cpp | 23 ++ tests/xml/test_ref.cpp | 7 +- tests/xml/test_ref.hpp | 4 - tests/xml/test_save_load.cpp | 16 +- tests/xml/test_save_load.hpp | 4 - tests/xml/test_set.cpp | 7 +- tests/xml/test_set.hpp | 4 - tests/xml/test_size.cpp | 7 +- tests/xml/test_size.hpp | 4 - tests/xml/test_string_map.cpp | 9 +- tests/xml/test_string_map.hpp | 4 - tests/xml/test_tagged_union.cpp | 8 +- tests/xml/test_tagged_union.hpp | 4 - tests/xml/test_timestamp.cpp | 7 +- tests/xml/test_timestamp.hpp | 4 - tests/xml/test_unique_ptr.cpp | 7 +- tests/xml/test_unique_ptr.hpp | 4 - tests/xml/test_unique_ptr2.cpp | 7 +- tests/xml/test_unique_ptr2.hpp | 4 - tests/xml/test_variant.cpp | 7 +- tests/xml/test_variant.hpp | 4 - tests/xml/test_wstring.cpp | 14 +- tests/xml/test_wstring.hpp | 3 - tests/xml/test_xml_content.cpp | 6 +- tests/xml/test_xml_content.hpp | 4 - tests/xml/tests.cpp | 69 ------ tests/xml/write_and_read.hpp | 31 +-- tests/yaml/CMakeLists.txt | 16 +- tests/yaml/test_array.cpp | 8 +- tests/yaml/test_array.hpp | 4 - tests/yaml/test_box.cpp | 7 +- tests/yaml/test_box.hpp | 4 - tests/yaml/test_custom_class1.cpp | 7 +- tests/yaml/test_custom_class1.hpp | 4 - tests/yaml/test_custom_class3.cpp | 7 +- tests/yaml/test_custom_class3.hpp | 4 - tests/yaml/test_custom_class4.cpp | 7 +- tests/yaml/test_custom_class4.hpp | 4 - tests/yaml/test_custom_constructor.cpp | 57 ----- tests/yaml/test_custom_constructor.hpp | 4 - tests/yaml/test_default_values.cpp | 6 +- tests/yaml/test_default_values.hpp | 4 - tests/yaml/test_deque.cpp | 6 +- tests/yaml/test_deque.hpp | 4 - tests/yaml/test_enum.cpp | 6 +- tests/yaml/test_enum.hpp | 4 - tests/yaml/test_field_variant.cpp | 6 +- tests/yaml/test_field_variant.hpp | 4 - tests/yaml/test_flag_enum.cpp | 5 +- tests/yaml/test_flag_enum.hpp | 4 - tests/yaml/test_flag_enum_with_int.cpp | 5 +- tests/yaml/test_flag_enum_with_int.hpp | 4 - tests/yaml/test_flatten.cpp | 6 +- tests/yaml/test_flatten.hpp | 4 - tests/yaml/test_flatten_anonymous.cpp | 6 +- tests/yaml/test_flatten_anonymous.hpp | 4 - tests/yaml/test_forward_list.cpp | 6 +- tests/yaml/test_forward_list.hpp | 4 - tests/yaml/test_literal.cpp | 6 +- tests/yaml/test_literal.hpp | 4 - tests/yaml/test_literal_map.cpp | 6 +- tests/yaml/test_literal_map.hpp | 4 - tests/yaml/test_map.cpp | 6 +- tests/yaml/test_map.hpp | 4 - tests/yaml/test_map_with_key_validation.cpp | 6 +- tests/yaml/test_map_with_key_validation.hpp | 4 - tests/yaml/test_monster_example.cpp | 6 +- tests/yaml/test_monster_example.hpp | 4 - tests/yaml/test_readme_example.cpp | 6 +- tests/yaml/test_readme_example.hpp | 4 - tests/yaml/test_readme_example2.cpp | 5 +- tests/yaml/test_readme_example2.hpp | 4 - tests/yaml/test_ref.cpp | 6 +- tests/yaml/test_ref.hpp | 4 - tests/yaml/test_save_load.cpp | 16 +- tests/yaml/test_save_load.hpp | 4 - tests/yaml/test_set.cpp | 6 +- tests/yaml/test_set.hpp | 4 - tests/yaml/test_size.cpp | 6 +- tests/yaml/test_size.hpp | 4 - tests/yaml/test_string_map.cpp | 6 +- tests/yaml/test_string_map.hpp | 4 - tests/yaml/test_tagged_union.cpp | 7 +- tests/yaml/test_tagged_union.hpp | 4 - tests/yaml/test_timestamp.cpp | 6 +- tests/yaml/test_timestamp.hpp | 4 - tests/yaml/test_unique_ptr.cpp | 6 +- tests/yaml/test_unique_ptr.hpp | 4 - tests/yaml/test_unique_ptr2.cpp | 6 +- tests/yaml/test_unique_ptr2.hpp | 4 - tests/yaml/test_variant.cpp | 6 +- tests/yaml/test_variant.hpp | 4 - tests/yaml/test_wstring.cpp | 12 +- tests/yaml/test_wstring.hpp | 3 - tests/yaml/tests.cpp | 69 ------ tests/yaml/write_and_read.hpp | 28 +-- vcpkg.json | 4 + 682 files changed, 1126 insertions(+), 4802 deletions(-) delete mode 100644 tests/bson/test_array.hpp delete mode 100644 tests/bson/test_box.hpp delete mode 100644 tests/bson/test_custom_class1.hpp delete mode 100644 tests/bson/test_custom_class3.hpp delete mode 100644 tests/bson/test_custom_class4.hpp delete mode 100644 tests/bson/test_default_values.hpp delete mode 100644 tests/bson/test_deque.hpp delete mode 100644 tests/bson/test_enum.hpp delete mode 100644 tests/bson/test_field_variant.hpp delete mode 100644 tests/bson/test_flag_enum.hpp delete mode 100644 tests/bson/test_flag_enum_with_int.hpp delete mode 100644 tests/bson/test_flatten.hpp delete mode 100644 tests/bson/test_flatten_anonymous.hpp delete mode 100644 tests/bson/test_forward_list.hpp delete mode 100644 tests/bson/test_literal.hpp delete mode 100644 tests/bson/test_literal_map.hpp delete mode 100644 tests/bson/test_map.hpp delete mode 100644 tests/bson/test_map_with_key_validation.hpp delete mode 100644 tests/bson/test_monster_example.hpp delete mode 100644 tests/bson/test_readme_example.hpp delete mode 100644 tests/bson/test_readme_example2.hpp delete mode 100644 tests/bson/test_ref.hpp delete mode 100644 tests/bson/test_save_load.hpp delete mode 100644 tests/bson/test_set.hpp delete mode 100644 tests/bson/test_size.hpp delete mode 100644 tests/bson/test_string_map.hpp delete mode 100644 tests/bson/test_tagged_union.hpp delete mode 100644 tests/bson/test_timestamp.hpp delete mode 100644 tests/bson/test_unique_ptr.hpp delete mode 100644 tests/bson/test_unique_ptr2.hpp delete mode 100644 tests/bson/test_variant.hpp delete mode 100644 tests/bson/test_wstring.hpp delete mode 100644 tests/bson/tests.cpp delete mode 100644 tests/cbor/test_array.hpp delete mode 100644 tests/cbor/test_box.hpp delete mode 100644 tests/cbor/test_custom_class1.hpp delete mode 100644 tests/cbor/test_custom_class3.hpp delete mode 100644 tests/cbor/test_custom_class4.hpp delete mode 100644 tests/cbor/test_default_values.hpp delete mode 100644 tests/cbor/test_deque.hpp delete mode 100644 tests/cbor/test_enum.hpp delete mode 100644 tests/cbor/test_field_variant.hpp delete mode 100644 tests/cbor/test_flag_enum.hpp delete mode 100644 tests/cbor/test_flag_enum_with_int.hpp delete mode 100644 tests/cbor/test_flatten.hpp delete mode 100644 tests/cbor/test_flatten_anonymous.hpp delete mode 100644 tests/cbor/test_forward_list.hpp delete mode 100644 tests/cbor/test_literal.hpp delete mode 100644 tests/cbor/test_literal_map.hpp delete mode 100644 tests/cbor/test_map.hpp delete mode 100644 tests/cbor/test_map_with_key_validation.hpp delete mode 100644 tests/cbor/test_monster_example.hpp delete mode 100644 tests/cbor/test_readme_example.hpp delete mode 100644 tests/cbor/test_readme_example2.hpp delete mode 100644 tests/cbor/test_ref.hpp delete mode 100644 tests/cbor/test_save_load.hpp delete mode 100644 tests/cbor/test_set.hpp delete mode 100644 tests/cbor/test_size.hpp delete mode 100644 tests/cbor/test_string_map.hpp delete mode 100644 tests/cbor/test_tagged_union.hpp delete mode 100644 tests/cbor/test_timestamp.hpp delete mode 100644 tests/cbor/test_unique_ptr.hpp delete mode 100644 tests/cbor/test_unique_ptr2.hpp delete mode 100644 tests/cbor/test_variant.hpp delete mode 100644 tests/cbor/test_wstring.hpp delete mode 100644 tests/cbor/tests.cpp delete mode 100644 tests/flexbuffers/README.md delete mode 100644 tests/flexbuffers/test_all_of.cpp delete mode 100644 tests/flexbuffers/test_all_of.hpp delete mode 100644 tests/flexbuffers/test_anonymous_fields.cpp delete mode 100644 tests/flexbuffers/test_anonymous_fields.hpp create mode 100644 tests/flexbuffers/test_array.cpp delete mode 100644 tests/flexbuffers/test_box.hpp delete mode 100644 tests/flexbuffers/test_custom_class1.hpp delete mode 100644 tests/flexbuffers/test_custom_class2.cpp delete mode 100644 tests/flexbuffers/test_custom_class2.hpp delete mode 100644 tests/flexbuffers/test_custom_class3.hpp create mode 100644 tests/flexbuffers/test_custom_class4.cpp delete mode 100644 tests/flexbuffers/test_custom_constructor.cpp delete mode 100644 tests/flexbuffers/test_custom_constructor.hpp rename tests/flexbuffers/{test_optional_fields.cpp => test_default_values.cpp} (51%) delete mode 100644 tests/flexbuffers/test_deque.hpp create mode 100644 tests/flexbuffers/test_enum.cpp delete mode 100644 tests/flexbuffers/test_field_variant.hpp create mode 100644 tests/flexbuffers/test_flag_enum.cpp create mode 100644 tests/flexbuffers/test_flag_enum_with_int.cpp create mode 100644 tests/flexbuffers/test_flatten.cpp delete mode 100644 tests/flexbuffers/test_flatten_anonymous.hpp delete mode 100644 tests/flexbuffers/test_forward_list.hpp delete mode 100644 tests/flexbuffers/test_list.cpp delete mode 100644 tests/flexbuffers/test_list.hpp delete mode 100644 tests/flexbuffers/test_literal.hpp create mode 100644 tests/flexbuffers/test_literal_map.cpp delete mode 100644 tests/flexbuffers/test_map.hpp create mode 100644 tests/flexbuffers/test_map_with_key_validation.cpp rename tests/flexbuffers/{test_monster_example1.cpp => test_monster_example.cpp} (69%) delete mode 100644 tests/flexbuffers/test_monster_example1.hpp delete mode 100644 tests/flexbuffers/test_monster_example2.cpp delete mode 100644 tests/flexbuffers/test_monster_example2.hpp delete mode 100644 tests/flexbuffers/test_multimap.cpp delete mode 100644 tests/flexbuffers/test_multimap.hpp delete mode 100644 tests/flexbuffers/test_multiset.cpp delete mode 100644 tests/flexbuffers/test_multiset.hpp delete mode 100644 tests/flexbuffers/test_optional_fields.hpp delete mode 100644 tests/flexbuffers/test_readme_example.hpp create mode 100644 tests/flexbuffers/test_readme_example2.cpp delete mode 100644 tests/flexbuffers/test_ref.hpp delete mode 100644 tests/flexbuffers/test_save_load.hpp delete mode 100644 tests/flexbuffers/test_set.hpp create mode 100644 tests/flexbuffers/test_size.cpp delete mode 100644 tests/flexbuffers/test_string_map.hpp delete mode 100644 tests/flexbuffers/test_string_unordered_map.cpp delete mode 100644 tests/flexbuffers/test_string_unordered_map.hpp delete mode 100644 tests/flexbuffers/test_tagged_union.hpp create mode 100644 tests/flexbuffers/test_timestamp.cpp delete mode 100644 tests/flexbuffers/test_unique_ptr.hpp delete mode 100644 tests/flexbuffers/test_unique_ptr2.hpp delete mode 100644 tests/flexbuffers/test_unordered_map.cpp delete mode 100644 tests/flexbuffers/test_unordered_map.hpp delete mode 100644 tests/flexbuffers/test_unordered_multimap.cpp delete mode 100644 tests/flexbuffers/test_unordered_multimap.hpp delete mode 100644 tests/flexbuffers/test_unordered_multiset.cpp delete mode 100644 tests/flexbuffers/test_unordered_multiset.hpp delete mode 100644 tests/flexbuffers/test_unordered_set.cpp delete mode 100644 tests/flexbuffers/test_unordered_set.hpp delete mode 100644 tests/flexbuffers/test_variant.hpp delete mode 100644 tests/flexbuffers/test_wstring.hpp delete mode 100644 tests/flexbuffers/tests.cpp delete mode 100644 tests/json/test_all_of.hpp delete mode 100644 tests/json/test_alphanumeric_map.hpp delete mode 100644 tests/json/test_alphanumeric_unordered_map.hpp delete mode 100644 tests/json/test_and_then.hpp delete mode 100644 tests/json/test_anonymous_fields.hpp delete mode 100644 tests/json/test_any_of.hpp delete mode 100644 tests/json/test_apply.hpp delete mode 100644 tests/json/test_array.hpp delete mode 100644 tests/json/test_as.hpp delete mode 100644 tests/json/test_as2.hpp delete mode 100644 tests/json/test_as_flatten.hpp delete mode 100644 tests/json/test_box.hpp delete mode 100644 tests/json/test_box2.hpp delete mode 100644 tests/json/test_c_array_class1.hpp delete mode 100644 tests/json/test_c_array_class2.hpp delete mode 100644 tests/json/test_c_array_class3.hpp delete mode 100644 tests/json/test_c_array_class4.hpp delete mode 100644 tests/json/test_c_array_class5.hpp delete mode 100644 tests/json/test_const_fields.hpp delete mode 100644 tests/json/test_custom_class1.hpp delete mode 100644 tests/json/test_custom_class2.hpp delete mode 100644 tests/json/test_custom_class3.hpp delete mode 100644 tests/json/test_custom_class4.hpp delete mode 100644 tests/json/test_custom_constructor.hpp delete mode 100644 tests/json/test_default_values.hpp delete mode 100644 tests/json/test_deque.hpp delete mode 100644 tests/json/test_email.hpp delete mode 100644 tests/json/test_empty_object.hpp delete mode 100644 tests/json/test_enum1.hpp delete mode 100644 tests/json/test_enum2.hpp delete mode 100644 tests/json/test_enum3.hpp delete mode 100644 tests/json/test_enum4.hpp delete mode 100644 tests/json/test_enum5.hpp delete mode 100644 tests/json/test_enum6.hpp delete mode 100644 tests/json/test_enum7.hpp delete mode 100644 tests/json/test_error_messages.hpp delete mode 100644 tests/json/test_field_variant.hpp delete mode 100644 tests/json/test_flag_enum1.hpp delete mode 100644 tests/json/test_flag_enum2.hpp delete mode 100644 tests/json/test_flag_enum_with_int.hpp delete mode 100644 tests/json/test_flatten.hpp delete mode 100644 tests/json/test_flatten_anonymous.hpp delete mode 100644 tests/json/test_forward_list.hpp delete mode 100644 tests/json/test_inheritance.hpp delete mode 100644 tests/json/test_inheritance2.hpp delete mode 100644 tests/json/test_inside_function.hpp delete mode 100644 tests/json/test_json_schema.hpp delete mode 100644 tests/json/test_list.hpp delete mode 100644 tests/json/test_literal.hpp delete mode 100644 tests/json/test_literal_map.hpp delete mode 100644 tests/json/test_literal_unordered_map.hpp delete mode 100644 tests/json/test_map.hpp delete mode 100644 tests/json/test_map_with_key_validation.hpp delete mode 100644 tests/json/test_meta_fields.hpp delete mode 100644 tests/json/test_monster_example.hpp delete mode 100644 tests/json/test_move_replace.hpp delete mode 100644 tests/json/test_multimap.hpp delete mode 100644 tests/json/test_multiset.hpp delete mode 100644 tests/json/test_one_of.hpp delete mode 100644 tests/json/test_optional_fields.hpp delete mode 100644 tests/json/test_readme_example.hpp delete mode 100644 tests/json/test_ref.hpp delete mode 100644 tests/json/test_replace.hpp delete mode 100644 tests/json/test_replace2.hpp delete mode 100644 tests/json/test_replace_flatten.hpp delete mode 100644 tests/json/test_replace_flatten2.hpp delete mode 100644 tests/json/test_replace_with_other_struct.hpp delete mode 100644 tests/json/test_replace_with_other_struct2.hpp delete mode 100644 tests/json/test_result.hpp delete mode 100644 tests/json/test_save_load.hpp delete mode 100644 tests/json/test_set.hpp delete mode 100644 tests/json/test_shared_ptr.hpp delete mode 100644 tests/json/test_size.hpp delete mode 100644 tests/json/test_std_ref.hpp delete mode 100644 tests/json/test_string_map.hpp delete mode 100644 tests/json/test_string_unordered_map.hpp delete mode 100644 tests/json/test_tagged_union.hpp delete mode 100644 tests/json/test_tagged_union2.hpp delete mode 100644 tests/json/test_tagged_union3.hpp delete mode 100644 tests/json/test_timestamp.hpp delete mode 100644 tests/json/test_transform.hpp delete mode 100644 tests/json/test_unique_ptr.hpp delete mode 100644 tests/json/test_unique_ptr2.hpp delete mode 100644 tests/json/test_unnamed_namespace.hpp delete mode 100644 tests/json/test_unordered_map.hpp delete mode 100644 tests/json/test_unordered_multimap.hpp delete mode 100644 tests/json/test_unordered_multiset.hpp delete mode 100644 tests/json/test_unordered_set.hpp delete mode 100644 tests/json/test_variant.hpp delete mode 100644 tests/json/test_view.hpp delete mode 100644 tests/json/test_wstring.hpp delete mode 100644 tests/json/tests.cpp delete mode 100644 tests/msgpack/test_array.hpp delete mode 100644 tests/msgpack/test_box.hpp delete mode 100644 tests/msgpack/test_custom_class1.hpp delete mode 100644 tests/msgpack/test_custom_class3.hpp delete mode 100644 tests/msgpack/test_custom_class4.hpp delete mode 100644 tests/msgpack/test_default_values.hpp delete mode 100644 tests/msgpack/test_deque.hpp delete mode 100644 tests/msgpack/test_enum.hpp delete mode 100644 tests/msgpack/test_field_variant.hpp delete mode 100644 tests/msgpack/test_flag_enum.hpp delete mode 100644 tests/msgpack/test_flag_enum_with_int.hpp delete mode 100644 tests/msgpack/test_flatten.hpp delete mode 100644 tests/msgpack/test_flatten_anonymous.hpp delete mode 100644 tests/msgpack/test_forward_list.hpp delete mode 100644 tests/msgpack/test_literal.hpp delete mode 100644 tests/msgpack/test_literal_map.hpp delete mode 100644 tests/msgpack/test_map.hpp delete mode 100644 tests/msgpack/test_map_with_key_validation.hpp delete mode 100644 tests/msgpack/test_monster_example.hpp delete mode 100644 tests/msgpack/test_readme_example.hpp delete mode 100644 tests/msgpack/test_readme_example2.hpp delete mode 100644 tests/msgpack/test_ref.hpp delete mode 100644 tests/msgpack/test_save_load.hpp delete mode 100644 tests/msgpack/test_set.hpp delete mode 100644 tests/msgpack/test_size.hpp delete mode 100644 tests/msgpack/test_string_map.hpp delete mode 100644 tests/msgpack/test_tagged_union.hpp delete mode 100644 tests/msgpack/test_timestamp.hpp delete mode 100644 tests/msgpack/test_unique_ptr.hpp delete mode 100644 tests/msgpack/test_unique_ptr2.hpp delete mode 100644 tests/msgpack/test_variant.hpp delete mode 100644 tests/msgpack/test_wstring.hpp delete mode 100644 tests/msgpack/tests.cpp delete mode 100644 tests/toml/test_array.hpp delete mode 100644 tests/toml/test_box.hpp delete mode 100644 tests/toml/test_custom_class1.hpp delete mode 100644 tests/toml/test_custom_class3.hpp delete mode 100644 tests/toml/test_custom_class4.hpp delete mode 100644 tests/toml/test_custom_constructor.cpp delete mode 100644 tests/toml/test_custom_constructor.hpp delete mode 100644 tests/toml/test_default_values.hpp delete mode 100644 tests/toml/test_deque.hpp delete mode 100644 tests/toml/test_enum.hpp delete mode 100644 tests/toml/test_field_variant.hpp delete mode 100644 tests/toml/test_flag_enum.hpp delete mode 100644 tests/toml/test_flag_enum_with_int.hpp delete mode 100644 tests/toml/test_flatten.hpp delete mode 100644 tests/toml/test_flatten_anonymous.hpp delete mode 100644 tests/toml/test_forward_list.hpp delete mode 100644 tests/toml/test_literal.hpp delete mode 100644 tests/toml/test_literal_map.hpp delete mode 100644 tests/toml/test_map.hpp delete mode 100644 tests/toml/test_map_with_key_validation.hpp delete mode 100644 tests/toml/test_monster_example.hpp delete mode 100644 tests/toml/test_readme_example.hpp delete mode 100644 tests/toml/test_readme_example2.hpp delete mode 100644 tests/toml/test_ref.hpp delete mode 100644 tests/toml/test_save_load.hpp delete mode 100644 tests/toml/test_set.hpp delete mode 100644 tests/toml/test_size.hpp delete mode 100644 tests/toml/test_string_map.hpp delete mode 100644 tests/toml/test_tagged_union.hpp delete mode 100644 tests/toml/test_timestamp.hpp delete mode 100644 tests/toml/test_unique_ptr.hpp delete mode 100644 tests/toml/test_unique_ptr2.hpp delete mode 100644 tests/toml/test_variant.hpp delete mode 100644 tests/toml/test_wstring.hpp delete mode 100644 tests/toml/tests.cpp delete mode 100755 tests/xml/get_pugixml.sh delete mode 100644 tests/xml/test_array.hpp delete mode 100644 tests/xml/test_attributes.cpp delete mode 100644 tests/xml/test_attributes.hpp delete mode 100644 tests/xml/test_box.hpp delete mode 100644 tests/xml/test_custom_class1.hpp delete mode 100644 tests/xml/test_custom_class3.hpp delete mode 100644 tests/xml/test_custom_class4.hpp delete mode 100644 tests/xml/test_default_values.hpp delete mode 100644 tests/xml/test_deque.hpp delete mode 100644 tests/xml/test_enum.hpp delete mode 100644 tests/xml/test_field_variant.hpp delete mode 100644 tests/xml/test_flag_enum.hpp delete mode 100644 tests/xml/test_flag_enum_with_int.hpp delete mode 100644 tests/xml/test_flatten.hpp delete mode 100644 tests/xml/test_flatten_anonymous.hpp delete mode 100644 tests/xml/test_forward_list.hpp delete mode 100644 tests/xml/test_literal.hpp delete mode 100644 tests/xml/test_literal_map.hpp delete mode 100644 tests/xml/test_map.hpp delete mode 100644 tests/xml/test_map_with_key_validation.hpp delete mode 100644 tests/xml/test_monster_example.hpp delete mode 100644 tests/xml/test_readme_example.hpp create mode 100644 tests/xml/test_readme_example2.cpp delete mode 100644 tests/xml/test_ref.hpp delete mode 100644 tests/xml/test_save_load.hpp delete mode 100644 tests/xml/test_set.hpp delete mode 100644 tests/xml/test_size.hpp delete mode 100644 tests/xml/test_string_map.hpp delete mode 100644 tests/xml/test_tagged_union.hpp delete mode 100644 tests/xml/test_timestamp.hpp delete mode 100644 tests/xml/test_unique_ptr.hpp delete mode 100644 tests/xml/test_unique_ptr2.hpp delete mode 100644 tests/xml/test_variant.hpp delete mode 100644 tests/xml/test_wstring.hpp delete mode 100644 tests/xml/test_xml_content.hpp delete mode 100644 tests/xml/tests.cpp delete mode 100644 tests/yaml/test_array.hpp delete mode 100644 tests/yaml/test_box.hpp delete mode 100644 tests/yaml/test_custom_class1.hpp delete mode 100644 tests/yaml/test_custom_class3.hpp delete mode 100644 tests/yaml/test_custom_class4.hpp delete mode 100644 tests/yaml/test_custom_constructor.cpp delete mode 100644 tests/yaml/test_custom_constructor.hpp delete mode 100644 tests/yaml/test_default_values.hpp delete mode 100644 tests/yaml/test_deque.hpp delete mode 100644 tests/yaml/test_enum.hpp delete mode 100644 tests/yaml/test_field_variant.hpp delete mode 100644 tests/yaml/test_flag_enum.hpp delete mode 100644 tests/yaml/test_flag_enum_with_int.hpp delete mode 100644 tests/yaml/test_flatten.hpp delete mode 100644 tests/yaml/test_flatten_anonymous.hpp delete mode 100644 tests/yaml/test_forward_list.hpp delete mode 100644 tests/yaml/test_literal.hpp delete mode 100644 tests/yaml/test_literal_map.hpp delete mode 100644 tests/yaml/test_map.hpp delete mode 100644 tests/yaml/test_map_with_key_validation.hpp delete mode 100644 tests/yaml/test_monster_example.hpp delete mode 100644 tests/yaml/test_readme_example.hpp delete mode 100644 tests/yaml/test_readme_example2.hpp delete mode 100644 tests/yaml/test_ref.hpp delete mode 100644 tests/yaml/test_save_load.hpp delete mode 100644 tests/yaml/test_set.hpp delete mode 100644 tests/yaml/test_size.hpp delete mode 100644 tests/yaml/test_string_map.hpp delete mode 100644 tests/yaml/test_tagged_union.hpp delete mode 100644 tests/yaml/test_timestamp.hpp delete mode 100644 tests/yaml/test_unique_ptr.hpp delete mode 100644 tests/yaml/test_unique_ptr2.hpp delete mode 100644 tests/yaml/test_variant.hpp delete mode 100644 tests/yaml/test_wstring.hpp delete mode 100644 tests/yaml/tests.cpp diff --git a/.gitignore b/.gitignore index 36fa4976..48757cde 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ *.cbor *.json *.fb +*.flexbuf *.msgpack *.toml *.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bdb1b33..cc26bcb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ option(REFLECTCPP_YAML "Enable YAML support" OFF) option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) set(REFLECTCPP_USE_VCPKG_DEFAULT OFF) -if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) +if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML) # enable vcpkg per default if require features other than JSON set(REFLECTCPP_USE_VCPKG_DEFAULT ON) endif() @@ -85,10 +85,14 @@ endif () target_compile_options(reflectcpp PRIVATE -Wall) if (REFLECTCPP_BUILD_TESTS) + if (MSVC) + set(REFLECT_CPP_GTEST_LIB "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/gtest${CMAKE_STATIC_LIBRARY_SUFFIX}") + else () + set(REFLECT_CPP_GTEST_LIB "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libgtest${CMAKE_STATIC_LIBRARY_SUFFIX}") + endif () add_subdirectory(tests) endif () - include(GNUInstallDirs) include(CMakePackageConfigHelpers) diff --git a/README.md b/README.md index 579dcbd5..14cf64ca 100644 --- a/README.md +++ b/README.md @@ -530,6 +530,17 @@ vcpkg is a great, but very ambitious and complex project (just like C++ is a gre ## Compiling and running the tests +reflect-cpp uses vcpkg for dependency management, including +gtest, which is required for the tests. + +```bash +# bootstrap vcpkg if you haven't done so already +git submodule update --init +./vcpkg/bootstrap-vcpkg.sh # Linux, macOS +./vcpkg/bootstrap-vcpkg.bat # Windows +# You may be prompted to install additional dependencies. +``` + ### JSON only To compile the tests, do the following: @@ -551,12 +562,6 @@ To run the tests, do the following: To compile the tests with serialization formats other than JSON, do the following: ```bash -# bootstrap vcpkg if you haven't done so already -git submodule update --init -./vcpkg/bootstrap-vcpkg.sh # Linux, macOS -./vcpkg/bootstrap-vcpkg.bat # Windows -# You may be prompted to install additional dependencies. - cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release cmake --build build -j 4 # gcc, clang cmake --build build --config Release -j 4 # MSVC diff --git a/tests/bson/CMakeLists.txt b/tests/bson/CMakeLists.txt index 318ba7a0..40d4d467 100644 --- a/tests/bson/CMakeLists.txt +++ b/tests/bson/CMakeLists.txt @@ -1,8 +1,18 @@ project(reflect-cpp-bson-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-bson-tests ${SOURCES}) +add_executable( + reflect-cpp-bson-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) target_include_directories(reflect-cpp-bson-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") -target_link_libraries(reflect-cpp-bson-tests PRIVATE reflectcpp) + +target_link_libraries( + reflect-cpp-bson-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/bson/test_array.cpp b/tests/bson/test_array.cpp index 970ffd11..c50694fc 100644 --- a/tests/bson/test_array.cpp +++ b/tests/bson/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include // Make sure things still compile when -// rfl.hpp is included after rfl/bson.hpp. +// rfl.hpp is included after rfl/cbor.hpp. #include #include "write_and_read.hpp" @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/bson/test_array.hpp b/tests/bson/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/bson/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_box.cpp b/tests/bson/test_box.cpp index fdb3516a..c570a725 100644 --- a/tests/bson/test_box.cpp +++ b/tests/bson/test_box.cpp @@ -1,5 +1,3 @@ -#include "test_box.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/bson/test_box.hpp b/tests/bson/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/bson/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_custom_class1.cpp b/tests/bson/test_custom_class1.cpp index fe8d0ea0..ddc9cc76 100644 --- a/tests/bson/test_custom_class1.cpp +++ b/tests/bson/test_custom_class1.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class1.hpp" - #include #include #include @@ -31,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/bson/test_custom_class1.hpp b/tests/bson/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/bson/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_custom_class3.cpp b/tests/bson/test_custom_class3.cpp index 598364b1..75cd152e 100644 --- a/tests/bson/test_custom_class3.cpp +++ b/tests/bson/test_custom_class3.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class3.hpp" - #include #include #include @@ -56,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/bson/test_custom_class3.hpp b/tests/bson/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/bson/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_custom_class4.cpp b/tests/bson/test_custom_class4.cpp index 2b3b3bac..8a0edea4 100644 --- a/tests/bson/test_custom_class4.cpp +++ b/tests/bson/test_custom_class4.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class4.hpp" - #include #include #include @@ -57,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/bson/test_custom_class4.hpp b/tests/bson/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/bson/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_default_values.cpp b/tests/bson/test_default_values.cpp index f6af2352..99c9de6c 100644 --- a/tests/bson/test_default_values.cpp +++ b/tests/bson/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/bson/test_default_values.hpp b/tests/bson/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/bson/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_deque.cpp b/tests/bson/test_deque.cpp index 6dfa5803..6483cb8a 100644 --- a/tests/bson/test_deque.cpp +++ b/tests/bson/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/bson/test_deque.hpp b/tests/bson/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/bson/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_enum.cpp b/tests/bson/test_enum.cpp index d4d56a8b..4390668c 100644 --- a/tests/bson/test_enum.cpp +++ b/tests/bson/test_enum.cpp @@ -1,5 +1,3 @@ -#include "test_enum.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/bson/test_enum.hpp b/tests/bson/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/bson/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/bson/test_field_variant.cpp b/tests/bson/test_field_variant.cpp index 8b17665f..ef4e20ab 100644 --- a/tests/bson/test_field_variant.cpp +++ b/tests/bson/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -28,9 +26,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/bson/test_field_variant.hpp b/tests/bson/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/bson/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_flag_enum.cpp b/tests/bson/test_flag_enum.cpp index 1d3f76af..1570d740 100644 --- a/tests/bson/test_flag_enum.cpp +++ b/tests/bson/test_flag_enum.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/bson/test_flag_enum.hpp b/tests/bson/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/bson/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/bson/test_flag_enum_with_int.cpp b/tests/bson/test_flag_enum_with_int.cpp index 4c7deefe..60b0ac02 100644 --- a/tests/bson/test_flag_enum_with_int.cpp +++ b/tests/bson/test_flag_enum_with_int.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/bson/test_flag_enum_with_int.hpp b/tests/bson/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/bson/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/bson/test_flatten.cpp b/tests/bson/test_flatten.cpp index c7f1d9be..66388a31 100644 --- a/tests/bson/test_flatten.cpp +++ b/tests/bson/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/bson/test_flatten.hpp b/tests/bson/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/bson/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_flatten_anonymous.cpp b/tests/bson/test_flatten_anonymous.cpp index 05e61464..15cd0cf2 100644 --- a/tests/bson/test_flatten_anonymous.cpp +++ b/tests/bson/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/bson/test_flatten_anonymous.hpp b/tests/bson/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/bson/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_forward_list.cpp b/tests/bson/test_forward_list.cpp index dd8d9e56..81f0699e 100644 --- a/tests/bson/test_forward_list.cpp +++ b/tests/bson/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/bson/test_forward_list.hpp b/tests/bson/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/bson/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_literal.cpp b/tests/bson/test_literal.cpp index 00578178..12cbad32 100644 --- a/tests/bson/test_literal.cpp +++ b/tests/bson/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/bson/test_literal.hpp b/tests/bson/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/bson/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_literal_map.cpp b/tests/bson/test_literal_map.cpp index 9bbebafb..83710d55 100644 --- a/tests/bson/test_literal_map.cpp +++ b/tests/bson/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -14,9 +12,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/bson/test_literal_map.hpp b/tests/bson/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/bson/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_map.cpp b/tests/bson/test_map.cpp index e37ce564..537e77f6 100644 --- a/tests/bson/test_map.cpp +++ b/tests/bson/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/bson/test_map.hpp b/tests/bson/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/bson/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_map_with_key_validation.cpp b/tests/bson/test_map_with_key_validation.cpp index e758219d..991f3e50 100644 --- a/tests/bson/test_map_with_key_validation.cpp +++ b/tests/bson/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/bson/test_map_with_key_validation.hpp b/tests/bson/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/bson/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_monster_example.cpp b/tests/bson/test_monster_example.cpp index 052d8390..49887c35 100644 --- a/tests/bson/test_monster_example.cpp +++ b/tests/bson/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -38,9 +36,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/bson/test_monster_example.hpp b/tests/bson/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/bson/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_readme_example.cpp b/tests/bson/test_readme_example.cpp index 4e6f8ee0..e42d8817 100644 --- a/tests/bson/test_readme_example.cpp +++ b/tests/bson/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/bson/test_readme_example.hpp b/tests/bson/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/bson/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_readme_example2.cpp b/tests/bson/test_readme_example2.cpp index a2d212a4..f93de197 100644 --- a/tests/bson/test_readme_example2.cpp +++ b/tests/bson/test_readme_example2.cpp @@ -4,7 +4,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_readme_example2 { @@ -15,9 +14,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_readme_example2) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/bson/test_readme_example2.hpp b/tests/bson/test_readme_example2.hpp deleted file mode 100644 index 5c6b011c..00000000 --- a/tests/bson/test_readme_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example2 { -void test(); -} - diff --git a/tests/bson/test_ref.cpp b/tests/bson/test_ref.cpp index 32a32e6c..7b7ca3a3 100644 --- a/tests/bson/test_ref.cpp +++ b/tests/bson/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/bson/test_ref.hpp b/tests/bson/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/bson/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_save_load.cpp b/tests/bson/test_save_load.cpp index d51c34c8..a72dc2a9 100644 --- a/tests/bson/test_save_load.cpp +++ b/tests/bson/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::bson::write(homer1); const auto string2 = rfl::bson::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/bson/test_save_load.hpp b/tests/bson/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/bson/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/bson/test_set.cpp b/tests/bson/test_set.cpp index 9f2516ce..ec6f8030 100644 --- a/tests/bson/test_set.cpp +++ b/tests/bson/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/bson/test_set.hpp b/tests/bson/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/bson/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_size.cpp b/tests/bson/test_size.cpp index c37b9bd8..0de9abf5 100644 --- a/tests/bson/test_size.cpp +++ b/tests/bson/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/bson/test_size.hpp b/tests/bson/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/bson/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_string_map.cpp b/tests/bson/test_string_map.cpp index cb4fb903..b4aef817 100644 --- a/tests/bson/test_string_map.cpp +++ b/tests/bson/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -10,9 +8,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/bson/test_string_map.hpp b/tests/bson/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/bson/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_tagged_union.cpp b/tests/bson/test_tagged_union.cpp index a9dd1abc..630b65e6 100644 --- a/tests/bson/test_tagged_union.cpp +++ b/tests/bson/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -26,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/bson/test_tagged_union.hpp b/tests/bson/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/bson/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_timestamp.cpp b/tests/bson/test_timestamp.cpp index 8af261d1..c57e06fc 100644 --- a/tests/bson/test_timestamp.cpp +++ b/tests/bson/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/bson/test_timestamp.hpp b/tests/bson/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/bson/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_unique_ptr.cpp b/tests/bson/test_unique_ptr.cpp index b1833533..4091de86 100644 --- a/tests/bson/test_unique_ptr.cpp +++ b/tests/bson/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/bson/test_unique_ptr.hpp b/tests/bson/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/bson/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_unique_ptr2.cpp b/tests/bson/test_unique_ptr2.cpp index e3007267..a9ba69b6 100644 --- a/tests/bson/test_unique_ptr2.cpp +++ b/tests/bson/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/bson/test_unique_ptr2.hpp b/tests/bson/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/bson/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_variant.cpp b/tests/bson/test_variant.cpp index cc3d6cbe..cea0b0b4 100644 --- a/tests/bson/test_variant.cpp +++ b/tests/bson/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -26,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(bson, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/bson/test_variant.hpp b/tests/bson/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/bson/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/bson/test_wstring.cpp b/tests/bson/test_wstring.cpp index ba6202c0..0eaaf93c 100644 --- a/tests/bson/test_wstring.cpp +++ b/tests/bson/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(bson, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/bson/test_wstring.hpp b/tests/bson/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/bson/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/bson/tests.cpp b/tests/bson/tests.cpp deleted file mode 100644 index 8a9fe0fb..00000000 --- a/tests/bson/tests.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "test_array.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_readme_example2.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_readme_example2::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/bson/write_and_read.hpp b/tests/bson/write_and_read.hpp index f009be61..15096a38 100644 --- a/tests/bson/write_and_read.hpp +++ b/tests/bson/write_and_read.hpp @@ -1,39 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto bytes1 = rfl::bson::write(_struct); - - const auto res = rfl::bson::read(bytes1); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << std::endl; - return; - } - - const auto bytes2 = rfl::bson::write(res.value()); - - if (bytes1.size() != bytes2.size()) { - std::cout << "Test failed on write. Number of bytes was different." - << std::endl - << std::endl; - return; - } - - if (bytes1 != bytes2) { - std::cout << "Test failed on write. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::bson::write(_struct); + const auto res = rfl::bson::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::bson::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/cbor/CMakeLists.txt b/tests/cbor/CMakeLists.txt index 7f457ee5..ba5a8e0f 100644 --- a/tests/cbor/CMakeLists.txt +++ b/tests/cbor/CMakeLists.txt @@ -1,8 +1,19 @@ project(reflect-cpp-cbor-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-cbor-tests ${SOURCES}) +add_executable( + reflect-cpp-cbor-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) +target_include_directories(reflect-cpp-cbor-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") target_include_directories(reflect-cpp-cbor-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor") -target_link_libraries(reflect-cpp-cbor-tests PRIVATE reflectcpp) + +target_link_libraries( + reflect-cpp-cbor-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/cbor/test_array.cpp b/tests/cbor/test_array.cpp index 108f730e..55274d49 100644 --- a/tests/cbor/test_array.cpp +++ b/tests/cbor/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/cbor/test_array.hpp b/tests/cbor/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/cbor/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_box.cpp b/tests/cbor/test_box.cpp index fdb3516a..3fd68a69 100644 --- a/tests/cbor/test_box.cpp +++ b/tests/cbor/test_box.cpp @@ -1,5 +1,3 @@ -#include "test_box.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/cbor/test_box.hpp b/tests/cbor/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/cbor/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_custom_class1.cpp b/tests/cbor/test_custom_class1.cpp index fe8d0ea0..448ac187 100644 --- a/tests/cbor/test_custom_class1.cpp +++ b/tests/cbor/test_custom_class1.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class1.hpp" - #include #include #include @@ -31,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/cbor/test_custom_class1.hpp b/tests/cbor/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/cbor/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_custom_class3.cpp b/tests/cbor/test_custom_class3.cpp index 598364b1..991df513 100644 --- a/tests/cbor/test_custom_class3.cpp +++ b/tests/cbor/test_custom_class3.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class3.hpp" - #include #include #include @@ -56,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/cbor/test_custom_class3.hpp b/tests/cbor/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/cbor/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_custom_class4.cpp b/tests/cbor/test_custom_class4.cpp index 2b3b3bac..45b6d914 100644 --- a/tests/cbor/test_custom_class4.cpp +++ b/tests/cbor/test_custom_class4.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class4.hpp" - #include #include #include @@ -57,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/cbor/test_custom_class4.hpp b/tests/cbor/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/cbor/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_default_values.cpp b/tests/cbor/test_default_values.cpp index f6af2352..d69109e2 100644 --- a/tests/cbor/test_default_values.cpp +++ b/tests/cbor/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/cbor/test_default_values.hpp b/tests/cbor/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/cbor/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_deque.cpp b/tests/cbor/test_deque.cpp index 6dfa5803..d897da5d 100644 --- a/tests/cbor/test_deque.cpp +++ b/tests/cbor/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/cbor/test_deque.hpp b/tests/cbor/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/cbor/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_enum.cpp b/tests/cbor/test_enum.cpp index d4d56a8b..4e485288 100644 --- a/tests/cbor/test_enum.cpp +++ b/tests/cbor/test_enum.cpp @@ -1,5 +1,3 @@ -#include "test_enum.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/cbor/test_enum.hpp b/tests/cbor/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/cbor/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/cbor/test_field_variant.cpp b/tests/cbor/test_field_variant.cpp index 8b17665f..1276609d 100644 --- a/tests/cbor/test_field_variant.cpp +++ b/tests/cbor/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -28,9 +26,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/cbor/test_field_variant.hpp b/tests/cbor/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/cbor/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_flag_enum.cpp b/tests/cbor/test_flag_enum.cpp index 1d3f76af..3473dc84 100644 --- a/tests/cbor/test_flag_enum.cpp +++ b/tests/cbor/test_flag_enum.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/cbor/test_flag_enum.hpp b/tests/cbor/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/cbor/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/cbor/test_flag_enum_with_int.cpp b/tests/cbor/test_flag_enum_with_int.cpp index 4c7deefe..78f558f8 100644 --- a/tests/cbor/test_flag_enum_with_int.cpp +++ b/tests/cbor/test_flag_enum_with_int.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/cbor/test_flag_enum_with_int.hpp b/tests/cbor/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/cbor/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/cbor/test_flatten.cpp b/tests/cbor/test_flatten.cpp index c7f1d9be..3896a206 100644 --- a/tests/cbor/test_flatten.cpp +++ b/tests/cbor/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/cbor/test_flatten.hpp b/tests/cbor/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/cbor/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_flatten_anonymous.cpp b/tests/cbor/test_flatten_anonymous.cpp index 05e61464..3a90ba02 100644 --- a/tests/cbor/test_flatten_anonymous.cpp +++ b/tests/cbor/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/cbor/test_flatten_anonymous.hpp b/tests/cbor/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/cbor/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_forward_list.cpp b/tests/cbor/test_forward_list.cpp index dd8d9e56..444d581f 100644 --- a/tests/cbor/test_forward_list.cpp +++ b/tests/cbor/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/cbor/test_forward_list.hpp b/tests/cbor/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/cbor/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_literal.cpp b/tests/cbor/test_literal.cpp index 00578178..d444ec33 100644 --- a/tests/cbor/test_literal.cpp +++ b/tests/cbor/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/cbor/test_literal.hpp b/tests/cbor/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/cbor/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_literal_map.cpp b/tests/cbor/test_literal_map.cpp index 9bbebafb..3c2a096d 100644 --- a/tests/cbor/test_literal_map.cpp +++ b/tests/cbor/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -14,9 +12,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/cbor/test_literal_map.hpp b/tests/cbor/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/cbor/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_map.cpp b/tests/cbor/test_map.cpp index e37ce564..4ac14474 100644 --- a/tests/cbor/test_map.cpp +++ b/tests/cbor/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/cbor/test_map.hpp b/tests/cbor/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/cbor/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_map_with_key_validation.cpp b/tests/cbor/test_map_with_key_validation.cpp index e758219d..9286e2ad 100644 --- a/tests/cbor/test_map_with_key_validation.cpp +++ b/tests/cbor/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/cbor/test_map_with_key_validation.hpp b/tests/cbor/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/cbor/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_monster_example.cpp b/tests/cbor/test_monster_example.cpp index 052d8390..3a27276d 100644 --- a/tests/cbor/test_monster_example.cpp +++ b/tests/cbor/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -38,9 +36,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/cbor/test_monster_example.hpp b/tests/cbor/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/cbor/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_readme_example.cpp b/tests/cbor/test_readme_example.cpp index 4e6f8ee0..092608fd 100644 --- a/tests/cbor/test_readme_example.cpp +++ b/tests/cbor/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/cbor/test_readme_example.hpp b/tests/cbor/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/cbor/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_readme_example2.cpp b/tests/cbor/test_readme_example2.cpp index a2d212a4..b1b69c42 100644 --- a/tests/cbor/test_readme_example2.cpp +++ b/tests/cbor/test_readme_example2.cpp @@ -4,7 +4,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_readme_example2 { @@ -15,9 +14,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_readme_example2) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/cbor/test_readme_example2.hpp b/tests/cbor/test_readme_example2.hpp deleted file mode 100644 index 5c6b011c..00000000 --- a/tests/cbor/test_readme_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example2 { -void test(); -} - diff --git a/tests/cbor/test_ref.cpp b/tests/cbor/test_ref.cpp index 32a32e6c..653783fd 100644 --- a/tests/cbor/test_ref.cpp +++ b/tests/cbor/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/cbor/test_ref.hpp b/tests/cbor/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/cbor/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_save_load.cpp b/tests/cbor/test_save_load.cpp index c2b19a43..4f4dcbd1 100644 --- a/tests/cbor/test_save_load.cpp +++ b/tests/cbor/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::cbor::write(homer1); const auto string2 = rfl::cbor::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/cbor/test_save_load.hpp b/tests/cbor/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/cbor/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/cbor/test_set.cpp b/tests/cbor/test_set.cpp index 9f2516ce..66f6d2e1 100644 --- a/tests/cbor/test_set.cpp +++ b/tests/cbor/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/cbor/test_set.hpp b/tests/cbor/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/cbor/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_size.cpp b/tests/cbor/test_size.cpp index c37b9bd8..984fa087 100644 --- a/tests/cbor/test_size.cpp +++ b/tests/cbor/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/cbor/test_size.hpp b/tests/cbor/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/cbor/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_string_map.cpp b/tests/cbor/test_string_map.cpp index cb4fb903..70934445 100644 --- a/tests/cbor/test_string_map.cpp +++ b/tests/cbor/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -10,9 +8,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/cbor/test_string_map.hpp b/tests/cbor/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/cbor/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_tagged_union.cpp b/tests/cbor/test_tagged_union.cpp index a9dd1abc..adbf422c 100644 --- a/tests/cbor/test_tagged_union.cpp +++ b/tests/cbor/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -26,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/cbor/test_tagged_union.hpp b/tests/cbor/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/cbor/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_timestamp.cpp b/tests/cbor/test_timestamp.cpp index 8af261d1..da86861c 100644 --- a/tests/cbor/test_timestamp.cpp +++ b/tests/cbor/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/cbor/test_timestamp.hpp b/tests/cbor/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/cbor/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_unique_ptr.cpp b/tests/cbor/test_unique_ptr.cpp index b1833533..fc667206 100644 --- a/tests/cbor/test_unique_ptr.cpp +++ b/tests/cbor/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/cbor/test_unique_ptr.hpp b/tests/cbor/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/cbor/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_unique_ptr2.cpp b/tests/cbor/test_unique_ptr2.cpp index e3007267..b2f83eed 100644 --- a/tests/cbor/test_unique_ptr2.cpp +++ b/tests/cbor/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/cbor/test_unique_ptr2.hpp b/tests/cbor/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/cbor/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_variant.cpp b/tests/cbor/test_variant.cpp index cc3d6cbe..82f83604 100644 --- a/tests/cbor/test_variant.cpp +++ b/tests/cbor/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -26,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(cbor, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/cbor/test_variant.hpp b/tests/cbor/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/cbor/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/cbor/test_wstring.cpp b/tests/cbor/test_wstring.cpp index ba6202c0..36759b16 100644 --- a/tests/cbor/test_wstring.cpp +++ b/tests/cbor/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(cbor, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/cbor/test_wstring.hpp b/tests/cbor/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/cbor/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/cbor/tests.cpp b/tests/cbor/tests.cpp deleted file mode 100644 index 8a9fe0fb..00000000 --- a/tests/cbor/tests.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "test_array.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_readme_example2.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_readme_example2::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/cbor/write_and_read.hpp b/tests/cbor/write_and_read.hpp index 0864ad53..f0078856 100644 --- a/tests/cbor/write_and_read.hpp +++ b/tests/cbor/write_and_read.hpp @@ -1,39 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto bytes1 = rfl::cbor::write(_struct); - - const auto res = rfl::cbor::read(bytes1); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << std::endl; - return; - } - - const auto bytes2 = rfl::cbor::write(res.value()); - - if (bytes1.size() != bytes2.size()) { - std::cout << "Test failed on write. Number of bytes was different." - << std::endl - << std::endl; - return; - } - - if (bytes1 != bytes2) { - std::cout << "Test failed on write. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::cbor::write(_struct); + const auto res = rfl::cbor::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::cbor::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/flexbuffers/CMakeLists.txt b/tests/flexbuffers/CMakeLists.txt index 19125e19..aaf8a05f 100644 --- a/tests/flexbuffers/CMakeLists.txt +++ b/tests/flexbuffers/CMakeLists.txt @@ -1,7 +1,18 @@ -project(reflect-cpp-flexbuffer-tests) +project(reflect-cpp-flexbuffers-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-flexbuffers-tests ${SOURCES}) +add_executable( + reflect-cpp-flexbuffers-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) -target_link_libraries(reflect-cpp-flexbuffers-tests PRIVATE reflectcpp) +target_include_directories(reflect-cpp-flexbuffers-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") + +target_link_libraries( + reflect-cpp-flexbuffers-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/flexbuffers/README.md b/tests/flexbuffers/README.md deleted file mode 100644 index 9acf80e9..00000000 --- a/tests/flexbuffers/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Tests for flexbuffers - -This folder contains the tests for flexbuffers. A Docker-based build pipeline is provided for Linux users. - -Build the Docker container containing the build environment: - -``` -./build_docker.sh -``` - -Compile the tests: - -``` -./compile.sh -``` - -Run the tests: - -``` -./build/reflect-cpp-flatbuffers-tests -``` - -To run the scripts, you may have to run `chmod +x build_docker.sh` and `chmod +x compile.sh` first. diff --git a/tests/flexbuffers/test_all_of.cpp b/tests/flexbuffers/test_all_of.cpp deleted file mode 100644 index a75d37a8..00000000 --- a/tests/flexbuffers/test_all_of.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "test_all_of.hpp" - -#include -#include -#include -#include -#include - -// Make sure things still compile when -// rfl.hpp is included after rfl/flexbuf.hpp. -#include - -#include "write_and_read.hpp" - -namespace test_all_of { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Age = rfl::Validator, rfl::Maximum<130>>>; - - struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"age", Age> age; - }; - - const auto homer = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; - - write_and_read(homer); -} -} // namespace test_all_of diff --git a/tests/flexbuffers/test_all_of.hpp b/tests/flexbuffers/test_all_of.hpp deleted file mode 100644 index 082d508a..00000000 --- a/tests/flexbuffers/test_all_of.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_all_of{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_anonymous_fields.cpp b/tests/flexbuffers/test_anonymous_fields.cpp deleted file mode 100644 index 8ff2a704..00000000 --- a/tests/flexbuffers/test_anonymous_fields.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "test_anonymous_fields.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_anonymous_fields { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - std::string first_name; - std::string last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - Age age; - rfl::Email email; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com"}; - - const auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read(homer); -} -} // namespace test_anonymous_fields diff --git a/tests/flexbuffers/test_anonymous_fields.hpp b/tests/flexbuffers/test_anonymous_fields.hpp deleted file mode 100644 index 16c47d36..00000000 --- a/tests/flexbuffers/test_anonymous_fields.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_anonymous_fields{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_array.cpp b/tests/flexbuffers/test_array.cpp new file mode 100644 index 00000000..a4bebf0a --- /dev/null +++ b/tests/flexbuffers/test_array.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/cbor.hpp. +#include + +#include "write_and_read.hpp" + +namespace test_array { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +TEST(flexbuf, test_array) { + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = Person{ + .first_name = "Homer", + .children = std::make_unique>(std::array{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read(homer); +} +} // namespace test_array diff --git a/tests/flexbuffers/test_box.cpp b/tests/flexbuffers/test_box.cpp index 00625393..3f45b087 100644 --- a/tests/flexbuffers/test_box.cpp +++ b/tests/flexbuffers/test_box.cpp @@ -1,9 +1,6 @@ -#include "test_box.hpp" - #include #include #include -#include #include #include #include @@ -14,13 +11,15 @@ namespace test_box { struct DecisionTree { struct Leaf { - rfl::Field<"value", double> value; + using Tag = rfl::Literal<"Leaf">; + double value; }; struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", rfl::Box> lesser; - rfl::Field<"right", rfl::Box> greater; + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Box lesser; + rfl::Box greater; }; using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; @@ -28,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; @@ -43,5 +40,6 @@ void test() { const DecisionTree tree{.leaf_or_node = std::move(node)}; write_and_read(tree); + } } // namespace test_box diff --git a/tests/flexbuffers/test_box.hpp b/tests/flexbuffers/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/flexbuffers/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class1.cpp b/tests/flexbuffers/test_custom_class1.cpp index c0685f84..899e899e 100644 --- a/tests/flexbuffers/test_custom_class1.cpp +++ b/tests/flexbuffers/test_custom_class1.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class1.hpp" - #include #include #include -#include #include #include #include @@ -14,9 +11,9 @@ namespace test_custom_class1 { struct Person { struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::vector> children = rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; }; using ReflectionType = PersonImpl; @@ -32,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/flexbuffers/test_custom_class1.hpp b/tests/flexbuffers/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/flexbuffers/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class2.cpp b/tests/flexbuffers/test_custom_class2.cpp deleted file mode 100644 index 6e8487c5..00000000 --- a/tests/flexbuffers/test_custom_class2.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "test_custom_class2.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class2 { - -struct Person { - Person(const std::string& _first_name, const std::string& _last_name, - const int _age) - : first_name_(_first_name), last_name_(_last_name), age_(_age) {} - - const auto& first_name() const { return first_name_; } - - const auto& last_name() const { return last_name_; } - - auto age() const { return age_; } - - private: - std::string first_name_; - std::string last_name_; - int age_; -}; - -struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"age", int> age; - - static PersonImpl from_class(const Person& _p) noexcept { - return PersonImpl{.first_name = _p.first_name(), - .last_name = _p.last_name(), - .age = _p.age()}; - } - - Person to_class() const { return Person(first_name(), last_name(), age()); } -}; - -} // namespace test_custom_class2 - -namespace rfl { -namespace parsing { - -template -struct Parser - : public CustomParser {}; - -} // namespace parsing -} // namespace rfl - -namespace test_custom_class2 { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = test_custom_class2::Person("Bart", "Simpson", 10); - - write_and_read(bart); -} - -} // namespace test_custom_class2 diff --git a/tests/flexbuffers/test_custom_class2.hpp b/tests/flexbuffers/test_custom_class2.hpp deleted file mode 100644 index 21cfbd51..00000000 --- a/tests/flexbuffers/test_custom_class2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class2{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class3.cpp b/tests/flexbuffers/test_custom_class3.cpp index fdd799d4..edd41c42 100644 --- a/tests/flexbuffers/test_custom_class3.cpp +++ b/tests/flexbuffers/test_custom_class3.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class3.hpp" - #include #include #include -#include #include #include #include @@ -13,11 +10,9 @@ namespace test_custom_class3 { struct Person { - Person(const std::string& _first_name, - const rfl::Box& _last_name, int _age) - : first_name_(_first_name), - last_name_(rfl::make_box(*_last_name)), - age_(_age) {} + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} const auto& first_name() const { return first_name_; } @@ -27,22 +22,23 @@ struct Person { private: std::string first_name_; - rfl::Box last_name_; + std::string last_name_; int age_; }; struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", rfl::Box> last_name; - rfl::Field<"age", int> age; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + int age; static PersonImpl from_class(const Person& _p) noexcept { return PersonImpl{.first_name = _p.first_name(), - .last_name = rfl::make_box(*_p.last_name()), + .last_name = _p.last_name(), .age = _p.age()}; } -}; + Person to_class() const { return Person(first_name(), last_name(), age); } +}; } // namespace test_custom_class3 namespace rfl { @@ -58,11 +54,8 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = test_custom_class3::Person( - "Bart", rfl::make_box("Simpson"), 10); +TEST(flexbuf, test_custom_class3) { + const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); } diff --git a/tests/flexbuffers/test_custom_class3.hpp b/tests/flexbuffers/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/flexbuffers/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class4.cpp b/tests/flexbuffers/test_custom_class4.cpp new file mode 100644 index 00000000..1ba35f4a --- /dev/null +++ b/tests/flexbuffers/test_custom_class4.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class4 { + +TEST(flexbuf, test_custom_class4) { + const auto bart = test_custom_class4::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart); +} +} // namespace test_custom_class4 diff --git a/tests/flexbuffers/test_custom_constructor.cpp b/tests/flexbuffers/test_custom_constructor.cpp deleted file mode 100644 index bad09e24..00000000 --- a/tests/flexbuffers/test_custom_constructor.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "test_custom_constructor.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_constructor { - -struct Person { - static rfl::Result from_flexbuf( - typename rfl::flexbuf::Reader::InputVarType _obj) { - /// This only exists for the purpose of the test. - const auto change_first_name = [](auto&& _person) { - return rfl::replace(std::move(_person), - rfl::Field<"firstName", std::string>("Bart")); - }; - const auto from_nt = [](auto&& _nt) { - return rfl::from_named_tuple(std::move(_nt)); - }; - return rfl::flexbuf::read>(_obj) - .transform(from_nt) - .transform(change_first_name); - } - - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto person1 = Person{ - .first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"}; - - const auto bytes = rfl::flexbuf::write(person1); - - const auto res = rfl::flexbuf::read(bytes); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } - - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } -} -} // namespace test_custom_constructor diff --git a/tests/flexbuffers/test_custom_constructor.hpp b/tests/flexbuffers/test_custom_constructor.hpp deleted file mode 100644 index 013721d0..00000000 --- a/tests/flexbuffers/test_custom_constructor.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_constructor{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_optional_fields.cpp b/tests/flexbuffers/test_default_values.cpp similarity index 51% rename from tests/flexbuffers/test_optional_fields.cpp rename to tests/flexbuffers/test_default_values.cpp index f15c35a8..31d68ebf 100644 --- a/tests/flexbuffers/test_optional_fields.cpp +++ b/tests/flexbuffers/test_default_values.cpp @@ -1,36 +1,28 @@ -#include "test_optional_fields.hpp" - +#include #include #include -#include #include #include #include #include "write_and_read.hpp" -namespace test_optional_fields { +namespace test_default_values { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::optional>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_default_values) { const auto bart = Person{.first_name = "Bart"}; - const auto lisa = Person{.first_name = "Lisa"}; - const auto maggie = Person{.first_name = "Maggie"}; - const auto homer = Person{.first_name = "Homer", .children = std::vector({bart, lisa, maggie})}; write_and_read(homer); } -} // namespace test_optional_fields +} // namespace test_default_values diff --git a/tests/flexbuffers/test_deque.cpp b/tests/flexbuffers/test_deque.cpp index 7e1d1139..aa120c37 100644 --- a/tests/flexbuffers/test_deque.cpp +++ b/tests/flexbuffers/test_deque.cpp @@ -1,8 +1,5 @@ -#include "test_deque.hpp" - #include #include -#include #include #include #include @@ -12,15 +9,12 @@ namespace test_deque { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); @@ -30,5 +24,6 @@ void test() { Person{.first_name = "Homer", .children = std::move(children)}; write_and_read(homer); + } } // namespace test_deque diff --git a/tests/flexbuffers/test_deque.hpp b/tests/flexbuffers/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/flexbuffers/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_enum.cpp b/tests/flexbuffers/test_enum.cpp new file mode 100644 index 00000000..7802e8af --- /dev/null +++ b/tests/flexbuffers/test_enum.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_enum { + +enum class Color { red, green, blue, yellow }; + +struct Circle { + float radius; + Color color; +}; + +TEST(flexbuf, test_enum) { + const auto circle = Circle{.radius = 2.0, .color = Color::green}; + + write_and_read(circle); +} + +} // namespace test_enum diff --git a/tests/flexbuffers/test_field_variant.cpp b/tests/flexbuffers/test_field_variant.cpp index e87ce872..2c1a830f 100644 --- a/tests/flexbuffers/test_field_variant.cpp +++ b/tests/flexbuffers/test_field_variant.cpp @@ -1,9 +1,6 @@ -#include "test_field_variant.hpp" - #include #include #include -#include #include #include #include @@ -13,25 +10,23 @@ namespace test_field_variant { struct Circle { - rfl::Field<"radius", double> radius; + double radius; }; struct Rectangle { - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; + double height; + double width; }; struct Square { - rfl::Field<"width", double> width; + double width; }; using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/flexbuffers/test_field_variant.hpp b/tests/flexbuffers/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/flexbuffers/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_flag_enum.cpp b/tests/flexbuffers/test_flag_enum.cpp new file mode 100644 index 00000000..ccba582b --- /dev/null +++ b/tests/flexbuffers/test_flag_enum.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flag_enum { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +TEST(flexbuf, test_flag_enum) { + const auto circle = + Circle{.radius = 2.0, .color = Color::blue | Color::orange}; + + write_and_read(circle); +} + +} // namespace test_flag_enum diff --git a/tests/flexbuffers/test_flag_enum_with_int.cpp b/tests/flexbuffers/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..1dc35155 --- /dev/null +++ b/tests/flexbuffers/test_flag_enum_with_int.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flag_enum_with_int { + +enum class Color { + red = 256, + green = 512, + blue = 1024, + yellow = 2048, + orange = (256 | 2048) // red + yellow = orange +}; + +inline Color operator|(Color c1, Color c2) { + return static_cast(static_cast(c1) | static_cast(c2)); +} + +struct Circle { + float radius; + Color color; +}; + +TEST(flexbuf, test_flag_enum_with_int) { + const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/flexbuffers/test_flatten.cpp b/tests/flexbuffers/test_flatten.cpp new file mode 100644 index 00000000..fa7d9431 --- /dev/null +++ b/tests/flexbuffers/test_flatten.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; +}; + +TEST(flexbuf, test_flatten) { + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten diff --git a/tests/flexbuffers/test_flatten_anonymous.cpp b/tests/flexbuffers/test_flatten_anonymous.cpp index 7db468dd..13ba28de 100644 --- a/tests/flexbuffers/test_flatten_anonymous.cpp +++ b/tests/flexbuffers/test_flatten_anonymous.cpp @@ -1,9 +1,6 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include -#include #include #include #include @@ -24,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), @@ -36,4 +31,5 @@ void test() { write_and_read(employee); } + } // namespace test_flatten_anonymous diff --git a/tests/flexbuffers/test_flatten_anonymous.hpp b/tests/flexbuffers/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/flexbuffers/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_forward_list.cpp b/tests/flexbuffers/test_forward_list.cpp index 4743e019..c747a3f5 100644 --- a/tests/flexbuffers/test_forward_list.cpp +++ b/tests/flexbuffers/test_forward_list.cpp @@ -1,8 +1,5 @@ -#include "test_forward_list.hpp" - #include #include -#include #include #include #include @@ -12,15 +9,12 @@ namespace test_forward_list { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); @@ -30,5 +24,6 @@ void test() { Person{.first_name = "Homer", .children = std::move(children)}; write_and_read(homer); + } } // namespace test_forward_list diff --git a/tests/flexbuffers/test_forward_list.hpp b/tests/flexbuffers/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/flexbuffers/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_list.cpp b/tests/flexbuffers/test_list.cpp deleted file mode 100644 index 5190d244..00000000 --- a/tests/flexbuffers/test_list.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "test_list.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_list { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->emplace_back(Person{.first_name = "Bart"}); - children->emplace_back(Person{.first_name = "Lisa"}); - children->emplace_back(Person{.first_name = "Maggie"}); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read(homer); -} -} // namespace test_list diff --git a/tests/flexbuffers/test_list.hpp b/tests/flexbuffers/test_list.hpp deleted file mode 100644 index 956d36cc..00000000 --- a/tests/flexbuffers/test_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_literal.cpp b/tests/flexbuffers/test_literal.cpp index af92ef3e..9c5d8b8b 100644 --- a/tests/flexbuffers/test_literal.cpp +++ b/tests/flexbuffers/test_literal.cpp @@ -1,9 +1,6 @@ -#include "test_literal.hpp" - #include #include #include -#include #include #include #include @@ -16,14 +13,12 @@ using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; using LastName = rfl::Literal<"Simpson">; struct Person { - rfl::Field<"firstName", FirstName> first_name; - rfl::Field<"lastName", LastName> last_name = rfl::default_value; - rfl::Field<"children", std::vector> children = rfl::default_value; + rfl::Rename<"firstName", FirstName> first_name; + rfl::Rename<"lastName", LastName> last_name; + std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/flexbuffers/test_literal.hpp b/tests/flexbuffers/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/flexbuffers/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_literal_map.cpp b/tests/flexbuffers/test_literal_map.cpp new file mode 100644 index 00000000..46fa5d14 --- /dev/null +++ b/tests/flexbuffers/test_literal_map.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal_map { + +using FieldName = rfl::Literal<"firstName", "lastName">; + +TEST(flexbuf, test_literal_map) { + std::map> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_literal_map diff --git a/tests/flexbuffers/test_map.cpp b/tests/flexbuffers/test_map.cpp index 97a8a74b..119290bb 100644 --- a/tests/flexbuffers/test_map.cpp +++ b/tests/flexbuffers/test_map.cpp @@ -1,9 +1,6 @@ -#include "test_map.hpp" - #include #include #include -#include #include #include @@ -12,19 +9,16 @@ namespace test_map { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); +TEST(flexbuf, test_map) { + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); const auto homer = Person{.first_name = "Homer", .children = std::move(children)}; diff --git a/tests/flexbuffers/test_map.hpp b/tests/flexbuffers/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/flexbuffers/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_map_with_key_validation.cpp b/tests/flexbuffers/test_map_with_key_validation.cpp new file mode 100644 index 00000000..3a978ead --- /dev/null +++ b/tests/flexbuffers/test_map_with_key_validation.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +TEST(flexbuf, test_map_with_key_validation) { + auto children = std::make_unique>(); + + children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/flexbuffers/test_monster_example1.cpp b/tests/flexbuffers/test_monster_example.cpp similarity index 69% rename from tests/flexbuffers/test_monster_example1.cpp rename to tests/flexbuffers/test_monster_example.cpp index b1db84b2..d6c65eb5 100644 --- a/tests/flexbuffers/test_monster_example1.cpp +++ b/tests/flexbuffers/test_monster_example.cpp @@ -1,17 +1,14 @@ -#include "test_monster_example1.hpp" - #include #include -#include #include #include #include #include "write_and_read.hpp" -namespace test_monster_example1 { +namespace test_monster_example { -enum class Color { Red, Green, Blue }; +using Color = rfl::Literal<"Red", "Green", "Blue">; struct Weapon { std::string name; @@ -32,16 +29,14 @@ struct Monster { short hp = 100; std::string name; bool friendly = false; - std::vector inventory; - Color color = Color::Blue; + std::vector inventory; + Color color = Color::make<"Blue">(); std::vector weapons; Equipment equipped; std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; @@ -49,19 +44,17 @@ void test() { 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 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::Red, + .color = Color::make<"Red">(), .weapons = weapons, .equipped = rfl::make_field<"weapon">(axe)}; write_and_read(orc); } - -} // namespace test_monster_example1 +} // namespace test_monster_example diff --git a/tests/flexbuffers/test_monster_example1.hpp b/tests/flexbuffers/test_monster_example1.hpp deleted file mode 100644 index 80cd680d..00000000 --- a/tests/flexbuffers/test_monster_example1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example1{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_monster_example2.cpp b/tests/flexbuffers/test_monster_example2.cpp deleted file mode 100644 index 3cb2ab73..00000000 --- a/tests/flexbuffers/test_monster_example2.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "test_monster_example2.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_monster_example2 { - -using Color = rfl::Literal<"Red", "Green", "Blue">; - -struct Weapon { - rfl::Field<"name", std::string> name; - rfl::Field<"damage", short> damage; -}; - -using Equipment = rfl::Variant>; - -struct Vec3 { - rfl::Field<"x", float> x; - rfl::Field<"y", float> y; - rfl::Field<"z", float> z; -}; - -struct Monster { - rfl::Field<"pos", Vec3> pos; - rfl::Field<"mana", short> mana = 150; - rfl::Field<"hp", short> hp = 100; - rfl::Field<"name", std::string> name; - rfl::Field<"friendly", bool> friendly = false; - rfl::Field<"inventory", std::vector> inventory; - rfl::Field<"color", Color> color = Color::make<"Blue">(); - rfl::Field<"weapons", std::vector> weapons; - rfl::Field<"equipped", Equipment> equipped; - rfl::Field<"path", std::vector> path; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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), - .path = rfl::default_value}; - - write_and_read(orc); -} -} // namespace test_monster_example2 diff --git a/tests/flexbuffers/test_monster_example2.hpp b/tests/flexbuffers/test_monster_example2.hpp deleted file mode 100644 index 9d82a452..00000000 --- a/tests/flexbuffers/test_monster_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example2{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_multimap.cpp b/tests/flexbuffers/test_multimap.cpp deleted file mode 100644 index add150d1..00000000 --- a/tests/flexbuffers/test_multimap.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "test_multimap.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multimap { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read(homer); -} -} // namespace test_multimap diff --git a/tests/flexbuffers/test_multimap.hpp b/tests/flexbuffers/test_multimap.hpp deleted file mode 100644 index 81845572..00000000 --- a/tests/flexbuffers/test_multimap.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_multimap{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_multiset.cpp b/tests/flexbuffers/test_multiset.cpp deleted file mode 100644 index 6a5a31ec..00000000 --- a/tests/flexbuffers/test_multiset.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "test_multiset.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multiset { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read(homer); -} -} // namespace test_multiset diff --git a/tests/flexbuffers/test_multiset.hpp b/tests/flexbuffers/test_multiset.hpp deleted file mode 100644 index adfcf186..00000000 --- a/tests/flexbuffers/test_multiset.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_multiset{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_optional_fields.hpp b/tests/flexbuffers/test_optional_fields.hpp deleted file mode 100644 index 640cab95..00000000 --- a/tests/flexbuffers/test_optional_fields.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_optional_fields{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_readme_example.cpp b/tests/flexbuffers/test_readme_example.cpp index 7bc77c80..4212bf18 100644 --- a/tests/flexbuffers/test_readme_example.cpp +++ b/tests/flexbuffers/test_readme_example.cpp @@ -1,8 +1,5 @@ -#include "test_readme_example.hpp" - #include #include -#include #include #include #include @@ -11,49 +8,39 @@ namespace test_readme_example { -using Age = rfl::Validator, rfl::Maximum<130>>>; +using Age = rfl::Validator, rfl::Maximum<130>>; struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; - rfl::Field<"age", Age> age; - rfl::Field<"email", rfl::Email> email; - rfl::Field<"children", std::vector> children; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_readme_example) { const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", .birthday = "1987-04-19", .age = 10, - .email = "bart@simpson.com", - .children = std::vector()}; - - const auto lisa = Person{ - .first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com", - .children = rfl::default_value // same as std::vector() - }; - - const auto maggie = - rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), - rfl::make_field<"email">(std::string("maggie@simpson.com")), - rfl::make_field<"age">(0)); - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; write_and_read(homer); } diff --git a/tests/flexbuffers/test_readme_example.hpp b/tests/flexbuffers/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/flexbuffers/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_readme_example2.cpp b/tests/flexbuffers/test_readme_example2.cpp new file mode 100644 index 00000000..02cabd76 --- /dev/null +++ b/tests/flexbuffers/test_readme_example2.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +TEST(flexbuf, test_readme_example2) { + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/flexbuffers/test_ref.cpp b/tests/flexbuffers/test_ref.cpp index 41ceca1c..1e9e6733 100644 --- a/tests/flexbuffers/test_ref.cpp +++ b/tests/flexbuffers/test_ref.cpp @@ -1,9 +1,6 @@ -#include "test_ref.hpp" - #include #include #include -#include #include #include #include @@ -14,13 +11,15 @@ namespace test_ref { struct DecisionTree { struct Leaf { - rfl::Field<"value", double> value; + using Tag = rfl::Literal<"Leaf">; + double value; }; struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", rfl::Ref> lesser; - rfl::Field<"right", rfl::Ref> greater; + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + rfl::Ref lesser; + rfl::Ref greater; }; using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; @@ -28,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; @@ -43,5 +40,6 @@ void test() { const DecisionTree tree{.leaf_or_node = std::move(node)}; write_and_read(tree); + } } // namespace test_ref diff --git a/tests/flexbuffers/test_ref.hpp b/tests/flexbuffers/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/flexbuffers/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_save_load.cpp b/tests/flexbuffers/test_save_load.cpp index 9d9d7d7e..5aaa4507 100644 --- a/tests/flexbuffers/test_save_load.cpp +++ b/tests/flexbuffers/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include #include -#include "write_and_read.hpp" +#include namespace test_save_load { @@ -16,17 +14,15 @@ using Age = rfl::Validator, rfl::Maximum<130>>>; struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; - rfl::Field<"age", Age> age; - rfl::Field<"email", rfl::Email> email; - rfl::Field<"children", std::vector> children; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -34,19 +30,17 @@ void test() { .email = "bart@simpson.com", .children = std::vector()}; - const auto lisa = Person{ - .first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com", - .children = rfl::default_value // same as std::vector() - }; + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; - const auto maggie = - rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), - rfl::make_field<"email">(std::string("maggie@simpson.com")), - rfl::make_field<"age">(0)); + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; const auto homer1 = Person{.first_name = "Homer", @@ -56,19 +50,13 @@ void test() { .email = "homer@simpson.com", .children = std::vector({bart, lisa, maggie})}; - rfl::flexbuf::save("homer.fb", homer1); - - const auto homer2 = rfl::flexbuf::load("homer.fb").value(); + rfl::flexbuf::save("homer.flexbuf", homer1); - const auto bytes1 = rfl::flexbuf::write(homer1); - const auto bytes2 = rfl::flexbuf::write(homer2); + const auto homer2 = rfl::flexbuf::load("homer.flexbuf").value(); - if (bytes1 != bytes2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } + const auto string1 = rfl::flexbuf::write(homer1); + const auto string2 = rfl::flexbuf::write(homer2); - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/flexbuffers/test_save_load.hpp b/tests/flexbuffers/test_save_load.hpp deleted file mode 100644 index a8829fd1..00000000 --- a/tests/flexbuffers/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_set.cpp b/tests/flexbuffers/test_set.cpp index 4da6eeff..5b7d1031 100644 --- a/tests/flexbuffers/test_set.cpp +++ b/tests/flexbuffers/test_set.cpp @@ -1,8 +1,5 @@ -#include "test_set.hpp" - #include #include -#include #include #include #include @@ -12,15 +9,12 @@ namespace test_set { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/flexbuffers/test_set.hpp b/tests/flexbuffers/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/flexbuffers/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_size.cpp b/tests/flexbuffers/test_size.cpp new file mode 100644 index 00000000..b07f02d5 --- /dev/null +++ b/tests/flexbuffers/test_size.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +TEST(flexbuf, test_size) { + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_size diff --git a/tests/flexbuffers/test_string_map.cpp b/tests/flexbuffers/test_string_map.cpp index fc2157cb..1f45634e 100644 --- a/tests/flexbuffers/test_string_map.cpp +++ b/tests/flexbuffers/test_string_map.cpp @@ -1,19 +1,14 @@ -#include "test_string_map.hpp" - #include #include #include #include -#include #include #include #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/flexbuffers/test_string_map.hpp b/tests/flexbuffers/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/flexbuffers/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_string_unordered_map.cpp b/tests/flexbuffers/test_string_unordered_map.cpp deleted file mode 100644 index 07241877..00000000 --- a/tests/flexbuffers/test_string_unordered_map.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "test_string_unordered_map.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_string_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Map = std::unordered_map>; - Map homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; -} -} // namespace test_string_unordered_map diff --git a/tests/flexbuffers/test_string_unordered_map.hpp b/tests/flexbuffers/test_string_unordered_map.hpp deleted file mode 100644 index 268057a3..00000000 --- a/tests/flexbuffers/test_string_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_tagged_union.cpp b/tests/flexbuffers/test_tagged_union.cpp index f09d141d..da123f2e 100644 --- a/tests/flexbuffers/test_tagged_union.cpp +++ b/tests/flexbuffers/test_tagged_union.cpp @@ -1,9 +1,6 @@ -#include "test_tagged_union.hpp" - #include #include #include -#include #include #include #include @@ -25,13 +22,10 @@ struct Square { double width; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; +TEST(flexbuf, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/flexbuffers/test_tagged_union.hpp b/tests/flexbuffers/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/flexbuffers/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_timestamp.cpp b/tests/flexbuffers/test_timestamp.cpp new file mode 100644 index 00000000..1c1ce052 --- /dev/null +++ b/tests/flexbuffers/test_timestamp.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +TEST(flexbuf, test_timestamp) { + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart); +} +} // namespace test_timestamp diff --git a/tests/flexbuffers/test_unique_ptr.cpp b/tests/flexbuffers/test_unique_ptr.cpp index c093a161..5adc14e9 100644 --- a/tests/flexbuffers/test_unique_ptr.cpp +++ b/tests/flexbuffers/test_unique_ptr.cpp @@ -1,9 +1,6 @@ -#include "test_unique_ptr.hpp" - #include #include #include -#include #include #include #include @@ -13,15 +10,12 @@ namespace test_unique_ptr { struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> children = - rfl::default_value; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/flexbuffers/test_unique_ptr.hpp b/tests/flexbuffers/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/flexbuffers/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_unique_ptr2.cpp b/tests/flexbuffers/test_unique_ptr2.cpp index dd02f9b0..4a5b76f7 100644 --- a/tests/flexbuffers/test_unique_ptr2.cpp +++ b/tests/flexbuffers/test_unique_ptr2.cpp @@ -1,9 +1,6 @@ -#include "test_unique_ptr2.hpp" - #include #include #include -#include #include #include #include @@ -14,13 +11,15 @@ namespace test_unique_ptr2 { struct DecisionTree { struct Leaf { - rfl::Field<"value", double> value; + using Tag = rfl::Literal<"Leaf">; + double value; }; struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", std::unique_ptr> lesser; - rfl::Field<"right", std::unique_ptr> greater; + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr greater; }; using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; @@ -28,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(flexbuf, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/flexbuffers/test_unique_ptr2.hpp b/tests/flexbuffers/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/flexbuffers/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_map.cpp b/tests/flexbuffers/test_unordered_map.cpp deleted file mode 100644 index f063937a..00000000 --- a/tests/flexbuffers/test_unordered_map.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "test_unordered_map.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_map { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> - children = rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; -} -} // namespace test_unordered_map diff --git a/tests/flexbuffers/test_unordered_map.hpp b/tests/flexbuffers/test_unordered_map.hpp deleted file mode 100644 index 9b059a96..00000000 --- a/tests/flexbuffers/test_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_multimap.cpp b/tests/flexbuffers/test_unordered_multimap.cpp deleted file mode 100644 index d304b26e..00000000 --- a/tests/flexbuffers/test_unordered_multimap.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "test_unordered_multimap.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multimap { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> - children = rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>(); - children->insert(std::make_pair(1, Person{.first_name = "Bart"})); - children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); - children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; -} -} // namespace test_unordered_multimap diff --git a/tests/flexbuffers/test_unordered_multimap.hpp b/tests/flexbuffers/test_unordered_multimap.hpp deleted file mode 100644 index 0f36f705..00000000 --- a/tests/flexbuffers/test_unordered_multimap.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_multimap{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_multiset.cpp b/tests/flexbuffers/test_unordered_multiset.cpp deleted file mode 100644 index c72e2bc5..00000000 --- a/tests/flexbuffers/test_unordered_multiset.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "test_unordered_multiset.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multiset { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> - children = rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered multisets are unpredictable. We therefore only make sure that - // this compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; -} -} // namespace test_unordered_multiset diff --git a/tests/flexbuffers/test_unordered_multiset.hpp b/tests/flexbuffers/test_unordered_multiset.hpp deleted file mode 100644 index 491ff7ba..00000000 --- a/tests/flexbuffers/test_unordered_multiset.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_multiset{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_set.cpp b/tests/flexbuffers/test_unordered_set.cpp deleted file mode 100644 index 1d9bbbf3..00000000 --- a/tests/flexbuffers/test_unordered_set.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "test_unordered_set.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_set { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::unique_ptr>> - children = rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_set({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered sets are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; -} -} // namespace test_unordered_set diff --git a/tests/flexbuffers/test_unordered_set.hpp b/tests/flexbuffers/test_unordered_set.hpp deleted file mode 100644 index be2b257f..00000000 --- a/tests/flexbuffers/test_unordered_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_variant.cpp b/tests/flexbuffers/test_variant.cpp index 2861a3ab..8adc8d80 100644 --- a/tests/flexbuffers/test_variant.cpp +++ b/tests/flexbuffers/test_variant.cpp @@ -1,9 +1,6 @@ -#include "test_variant.hpp" - #include #include #include -#include #include #include #include @@ -13,23 +10,21 @@ namespace test_variant { struct Circle { - rfl::Field<"radius", double> radius; + double radius; }; struct Rectangle { - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; + double height; + double width; }; struct Square { - rfl::Field<"width", double> width; + double width; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Shapes = std::variant>; +using Shapes = std::variant>; +TEST(flexbuf, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/flexbuffers/test_variant.hpp b/tests/flexbuffers/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/flexbuffers/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/flexbuffers/test_wstring.cpp b/tests/flexbuffers/test_wstring.cpp index ba6202c0..e0e1dc1f 100644 --- a/tests/flexbuffers/test_wstring.cpp +++ b/tests/flexbuffers/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(flexbuf, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/flexbuffers/test_wstring.hpp b/tests/flexbuffers/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/flexbuffers/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp deleted file mode 100644 index aab6d5a7..00000000 --- a/tests/flexbuffers/tests.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "test_all_of.hpp" -#include "test_anonymous_fields.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class2.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_constructor.hpp" -#include "test_deque.hpp" -#include "test_field_variant.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_list.hpp" -#include "test_literal.hpp" -#include "test_map.hpp" -#include "test_monster_example1.hpp" -#include "test_monster_example2.hpp" -#include "test_multimap.hpp" -#include "test_multiset.hpp" -#include "test_optional_fields.hpp" -#include "test_readme_example.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_string_map.hpp" -#include "test_string_unordered_map.hpp" -#include "test_tagged_union.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_unordered_map.hpp" -#include "test_unordered_multimap.hpp" -#include "test_unordered_multiset.hpp" -#include "test_unordered_set.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_monster_example1::test(); - test_monster_example2::test(); - test_anonymous_fields::test(); - test_flatten_anonymous::test(); - test_optional_fields::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_literal::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_custom_class1::test(); - test_custom_class2::test(); - test_custom_class3::test(); - test_box::test(); - test_ref::test(); - - test_deque::test(); - test_forward_list::test(); - test_list::test(); - test_map::test(); - test_multimap::test(); - test_multiset::test(); - test_set::test(); - test_string_map::test(); - test_string_unordered_map::test(); - test_unordered_map::test(); - test_unordered_multimap::test(); - test_unordered_multiset::test(); - test_unordered_set::test(); - - test_custom_constructor::test(); - - test_all_of::test(); - - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/flexbuffers/write_and_read.hpp b/tests/flexbuffers/write_and_read.hpp index 8bf426f3..a7f6d9ac 100644 --- a/tests/flexbuffers/write_and_read.hpp +++ b/tests/flexbuffers/write_and_read.hpp @@ -1,39 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto bytes1 = rfl::flexbuf::write(_struct); - - const auto res = rfl::flexbuf::read(bytes1); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << std::endl; - return; - } - - const auto bytes2 = rfl::flexbuf::write(res.value()); - - if (bytes1.size() != bytes2.size()) { - std::cout << "Test failed on write. Number of bytes was different." - << std::endl - << std::endl; - return; - } - - if (bytes1 != bytes2) { - std::cout << "Test failed on write. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::flexbuf::write(_struct); + const auto res = rfl::flexbuf::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::flexbuf::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index 1e077f6a..9ddfd6e0 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -2,6 +2,17 @@ project(reflect-cpp-json-tests) file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-json-tests ${SOURCES}) +add_executable( + reflect-cpp-json-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) -target_link_libraries(reflect-cpp-json-tests PRIVATE reflectcpp) +target_include_directories(reflect-cpp-json-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") + +target_link_libraries( + reflect-cpp-json-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/json/test_all_of.cpp b/tests/json/test_all_of.cpp index a296d1a3..49130cdb 100644 --- a/tests/json/test_all_of.cpp +++ b/tests/json/test_all_of.cpp @@ -1,5 +1,3 @@ -#include "test_all_of.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { Age age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_all_of) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/json/test_all_of.hpp b/tests/json/test_all_of.hpp deleted file mode 100644 index 082d508a..00000000 --- a/tests/json/test_all_of.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_all_of{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_alphanumeric_map.cpp b/tests/json/test_alphanumeric_map.cpp index 803a30fc..091cff53 100644 --- a/tests/json/test_alphanumeric_map.cpp +++ b/tests/json/test_alphanumeric_map.cpp @@ -1,5 +1,3 @@ -#include "test_alphanumeric_map.hpp" - #include #include #include @@ -12,9 +10,7 @@ namespace test_alphanumeric_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_alphanumeric_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/json/test_alphanumeric_map.hpp b/tests/json/test_alphanumeric_map.hpp deleted file mode 100644 index 389fdc7b..00000000 --- a/tests/json/test_alphanumeric_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_alphanumeric_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_alphanumeric_unordered_map.cpp b/tests/json/test_alphanumeric_unordered_map.cpp index 3f8bc467..fa9c2790 100644 --- a/tests/json/test_alphanumeric_unordered_map.cpp +++ b/tests/json/test_alphanumeric_unordered_map.cpp @@ -1,5 +1,3 @@ -#include "test_alphanumeric_unordered_map.hpp" - #include #include #include @@ -11,9 +9,7 @@ #include "write_and_read.hpp" namespace test_alphanumeric_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_alphanumeric_unordered_map) { using Map = std::unordered_map>; Map homer; @@ -26,6 +22,5 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; } } // namespace test_alphanumeric_unordered_map diff --git a/tests/json/test_alphanumeric_unordered_map.hpp b/tests/json/test_alphanumeric_unordered_map.hpp deleted file mode 100644 index d9f85cd2..00000000 --- a/tests/json/test_alphanumeric_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_alphanumeric_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_and_then.cpp b/tests/json/test_and_then.cpp index 835fd99c..d33b9ca7 100644 --- a/tests/json/test_and_then.cpp +++ b/tests/json/test_and_then.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -7,7 +6,6 @@ #include #include -#include "test_replace.hpp" #include "write_and_read.hpp" namespace test_and_then { @@ -18,9 +16,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_and_then) { const auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; diff --git a/tests/json/test_and_then.hpp b/tests/json/test_and_then.hpp deleted file mode 100644 index 8e0bde5a..00000000 --- a/tests/json/test_and_then.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_and_then { -void test(); -} - diff --git a/tests/json/test_anonymous_fields.cpp b/tests/json/test_anonymous_fields.cpp index 60f98841..08d7210e 100644 --- a/tests/json/test_anonymous_fields.cpp +++ b/tests/json/test_anonymous_fields.cpp @@ -1,5 +1,3 @@ -#include "test_anonymous_fields.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_anonymous_fields) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", diff --git a/tests/json/test_anonymous_fields.hpp b/tests/json/test_anonymous_fields.hpp deleted file mode 100644 index 16c47d36..00000000 --- a/tests/json/test_anonymous_fields.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_anonymous_fields{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_any_of.cpp b/tests/json/test_any_of.cpp index f0c259f2..9238e4ee 100644 --- a/tests/json/test_any_of.cpp +++ b/tests/json/test_any_of.cpp @@ -1,5 +1,3 @@ -#include "test_any_of.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { Age age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_any_of) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/json/test_any_of.hpp b/tests/json/test_any_of.hpp deleted file mode 100644 index 128c8ebc..00000000 --- a/tests/json/test_any_of.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_any_of{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_apply.cpp b/tests/json/test_apply.cpp index bcb97db7..8b29ac32 100644 --- a/tests/json/test_apply.cpp +++ b/tests/json/test_apply.cpp @@ -7,7 +7,6 @@ #include #include -#include "test_replace.hpp" #include "write_and_read.hpp" namespace test_apply { @@ -18,9 +17,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_apply) { auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; const auto view = rfl::to_view(lisa); diff --git a/tests/json/test_apply.hpp b/tests/json/test_apply.hpp deleted file mode 100644 index cffdf39e..00000000 --- a/tests/json/test_apply.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_apply { -void test(); -} - diff --git a/tests/json/test_array.cpp b/tests/json/test_array.cpp index 54706e42..dcc9cee4 100644 --- a/tests/json/test_array.cpp +++ b/tests/json/test_array.cpp @@ -1,4 +1,3 @@ -#include "test_array.hpp" #include #include @@ -18,9 +17,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/json/test_array.hpp b/tests/json/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/json/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_as.cpp b/tests/json/test_as.cpp index a5a2d96d..95f578d4 100644 --- a/tests/json/test_as.cpp +++ b/tests/json/test_as.cpp @@ -1,5 +1,3 @@ -#include "test_as.hpp" - #include #include #include @@ -28,8 +26,7 @@ struct C { rfl::Field<"f4", rfl::Box> f4; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; +TEST(json, test_as) { auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; diff --git a/tests/json/test_as.hpp b/tests/json/test_as.hpp deleted file mode 100644 index a2a8e1ab..00000000 --- a/tests/json/test_as.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_as{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_as2.cpp b/tests/json/test_as2.cpp index fe297002..ebc2a666 100644 --- a/tests/json/test_as2.cpp +++ b/tests/json/test_as2.cpp @@ -1,5 +1,3 @@ -#include "test_as2.hpp" - #include #include #include @@ -28,9 +26,7 @@ struct C { rfl::Box f4; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_as2) { auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; diff --git a/tests/json/test_as2.hpp b/tests/json/test_as2.hpp deleted file mode 100644 index 127cd8f3..00000000 --- a/tests/json/test_as2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_as2 { -void test(); -} - diff --git a/tests/json/test_as_flatten.cpp b/tests/json/test_as_flatten.cpp index 17036d5e..375262a9 100644 --- a/tests/json/test_as_flatten.cpp +++ b/tests/json/test_as_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_as_flatten.hpp" - #include #include #include @@ -28,9 +26,7 @@ struct C { int f5; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_as_flatten) { auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; diff --git a/tests/json/test_as_flatten.hpp b/tests/json/test_as_flatten.hpp deleted file mode 100644 index 9b7df9d4..00000000 --- a/tests/json/test_as_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_as_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_box.cpp b/tests/json/test_box.cpp index a7c9090e..b5826ef0 100644 --- a/tests/json/test_box.cpp +++ b/tests/json/test_box.cpp @@ -1,5 +1,3 @@ -#include "test_box.hpp" - #include #include #include @@ -30,9 +28,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/json/test_box.hpp b/tests/json/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/json/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_box2.cpp b/tests/json/test_box2.cpp index 5733948f..d22cfd94 100644 --- a/tests/json/test_box2.cpp +++ b/tests/json/test_box2.cpp @@ -6,22 +6,15 @@ #include #include -#include "test_box.hpp" +#include namespace test_box2 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_box2) { auto ptr = std::make_unique("Hello World!"); const rfl::Result> box = rfl::make_box(std::move(ptr)); - if (box) { - std::cout << "OK" << std::endl << std::endl; - } else { - std::cout << "Expected the result to be successful." << std::endl - << std::endl; - } -} + ASSERT_TRUE(box && true); + } } // namespace test_box2 diff --git a/tests/json/test_box2.hpp b/tests/json/test_box2.hpp deleted file mode 100644 index 63c3c303..00000000 --- a/tests/json/test_box2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box2 { -void test(); -} - diff --git a/tests/json/test_c_array_class1.cpp b/tests/json/test_c_array_class1.cpp index 91de3284..34e3c16c 100644 --- a/tests/json/test_c_array_class1.cpp +++ b/tests/json/test_c_array_class1.cpp @@ -1,5 +1,3 @@ -#include "test_c_array_class1.hpp" - #include #include #include @@ -14,9 +12,7 @@ struct Test1 { std::vector classes[3]; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_c_array_class1) { Test1 test1 = {.classes = {{"Little A", "Little B", "Little C"}, {"BIG A", "BIG B", "BIG C"}, {"??", "$%", "#@"}}}; diff --git a/tests/json/test_c_array_class1.hpp b/tests/json/test_c_array_class1.hpp deleted file mode 100644 index 8bb514e0..00000000 --- a/tests/json/test_c_array_class1.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_c_array_class1 { - -void test(); - -} diff --git a/tests/json/test_c_array_class2.cpp b/tests/json/test_c_array_class2.cpp index e61134ef..c1a93375 100644 --- a/tests/json/test_c_array_class2.cpp +++ b/tests/json/test_c_array_class2.cpp @@ -1,5 +1,3 @@ -#include "test_c_array_class2.hpp" - #include #include #include @@ -15,12 +13,11 @@ struct Test2 { std::string s; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_c_array_class2) { Test2 test2; for (int i = 0; i < 1 << 4; i++) { - test2.multi_dimension_arr[i >> 3 & 1][i >> 2 & 1][i >> 1 & 1][i >> 0 & 1] = i; + test2.multi_dimension_arr[i >> 3 & 1][i >> 2 & 1][i >> 1 & 1][i >> 0 & 1] = + i; } test2.s = "Hello, world!"; @@ -29,4 +26,4 @@ void test() { R"({"multi_dimension_arr":[[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]],"s":"Hello, world!"})"); } -} // namespace test_c_array +} // namespace test_c_array_class2 diff --git a/tests/json/test_c_array_class2.hpp b/tests/json/test_c_array_class2.hpp deleted file mode 100644 index 9e082e55..00000000 --- a/tests/json/test_c_array_class2.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_c_array_class2 { - -void test(); - -} diff --git a/tests/json/test_c_array_class3.cpp b/tests/json/test_c_array_class3.cpp index b49ce329..25560ff9 100644 --- a/tests/json/test_c_array_class3.cpp +++ b/tests/json/test_c_array_class3.cpp @@ -1,7 +1,5 @@ -#include "test_c_array_class3.hpp" - -#include #include +#include #include "rfl.hpp" #include "rfl/json.hpp" @@ -11,11 +9,9 @@ namespace test_c_array_class3 { using Test3 = std::array[3]; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_c_array_class3) { Test3 test3 = {1, 2, 3, 4, 5, 6, 7, 8, 9}; write_and_read(test3, "[[1,2,3],[4,5,6],[7,8,9]]"); } -} // namespace test_c_array +} // namespace test_c_array_class3 diff --git a/tests/json/test_c_array_class3.hpp b/tests/json/test_c_array_class3.hpp deleted file mode 100644 index 9a2931da..00000000 --- a/tests/json/test_c_array_class3.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_c_array_class3 { - -void test(); - -} diff --git a/tests/json/test_c_array_class4.cpp b/tests/json/test_c_array_class4.cpp index 8ba85777..6e2cd7d4 100644 --- a/tests/json/test_c_array_class4.cpp +++ b/tests/json/test_c_array_class4.cpp @@ -1,5 +1,3 @@ -#include "test_c_array_class4.hpp" - #include #include @@ -15,9 +13,7 @@ struct Test4 { int c[2][2]; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_c_array_class4) { Test4 test4 = {.a = {1, 2, 3}, .b = {4, 5, 6}, .c = {{7, 8}, {9, 10}}}; write_and_read(test4, R"({"a":[1,2,3],"b":[4,5,6],"c":[[7,8],[9,10]]})"); diff --git a/tests/json/test_c_array_class4.hpp b/tests/json/test_c_array_class4.hpp deleted file mode 100644 index 32857ec4..00000000 --- a/tests/json/test_c_array_class4.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_c_array_class4 { - -void test(); - -} diff --git a/tests/json/test_c_array_class5.cpp b/tests/json/test_c_array_class5.cpp index 55fe7b29..375b0985 100644 --- a/tests/json/test_c_array_class5.cpp +++ b/tests/json/test_c_array_class5.cpp @@ -1,5 +1,3 @@ -#include "test_c_array_class5.hpp" - #include #include @@ -15,9 +13,7 @@ struct Test5 { int c[2][2]; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_c_array_class5) { Test5 t1 = {.a = {1, 2, 3}, .b = {4, 5, 6}, .c = {{7, 8}, {9, 10}}}; const auto t2 = rfl::replace(t1, rfl::make_field<"b", int[3]>({1, 2, 3})); diff --git a/tests/json/test_c_array_class5.hpp b/tests/json/test_c_array_class5.hpp deleted file mode 100644 index 4b87e475..00000000 --- a/tests/json/test_c_array_class5.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_c_array_class5 { - -void test(); - -} diff --git a/tests/json/test_const_fields.cpp b/tests/json/test_const_fields.cpp index 26537888..f3dcb697 100644 --- a/tests/json/test_const_fields.cpp +++ b/tests/json/test_const_fields.cpp @@ -1,5 +1,3 @@ -#include "test_const_fields.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Person { const std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_const_fields) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/json/test_const_fields.hpp b/tests/json/test_const_fields.hpp deleted file mode 100644 index 1bfeacb9..00000000 --- a/tests/json/test_const_fields.hpp +++ /dev/null @@ -1,5 +0,0 @@ -namespace test_const_fields { - -void test(); - -} diff --git a/tests/json/test_custom_class1.cpp b/tests/json/test_custom_class1.cpp index 2e14e575..e112340f 100644 --- a/tests/json/test_custom_class1.cpp +++ b/tests/json/test_custom_class1.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class1.hpp" - #include #include #include @@ -32,8 +30,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; +TEST(json, test_custom_class1) { const auto bart = Person("Bart"); diff --git a/tests/json/test_custom_class1.hpp b/tests/json/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/json/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_custom_class2.cpp b/tests/json/test_custom_class2.cpp index adfa1021..86582980 100644 --- a/tests/json/test_custom_class2.cpp +++ b/tests/json/test_custom_class2.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class2.hpp" - #include #include #include @@ -31,15 +29,10 @@ struct FiveDigitCode { int code; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_custom_class2) { const auto result = rfl::json::read("123"); - if (result) { - std::cout << "Failed: Expected an error, but got none." << std::endl; - return; - } + EXPECT_TRUE(!result && true); const auto c = FiveDigitCode(12345); diff --git a/tests/json/test_custom_class2.hpp b/tests/json/test_custom_class2.hpp deleted file mode 100644 index 21cfbd51..00000000 --- a/tests/json/test_custom_class2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class2{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_custom_class3.cpp b/tests/json/test_custom_class3.cpp index 5da2c207..c476b555 100644 --- a/tests/json/test_custom_class3.cpp +++ b/tests/json/test_custom_class3.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class3.hpp" - #include #include #include @@ -57,9 +55,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})"); diff --git a/tests/json/test_custom_class3.hpp b/tests/json/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/json/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_custom_class4.cpp b/tests/json/test_custom_class4.cpp index 7057cbdf..4e83124f 100644 --- a/tests/json/test_custom_class4.cpp +++ b/tests/json/test_custom_class4.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class4.hpp" - #include #include #include @@ -58,9 +56,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/json/test_custom_class4.hpp b/tests/json/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/json/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_custom_constructor.cpp b/tests/json/test_custom_constructor.cpp index 28ec28dc..0c614806 100644 --- a/tests/json/test_custom_constructor.cpp +++ b/tests/json/test_custom_constructor.cpp @@ -1,5 +1,3 @@ -#include "test_custom_constructor.hpp" - #include #include #include @@ -32,24 +30,12 @@ struct Person { rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_custom_constructor) { const auto res = rfl::json::read( R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})"); - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } + EXPECT_TRUE (res && true); - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } + EXPECT_EQ(res.value().first_name(), "Bart"); } } // namespace test_custom_constructor diff --git a/tests/json/test_custom_constructor.hpp b/tests/json/test_custom_constructor.hpp deleted file mode 100644 index 013721d0..00000000 --- a/tests/json/test_custom_constructor.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_constructor{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_default_values.cpp b/tests/json/test_default_values.cpp index a8105197..db69e1b7 100644 --- a/tests/json/test_default_values.cpp +++ b/tests/json/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/json/test_default_values.hpp b/tests/json/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/json/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_deque.cpp b/tests/json/test_deque.cpp index 059a9a7f..dca0e9f7 100644 --- a/tests/json/test_deque.cpp +++ b/tests/json/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_deque) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/json/test_deque.hpp b/tests/json/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/json/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_email.cpp b/tests/json/test_email.cpp index 861963e2..0498075f 100644 --- a/tests/json/test_email.cpp +++ b/tests/json/test_email.cpp @@ -1,5 +1,3 @@ -#include "test_email.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { rfl::Email email; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_email) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .email = "homer@simpson.com"}; diff --git a/tests/json/test_email.hpp b/tests/json/test_email.hpp deleted file mode 100644 index 3d9e15a0..00000000 --- a/tests/json/test_email.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_email{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_empty_object.cpp b/tests/json/test_empty_object.cpp index 7e294224..84ad6949 100644 --- a/tests/json/test_empty_object.cpp +++ b/tests/json/test_empty_object.cpp @@ -1,5 +1,3 @@ -#include "test_empty_object.hpp" - #include #include #include @@ -13,9 +11,7 @@ namespace test_empty_object { struct Empty {}; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_empty_object) { const auto empty = Empty{}; write_and_read(empty, R"({})"); diff --git a/tests/json/test_empty_object.hpp b/tests/json/test_empty_object.hpp deleted file mode 100644 index 8eea4ddd..00000000 --- a/tests/json/test_empty_object.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_empty_object { -void test(); -} - diff --git a/tests/json/test_enum1.cpp b/tests/json/test_enum1.cpp index 358a4eb1..27960a6b 100644 --- a/tests/json/test_enum1.cpp +++ b/tests/json/test_enum1.cpp @@ -1,5 +1,3 @@ -#include "test_enum1.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum1) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle, R"({"radius":2.0,"color":"green"})"); diff --git a/tests/json/test_enum1.hpp b/tests/json/test_enum1.hpp deleted file mode 100644 index 97af2fb1..00000000 --- a/tests/json/test_enum1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum1 { -void test(); -} - diff --git a/tests/json/test_enum2.cpp b/tests/json/test_enum2.cpp index 7a9514bd..d1026439 100644 --- a/tests/json/test_enum2.cpp +++ b/tests/json/test_enum2.cpp @@ -1,5 +1,3 @@ -#include "test_enum2.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum2) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::green}; if (auto color = rfl::string_to_enum("red"); color) { diff --git a/tests/json/test_enum2.hpp b/tests/json/test_enum2.hpp deleted file mode 100644 index 7941d5e9..00000000 --- a/tests/json/test_enum2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum2 { -void test(); -} - diff --git a/tests/json/test_enum3.cpp b/tests/json/test_enum3.cpp index 77b1ffff..84a17a73 100644 --- a/tests/json/test_enum3.cpp +++ b/tests/json/test_enum3.cpp @@ -1,5 +1,3 @@ -#include "test_enum3.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum3) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; if (auto color = rfl::string_to_enum("bart"); color) { diff --git a/tests/json/test_enum3.hpp b/tests/json/test_enum3.hpp deleted file mode 100644 index 4bf7efab..00000000 --- a/tests/json/test_enum3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum3 { -void test(); -} - diff --git a/tests/json/test_enum4.cpp b/tests/json/test_enum4.cpp index 7be74536..3afc184e 100644 --- a/tests/json/test_enum4.cpp +++ b/tests/json/test_enum4.cpp @@ -1,5 +1,3 @@ -#include "test_enum4.hpp" - #include #include #include @@ -19,8 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; +TEST(json, test_enum4) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; diff --git a/tests/json/test_enum4.hpp b/tests/json/test_enum4.hpp deleted file mode 100644 index 070d7b81..00000000 --- a/tests/json/test_enum4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum4 { -void test(); -} - diff --git a/tests/json/test_enum5.cpp b/tests/json/test_enum5.cpp index 3673011f..3040dc7a 100644 --- a/tests/json/test_enum5.cpp +++ b/tests/json/test_enum5.cpp @@ -1,5 +1,3 @@ -#include "test_enum5.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum5) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::blue}; rfl::get_enumerators().apply([&](auto field) { diff --git a/tests/json/test_enum5.hpp b/tests/json/test_enum5.hpp deleted file mode 100644 index 1d7854eb..00000000 --- a/tests/json/test_enum5.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum5 { -void test(); -} - diff --git a/tests/json/test_enum6.cpp b/tests/json/test_enum6.cpp index 1afb8b0b..a79e4616 100644 --- a/tests/json/test_enum6.cpp +++ b/tests/json/test_enum6.cpp @@ -1,5 +1,3 @@ -#include "test_enum6.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum6) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::red}; rfl::get_underlying_enumerators().apply([&](const auto& field) { diff --git a/tests/json/test_enum6.hpp b/tests/json/test_enum6.hpp deleted file mode 100644 index 5c8023f4..00000000 --- a/tests/json/test_enum6.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum6 { -void test(); -} - diff --git a/tests/json/test_enum7.cpp b/tests/json/test_enum7.cpp index 0d78a754..a483410f 100644 --- a/tests/json/test_enum7.cpp +++ b/tests/json/test_enum7.cpp @@ -1,5 +1,3 @@ -#include "test_enum7.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include #include -#include "write_and_read.hpp" +#include namespace test_enum7 { @@ -19,9 +17,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_enum7) { constexpr auto enumerator_array = rfl::get_enumerator_array(); static_assert(enumerator_array[0].first == "red"); static_assert(enumerator_array[1].first == "green"); @@ -44,7 +40,7 @@ void test() { static_assert(enumerator_array_underlying[3].second == 3); // This is a compile-time test - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(true); } } // namespace test_enum7 diff --git a/tests/json/test_enum7.hpp b/tests/json/test_enum7.hpp deleted file mode 100644 index b8be5df5..00000000 --- a/tests/json/test_enum7.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum7 { -void test(); -} - diff --git a/tests/json/test_error_messages.cpp b/tests/json/test_error_messages.cpp index aa475a1d..3f224c37 100644 --- a/tests/json/test_error_messages.cpp +++ b/tests/json/test_error_messages.cpp @@ -1,5 +1,3 @@ -#include "test_error_messages.hpp" - #include #include #include @@ -7,7 +5,7 @@ #include #include -#include "write_and_read.hpp" +#include namespace test_error_messages { @@ -18,9 +16,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_error_messages) { const std::string faulty_string = R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})"; @@ -31,18 +27,8 @@ void test() { 2) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'. 3) Field named 'children' not found.)"; - if (!result.error()) { - std::cout << "Expected an error, got none." << std::endl << std::endl; - } - - if (result.error().value().what() != expected) { - std::cout << "Expected: " << std::endl - << expected << std::endl - << "Got:" << std::endl - << result.error().value().what() << std::endl - << std::endl; - } + EXPECT_TRUE(result.error() && true); - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(result.error().value().what(), expected); } } // namespace test_error_messages diff --git a/tests/json/test_error_messages.hpp b/tests/json/test_error_messages.hpp deleted file mode 100644 index 726d24a3..00000000 --- a/tests/json/test_error_messages.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_error_messages{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_field_variant.cpp b/tests/json/test_field_variant.cpp index 75543dcb..b8076c52 100644 --- a/tests/json/test_field_variant.cpp +++ b/tests/json/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -29,9 +27,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/json/test_field_variant.hpp b/tests/json/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/json/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_flag_enum1.cpp b/tests/json/test_flag_enum1.cpp index ad35b0bb..c02afb8c 100644 --- a/tests/json/test_flag_enum1.cpp +++ b/tests/json/test_flag_enum1.cpp @@ -1,5 +1,3 @@ -#include "test_flag_enum1.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_flag_enum1) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/json/test_flag_enum1.hpp b/tests/json/test_flag_enum1.hpp deleted file mode 100644 index cafcff27..00000000 --- a/tests/json/test_flag_enum1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum1 { -void test(); -} - diff --git a/tests/json/test_flag_enum2.cpp b/tests/json/test_flag_enum2.cpp index 4b5fc1ab..1aa6ce07 100644 --- a/tests/json/test_flag_enum2.cpp +++ b/tests/json/test_flag_enum2.cpp @@ -1,5 +1,3 @@ -#include "test_flag_enum2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_flag_enum2) { auto mutable_circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/json/test_flag_enum2.hpp b/tests/json/test_flag_enum2.hpp deleted file mode 100644 index 55a52008..00000000 --- a/tests/json/test_flag_enum2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum2 { -void test(); -} - diff --git a/tests/json/test_flag_enum_with_int.cpp b/tests/json/test_flag_enum_with_int.cpp index 63c1e4b6..5c3381f5 100644 --- a/tests/json/test_flag_enum_with_int.cpp +++ b/tests/json/test_flag_enum_with_int.cpp @@ -1,5 +1,3 @@ -#include "test_flag_enum_with_int.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle, R"({"radius":2.0,"color":"16|red|green|blue|8192"})"); diff --git a/tests/json/test_flag_enum_with_int.hpp b/tests/json/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/json/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/json/test_flatten.cpp b/tests/json/test_flatten.cpp index e4437ba1..ee8957e9 100644 --- a/tests/json/test_flatten.cpp +++ b/tests/json/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/json/test_flatten.hpp b/tests/json/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/json/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_flatten_anonymous.cpp b/tests/json/test_flatten_anonymous.cpp index 21c7c809..3b9fa0b2 100644 --- a/tests/json/test_flatten_anonymous.cpp +++ b/tests/json/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/json/test_flatten_anonymous.hpp b/tests/json/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/json/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_forward_list.cpp b/tests/json/test_forward_list.cpp index d46ef96f..173fc15a 100644 --- a/tests/json/test_forward_list.cpp +++ b/tests/json/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/json/test_forward_list.hpp b/tests/json/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/json/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_inheritance.cpp b/tests/json/test_inheritance.cpp index 1852f21b..227c3926 100644 --- a/tests/json/test_inheritance.cpp +++ b/tests/json/test_inheritance.cpp @@ -4,11 +4,11 @@ #include #include -namespace test_inheritance { +#include -void test() { - std::cout << std::source_location::current().function_name() << std::endl; +namespace test_inheritance { +TEST(json, test_inheritance) { struct S { int x; }; @@ -20,7 +20,7 @@ void test() { static_assert(name == "x"); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(true); } } // namespace test_inheritance diff --git a/tests/json/test_inheritance.hpp b/tests/json/test_inheritance.hpp deleted file mode 100644 index 31d51d1a..00000000 --- a/tests/json/test_inheritance.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_inheritance{ -void test(); -} diff --git a/tests/json/test_inheritance2.cpp b/tests/json/test_inheritance2.cpp index 21b8475a..ada6c8d5 100644 --- a/tests/json/test_inheritance2.cpp +++ b/tests/json/test_inheritance2.cpp @@ -5,6 +5,8 @@ #include "rfl/internal/num_fields.hpp" +#include + namespace test_inheritance2 { struct EmptyBase1 {}; @@ -27,9 +29,7 @@ struct EmptyDerived0 : BaseX, EmptyBase1 {}; struct EmptyDerived1 : EmptyBase1, BaseX {}; struct EmptyDerived2 : EmptyBase1, EmptyBase2, BaseX {}; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_inheritance2) { Derived1 derived1; const auto derived1_view = rfl::to_view(derived1); static_assert(derived1_view.size() == 2); @@ -50,7 +50,7 @@ void test() { auto empty_derived2_view = rfl::to_view(empty_derived2); static_assert(empty_derived0_view.size() == 2); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(true); } } // namespace test_inheritance2 diff --git a/tests/json/test_inheritance2.hpp b/tests/json/test_inheritance2.hpp deleted file mode 100644 index c347aac6..00000000 --- a/tests/json/test_inheritance2.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_inheritance2 { -void test(); -} diff --git a/tests/json/test_inside_function.cpp b/tests/json/test_inside_function.cpp index 76a11cee..4166075b 100644 --- a/tests/json/test_inside_function.cpp +++ b/tests/json/test_inside_function.cpp @@ -1,5 +1,3 @@ -#include "test_inside_function.hpp" - #include #include #include @@ -11,9 +9,7 @@ namespace test_inside_function { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_inside_function) { using Age = rfl::Validator, rfl::Maximum<130>>; struct Person { diff --git a/tests/json/test_inside_function.hpp b/tests/json/test_inside_function.hpp deleted file mode 100644 index e7cf82f6..00000000 --- a/tests/json/test_inside_function.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_inside_function { -void test(); -} - diff --git a/tests/json/test_json_schema.cpp b/tests/json/test_json_schema.cpp index f51b48b3..ab3ae224 100644 --- a/tests/json/test_json_schema.cpp +++ b/tests/json/test_json_schema.cpp @@ -7,7 +7,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_json_schema { @@ -55,9 +54,7 @@ struct Person { rfl::Rename<"fieldVariant", FieldVariant> field_variant; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_json_schema) { const auto json_schema = rfl::json::to_schema< rfl::Description<"JSON schema that describes the required " "attributes for the person class.", @@ -66,16 +63,6 @@ void test() { const std::string expected = R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","definitions":{"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"radius":{"type":"number"},"shape":{"type":"string","enum":["Circle"]}},"required":["radius","shape"]},"test_json_schema__Person":{"type":"object","properties":{"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"color":{"type":"string","enum":["red","green","blue"]},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":["circle"]}]},"firstName":{"type":"string"},"lastName":{"type":"string"},"salary":{"type":"number"},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"town":{"type":"string"},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]}},"required":["age","children","color","email","fieldVariant","firstName","lastName","salary","taggedUnion","town","tuple","variant"]},"test_json_schema__Rectangle":{"type":"object","properties":{"height":{"type":"number"},"width":{"type":"number"}},"required":["height","width"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"height":{"type":"number"},"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"}},"required":["height","shape","width"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})"; - if (json_schema != expected) { - std::cout << "Test failed. Expected:" << std::endl - << expected << std::endl - << "Got: " << std::endl - << json_schema << std::endl - << std::endl; - - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(json_schema, expected); } } // namespace test_json_schema diff --git a/tests/json/test_json_schema.hpp b/tests/json/test_json_schema.hpp deleted file mode 100644 index ec30a8ba..00000000 --- a/tests/json/test_json_schema.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_json_schema { -void test(); -} - diff --git a/tests/json/test_list.cpp b/tests/json/test_list.cpp index cffb8f52..dfa9ce9a 100644 --- a/tests/json/test_list.cpp +++ b/tests/json/test_list.cpp @@ -1,5 +1,3 @@ -#include "test_list.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_list) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/json/test_list.hpp b/tests/json/test_list.hpp deleted file mode 100644 index 956d36cc..00000000 --- a/tests/json/test_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_literal.cpp b/tests/json/test_literal.cpp index c695c552..e51818ce 100644 --- a/tests/json/test_literal.cpp +++ b/tests/json/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -21,9 +19,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart, diff --git a/tests/json/test_literal.hpp b/tests/json/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/json/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_literal_map.cpp b/tests/json/test_literal_map.cpp index 5f6afa8f..c7e50fc3 100644 --- a/tests/json/test_literal_map.cpp +++ b/tests/json/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -15,9 +13,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/json/test_literal_map.hpp b/tests/json/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/json/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_literal_unordered_map.cpp b/tests/json/test_literal_unordered_map.cpp index 9dfb6334..31e4d5f8 100644 --- a/tests/json/test_literal_unordered_map.cpp +++ b/tests/json/test_literal_unordered_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_unordered_map.hpp" - #include #include #include @@ -18,9 +16,7 @@ using FieldName = rfl::Literal<"firstName", "lastName">; using Map = std::unordered_map>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_literal_unordered_map) { Map homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), @@ -32,6 +28,7 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + + EXPECT_TRUE(true); } } // namespace test_literal_unordered_map diff --git a/tests/json/test_literal_unordered_map.hpp b/tests/json/test_literal_unordered_map.hpp deleted file mode 100644 index 870a82c3..00000000 --- a/tests/json/test_literal_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_map.cpp b/tests/json/test_map.cpp index 20a227ea..b8f8eb34 100644 --- a/tests/json/test_map.cpp +++ b/tests/json/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_map) { auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); diff --git a/tests/json/test_map.hpp b/tests/json/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/json/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_map_with_key_validation.cpp b/tests/json/test_map_with_key_validation.cpp index 994cfddc..fdffdc76 100644 --- a/tests/json/test_map_with_key_validation.cpp +++ b/tests/json/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); diff --git a/tests/json/test_map_with_key_validation.hpp b/tests/json/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/json/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_meta_fields.cpp b/tests/json/test_meta_fields.cpp index 1d759a89..04000212 100644 --- a/tests/json/test_meta_fields.cpp +++ b/tests/json/test_meta_fields.cpp @@ -1,8 +1,8 @@ -#include "test_meta_fields.hpp" - #include #include +#include + namespace test_meta_fields { struct Person { @@ -13,21 +13,9 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_meta_fields) { const auto fields = rfl::fields(); - /*std::cout << "Fields in " << rfl::type_name_t().str() << ":" - << std::endl; - for (const auto& f : fields) { - std::cout << "name: " << f.name() << ", type: " << f.type() << std::endl; - }*/ - - // The exact content is somewhat unpredictable, we just want to make sure it - // compiles. - if (fields.size() == 5) { - std::cout << "OK" << std::endl << std::endl; - } + EXPECT_EQ(fields.size(), 5); } } // namespace test_meta_fields diff --git a/tests/json/test_meta_fields.hpp b/tests/json/test_meta_fields.hpp deleted file mode 100644 index 903af004..00000000 --- a/tests/json/test_meta_fields.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_meta_fields { -void test(); -} - diff --git a/tests/json/test_monster_example.cpp b/tests/json/test_monster_example.cpp index 8887e249..84a7bd13 100644 --- a/tests/json/test_monster_example.cpp +++ b/tests/json/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -39,9 +37,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/json/test_monster_example.hpp b/tests/json/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/json/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_move_replace.cpp b/tests/json/test_move_replace.cpp index e4e15ba2..6410b646 100644 --- a/tests/json/test_move_replace.cpp +++ b/tests/json/test_move_replace.cpp @@ -1,5 +1,3 @@ -#include "test_move_replace.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Person { rfl::Field<"children", rfl::Box>> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_move_replace) { auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .children = rfl::make_box>()}; diff --git a/tests/json/test_move_replace.hpp b/tests/json/test_move_replace.hpp deleted file mode 100644 index 07704631..00000000 --- a/tests/json/test_move_replace.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_move_replace{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_multimap.cpp b/tests/json/test_multimap.cpp index cb74d75b..42f3469c 100644 --- a/tests/json/test_multimap.cpp +++ b/tests/json/test_multimap.cpp @@ -1,5 +1,3 @@ -#include "test_multimap.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_multimap) { auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); diff --git a/tests/json/test_multimap.hpp b/tests/json/test_multimap.hpp deleted file mode 100644 index 81845572..00000000 --- a/tests/json/test_multimap.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_multimap{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_multiset.cpp b/tests/json/test_multiset.cpp index 923144f0..e410b633 100644 --- a/tests/json/test_multiset.cpp +++ b/tests/json/test_multiset.cpp @@ -1,5 +1,3 @@ -#include "test_multiset.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_multiset) { auto children = std::make_unique>( std::multiset({"Bart", "Lisa", "Maggie"})); diff --git a/tests/json/test_multiset.hpp b/tests/json/test_multiset.hpp deleted file mode 100644 index adfcf186..00000000 --- a/tests/json/test_multiset.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_multiset{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_one_of.cpp b/tests/json/test_one_of.cpp index d554d37d..66333cd0 100644 --- a/tests/json/test_one_of.cpp +++ b/tests/json/test_one_of.cpp @@ -1,5 +1,3 @@ -#include "test_one_of.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { Age age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_one_of) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/json/test_one_of.hpp b/tests/json/test_one_of.hpp deleted file mode 100644 index 47b4e262..00000000 --- a/tests/json/test_one_of.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_one_of{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_optional_fields.cpp b/tests/json/test_optional_fields.cpp index dfedddbf..53895a52 100644 --- a/tests/json/test_optional_fields.cpp +++ b/tests/json/test_optional_fields.cpp @@ -1,5 +1,3 @@ -#include "test_optional_fields.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { rfl::Rename<"children", std::optional>> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, insert_name_here) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/json/test_optional_fields.hpp b/tests/json/test_optional_fields.hpp deleted file mode 100644 index 640cab95..00000000 --- a/tests/json/test_optional_fields.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_optional_fields{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_readme_example.cpp b/tests/json/test_readme_example.cpp index 3eef7acc..e50a8c52 100644 --- a/tests/json/test_readme_example.cpp +++ b/tests/json/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/json/test_readme_example.hpp b/tests/json/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/json/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_ref.cpp b/tests/json/test_ref.cpp index e3d3d23c..873e4264 100644 --- a/tests/json/test_ref.cpp +++ b/tests/json/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -30,9 +28,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/json/test_ref.hpp b/tests/json/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/json/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_replace.cpp b/tests/json/test_replace.cpp index 7c555e8d..f19ff14d 100644 --- a/tests/json/test_replace.cpp +++ b/tests/json/test_replace.cpp @@ -1,5 +1,3 @@ -#include "test_replace.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Person { rfl::Field<"children", rfl::Box>> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace) { auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .children = rfl::make_box>()}; diff --git a/tests/json/test_replace.hpp b/tests/json/test_replace.hpp deleted file mode 100644 index 9d421218..00000000 --- a/tests/json/test_replace.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_replace2.cpp b/tests/json/test_replace2.cpp index 79f0c46c..54214562 100644 --- a/tests/json/test_replace2.cpp +++ b/tests/json/test_replace2.cpp @@ -1,5 +1,3 @@ -#include "test_replace2.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Person { rfl::Box> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace2) { auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .children = rfl::make_box>()}; diff --git a/tests/json/test_replace2.hpp b/tests/json/test_replace2.hpp deleted file mode 100644 index 0dff70b7..00000000 --- a/tests/json/test_replace2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace2 { -void test(); -} - diff --git a/tests/json/test_replace_flatten.cpp b/tests/json/test_replace_flatten.cpp index e57e8b88..59f9e54c 100644 --- a/tests/json/test_replace_flatten.cpp +++ b/tests/json/test_replace_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_replace_flatten.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace_flatten) { auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/json/test_replace_flatten.hpp b/tests/json/test_replace_flatten.hpp deleted file mode 100644 index 5bc5f716..00000000 --- a/tests/json/test_replace_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_replace_flatten2.cpp b/tests/json/test_replace_flatten2.cpp index bb92ebcf..95300fdc 100644 --- a/tests/json/test_replace_flatten2.cpp +++ b/tests/json/test_replace_flatten2.cpp @@ -1,5 +1,3 @@ -#include "test_replace_flatten2.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace_flatten2) { auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/json/test_replace_flatten2.hpp b/tests/json/test_replace_flatten2.hpp deleted file mode 100644 index f31356ce..00000000 --- a/tests/json/test_replace_flatten2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace_flatten2 { -void test(); -} - diff --git a/tests/json/test_replace_with_other_struct.cpp b/tests/json/test_replace_with_other_struct.cpp index eb9e8f78..02526205 100644 --- a/tests/json/test_replace_with_other_struct.cpp +++ b/tests/json/test_replace_with_other_struct.cpp @@ -1,5 +1,3 @@ -#include "test_replace_with_other_struct.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace_with_other_struct) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, diff --git a/tests/json/test_replace_with_other_struct.hpp b/tests/json/test_replace_with_other_struct.hpp deleted file mode 100644 index 1315603a..00000000 --- a/tests/json/test_replace_with_other_struct.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace_with_other_struct{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_replace_with_other_struct2.cpp b/tests/json/test_replace_with_other_struct2.cpp index ce2b5545..71fbae07 100644 --- a/tests/json/test_replace_with_other_struct2.cpp +++ b/tests/json/test_replace_with_other_struct2.cpp @@ -1,5 +1,3 @@ -#include "test_replace_with_other_struct2.hpp" - #include #include #include @@ -24,9 +22,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_replace_with_other_struct2) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, diff --git a/tests/json/test_replace_with_other_struct2.hpp b/tests/json/test_replace_with_other_struct2.hpp deleted file mode 100644 index ab579406..00000000 --- a/tests/json/test_replace_with_other_struct2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_replace_with_other_struct2 { -void test(); -} - diff --git a/tests/json/test_result.cpp b/tests/json/test_result.cpp index 1db7f4c7..fa44361a 100644 --- a/tests/json/test_result.cpp +++ b/tests/json/test_result.cpp @@ -1,5 +1,3 @@ -#include "test_result.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_result) { const rfl::Result bart = Person{.first_name = "Bart"}; const rfl::Result lisa = Person{.first_name = "Lisa"}; diff --git a/tests/json/test_result.hpp b/tests/json/test_result.hpp deleted file mode 100644 index 170943c5..00000000 --- a/tests/json/test_result.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_result{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_save_load.cpp b/tests/json/test_save_load.cpp index 43f36a70..fe2fda48 100644 --- a/tests/json/test_save_load.cpp +++ b/tests/json/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,7 @@ void test() { const auto string1 = rfl::json::write(homer1); const auto string2 = rfl::json::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl + EXPECT_EQ(string1, string2) << "Test failed. Content was not identical." << std::endl << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; } } // namespace test_save_load diff --git a/tests/json/test_save_load.hpp b/tests/json/test_save_load.hpp deleted file mode 100644 index a8829fd1..00000000 --- a/tests/json/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_set.cpp b/tests/json/test_set.cpp index 19600193..6456cb0a 100644 --- a/tests/json/test_set.cpp +++ b/tests/json/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/json/test_set.hpp b/tests/json/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/json/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_shared_ptr.cpp b/tests/json/test_shared_ptr.cpp index 71c44d18..67c9ffaf 100644 --- a/tests/json/test_shared_ptr.cpp +++ b/tests/json/test_shared_ptr.cpp @@ -6,7 +6,6 @@ #include #include -#include "test_unique_ptr.hpp" #include "write_and_read.hpp" namespace test_shared_ptr { @@ -17,9 +16,7 @@ struct Person { std::shared_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_shared_ptr) { auto children = std::make_shared>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/json/test_shared_ptr.hpp b/tests/json/test_shared_ptr.hpp deleted file mode 100644 index ac05afcf..00000000 --- a/tests/json/test_shared_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_shared_ptr { -void test(); -} - diff --git a/tests/json/test_size.cpp b/tests/json/test_size.cpp index f4935f9d..a66163b6 100644 --- a/tests/json/test_size.cpp +++ b/tests/json/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/json/test_size.hpp b/tests/json/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/json/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_std_ref.cpp b/tests/json/test_std_ref.cpp index d1e1d5d8..e2cd1605 100644 --- a/tests/json/test_std_ref.cpp +++ b/tests/json/test_std_ref.cpp @@ -1,5 +1,3 @@ -#include "test_std_ref.hpp" - #include #include #include @@ -7,29 +5,24 @@ #include #include +#include + namespace test_std_ref { struct StdRefStruct { std::reference_wrapper a; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_std_ref) { int i = 10; StdRefStruct struct_ = {.a = std::ref(i)}; const auto json_string = rfl::json::write(struct_); const std::string& expected = R"({"a":10})"; - if (json_string != expected) { - std::cout << "Test failed on write. Expected:" << std::endl + EXPECT_EQ(json_string, expected) << "Test failed on write. Expected:" << std::endl << expected << std::endl << "Got: " << std::endl << json_string << std::endl << std::endl; - return; - } - std::cout << "OK" << std::endl << std::endl; } - } // namespace test_std_ref diff --git a/tests/json/test_std_ref.hpp b/tests/json/test_std_ref.hpp deleted file mode 100644 index b62bb265..00000000 --- a/tests/json/test_std_ref.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_std_ref { -void test(); -} diff --git a/tests/json/test_string_map.cpp b/tests/json/test_string_map.cpp index 41dc61d9..3b4f9cf8 100644 --- a/tests/json/test_string_map.cpp +++ b/tests/json/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -11,9 +9,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/json/test_string_map.hpp b/tests/json/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/json/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_string_unordered_map.cpp b/tests/json/test_string_unordered_map.cpp index 69a72829..bda628b9 100644 --- a/tests/json/test_string_unordered_map.cpp +++ b/tests/json/test_string_unordered_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_unordered_map.hpp" - #include #include #include @@ -11,9 +9,7 @@ #include "write_and_read.hpp" namespace test_string_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_string_unordered_map) { using Map = std::unordered_map>; Map homer; homer.insert( @@ -25,6 +21,6 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(true); } } // namespace test_string_unordered_map diff --git a/tests/json/test_string_unordered_map.hpp b/tests/json/test_string_unordered_map.hpp deleted file mode 100644 index 268057a3..00000000 --- a/tests/json/test_string_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_tagged_union.cpp b/tests/json/test_tagged_union.cpp index 2727e443..b393c09f 100644 --- a/tests/json/test_tagged_union.cpp +++ b/tests/json/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -27,9 +25,7 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r, R"({"shape":"Rectangle","height":10.0,"width":5.0})"); diff --git a/tests/json/test_tagged_union.hpp b/tests/json/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/json/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_tagged_union2.cpp b/tests/json/test_tagged_union2.cpp index 0fa84d9f..5b70e6f8 100644 --- a/tests/json/test_tagged_union2.cpp +++ b/tests/json/test_tagged_union2.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union2.hpp" - #include #include #include @@ -30,9 +28,7 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_tagged_union2) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r, R"({"shape":"rectangle","height":10.0,"width":5.0})"); diff --git a/tests/json/test_tagged_union2.hpp b/tests/json/test_tagged_union2.hpp deleted file mode 100644 index 05bce109..00000000 --- a/tests/json/test_tagged_union2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union2{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_tagged_union3.cpp b/tests/json/test_tagged_union3.cpp index 82b14a04..29c05870 100644 --- a/tests/json/test_tagged_union3.cpp +++ b/tests/json/test_tagged_union3.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union3.hpp" - #include #include #include @@ -32,9 +30,7 @@ struct Empty {}; using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle, Empty>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_tagged_union3) { const Shapes r = Rectangle{ .shape = rfl::Literal<"rectangle", "Rectangle", "rect">::make<"rect">(), .height = 10, diff --git a/tests/json/test_tagged_union3.hpp b/tests/json/test_tagged_union3.hpp deleted file mode 100644 index c40beebf..00000000 --- a/tests/json/test_tagged_union3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union3 { -void test(); -} - diff --git a/tests/json/test_timestamp.cpp b/tests/json/test_timestamp.cpp index 1adfb911..36fec8a4 100644 --- a/tests/json/test_timestamp.cpp +++ b/tests/json/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -20,15 +18,10 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_timestamp) { const auto result = TS::from_string("nonsense"); - if (result) { - std::cout << "Failed: Expected an error, but got none." << std::endl; - return; - } + ASSERT_TRUE(!result && true); const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; diff --git a/tests/json/test_timestamp.hpp b/tests/json/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/json/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_transform.cpp b/tests/json/test_transform.cpp index 9c70c48f..fe41f717 100644 --- a/tests/json/test_transform.cpp +++ b/tests/json/test_transform.cpp @@ -7,7 +7,6 @@ #include #include -#include "test_replace.hpp" #include "write_and_read.hpp" namespace test_transform { @@ -18,9 +17,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_transform) { const auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; diff --git a/tests/json/test_transform.hpp b/tests/json/test_transform.hpp deleted file mode 100644 index c02101da..00000000 --- a/tests/json/test_transform.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_transform { -void test(); -} - diff --git a/tests/json/test_unique_ptr.cpp b/tests/json/test_unique_ptr.cpp index e354862d..131eb69a 100644 --- a/tests/json/test_unique_ptr.cpp +++ b/tests/json/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/json/test_unique_ptr.hpp b/tests/json/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/json/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_unique_ptr2.cpp b/tests/json/test_unique_ptr2.cpp index 4f62b94e..45c23d11 100644 --- a/tests/json/test_unique_ptr2.cpp +++ b/tests/json/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -30,9 +28,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/json/test_unique_ptr2.hpp b/tests/json/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/json/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_unnamed_namespace.cpp b/tests/json/test_unnamed_namespace.cpp index 9347925f..01070922 100644 --- a/tests/json/test_unnamed_namespace.cpp +++ b/tests/json/test_unnamed_namespace.cpp @@ -1,6 +1,3 @@ - -#include "test_unnamed_namespace.hpp" - #include #include #include @@ -28,9 +25,7 @@ struct Person { } // namespace -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unnamed_namespace) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/json/test_unnamed_namespace.hpp b/tests/json/test_unnamed_namespace.hpp deleted file mode 100644 index 8f64abd1..00000000 --- a/tests/json/test_unnamed_namespace.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unnamed_namespace { -void test(); -} - diff --git a/tests/json/test_unordered_map.cpp b/tests/json/test_unordered_map.cpp index 8876dce8..2be519d0 100644 --- a/tests/json/test_unordered_map.cpp +++ b/tests/json/test_unordered_map.cpp @@ -1,5 +1,3 @@ -#include "test_unordered_map.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unordered_map) { auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); @@ -32,6 +28,7 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + + EXPECT_TRUE(true); } } // namespace test_unordered_map diff --git a/tests/json/test_unordered_map.hpp b/tests/json/test_unordered_map.hpp deleted file mode 100644 index 9b059a96..00000000 --- a/tests/json/test_unordered_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_unordered_multimap.cpp b/tests/json/test_unordered_multimap.cpp index b9020abe..44e564aa 100644 --- a/tests/json/test_unordered_multimap.cpp +++ b/tests/json/test_unordered_multimap.cpp @@ -1,5 +1,3 @@ -#include "test_unordered_multimap.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unordered_multimap) { auto children = std::make_unique>(); children->insert(std::make_pair(1, Person{.first_name = "Bart"})); children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); @@ -32,6 +28,6 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(true); } } // namespace test_unordered_multimap diff --git a/tests/json/test_unordered_multimap.hpp b/tests/json/test_unordered_multimap.hpp deleted file mode 100644 index 0f36f705..00000000 --- a/tests/json/test_unordered_multimap.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_multimap{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_unordered_multiset.cpp b/tests/json/test_unordered_multiset.cpp index 8cf957d7..310115ad 100644 --- a/tests/json/test_unordered_multiset.cpp +++ b/tests/json/test_unordered_multiset.cpp @@ -1,5 +1,3 @@ -#include "test_unordered_multiset.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unordered_multiset) { auto children = std::make_unique>( std::unordered_multiset({"Bart", "Lisa", "Maggie"})); @@ -30,6 +26,6 @@ void test() { // this compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(homer2 && true); } } // namespace test_unordered_multiset diff --git a/tests/json/test_unordered_multiset.hpp b/tests/json/test_unordered_multiset.hpp deleted file mode 100644 index 491ff7ba..00000000 --- a/tests/json/test_unordered_multiset.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_multiset{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_unordered_set.cpp b/tests/json/test_unordered_set.cpp index 0bfe06c5..ad229fda 100644 --- a/tests/json/test_unordered_set.cpp +++ b/tests/json/test_unordered_set.cpp @@ -1,5 +1,3 @@ -#include "test_unordered_set.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_unordered_set) { auto children = std::make_unique>( std::unordered_set({"Bart", "Lisa", "Maggie"})); @@ -30,6 +26,6 @@ void test() { // compiles. const auto json_string = rfl::json::write(homer); const auto homer2 = rfl::json::read(json_string); - std::cout << "OK" << std::endl << std::endl; + EXPECT_TRUE(homer2 && true); } } // namespace test_unordered_set diff --git a/tests/json/test_unordered_set.hpp b/tests/json/test_unordered_set.hpp deleted file mode 100644 index be2b257f..00000000 --- a/tests/json/test_unordered_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unordered_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_variant.cpp b/tests/json/test_variant.cpp index 18d6a056..3a574d58 100644 --- a/tests/json/test_variant.cpp +++ b/tests/json/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -27,9 +25,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r, R"({"height":10.0,"width":5.0})"); diff --git a/tests/json/test_variant.hpp b/tests/json/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/json/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/json/test_view.cpp b/tests/json/test_view.cpp index f4b4b042..3e732951 100644 --- a/tests/json/test_view.cpp +++ b/tests/json/test_view.cpp @@ -7,8 +7,6 @@ #include #include -#include "rfl/internal/num_fields.hpp" -#include "test_replace.hpp" #include "write_and_read.hpp" namespace test_view { @@ -19,9 +17,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_view) { auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; const auto view = rfl::to_view(lisa); diff --git a/tests/json/test_view.hpp b/tests/json/test_view.hpp deleted file mode 100644 index 89812d89..00000000 --- a/tests/json/test_view.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_view { -void test(); -} - diff --git a/tests/json/test_wstring.cpp b/tests/json/test_wstring.cpp index c6db773a..f00e3e5b 100644 --- a/tests/json/test_wstring.cpp +++ b/tests/json/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -11,9 +9,7 @@ #include "write_and_read.hpp" namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(json, test_wstring) { std::map homer; homer.insert(std::make_pair("firstName", L"Homer")); diff --git a/tests/json/test_wstring.hpp b/tests/json/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/json/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp deleted file mode 100644 index fd415c17..00000000 --- a/tests/json/tests.cpp +++ /dev/null @@ -1,196 +0,0 @@ -#include "test_all_of.hpp" -#include "test_alphanumeric_map.hpp" -#include "test_alphanumeric_unordered_map.hpp" -#include "test_and_then.hpp" -#include "test_anonymous_fields.hpp" -#include "test_any_of.hpp" -#include "test_apply.hpp" -#include "test_array.hpp" -#include "test_as.hpp" -#include "test_as2.hpp" -#include "test_as_flatten.hpp" -#include "test_box.hpp" -#include "test_box2.hpp" -#include "test_c_array_class1.hpp" -#include "test_c_array_class2.hpp" -#include "test_c_array_class3.hpp" -#include "test_c_array_class4.hpp" -#include "test_c_array_class5.hpp" -#include "test_const_fields.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class2.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_custom_constructor.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_email.hpp" -#include "test_empty_object.hpp" -#include "test_enum1.hpp" -#include "test_enum2.hpp" -#include "test_enum3.hpp" -#include "test_enum4.hpp" -#include "test_enum5.hpp" -#include "test_enum6.hpp" -#include "test_enum7.hpp" -#include "test_error_messages.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum1.hpp" -#include "test_flag_enum2.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_inside_function.hpp" -#include "test_json_schema.hpp" -#include "test_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_literal_unordered_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_meta_fields.hpp" -#include "test_monster_example.hpp" -#include "test_multimap.hpp" -#include "test_multiset.hpp" -#include "test_one_of.hpp" -#include "test_optional_fields.hpp" -#include "test_readme_example.hpp" -#include "test_ref.hpp" -#include "test_replace.hpp" -#include "test_replace2.hpp" -#include "test_replace_flatten.hpp" -#include "test_replace_flatten2.hpp" -#include "test_replace_with_other_struct.hpp" -#include "test_replace_with_other_struct2.hpp" -#include "test_result.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_shared_ptr.hpp" -#include "test_size.hpp" -#include "test_std_ref.hpp" -#include "test_string_map.hpp" -#include "test_string_unordered_map.hpp" -#include "test_tagged_union.hpp" -#include "test_tagged_union2.hpp" -#include "test_tagged_union3.hpp" -#include "test_timestamp.hpp" -#include "test_transform.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_unnamed_namespace.hpp" -#include "test_unordered_map.hpp" -#include "test_unordered_multimap.hpp" -#include "test_unordered_multiset.hpp" -#include "test_unordered_set.hpp" -#include "test_variant.hpp" -#include "test_view.hpp" -#include "test_wstring.hpp" -#include "test_inheritance.hpp" -#include "test_inheritance2.hpp" - -int main() { - test_readme_example::test(); - test_default_values::test(); - test_optional_fields::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_shared_ptr::test(); - test_literal::test(); - test_variant::test(); - test_tagged_union::test(); - test_tagged_union2::test(); - test_tagged_union3::test(); - test_field_variant::test(); - test_ref::test(); - test_box::test(); - test_box2::test(); - test_array::test(); - test_timestamp::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_deque::test(); - test_forward_list::test(); - test_list::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_unordered_map::test(); - test_set::test(); - test_unordered_set::test(); - test_multimap::test(); - test_unordered_multimap::test(); - test_multiset::test(); - test_unordered_multiset::test(); - test_std_ref::test(); - test_string_map::test(); - test_string_unordered_map::test(); - test_alphanumeric_map::test(); - test_alphanumeric_unordered_map::test(); - test_literal_map::test(); - test_literal_unordered_map::test(); - test_error_messages::test(); - test_result::test(); - test_anonymous_fields::test(); - test_monster_example::test(); - test_unnamed_namespace::test(); - test_inside_function::test(); - test_const_fields::test(); - test_empty_object::test(); - - test_enum1::test(); - test_enum2::test(); - test_enum3::test(); - test_enum4::test(); - test_enum5::test(); - test_enum6::test(); - test_enum7::test(); - test_flag_enum1::test(); - test_flag_enum2::test(); - test_flag_enum_with_int::test(); - - test_custom_class1::test(); - test_custom_class2::test(); - test_custom_class3::test(); - test_custom_class4::test(); - - test_c_array_class1::test(); - test_c_array_class2::test(); - test_c_array_class3::test(); - test_c_array_class4::test(); - test_c_array_class5::test(); - - test_replace::test(); - test_replace2::test(); - test_replace_flatten::test(); - test_replace_flatten2::test(); - test_replace_with_other_struct::test(); - test_replace_with_other_struct2::test(); - test_as::test(); - test_as2::test(); - test_as_flatten::test(); - - test_view::test(); - test_apply::test(); - test_transform::test(); - test_and_then::test(); - - test_custom_constructor::test(); - - test_all_of::test(); - test_any_of::test(); - test_one_of::test(); - test_email::test(); - test_size::test(); - - test_save_load::test(); - - test_meta_fields::test(); - - test_wstring::test(); - test_json_schema::test(); - - test_inheritance::test(); - test_inheritance2::test(); - - return 0; -} diff --git a/tests/json/write_and_read.hpp b/tests/json/write_and_read.hpp index 7aae95f2..5f2c9e79 100644 --- a/tests/json/write_and_read.hpp +++ b/tests/json/write_and_read.hpp @@ -1,37 +1,31 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct, const std::string& _expected) { - const auto json_string1 = rfl::json::write(_struct); - if (json_string1 != _expected) { - std::cout << "Test failed on write. Expected:" << std::endl - << _expected << std::endl - << "Got: " << std::endl - << json_string1 << std::endl - << std::endl; - return; - } - const auto res = rfl::json::read(json_string1); - if (!res) { - std::cout << "Test failed on read. Error: " - << res.error().value().what() << std::endl; - return; - } - const auto json_string2 = rfl::json::write(res.value()); - if (json_string2 != _expected) { - std::cout << "Test failed on read. Expected:" << std::endl - << _expected << std::endl - << "Got: " << std::endl - << json_string2 << std::endl - << std::endl; - return; - } - std::cout << "OK" << std::endl << std::endl; + const auto json_string1 = rfl::json::write(_struct); + EXPECT_EQ(json_string1, _expected) + << "Test failed on write. Expected:" << std::endl + << _expected << std::endl + << "Got: " << std::endl + << json_string1 << std::endl + << std::endl; + const auto res = rfl::json::read(json_string1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto json_string2 = rfl::json::write(res.value()); + EXPECT_EQ(json_string2, _expected) + << "Test failed on read. Expected:" << std::endl + << _expected << std::endl + << "Got: " << std::endl + << json_string2 << std::endl + << std::endl; } #endif diff --git a/tests/msgpack/CMakeLists.txt b/tests/msgpack/CMakeLists.txt index 222ea521..b49c98e1 100644 --- a/tests/msgpack/CMakeLists.txt +++ b/tests/msgpack/CMakeLists.txt @@ -1,8 +1,18 @@ project(reflect-cpp-msgpack-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-msgpack-tests ${SOURCES}) +add_executable( + reflect-cpp-msgpack-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) target_include_directories(reflect-cpp-msgpack-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") -target_link_libraries(reflect-cpp-msgpack-tests PRIVATE reflectcpp) + +target_link_libraries( + reflect-cpp-msgpack-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/msgpack/test_array.cpp b/tests/msgpack/test_array.cpp index 75f670d2..a6943a79 100644 --- a/tests/msgpack/test_array.cpp +++ b/tests/msgpack/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/msgpack/test_array.hpp b/tests/msgpack/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/msgpack/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_box.cpp b/tests/msgpack/test_box.cpp index fdb3516a..9685ffd1 100644 --- a/tests/msgpack/test_box.cpp +++ b/tests/msgpack/test_box.cpp @@ -1,5 +1,3 @@ -#include "test_box.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/msgpack/test_box.hpp b/tests/msgpack/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/msgpack/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_custom_class1.cpp b/tests/msgpack/test_custom_class1.cpp index fe8d0ea0..b61d122c 100644 --- a/tests/msgpack/test_custom_class1.cpp +++ b/tests/msgpack/test_custom_class1.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class1.hpp" - #include #include #include @@ -31,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/msgpack/test_custom_class1.hpp b/tests/msgpack/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/msgpack/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_custom_class3.cpp b/tests/msgpack/test_custom_class3.cpp index 598364b1..7ef3a094 100644 --- a/tests/msgpack/test_custom_class3.cpp +++ b/tests/msgpack/test_custom_class3.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class3.hpp" - #include #include #include @@ -56,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/msgpack/test_custom_class3.hpp b/tests/msgpack/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/msgpack/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_custom_class4.cpp b/tests/msgpack/test_custom_class4.cpp index 2b3b3bac..46906e0f 100644 --- a/tests/msgpack/test_custom_class4.cpp +++ b/tests/msgpack/test_custom_class4.cpp @@ -1,5 +1,3 @@ -#include "test_custom_class4.hpp" - #include #include #include @@ -57,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/msgpack/test_custom_class4.hpp b/tests/msgpack/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/msgpack/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_default_values.cpp b/tests/msgpack/test_default_values.cpp index f6af2352..078e53a0 100644 --- a/tests/msgpack/test_default_values.cpp +++ b/tests/msgpack/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/msgpack/test_default_values.hpp b/tests/msgpack/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/msgpack/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_deque.cpp b/tests/msgpack/test_deque.cpp index 6dfa5803..d21c3e5f 100644 --- a/tests/msgpack/test_deque.cpp +++ b/tests/msgpack/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/msgpack/test_deque.hpp b/tests/msgpack/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/msgpack/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_enum.cpp b/tests/msgpack/test_enum.cpp index d4d56a8b..c5482dfa 100644 --- a/tests/msgpack/test_enum.cpp +++ b/tests/msgpack/test_enum.cpp @@ -1,5 +1,3 @@ -#include "test_enum.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/msgpack/test_enum.hpp b/tests/msgpack/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/msgpack/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/msgpack/test_field_variant.cpp b/tests/msgpack/test_field_variant.cpp index 8b17665f..caf7295c 100644 --- a/tests/msgpack/test_field_variant.cpp +++ b/tests/msgpack/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -28,9 +26,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/msgpack/test_field_variant.hpp b/tests/msgpack/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/msgpack/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_flag_enum.cpp b/tests/msgpack/test_flag_enum.cpp index 1d3f76af..775a10c7 100644 --- a/tests/msgpack/test_flag_enum.cpp +++ b/tests/msgpack/test_flag_enum.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/msgpack/test_flag_enum.hpp b/tests/msgpack/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/msgpack/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/msgpack/test_flag_enum_with_int.cpp b/tests/msgpack/test_flag_enum_with_int.cpp index 4c7deefe..5ae88fa9 100644 --- a/tests/msgpack/test_flag_enum_with_int.cpp +++ b/tests/msgpack/test_flag_enum_with_int.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/msgpack/test_flag_enum_with_int.hpp b/tests/msgpack/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/msgpack/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/msgpack/test_flatten.cpp b/tests/msgpack/test_flatten.cpp index c7f1d9be..972a67e4 100644 --- a/tests/msgpack/test_flatten.cpp +++ b/tests/msgpack/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/msgpack/test_flatten.hpp b/tests/msgpack/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/msgpack/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_flatten_anonymous.cpp b/tests/msgpack/test_flatten_anonymous.cpp index 05e61464..e4639fd6 100644 --- a/tests/msgpack/test_flatten_anonymous.cpp +++ b/tests/msgpack/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/msgpack/test_flatten_anonymous.hpp b/tests/msgpack/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/msgpack/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_forward_list.cpp b/tests/msgpack/test_forward_list.cpp index dd8d9e56..0c71740f 100644 --- a/tests/msgpack/test_forward_list.cpp +++ b/tests/msgpack/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/msgpack/test_forward_list.hpp b/tests/msgpack/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/msgpack/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_literal.cpp b/tests/msgpack/test_literal.cpp index 00578178..1ad7059f 100644 --- a/tests/msgpack/test_literal.cpp +++ b/tests/msgpack/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/msgpack/test_literal.hpp b/tests/msgpack/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/msgpack/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_literal_map.cpp b/tests/msgpack/test_literal_map.cpp index 9bbebafb..fc0d6dd2 100644 --- a/tests/msgpack/test_literal_map.cpp +++ b/tests/msgpack/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -14,9 +12,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/msgpack/test_literal_map.hpp b/tests/msgpack/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/msgpack/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_map.cpp b/tests/msgpack/test_map.cpp index e37ce564..e64c3e62 100644 --- a/tests/msgpack/test_map.cpp +++ b/tests/msgpack/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/msgpack/test_map.hpp b/tests/msgpack/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/msgpack/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_map_with_key_validation.cpp b/tests/msgpack/test_map_with_key_validation.cpp index e758219d..04a30bdd 100644 --- a/tests/msgpack/test_map_with_key_validation.cpp +++ b/tests/msgpack/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/msgpack/test_map_with_key_validation.hpp b/tests/msgpack/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/msgpack/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_monster_example.cpp b/tests/msgpack/test_monster_example.cpp index 052d8390..5b424146 100644 --- a/tests/msgpack/test_monster_example.cpp +++ b/tests/msgpack/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -38,9 +36,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/msgpack/test_monster_example.hpp b/tests/msgpack/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/msgpack/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_readme_example.cpp b/tests/msgpack/test_readme_example.cpp index 4e6f8ee0..d48b38ed 100644 --- a/tests/msgpack/test_readme_example.cpp +++ b/tests/msgpack/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/msgpack/test_readme_example.hpp b/tests/msgpack/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/msgpack/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_readme_example2.cpp b/tests/msgpack/test_readme_example2.cpp index a2d212a4..a490f33c 100644 --- a/tests/msgpack/test_readme_example2.cpp +++ b/tests/msgpack/test_readme_example2.cpp @@ -4,7 +4,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_readme_example2 { @@ -15,9 +14,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_readme_example2) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/msgpack/test_readme_example2.hpp b/tests/msgpack/test_readme_example2.hpp deleted file mode 100644 index 5c6b011c..00000000 --- a/tests/msgpack/test_readme_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example2 { -void test(); -} - diff --git a/tests/msgpack/test_ref.cpp b/tests/msgpack/test_ref.cpp index 32a32e6c..a2121d6e 100644 --- a/tests/msgpack/test_ref.cpp +++ b/tests/msgpack/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/msgpack/test_ref.hpp b/tests/msgpack/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/msgpack/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_save_load.cpp b/tests/msgpack/test_save_load.cpp index 3e8c4703..d6d64e39 100644 --- a/tests/msgpack/test_save_load.cpp +++ b/tests/msgpack/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::msgpack::write(homer1); const auto string2 = rfl::msgpack::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/msgpack/test_save_load.hpp b/tests/msgpack/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/msgpack/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/msgpack/test_set.cpp b/tests/msgpack/test_set.cpp index 9f2516ce..2dbd2b37 100644 --- a/tests/msgpack/test_set.cpp +++ b/tests/msgpack/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/msgpack/test_set.hpp b/tests/msgpack/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/msgpack/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_size.cpp b/tests/msgpack/test_size.cpp index c37b9bd8..38301fe0 100644 --- a/tests/msgpack/test_size.cpp +++ b/tests/msgpack/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/msgpack/test_size.hpp b/tests/msgpack/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/msgpack/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_string_map.cpp b/tests/msgpack/test_string_map.cpp index cb4fb903..ed8af4d5 100644 --- a/tests/msgpack/test_string_map.cpp +++ b/tests/msgpack/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -10,9 +8,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/msgpack/test_string_map.hpp b/tests/msgpack/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/msgpack/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_tagged_union.cpp b/tests/msgpack/test_tagged_union.cpp index a9dd1abc..889f89a4 100644 --- a/tests/msgpack/test_tagged_union.cpp +++ b/tests/msgpack/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -26,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/msgpack/test_tagged_union.hpp b/tests/msgpack/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/msgpack/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_timestamp.cpp b/tests/msgpack/test_timestamp.cpp index 8af261d1..6ded3cfe 100644 --- a/tests/msgpack/test_timestamp.cpp +++ b/tests/msgpack/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/msgpack/test_timestamp.hpp b/tests/msgpack/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/msgpack/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_unique_ptr.cpp b/tests/msgpack/test_unique_ptr.cpp index b1833533..73732e55 100644 --- a/tests/msgpack/test_unique_ptr.cpp +++ b/tests/msgpack/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/msgpack/test_unique_ptr.hpp b/tests/msgpack/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/msgpack/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_unique_ptr2.cpp b/tests/msgpack/test_unique_ptr2.cpp index e3007267..35d374c6 100644 --- a/tests/msgpack/test_unique_ptr2.cpp +++ b/tests/msgpack/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/msgpack/test_unique_ptr2.hpp b/tests/msgpack/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/msgpack/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_variant.cpp b/tests/msgpack/test_variant.cpp index cc3d6cbe..fc038fca 100644 --- a/tests/msgpack/test_variant.cpp +++ b/tests/msgpack/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -26,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(msgpack, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/msgpack/test_variant.hpp b/tests/msgpack/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/msgpack/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/msgpack/test_wstring.cpp b/tests/msgpack/test_wstring.cpp index ba6202c0..3b2bb994 100644 --- a/tests/msgpack/test_wstring.cpp +++ b/tests/msgpack/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(msgpack, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/msgpack/test_wstring.hpp b/tests/msgpack/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/msgpack/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/msgpack/tests.cpp b/tests/msgpack/tests.cpp deleted file mode 100644 index 8a9fe0fb..00000000 --- a/tests/msgpack/tests.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "test_array.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_readme_example2.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_readme_example2::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/msgpack/write_and_read.hpp b/tests/msgpack/write_and_read.hpp index 9843c18f..bd1918d9 100644 --- a/tests/msgpack/write_and_read.hpp +++ b/tests/msgpack/write_and_read.hpp @@ -1,39 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto bytes1 = rfl::msgpack::write(_struct); - - const auto res = rfl::msgpack::read(bytes1); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << std::endl; - return; - } - - const auto bytes2 = rfl::msgpack::write(res.value()); - - if (bytes1.size() != bytes2.size()) { - std::cout << "Test failed on write. Number of bytes was different." - << std::endl - << std::endl; - return; - } - - if (bytes1 != bytes2) { - std::cout << "Test failed on write. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::msgpack::write(_struct); + const auto res = rfl::msgpack::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::msgpack::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/toml/CMakeLists.txt b/tests/toml/CMakeLists.txt index c80c52fa..31763819 100644 --- a/tests/toml/CMakeLists.txt +++ b/tests/toml/CMakeLists.txt @@ -1,9 +1,18 @@ project(reflect-cpp-toml-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-toml-tests ${SOURCES}) +add_executable( + reflect-cpp-toml-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) target_include_directories(reflect-cpp-toml-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") -target_link_libraries(reflect-cpp-toml-tests PRIVATE reflectcpp) +target_link_libraries( + reflect-cpp-toml-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/toml/test_array.cpp b/tests/toml/test_array.cpp index f3f10206..421f2004 100644 --- a/tests/toml/test_array.cpp +++ b/tests/toml/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include // Make sure things still compile when -// rfl.hpp is included after rfl/yaml.hpp. +// rfl.hpp is included after rfl/cbor.hpp. #include #include "write_and_read.hpp" @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/toml/test_array.hpp b/tests/toml/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/toml/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_box.cpp b/tests/toml/test_box.cpp index 04d88641..b9a57c67 100644 --- a/tests/toml/test_box.cpp +++ b/tests/toml/test_box.cpp @@ -1,9 +1,6 @@ -#include "test_box.hpp" - #include #include #include -#include #include #include #include @@ -30,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/toml/test_box.hpp b/tests/toml/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/toml/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_custom_class1.cpp b/tests/toml/test_custom_class1.cpp index 35f4a1da..1a020c27 100644 --- a/tests/toml/test_custom_class1.cpp +++ b/tests/toml/test_custom_class1.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class1.hpp" - #include #include #include -#include #include #include #include @@ -32,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/toml/test_custom_class1.hpp b/tests/toml/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/toml/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_custom_class3.cpp b/tests/toml/test_custom_class3.cpp index 2ea105e4..c5f70a25 100644 --- a/tests/toml/test_custom_class3.cpp +++ b/tests/toml/test_custom_class3.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class3.hpp" - #include #include #include -#include #include #include #include @@ -57,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/toml/test_custom_class3.hpp b/tests/toml/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/toml/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_custom_class4.cpp b/tests/toml/test_custom_class4.cpp index e2d79a94..fc3c009e 100644 --- a/tests/toml/test_custom_class4.cpp +++ b/tests/toml/test_custom_class4.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class4.hpp" - #include #include #include -#include #include #include #include @@ -58,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/toml/test_custom_class4.hpp b/tests/toml/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/toml/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_custom_constructor.cpp b/tests/toml/test_custom_constructor.cpp deleted file mode 100644 index 03123d2a..00000000 --- a/tests/toml/test_custom_constructor.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "test_custom_constructor.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_constructor { - -struct Person { - static rfl::Result from_toml_obj( - typename rfl::toml::Reader::InputVarType _obj) { - /// This only exists for the purpose of the test. - const auto change_first_name = [](auto&& _person) { - return rfl::replace(std::move(_person), - rfl::Field<"firstName", std::string>("Bart")); - }; - const auto from_nt = [](auto&& _nt) { - return rfl::from_named_tuple(std::move(_nt)); - }; - return rfl::toml::read>(_obj) - .transform(from_nt) - .transform(change_first_name); - } - - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto res = rfl::toml::read( - R"(firstName = 'Homer' -lastName = 'Simpson' -birthday = '1987-04-19')"); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } - - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } -} -} // namespace test_custom_constructor diff --git a/tests/toml/test_custom_constructor.hpp b/tests/toml/test_custom_constructor.hpp deleted file mode 100644 index d3b5b950..00000000 --- a/tests/toml/test_custom_constructor.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_constructor { -void test(); -} - diff --git a/tests/toml/test_default_values.cpp b/tests/toml/test_default_values.cpp index f6af2352..e5deb0af 100644 --- a/tests/toml/test_default_values.cpp +++ b/tests/toml/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/toml/test_default_values.hpp b/tests/toml/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/toml/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_deque.cpp b/tests/toml/test_deque.cpp index 6dfa5803..d23c1e97 100644 --- a/tests/toml/test_deque.cpp +++ b/tests/toml/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/toml/test_deque.hpp b/tests/toml/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/toml/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_enum.cpp b/tests/toml/test_enum.cpp index d4d56a8b..e13df83f 100644 --- a/tests/toml/test_enum.cpp +++ b/tests/toml/test_enum.cpp @@ -1,5 +1,3 @@ -#include "test_enum.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/toml/test_enum.hpp b/tests/toml/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/toml/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/toml/test_field_variant.cpp b/tests/toml/test_field_variant.cpp index 8b17665f..e96360bf 100644 --- a/tests/toml/test_field_variant.cpp +++ b/tests/toml/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -28,9 +26,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/toml/test_field_variant.hpp b/tests/toml/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/toml/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_flag_enum.cpp b/tests/toml/test_flag_enum.cpp index 1d3f76af..c0d5e8d2 100644 --- a/tests/toml/test_flag_enum.cpp +++ b/tests/toml/test_flag_enum.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/toml/test_flag_enum.hpp b/tests/toml/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/toml/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/toml/test_flag_enum_with_int.cpp b/tests/toml/test_flag_enum_with_int.cpp index 4c7deefe..8a4fad20 100644 --- a/tests/toml/test_flag_enum_with_int.cpp +++ b/tests/toml/test_flag_enum_with_int.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/toml/test_flag_enum_with_int.hpp b/tests/toml/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/toml/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/toml/test_flatten.cpp b/tests/toml/test_flatten.cpp index c7f1d9be..29a2c1d5 100644 --- a/tests/toml/test_flatten.cpp +++ b/tests/toml/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/toml/test_flatten.hpp b/tests/toml/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/toml/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_flatten_anonymous.cpp b/tests/toml/test_flatten_anonymous.cpp index 05e61464..4b698c78 100644 --- a/tests/toml/test_flatten_anonymous.cpp +++ b/tests/toml/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/toml/test_flatten_anonymous.hpp b/tests/toml/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/toml/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_forward_list.cpp b/tests/toml/test_forward_list.cpp index dd8d9e56..da27ae84 100644 --- a/tests/toml/test_forward_list.cpp +++ b/tests/toml/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/toml/test_forward_list.hpp b/tests/toml/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/toml/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_literal.cpp b/tests/toml/test_literal.cpp index 00578178..6d447c36 100644 --- a/tests/toml/test_literal.cpp +++ b/tests/toml/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/toml/test_literal.hpp b/tests/toml/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/toml/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_literal_map.cpp b/tests/toml/test_literal_map.cpp index 9bbebafb..b07902f3 100644 --- a/tests/toml/test_literal_map.cpp +++ b/tests/toml/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -14,9 +12,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/toml/test_literal_map.hpp b/tests/toml/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/toml/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_map.cpp b/tests/toml/test_map.cpp index e37ce564..774ccc61 100644 --- a/tests/toml/test_map.cpp +++ b/tests/toml/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/toml/test_map.hpp b/tests/toml/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/toml/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_map_with_key_validation.cpp b/tests/toml/test_map_with_key_validation.cpp index e758219d..0c27f830 100644 --- a/tests/toml/test_map_with_key_validation.cpp +++ b/tests/toml/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/toml/test_map_with_key_validation.hpp b/tests/toml/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/toml/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_monster_example.cpp b/tests/toml/test_monster_example.cpp index 052d8390..91446f7f 100644 --- a/tests/toml/test_monster_example.cpp +++ b/tests/toml/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -38,9 +36,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/toml/test_monster_example.hpp b/tests/toml/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/toml/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_readme_example.cpp b/tests/toml/test_readme_example.cpp index 4e6f8ee0..970b8411 100644 --- a/tests/toml/test_readme_example.cpp +++ b/tests/toml/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/toml/test_readme_example.hpp b/tests/toml/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/toml/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_readme_example2.cpp b/tests/toml/test_readme_example2.cpp index a2d212a4..7ad06cad 100644 --- a/tests/toml/test_readme_example2.cpp +++ b/tests/toml/test_readme_example2.cpp @@ -4,7 +4,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_readme_example2 { @@ -15,9 +14,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_readme_example2) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/toml/test_readme_example2.hpp b/tests/toml/test_readme_example2.hpp deleted file mode 100644 index 5c6b011c..00000000 --- a/tests/toml/test_readme_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example2 { -void test(); -} - diff --git a/tests/toml/test_ref.cpp b/tests/toml/test_ref.cpp index 32a32e6c..ebb254d5 100644 --- a/tests/toml/test_ref.cpp +++ b/tests/toml/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/toml/test_ref.hpp b/tests/toml/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/toml/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_save_load.cpp b/tests/toml/test_save_load.cpp index fb14c76f..71faf284 100644 --- a/tests/toml/test_save_load.cpp +++ b/tests/toml/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::toml::write(homer1); const auto string2 = rfl::toml::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/toml/test_save_load.hpp b/tests/toml/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/toml/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/toml/test_set.cpp b/tests/toml/test_set.cpp index 9f2516ce..6a2f6ba3 100644 --- a/tests/toml/test_set.cpp +++ b/tests/toml/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/toml/test_set.hpp b/tests/toml/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/toml/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_size.cpp b/tests/toml/test_size.cpp index c37b9bd8..19daf958 100644 --- a/tests/toml/test_size.cpp +++ b/tests/toml/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/toml/test_size.hpp b/tests/toml/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/toml/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_string_map.cpp b/tests/toml/test_string_map.cpp index cb4fb903..cffd9293 100644 --- a/tests/toml/test_string_map.cpp +++ b/tests/toml/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -10,9 +8,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/toml/test_string_map.hpp b/tests/toml/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/toml/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_tagged_union.cpp b/tests/toml/test_tagged_union.cpp index a9dd1abc..944aeaae 100644 --- a/tests/toml/test_tagged_union.cpp +++ b/tests/toml/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -26,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/toml/test_tagged_union.hpp b/tests/toml/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/toml/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_timestamp.cpp b/tests/toml/test_timestamp.cpp index 8af261d1..0eb12367 100644 --- a/tests/toml/test_timestamp.cpp +++ b/tests/toml/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/toml/test_timestamp.hpp b/tests/toml/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/toml/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_unique_ptr.cpp b/tests/toml/test_unique_ptr.cpp index b1833533..ae2990ed 100644 --- a/tests/toml/test_unique_ptr.cpp +++ b/tests/toml/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/toml/test_unique_ptr.hpp b/tests/toml/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/toml/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_unique_ptr2.cpp b/tests/toml/test_unique_ptr2.cpp index e3007267..ac32675e 100644 --- a/tests/toml/test_unique_ptr2.cpp +++ b/tests/toml/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/toml/test_unique_ptr2.hpp b/tests/toml/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/toml/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_variant.cpp b/tests/toml/test_variant.cpp index cc3d6cbe..087880ae 100644 --- a/tests/toml/test_variant.cpp +++ b/tests/toml/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -26,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(toml, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/toml/test_variant.hpp b/tests/toml/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/toml/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/toml/test_wstring.cpp b/tests/toml/test_wstring.cpp index ba6202c0..8c0c4f9a 100644 --- a/tests/toml/test_wstring.cpp +++ b/tests/toml/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(toml, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/toml/test_wstring.hpp b/tests/toml/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/toml/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/toml/tests.cpp b/tests/toml/tests.cpp deleted file mode 100644 index 7a38771d..00000000 --- a/tests/toml/tests.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "test_array.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_custom_constructor.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_readme_example2.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_readme_example2::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_custom_constructor::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/toml/write_and_read.hpp b/tests/toml/write_and_read.hpp index 8b0d7cac..4c45b5d5 100644 --- a/tests/toml/write_and_read.hpp +++ b/tests/toml/write_and_read.hpp @@ -1,32 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto toml_string1 = rfl::toml::write(_struct); - const auto res = rfl::toml::read(toml_string1); - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << "Original string: " << toml_string1 << std::endl - << std::endl; - return; - } - const auto toml_string2 = rfl::toml::write(res.value()); - if (toml_string2 != toml_string1) { - std::cout << "Test failed on read. Expected:" << std::endl - << toml_string1 << std::endl - << "Got: " << std::endl - << toml_string2 << std::endl - << std::endl; - return; - } - // std::cout << toml_string1 << std::endl; - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::toml::write(_struct); + const auto res = rfl::toml::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::toml::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/xml/CMakeLists.txt b/tests/xml/CMakeLists.txt index c22bc116..fca21858 100644 --- a/tests/xml/CMakeLists.txt +++ b/tests/xml/CMakeLists.txt @@ -1,8 +1,18 @@ project(reflect-cpp-xml-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-xml-tests ${SOURCES}) +add_executable( + reflect-cpp-xml-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) -target_link_libraries(reflect-cpp-xml-tests PRIVATE reflectcpp) +target_include_directories(reflect-cpp-xml-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") +target_link_libraries( + reflect-cpp-xml-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/xml/get_pugixml.sh b/tests/xml/get_pugixml.sh deleted file mode 100755 index 3aa890cd..00000000 --- a/tests/xml/get_pugixml.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -wget https://github.com/zeux/pugixml/releases/download/v1.14/pugixml-1.14.tar.gz -tar -xzvf pugixml-1.14.tar.gz diff --git a/tests/xml/test_array.cpp b/tests/xml/test_array.cpp index bcc21feb..60c0a58b 100644 --- a/tests/xml/test_array.cpp +++ b/tests/xml/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include // Make sure things still compile when -// rfl.hpp is included after rfl/xml.hpp. +// rfl.hpp is included after rfl/cbor.hpp. #include #include "write_and_read.hpp" @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/xml/test_array.hpp b/tests/xml/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/xml/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_attributes.cpp b/tests/xml/test_attributes.cpp deleted file mode 100644 index 9e5e5931..00000000 --- a/tests/xml/test_attributes.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "test_attributes.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_attributes { - -using Age = rfl::Validator, rfl::Maximum<130>>; - -struct Person { - rfl::Rename<"firstName", rfl::Attribute> first_name; - rfl::Rename<"lastName", rfl::Attribute> last_name = "Simpson"; - rfl::Attribute town = "Springfield"; - rfl::Attribute> birthday; - rfl::Attribute age; - rfl::Attribute email; - std::vector child; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com"}; - - const auto lisa = Person{.first_name = "Lisa", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer = Person{.first_name = "Homer", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .child = std::vector({bart, lisa, maggie})}; - - write_and_read(homer); -} -} // namespace test_attributes diff --git a/tests/xml/test_attributes.hpp b/tests/xml/test_attributes.hpp deleted file mode 100644 index e77be6af..00000000 --- a/tests/xml/test_attributes.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_attributes { -void test(); -} - diff --git a/tests/xml/test_box.cpp b/tests/xml/test_box.cpp index 04d88641..582c9004 100644 --- a/tests/xml/test_box.cpp +++ b/tests/xml/test_box.cpp @@ -1,9 +1,6 @@ -#include "test_box.hpp" - #include #include #include -#include #include #include #include @@ -30,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/xml/test_box.hpp b/tests/xml/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/xml/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_custom_class1.cpp b/tests/xml/test_custom_class1.cpp index 35f4a1da..eee793eb 100644 --- a/tests/xml/test_custom_class1.cpp +++ b/tests/xml/test_custom_class1.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class1.hpp" - #include #include #include -#include #include #include #include @@ -32,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/xml/test_custom_class1.hpp b/tests/xml/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/xml/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_custom_class3.cpp b/tests/xml/test_custom_class3.cpp index 2ea105e4..3cbc23f6 100644 --- a/tests/xml/test_custom_class3.cpp +++ b/tests/xml/test_custom_class3.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class3.hpp" - #include #include #include -#include #include #include #include @@ -57,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/xml/test_custom_class3.hpp b/tests/xml/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/xml/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_custom_class4.cpp b/tests/xml/test_custom_class4.cpp index e2d79a94..55a5c693 100644 --- a/tests/xml/test_custom_class4.cpp +++ b/tests/xml/test_custom_class4.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class4.hpp" - #include #include #include -#include #include #include #include @@ -58,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/xml/test_custom_class4.hpp b/tests/xml/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/xml/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_default_values.cpp b/tests/xml/test_default_values.cpp index 7613a6e3..d1220cc9 100644 --- a/tests/xml/test_default_values.cpp +++ b/tests/xml/test_default_values.cpp @@ -1,9 +1,6 @@ -#include "test_default_values.hpp" - #include #include #include -#include #include #include #include @@ -18,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/xml/test_default_values.hpp b/tests/xml/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/xml/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_deque.cpp b/tests/xml/test_deque.cpp index cdcceac0..ede6a93f 100644 --- a/tests/xml/test_deque.cpp +++ b/tests/xml/test_deque.cpp @@ -1,8 +1,5 @@ -#include "test_deque.hpp" - #include #include -#include #include #include #include @@ -17,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/xml/test_deque.hpp b/tests/xml/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/xml/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_enum.cpp b/tests/xml/test_enum.cpp index 0095ee19..0634b0eb 100644 --- a/tests/xml/test_enum.cpp +++ b/tests/xml/test_enum.cpp @@ -1,9 +1,6 @@ -#include "test_enum.hpp" - #include #include #include -#include #include #include #include @@ -19,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/xml/test_enum.hpp b/tests/xml/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/xml/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/xml/test_field_variant.cpp b/tests/xml/test_field_variant.cpp index 114d000c..cd3adf8e 100644 --- a/tests/xml/test_field_variant.cpp +++ b/tests/xml/test_field_variant.cpp @@ -1,9 +1,6 @@ -#include "test_field_variant.hpp" - #include #include #include -#include #include #include #include @@ -29,12 +26,10 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); - write_and_read<"shape">(r); + write_and_read<"root">(r); } } // namespace test_field_variant diff --git a/tests/xml/test_field_variant.hpp b/tests/xml/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/xml/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_flag_enum.cpp b/tests/xml/test_flag_enum.cpp index 3846c1fd..d537322d 100644 --- a/tests/xml/test_flag_enum.cpp +++ b/tests/xml/test_flag_enum.cpp @@ -1,12 +1,10 @@ #include #include #include -#include #include #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -28,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/xml/test_flag_enum.hpp b/tests/xml/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/xml/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/xml/test_flag_enum_with_int.cpp b/tests/xml/test_flag_enum_with_int.cpp index 4a0eb52d..7dcc7204 100644 --- a/tests/xml/test_flag_enum_with_int.cpp +++ b/tests/xml/test_flag_enum_with_int.cpp @@ -1,12 +1,10 @@ #include #include #include -#include #include #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -28,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/xml/test_flag_enum_with_int.hpp b/tests/xml/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/xml/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/xml/test_flatten.cpp b/tests/xml/test_flatten.cpp index 468add92..2d691656 100644 --- a/tests/xml/test_flatten.cpp +++ b/tests/xml/test_flatten.cpp @@ -1,9 +1,6 @@ -#include "test_flatten.hpp" - #include #include #include -#include #include #include #include @@ -24,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/xml/test_flatten.hpp b/tests/xml/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/xml/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_flatten_anonymous.cpp b/tests/xml/test_flatten_anonymous.cpp index c1bc0f07..6bfb6f00 100644 --- a/tests/xml/test_flatten_anonymous.cpp +++ b/tests/xml/test_flatten_anonymous.cpp @@ -1,9 +1,6 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include -#include #include #include #include @@ -24,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/xml/test_flatten_anonymous.hpp b/tests/xml/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/xml/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_forward_list.cpp b/tests/xml/test_forward_list.cpp index ed0d07b8..c8bf9a52 100644 --- a/tests/xml/test_forward_list.cpp +++ b/tests/xml/test_forward_list.cpp @@ -1,8 +1,5 @@ -#include "test_forward_list.hpp" - #include #include -#include #include #include #include @@ -17,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/xml/test_forward_list.hpp b/tests/xml/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/xml/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_literal.cpp b/tests/xml/test_literal.cpp index 9b940423..71c589e1 100644 --- a/tests/xml/test_literal.cpp +++ b/tests/xml/test_literal.cpp @@ -1,9 +1,6 @@ -#include "test_literal.hpp" - #include #include #include -#include #include #include #include @@ -21,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/xml/test_literal.hpp b/tests/xml/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/xml/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_literal_map.cpp b/tests/xml/test_literal_map.cpp index b9a03851..6f531efd 100644 --- a/tests/xml/test_literal_map.cpp +++ b/tests/xml/test_literal_map.cpp @@ -1,10 +1,7 @@ -#include "test_literal_map.hpp" - #include #include #include #include -#include #include #include #include @@ -15,15 +12,13 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); homer.insert(std::make_pair(FieldName::make<"lastName">(), std::make_unique("Simpson"))); - write_and_read<"person">(homer); + write_and_read<"root">(homer); } } // namespace test_literal_map diff --git a/tests/xml/test_literal_map.hpp b/tests/xml/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/xml/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_map.cpp b/tests/xml/test_map.cpp index 3e897fe3..638a71e3 100644 --- a/tests/xml/test_map.cpp +++ b/tests/xml/test_map.cpp @@ -1,9 +1,6 @@ -#include "test_map.hpp" - #include #include #include -#include #include #include @@ -17,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/xml/test_map.hpp b/tests/xml/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/xml/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_map_with_key_validation.cpp b/tests/xml/test_map_with_key_validation.cpp index 20f831af..490665ad 100644 --- a/tests/xml/test_map_with_key_validation.cpp +++ b/tests/xml/test_map_with_key_validation.cpp @@ -1,9 +1,6 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include -#include #include #include @@ -17,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/xml/test_map_with_key_validation.hpp b/tests/xml/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/xml/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_monster_example.cpp b/tests/xml/test_monster_example.cpp index e9a443ad..b7003fc5 100644 --- a/tests/xml/test_monster_example.cpp +++ b/tests/xml/test_monster_example.cpp @@ -1,8 +1,5 @@ -#include "test_monster_example.hpp" - #include #include -#include #include #include #include @@ -32,16 +29,14 @@ struct Monster { short hp = 100; std::string name; bool friendly = false; - std::vector inventory; + std::vector inventory; Color color = Color::make<"Blue">(); std::vector weapons; Equipment equipped; std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; @@ -49,8 +44,7 @@ void test() { 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 inventory = std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); const auto orc = Monster{.pos = position, .mana = 150, @@ -62,6 +56,5 @@ void test() { .equipped = rfl::make_field<"weapon">(axe)}; write_and_read(orc); - } } // namespace test_monster_example diff --git a/tests/xml/test_monster_example.hpp b/tests/xml/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/xml/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_readme_example.cpp b/tests/xml/test_readme_example.cpp index f75b51ce..688d7182 100644 --- a/tests/xml/test_readme_example.cpp +++ b/tests/xml/test_readme_example.cpp @@ -1,8 +1,5 @@ -#include "test_readme_example.hpp" - #include #include -#include #include #include #include @@ -23,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/xml/test_readme_example.hpp b/tests/xml/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/xml/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_readme_example2.cpp b/tests/xml/test_readme_example2.cpp new file mode 100644 index 00000000..9468af5d --- /dev/null +++ b/tests/xml/test_readme_example2.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +TEST(xml, test_readme_example2) { + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/xml/test_ref.cpp b/tests/xml/test_ref.cpp index 39a0c726..da6c8d5f 100644 --- a/tests/xml/test_ref.cpp +++ b/tests/xml/test_ref.cpp @@ -1,9 +1,6 @@ -#include "test_ref.hpp" - #include #include #include -#include #include #include #include @@ -30,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/xml/test_ref.hpp b/tests/xml/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/xml/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_save_load.cpp b/tests/xml/test_save_load.cpp index 71adfcb3..a2ee4ad2 100644 --- a/tests/xml/test_save_load.cpp +++ b/tests/xml/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::xml::write(homer1); const auto string2 = rfl::xml::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/xml/test_save_load.hpp b/tests/xml/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/xml/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/xml/test_set.cpp b/tests/xml/test_set.cpp index fa135b37..2cea6dea 100644 --- a/tests/xml/test_set.cpp +++ b/tests/xml/test_set.cpp @@ -1,8 +1,5 @@ -#include "test_set.hpp" - #include #include -#include #include #include #include @@ -17,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/xml/test_set.hpp b/tests/xml/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/xml/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_size.cpp b/tests/xml/test_size.cpp index d3f5d6cf..df0cbcfc 100644 --- a/tests/xml/test_size.cpp +++ b/tests/xml/test_size.cpp @@ -1,8 +1,5 @@ -#include "test_size.hpp" - #include #include -#include #include #include #include @@ -20,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/xml/test_size.hpp b/tests/xml/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/xml/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_string_map.cpp b/tests/xml/test_string_map.cpp index b4cd9e42..10fcb3ee 100644 --- a/tests/xml/test_string_map.cpp +++ b/tests/xml/test_string_map.cpp @@ -1,25 +1,20 @@ -#include "test_string_map.hpp" - #include #include #include #include -#include #include #include #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); homer.insert( std::make_pair("lastName", std::make_unique("Simpson"))); - write_and_read<"person">(homer); + write_and_read<"root">(homer); } } // namespace test_string_map diff --git a/tests/xml/test_string_map.hpp b/tests/xml/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/xml/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_tagged_union.cpp b/tests/xml/test_tagged_union.cpp index 21d0e20c..79ea0014 100644 --- a/tests/xml/test_tagged_union.cpp +++ b/tests/xml/test_tagged_union.cpp @@ -1,9 +1,6 @@ -#include "test_tagged_union.hpp" - #include #include #include -#include #include #include #include @@ -27,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read<"root">(r); } } // namespace test_tagged_union diff --git a/tests/xml/test_tagged_union.hpp b/tests/xml/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/xml/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_timestamp.cpp b/tests/xml/test_timestamp.cpp index da643b1e..1a0efaef 100644 --- a/tests/xml/test_timestamp.cpp +++ b/tests/xml/test_timestamp.cpp @@ -1,9 +1,6 @@ -#include "test_timestamp.hpp" - #include #include #include -#include #include #include #include @@ -20,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/xml/test_timestamp.hpp b/tests/xml/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/xml/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_unique_ptr.cpp b/tests/xml/test_unique_ptr.cpp index 52c676cb..01f099fe 100644 --- a/tests/xml/test_unique_ptr.cpp +++ b/tests/xml/test_unique_ptr.cpp @@ -1,9 +1,6 @@ -#include "test_unique_ptr.hpp" - #include #include #include -#include #include #include #include @@ -18,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/xml/test_unique_ptr.hpp b/tests/xml/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/xml/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_unique_ptr2.cpp b/tests/xml/test_unique_ptr2.cpp index cd91ccb8..7efd891f 100644 --- a/tests/xml/test_unique_ptr2.cpp +++ b/tests/xml/test_unique_ptr2.cpp @@ -1,9 +1,6 @@ -#include "test_unique_ptr2.hpp" - #include #include #include -#include #include #include #include @@ -30,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/xml/test_unique_ptr2.hpp b/tests/xml/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/xml/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_variant.cpp b/tests/xml/test_variant.cpp index 49b9f041..bf64cdcb 100644 --- a/tests/xml/test_variant.cpp +++ b/tests/xml/test_variant.cpp @@ -1,9 +1,6 @@ -#include "test_variant.hpp" - #include #include #include -#include #include #include #include @@ -27,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read<"root">(r); diff --git a/tests/xml/test_variant.hpp b/tests/xml/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/xml/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/xml/test_wstring.cpp b/tests/xml/test_wstring.cpp index 52499e4a..5cfbbe94 100644 --- a/tests/xml/test_wstring.cpp +++ b/tests/xml/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,18 +7,16 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(xml, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; - write_and_read<"root">(test); + write_and_read(test); } } // namespace test_wstring diff --git a/tests/xml/test_wstring.hpp b/tests/xml/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/xml/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/xml/test_xml_content.cpp b/tests/xml/test_xml_content.cpp index be011125..7e47c2bd 100644 --- a/tests/xml/test_xml_content.cpp +++ b/tests/xml/test_xml_content.cpp @@ -1,5 +1,3 @@ -#include "test_xml_content.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(xml, test_xml_content) { const auto bart = Person{.xml_content = "Bart Simpson", .birthday = "1987-04-19", .email = "bart@simpson.com"}; diff --git a/tests/xml/test_xml_content.hpp b/tests/xml/test_xml_content.hpp deleted file mode 100644 index 58b774fb..00000000 --- a/tests/xml/test_xml_content.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_xml_content { -void test(); -} - diff --git a/tests/xml/tests.cpp b/tests/xml/tests.cpp deleted file mode 100644 index f8671ee9..00000000 --- a/tests/xml/tests.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "test_array.hpp" -#include "test_attributes.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_xml_content.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_attributes::test(); - test_xml_content::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/xml/write_and_read.hpp b/tests/xml/write_and_read.hpp index ceb85c8a..a3aa4719 100644 --- a/tests/xml/write_and_read.hpp +++ b/tests/xml/write_and_read.hpp @@ -1,36 +1,23 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include +#include #include #include -#include "rfl/internal/StringLiteral.hpp" - template < rfl::internal::StringLiteral _root_name = rfl::internal::StringLiteral(""), class T> void write_and_read(const T& _struct) { - const auto xml_string1 = rfl::xml::write<_root_name>(_struct); - const auto res = rfl::xml::read(xml_string1); - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << "Original string: " << xml_string1 << std::endl - << std::endl; - return; - } - const auto xml_string2 = rfl::xml::write<_root_name>(res.value()); - if (xml_string2 != xml_string1) { - std::cout << "Test failed on read. Expected:" << std::endl - << xml_string1 << std::endl - << "Got: " << std::endl - << xml_string2 << std::endl - << std::endl; - return; - } - // std::cout << xml_string1 << std::endl; - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::xml::write<_root_name>(_struct); + const auto res = rfl::xml::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::xml::write<_root_name>(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/tests/yaml/CMakeLists.txt b/tests/yaml/CMakeLists.txt index 238ef474..5445171a 100644 --- a/tests/yaml/CMakeLists.txt +++ b/tests/yaml/CMakeLists.txt @@ -1,8 +1,18 @@ project(reflect-cpp-yaml-tests) -file(GLOB_RECURSE SOURCES "*.cpp") +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp") -add_executable(reflect-cpp-yaml-tests ${SOURCES}) +add_executable( + reflect-cpp-yaml-tests + ${SOURCES} + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/src/gtest_main.cc" +) -target_link_libraries(reflect-cpp-yaml-tests PRIVATE reflectcpp) +target_include_directories(reflect-cpp-yaml-tests SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include") +target_link_libraries( + reflect-cpp-yaml-tests + PRIVATE + reflectcpp + "${REFLECT_CPP_GTEST_LIB}" +) diff --git a/tests/yaml/test_array.cpp b/tests/yaml/test_array.cpp index a4586d3b..713a52b0 100644 --- a/tests/yaml/test_array.cpp +++ b/tests/yaml/test_array.cpp @@ -1,5 +1,3 @@ -#include "test_array.hpp" - #include #include #include @@ -8,7 +6,7 @@ #include // Make sure things still compile when -// rfl.hpp is included after rfl/yaml.hpp. +// rfl.hpp is included after rfl/cbor.hpp. #include #include "write_and_read.hpp" @@ -21,9 +19,7 @@ struct Person { std::unique_ptr> children = nullptr; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_array) { auto bart = Person{.first_name = "Bart"}; auto lisa = Person{.first_name = "Lisa"}; diff --git a/tests/yaml/test_array.hpp b/tests/yaml/test_array.hpp deleted file mode 100644 index 502c3388..00000000 --- a/tests/yaml/test_array.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_array{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_box.cpp b/tests/yaml/test_box.cpp index 04d88641..ca6e0cfe 100644 --- a/tests/yaml/test_box.cpp +++ b/tests/yaml/test_box.cpp @@ -1,9 +1,6 @@ -#include "test_box.hpp" - #include #include #include -#include #include #include #include @@ -30,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_box) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/yaml/test_box.hpp b/tests/yaml/test_box.hpp deleted file mode 100644 index a564b9e1..00000000 --- a/tests/yaml/test_box.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_box{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_custom_class1.cpp b/tests/yaml/test_custom_class1.cpp index 35f4a1da..872d1ef7 100644 --- a/tests/yaml/test_custom_class1.cpp +++ b/tests/yaml/test_custom_class1.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class1.hpp" - #include #include #include -#include #include #include #include @@ -32,9 +29,7 @@ struct Person { PersonImpl impl; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_custom_class1) { const auto bart = Person("Bart"); write_and_read(bart); diff --git a/tests/yaml/test_custom_class1.hpp b/tests/yaml/test_custom_class1.hpp deleted file mode 100644 index eafe6cd0..00000000 --- a/tests/yaml/test_custom_class1.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class1{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_custom_class3.cpp b/tests/yaml/test_custom_class3.cpp index 2ea105e4..a59d5c22 100644 --- a/tests/yaml/test_custom_class3.cpp +++ b/tests/yaml/test_custom_class3.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class3.hpp" - #include #include #include -#include #include #include #include @@ -57,9 +54,7 @@ struct Parser namespace test_custom_class3 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_custom_class3) { const auto bart = Person("Bart", "Simpson", 10); write_and_read(bart); diff --git a/tests/yaml/test_custom_class3.hpp b/tests/yaml/test_custom_class3.hpp deleted file mode 100644 index 9a6fdab4..00000000 --- a/tests/yaml/test_custom_class3.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class3{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_custom_class4.cpp b/tests/yaml/test_custom_class4.cpp index e2d79a94..43bccf9f 100644 --- a/tests/yaml/test_custom_class4.cpp +++ b/tests/yaml/test_custom_class4.cpp @@ -1,9 +1,6 @@ -#include "test_custom_class4.hpp" - #include #include #include -#include #include #include #include @@ -58,9 +55,7 @@ struct Parser namespace test_custom_class4 { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_custom_class4) { const auto bart = test_custom_class4::Person( "Bart", rfl::make_box("Simpson"), 10); diff --git a/tests/yaml/test_custom_class4.hpp b/tests/yaml/test_custom_class4.hpp deleted file mode 100644 index 2d3b151a..00000000 --- a/tests/yaml/test_custom_class4.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_class4{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_custom_constructor.cpp b/tests/yaml/test_custom_constructor.cpp deleted file mode 100644 index 3bff855c..00000000 --- a/tests/yaml/test_custom_constructor.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "test_custom_constructor.hpp" - -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_constructor { - -struct Person { - static rfl::Result from_yaml_obj( - typename rfl::yaml::Reader::InputVarType _obj) { - /// This only exists for the purpose of the test. - const auto change_first_name = [](auto&& _person) { - return rfl::replace(std::move(_person), - rfl::Field<"firstName", std::string>("Bart")); - }; - const auto from_nt = [](auto&& _nt) { - return rfl::from_named_tuple(std::move(_nt)); - }; - return rfl::yaml::read>(_obj) - .transform(from_nt) - .transform(change_first_name); - } - - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto res = rfl::yaml::read( - R"(firstName: Homer -lastName: Simpson -birthday: 1987-04-19)"); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } - - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } -} -} // namespace test_custom_constructor diff --git a/tests/yaml/test_custom_constructor.hpp b/tests/yaml/test_custom_constructor.hpp deleted file mode 100644 index 013721d0..00000000 --- a/tests/yaml/test_custom_constructor.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_custom_constructor{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_default_values.cpp b/tests/yaml/test_default_values.cpp index f6af2352..d73fad6b 100644 --- a/tests/yaml/test_default_values.cpp +++ b/tests/yaml/test_default_values.cpp @@ -1,5 +1,3 @@ -#include "test_default_values.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_default_values) { const auto bart = Person{.first_name = "Bart"}; const auto lisa = Person{.first_name = "Lisa"}; const auto maggie = Person{.first_name = "Maggie"}; diff --git a/tests/yaml/test_default_values.hpp b/tests/yaml/test_default_values.hpp deleted file mode 100644 index c8f8360e..00000000 --- a/tests/yaml/test_default_values.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_default_values{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_deque.cpp b/tests/yaml/test_deque.cpp index 6dfa5803..6e0404c2 100644 --- a/tests/yaml/test_deque.cpp +++ b/tests/yaml/test_deque.cpp @@ -1,5 +1,3 @@ -#include "test_deque.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_default_values) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/yaml/test_deque.hpp b/tests/yaml/test_deque.hpp deleted file mode 100644 index 6781e880..00000000 --- a/tests/yaml/test_deque.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_deque{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_enum.cpp b/tests/yaml/test_enum.cpp index d4d56a8b..9a37b856 100644 --- a/tests/yaml/test_enum.cpp +++ b/tests/yaml/test_enum.cpp @@ -1,5 +1,3 @@ -#include "test_enum.hpp" - #include #include #include @@ -18,9 +16,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::green}; write_and_read(circle); diff --git a/tests/yaml/test_enum.hpp b/tests/yaml/test_enum.hpp deleted file mode 100644 index 2e2e0b3d..00000000 --- a/tests/yaml/test_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_enum { -void test(); -} - diff --git a/tests/yaml/test_field_variant.cpp b/tests/yaml/test_field_variant.cpp index 8b17665f..c75be2d1 100644 --- a/tests/yaml/test_field_variant.cpp +++ b/tests/yaml/test_field_variant.cpp @@ -1,5 +1,3 @@ -#include "test_field_variant.hpp" - #include #include #include @@ -28,9 +26,7 @@ using Shapes = rfl::Variant, rfl::Field<"rectangle", Rectangle>, rfl::Field<"square", rfl::Box>>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_field_variant) { const Shapes r = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); diff --git a/tests/yaml/test_field_variant.hpp b/tests/yaml/test_field_variant.hpp deleted file mode 100644 index ba93e732..00000000 --- a/tests/yaml/test_field_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_field_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_flag_enum.cpp b/tests/yaml/test_flag_enum.cpp index 1d3f76af..6c14a1db 100644 --- a/tests/yaml/test_flag_enum.cpp +++ b/tests/yaml/test_flag_enum.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_flag_enum) { const auto circle = Circle{.radius = 2.0, .color = Color::blue | Color::orange}; diff --git a/tests/yaml/test_flag_enum.hpp b/tests/yaml/test_flag_enum.hpp deleted file mode 100644 index 2f4dc7a0..00000000 --- a/tests/yaml/test_flag_enum.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum { -void test(); -} - diff --git a/tests/yaml/test_flag_enum_with_int.cpp b/tests/yaml/test_flag_enum_with_int.cpp index 4c7deefe..5f63353c 100644 --- a/tests/yaml/test_flag_enum_with_int.cpp +++ b/tests/yaml/test_flag_enum_with_int.cpp @@ -5,7 +5,6 @@ #include #include -#include "test_enum.hpp" #include "write_and_read.hpp" namespace test_flag_enum_with_int { @@ -27,9 +26,7 @@ struct Circle { Color color; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_flag_enum_with_int) { const auto circle = Circle{.radius = 2.0, .color = static_cast(10000)}; write_and_read(circle); diff --git a/tests/yaml/test_flag_enum_with_int.hpp b/tests/yaml/test_flag_enum_with_int.hpp deleted file mode 100644 index a7512b60..00000000 --- a/tests/yaml/test_flag_enum_with_int.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flag_enum_with_int { -void test(); -} - diff --git a/tests/yaml/test_flatten.cpp b/tests/yaml/test_flatten.cpp index c7f1d9be..40a7ed56 100644 --- a/tests/yaml/test_flatten.cpp +++ b/tests/yaml/test_flatten.cpp @@ -1,5 +1,3 @@ -#include "test_flatten.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { rfl::Field<"salary", float> salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_flatten) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/yaml/test_flatten.hpp b/tests/yaml/test_flatten.hpp deleted file mode 100644 index 24d60e11..00000000 --- a/tests/yaml/test_flatten.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_flatten_anonymous.cpp b/tests/yaml/test_flatten_anonymous.cpp index 05e61464..866c80ae 100644 --- a/tests/yaml/test_flatten_anonymous.cpp +++ b/tests/yaml/test_flatten_anonymous.cpp @@ -1,5 +1,3 @@ -#include "test_flatten_anonymous.hpp" - #include #include #include @@ -23,9 +21,7 @@ struct Employee { float salary; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_flatten_anonymous) { const auto employee = Employee{ .person = Person{.first_name = "Homer", .last_name = rfl::make_box("Simpson"), diff --git a/tests/yaml/test_flatten_anonymous.hpp b/tests/yaml/test_flatten_anonymous.hpp deleted file mode 100644 index 7ffa2785..00000000 --- a/tests/yaml/test_flatten_anonymous.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_flatten_anonymous{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_forward_list.cpp b/tests/yaml/test_forward_list.cpp index dd8d9e56..6b8b5eb8 100644 --- a/tests/yaml/test_forward_list.cpp +++ b/tests/yaml/test_forward_list.cpp @@ -1,5 +1,3 @@ -#include "test_forward_list.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_forward_list) { auto children = std::make_unique>(); children->emplace_front(Person{.first_name = "Maggie"}); children->emplace_front(Person{.first_name = "Lisa"}); diff --git a/tests/yaml/test_forward_list.hpp b/tests/yaml/test_forward_list.hpp deleted file mode 100644 index 9784a0c4..00000000 --- a/tests/yaml/test_forward_list.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_forward_list{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_literal.cpp b/tests/yaml/test_literal.cpp index 00578178..7c5f7e50 100644 --- a/tests/yaml/test_literal.cpp +++ b/tests/yaml/test_literal.cpp @@ -1,5 +1,3 @@ -#include "test_literal.hpp" - #include #include #include @@ -20,9 +18,7 @@ struct Person { std::vector children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_literal) { const auto bart = Person{.first_name = FirstName::make<"Bart">()}; write_and_read(bart); diff --git a/tests/yaml/test_literal.hpp b/tests/yaml/test_literal.hpp deleted file mode 100644 index ccd500ef..00000000 --- a/tests/yaml/test_literal.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_literal_map.cpp b/tests/yaml/test_literal_map.cpp index 9bbebafb..41b4dffc 100644 --- a/tests/yaml/test_literal_map.cpp +++ b/tests/yaml/test_literal_map.cpp @@ -1,5 +1,3 @@ -#include "test_literal_map.hpp" - #include #include #include @@ -14,9 +12,7 @@ namespace test_literal_map { using FieldName = rfl::Literal<"firstName", "lastName">; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_literal_map) { std::map> homer; homer.insert(std::make_pair(FieldName::make<"firstName">(), std::make_unique("Homer"))); diff --git a/tests/yaml/test_literal_map.hpp b/tests/yaml/test_literal_map.hpp deleted file mode 100644 index cc05d0c8..00000000 --- a/tests/yaml/test_literal_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_literal_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_map.cpp b/tests/yaml/test_map.cpp index e37ce564..68c63646 100644 --- a/tests/yaml/test_map.cpp +++ b/tests/yaml/test_map.cpp @@ -1,5 +1,3 @@ -#include "test_map.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::map children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_map) { auto children = std::map(); children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); diff --git a/tests/yaml/test_map.hpp b/tests/yaml/test_map.hpp deleted file mode 100644 index 9ae49728..00000000 --- a/tests/yaml/test_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_map_with_key_validation.cpp b/tests/yaml/test_map_with_key_validation.cpp index e758219d..6cde9a83 100644 --- a/tests/yaml/test_map_with_key_validation.cpp +++ b/tests/yaml/test_map_with_key_validation.cpp @@ -1,5 +1,3 @@ -#include "test_map_with_key_validation.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_map_with_key_validation) { auto children = std::make_unique>(); children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); diff --git a/tests/yaml/test_map_with_key_validation.hpp b/tests/yaml/test_map_with_key_validation.hpp deleted file mode 100644 index 1372f926..00000000 --- a/tests/yaml/test_map_with_key_validation.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_map_with_key_validation{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_monster_example.cpp b/tests/yaml/test_monster_example.cpp index 052d8390..9f6f81b2 100644 --- a/tests/yaml/test_monster_example.cpp +++ b/tests/yaml/test_monster_example.cpp @@ -1,5 +1,3 @@ -#include "test_monster_example.hpp" - #include #include #include @@ -38,9 +36,7 @@ struct Monster { std::vector path; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_monster_example) { const auto sword = Weapon{.name = "Sword", .damage = 3}; const auto axe = Weapon{.name = "Axe", .damage = 5}; diff --git a/tests/yaml/test_monster_example.hpp b/tests/yaml/test_monster_example.hpp deleted file mode 100644 index f2d959fc..00000000 --- a/tests/yaml/test_monster_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_monster_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_readme_example.cpp b/tests/yaml/test_readme_example.cpp index 4e6f8ee0..a877b3eb 100644 --- a/tests/yaml/test_readme_example.cpp +++ b/tests/yaml/test_readme_example.cpp @@ -1,5 +1,3 @@ -#include "test_readme_example.hpp" - #include #include #include @@ -22,9 +20,7 @@ struct Person { std::vector child; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_readme_example) { const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19", .age = 10, diff --git a/tests/yaml/test_readme_example.hpp b/tests/yaml/test_readme_example.hpp deleted file mode 100644 index 68c6cf81..00000000 --- a/tests/yaml/test_readme_example.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_readme_example2.cpp b/tests/yaml/test_readme_example2.cpp index a2d212a4..e42ac269 100644 --- a/tests/yaml/test_readme_example2.cpp +++ b/tests/yaml/test_readme_example2.cpp @@ -4,7 +4,6 @@ #include #include -#include "test_readme_example.hpp" #include "write_and_read.hpp" namespace test_readme_example2 { @@ -15,9 +14,7 @@ struct Person { int age; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_readme_example2) { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; diff --git a/tests/yaml/test_readme_example2.hpp b/tests/yaml/test_readme_example2.hpp deleted file mode 100644 index 5c6b011c..00000000 --- a/tests/yaml/test_readme_example2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_readme_example2 { -void test(); -} - diff --git a/tests/yaml/test_ref.cpp b/tests/yaml/test_ref.cpp index 32a32e6c..93d069d7 100644 --- a/tests/yaml/test_ref.cpp +++ b/tests/yaml/test_ref.cpp @@ -1,5 +1,3 @@ -#include "test_ref.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_ref) { const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/yaml/test_ref.hpp b/tests/yaml/test_ref.hpp deleted file mode 100644 index d289ba09..00000000 --- a/tests/yaml/test_ref.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_ref{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_save_load.cpp b/tests/yaml/test_save_load.cpp index c20fc181..ac656f0b 100644 --- a/tests/yaml/test_save_load.cpp +++ b/tests/yaml/test_save_load.cpp @@ -1,5 +1,3 @@ -#include "test_save_load.hpp" - #include #include #include @@ -8,6 +6,8 @@ #include #include +#include + namespace test_save_load { using Age = rfl::Validator children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_save_load) { const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19", @@ -59,12 +57,6 @@ void test() { const auto string1 = rfl::yaml::write(homer1); const auto string2 = rfl::yaml::write(homer2); - if (string1 != string2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; + EXPECT_EQ(string1, string2); } } // namespace test_save_load diff --git a/tests/yaml/test_save_load.hpp b/tests/yaml/test_save_load.hpp deleted file mode 100644 index 7bf10359..00000000 --- a/tests/yaml/test_save_load.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_save_load { -void test(); -} - diff --git a/tests/yaml/test_set.cpp b/tests/yaml/test_set.cpp index 9f2516ce..0f19fbcd 100644 --- a/tests/yaml/test_set.cpp +++ b/tests/yaml/test_set.cpp @@ -1,5 +1,3 @@ -#include "test_set.hpp" - #include #include #include @@ -16,9 +14,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_set) { auto children = std::make_unique>( std::set({"Bart", "Lisa", "Maggie"})); diff --git a/tests/yaml/test_set.hpp b/tests/yaml/test_set.hpp deleted file mode 100644 index 142a663b..00000000 --- a/tests/yaml/test_set.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_set{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_size.cpp b/tests/yaml/test_size.cpp index c37b9bd8..b3502ff4 100644 --- a/tests/yaml/test_size.cpp +++ b/tests/yaml/test_size.cpp @@ -1,5 +1,3 @@ -#include "test_size.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_size) { const auto bart = Person{ .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; diff --git a/tests/yaml/test_size.hpp b/tests/yaml/test_size.hpp deleted file mode 100644 index be330df0..00000000 --- a/tests/yaml/test_size.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_size{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_string_map.cpp b/tests/yaml/test_string_map.cpp index cb4fb903..fe682d62 100644 --- a/tests/yaml/test_string_map.cpp +++ b/tests/yaml/test_string_map.cpp @@ -1,5 +1,3 @@ -#include "test_string_map.hpp" - #include #include #include @@ -10,9 +8,7 @@ #include "write_and_read.hpp" namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_string_map) { std::map> homer; homer.insert( std::make_pair("firstName", std::make_unique("Homer"))); diff --git a/tests/yaml/test_string_map.hpp b/tests/yaml/test_string_map.hpp deleted file mode 100644 index 94cb975a..00000000 --- a/tests/yaml/test_string_map.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_string_map{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_tagged_union.cpp b/tests/yaml/test_tagged_union.cpp index a9dd1abc..b226f2e6 100644 --- a/tests/yaml/test_tagged_union.cpp +++ b/tests/yaml/test_tagged_union.cpp @@ -1,5 +1,3 @@ -#include "test_tagged_union.hpp" - #include #include #include @@ -26,11 +24,8 @@ struct Square { using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_tagged_union) { const Shapes r = Rectangle{.height = 10, .width = 5}; - write_and_read(r); } } // namespace test_tagged_union diff --git a/tests/yaml/test_tagged_union.hpp b/tests/yaml/test_tagged_union.hpp deleted file mode 100644 index 5d522ff9..00000000 --- a/tests/yaml/test_tagged_union.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_tagged_union{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_timestamp.cpp b/tests/yaml/test_timestamp.cpp index 8af261d1..64e3bdce 100644 --- a/tests/yaml/test_timestamp.cpp +++ b/tests/yaml/test_timestamp.cpp @@ -1,5 +1,3 @@ -#include "test_timestamp.hpp" - #include #include #include @@ -19,9 +17,7 @@ struct Person { TS birthday; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_timestamp) { const auto result = TS::from_string("nonsense"); if (result) { diff --git a/tests/yaml/test_timestamp.hpp b/tests/yaml/test_timestamp.hpp deleted file mode 100644 index 891d89b9..00000000 --- a/tests/yaml/test_timestamp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_timestamp{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr.cpp b/tests/yaml/test_unique_ptr.cpp index b1833533..35d74cc3 100644 --- a/tests/yaml/test_unique_ptr.cpp +++ b/tests/yaml/test_unique_ptr.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr.hpp" - #include #include #include @@ -17,9 +15,7 @@ struct Person { std::unique_ptr> children; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_unique_ptr) { auto children = std::make_unique>(); children->emplace_back(Person{.first_name = "Bart"}); children->emplace_back(Person{.first_name = "Lisa"}); diff --git a/tests/yaml/test_unique_ptr.hpp b/tests/yaml/test_unique_ptr.hpp deleted file mode 100644 index 428ea2a2..00000000 --- a/tests/yaml/test_unique_ptr.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr2.cpp b/tests/yaml/test_unique_ptr2.cpp index e3007267..3a8ee94a 100644 --- a/tests/yaml/test_unique_ptr2.cpp +++ b/tests/yaml/test_unique_ptr2.cpp @@ -1,5 +1,3 @@ -#include "test_unique_ptr2.hpp" - #include #include #include @@ -29,9 +27,7 @@ struct DecisionTree { rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; }; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_unique_ptr2) { auto leaf1 = DecisionTree::Leaf{.value = 3.0}; auto leaf2 = DecisionTree::Leaf{.value = 5.0}; diff --git a/tests/yaml/test_unique_ptr2.hpp b/tests/yaml/test_unique_ptr2.hpp deleted file mode 100644 index 74adc170..00000000 --- a/tests/yaml/test_unique_ptr2.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_unique_ptr2{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_variant.cpp b/tests/yaml/test_variant.cpp index cc3d6cbe..e9bc0c9a 100644 --- a/tests/yaml/test_variant.cpp +++ b/tests/yaml/test_variant.cpp @@ -1,5 +1,3 @@ -#include "test_variant.hpp" - #include #include #include @@ -26,9 +24,7 @@ struct Square { using Shapes = std::variant>; -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - +TEST(yaml, test_variant) { const Shapes r = Rectangle{.height = 10, .width = 5}; write_and_read(r); diff --git a/tests/yaml/test_variant.hpp b/tests/yaml/test_variant.hpp deleted file mode 100644 index 0e58ce71..00000000 --- a/tests/yaml/test_variant.hpp +++ /dev/null @@ -1,4 +0,0 @@ -namespace test_variant{ - void test(); -} - \ No newline at end of file diff --git a/tests/yaml/test_wstring.cpp b/tests/yaml/test_wstring.cpp index ba6202c0..5fadc26c 100644 --- a/tests/yaml/test_wstring.cpp +++ b/tests/yaml/test_wstring.cpp @@ -1,5 +1,3 @@ -#include "test_wstring.hpp" - #include #include #include @@ -9,17 +7,15 @@ #include "write_and_read.hpp" -struct Test { +struct TestStruct { std::string theNormalString; std::wstring theWiderString; }; namespace test_wstring { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Test test = Test{.theNormalString = "The normal string", - .theWiderString = L"The wider string"}; +TEST(yaml, test_wstring) { + const auto test = TestStruct{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; write_and_read(test); } diff --git a/tests/yaml/test_wstring.hpp b/tests/yaml/test_wstring.hpp deleted file mode 100644 index 0f93e784..00000000 --- a/tests/yaml/test_wstring.hpp +++ /dev/null @@ -1,3 +0,0 @@ -namespace test_wstring { - void test(); -} diff --git a/tests/yaml/tests.cpp b/tests/yaml/tests.cpp deleted file mode 100644 index 7a38771d..00000000 --- a/tests/yaml/tests.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "test_array.hpp" -#include "test_box.hpp" -#include "test_custom_class1.hpp" -#include "test_custom_class3.hpp" -#include "test_custom_class4.hpp" -#include "test_custom_constructor.hpp" -#include "test_default_values.hpp" -#include "test_deque.hpp" -#include "test_enum.hpp" -#include "test_field_variant.hpp" -#include "test_flag_enum.hpp" -#include "test_flag_enum_with_int.hpp" -#include "test_flatten.hpp" -#include "test_flatten_anonymous.hpp" -#include "test_forward_list.hpp" -#include "test_literal.hpp" -#include "test_literal_map.hpp" -#include "test_map.hpp" -#include "test_map_with_key_validation.hpp" -#include "test_monster_example.hpp" -#include "test_readme_example.hpp" -#include "test_readme_example2.hpp" -#include "test_ref.hpp" -#include "test_save_load.hpp" -#include "test_set.hpp" -#include "test_size.hpp" -#include "test_tagged_union.hpp" -#include "test_timestamp.hpp" -#include "test_unique_ptr.hpp" -#include "test_unique_ptr2.hpp" -#include "test_variant.hpp" -#include "test_wstring.hpp" - -int main() { - test_readme_example::test(); - test_readme_example2::test(); - test_flatten::test(); - test_flatten_anonymous::test(); - test_enum::test(); - test_flag_enum::test(); - test_flag_enum_with_int::test(); - test_map::test(); - test_map_with_key_validation::test(); - test_variant::test(); - test_field_variant::test(); - test_tagged_union::test(); - test_deque::test(); - test_forward_list::test(); - test_literal_map::test(); - test_literal::test(); - test_monster_example::test(); - test_ref::test(); - test_set::test(); - test_size::test(); - test_timestamp::test(); - test_unique_ptr::test(); - test_unique_ptr2::test(); - test_array::test(); - test_box::test(); - test_custom_class1::test(); - test_custom_class3::test(); - test_custom_class4::test(); - test_default_values::test(); - test_custom_constructor::test(); - test_save_load::test(); - test_wstring::test(); - - return 0; -} diff --git a/tests/yaml/write_and_read.hpp b/tests/yaml/write_and_read.hpp index 6f6e2743..6e4d5922 100644 --- a/tests/yaml/write_and_read.hpp +++ b/tests/yaml/write_and_read.hpp @@ -1,32 +1,20 @@ #ifndef WRITE_AND_READ_ #define WRITE_AND_READ_ +#include + #include #include #include template void write_and_read(const T& _struct) { - const auto yaml_string1 = rfl::yaml::write(_struct); - const auto res = rfl::yaml::read(yaml_string1); - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl - << "Original string: " << yaml_string1 << std::endl - << std::endl; - return; - } - const auto yaml_string2 = rfl::yaml::write(res.value()); - if (yaml_string2 != yaml_string1) { - std::cout << "Test failed on read. Expected:" << std::endl - << yaml_string1 << std::endl - << "Got: " << std::endl - << yaml_string2 << std::endl - << std::endl; - return; - } - // std::cout << yaml_string1 << std::endl; - std::cout << "OK" << std::endl << std::endl; + const auto serialized1 = rfl::yaml::write(_struct); + const auto res = rfl::yaml::read(serialized1); + EXPECT_TRUE(res && true) << "Test failed on read. Error: " + << res.error().value().what(); + const auto serialized2 = rfl::yaml::write(res.value()); + EXPECT_EQ(serialized1, serialized2); } #endif diff --git a/vcpkg.json b/vcpkg.json index 625ac896..7beb0dfe 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -31,6 +31,10 @@ "name": "yaml-cpp", "version>=": "0.8.0#1" }, + { + "name": "gtest", + "version>=": "1.14.0" + }, { "name": "vcpkg-cmake", "version>=": "2023-05-04" From bb3dc34eae2bff2c81dcb9b6385cecfa0d6ab333 Mon Sep 17 00:00:00 2001 From: toge Date: Wed, 24 Apr 2024 01:25:23 +0900 Subject: [PATCH 33/33] fix older msvc(19.36) compilation error (#91) --- include/rfl/parsing/NamedTupleParser.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index cfc19ae1..2412bd96 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -96,7 +96,7 @@ struct NamedTupleParser { if constexpr (_i == size) { return Type{Type::Object{_values}}; } else { - using F = std::tuple_element_t<_i, typename T::Fields>; + using F = std::tuple_element_t<_i, typename NamedTuple::Fields>; _values[std::string(F::name())] = Parser::to_schema(_definitions); return to_schema<_i + 1>(_definitions, _values);