-
Notifications
You must be signed in to change notification settings - Fork 179
Feat/parse null into nullopt optionals #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
liuzicheng1987
merged 3 commits into
getml:main
from
Fellfalla:feat/parse-null-into-nullopt-optionals
Apr 27, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <rfl/yaml.hpp> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace test_read_null { | ||
|
|
||
| struct Person { | ||
| std::string first_name; | ||
| std::optional<std::string> last_name; | ||
| std::optional<int> age; | ||
| std::string city; | ||
| float weight; | ||
| double height; | ||
| }; | ||
|
|
||
| TEST(yaml, test_read_null) { | ||
| const std::string yaml_str = | ||
| "first_name: Homer\n" | ||
| "last_name: null\n" | ||
| "age: ~\n" | ||
| "city: null\n" | ||
| "weight: .nan\n" | ||
| "height: .nan\n"; | ||
|
|
||
| const auto res = rfl::yaml::read<Person>(yaml_str); | ||
|
|
||
| ASSERT_TRUE(res && true) << "Test failed on read. Error: " | ||
| << res.error().what(); | ||
| EXPECT_EQ(res.value().first_name, "Homer"); | ||
| EXPECT_FALSE(res.value().last_name.has_value()); | ||
| EXPECT_FALSE(res.value().age.has_value()); | ||
| EXPECT_TRUE(std::isnan(res.value().weight)); | ||
| EXPECT_TRUE(std::isnan(res.value().height)); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_required_field_fails) { | ||
| const std::string yaml_str = | ||
| "first_name: Homer\n" | ||
| "last_name: null\n" | ||
| "age: ~\n" | ||
| "city: Springfield\n" | ||
| "weight: null\n" | ||
| "height: null\n"; | ||
|
|
||
| const auto res = rfl::yaml::read<Person>(yaml_str); | ||
| EXPECT_FALSE(res.has_value()); | ||
| } | ||
|
|
||
| // One struct per primitive type — each tested with a null value below. | ||
|
|
||
| struct IntHolder { int value; }; | ||
| struct Int8Holder { int8_t value; }; | ||
| struct Int16Holder { int16_t value; }; | ||
| struct Int32Holder { int32_t value; }; | ||
| struct Int64Holder { int64_t value; }; | ||
| struct UInt8Holder { uint8_t value; }; | ||
| struct UInt16Holder { uint16_t value; }; | ||
| struct UInt32Holder { uint32_t value; }; | ||
| struct UInt64Holder { uint64_t value; }; | ||
| struct FloatHolder { float value; }; | ||
| struct DoubleHolder { double value; }; | ||
| struct BoolHolder { bool value; }; | ||
|
|
||
| struct OptionalIntHolder { std::optional<int> value; }; | ||
| struct OptionalStringHolder { std::optional<std::string> value; }; | ||
|
|
||
| static constexpr std::string_view kNullYaml = "value: null\n"; | ||
| static constexpr std::string_view kNaNYaml = "value: .nan\n"; | ||
|
|
||
| TEST(yaml, test_null_for_int_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<IntHolder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_int8_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<Int8Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_int16_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<Int16Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_int32_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<Int32Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_int64_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<Int64Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_uint8_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<UInt8Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_uint16_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<UInt16Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_uint32_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<UInt32Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_uint64_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<UInt64Holder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_float_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<FloatHolder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_nan_for_float_succeeds) { | ||
| EXPECT_TRUE(std::isnan(rfl::yaml::read<FloatHolder>(kNaNYaml).value().value)); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_double_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<DoubleHolder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_nan_for_double_succeeds) { | ||
| EXPECT_TRUE(std::isnan(rfl::yaml::read<DoubleHolder>(kNaNYaml).value().value)); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_bool_fails) { | ||
| EXPECT_FALSE(rfl::yaml::read<BoolHolder>(kNullYaml).has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_optional_int_is_nullopt) { | ||
| const auto res = rfl::yaml::read<OptionalIntHolder>(kNullYaml); | ||
| ASSERT_TRUE(res.has_value()); | ||
| EXPECT_FALSE(res.value().value.has_value()); | ||
| } | ||
|
|
||
| TEST(yaml, test_null_for_optional_string_is_nullopt) { | ||
| const auto res = rfl::yaml::read<OptionalStringHolder>(kNullYaml); | ||
| ASSERT_TRUE(res.has_value()); | ||
| EXPECT_FALSE(res.value().value.has_value()); | ||
| } | ||
|
|
||
| } // namespace test_read_null | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.