|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <rfl.hpp> |
| 3 | +#include <rfl/json.hpp> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "write_and_read.hpp" |
| 8 | + |
| 9 | +namespace test_positional_and_short_json { |
| 10 | + |
| 11 | +struct PositionalConfig { |
| 12 | + rfl::Positional<std::string> input_file; |
| 13 | + rfl::Positional<int> count; |
| 14 | +}; |
| 15 | + |
| 16 | +TEST(json, test_positional_write_and_read) { |
| 17 | + write_and_read( |
| 18 | + PositionalConfig{"data.csv", 42}, |
| 19 | + R"({"input_file":"data.csv","count":42})"); |
| 20 | +} |
| 21 | + |
| 22 | +struct ShortConfig { |
| 23 | + rfl::Short<"p", int> port; |
| 24 | + rfl::Short<"v", bool> verbose; |
| 25 | + std::string host; |
| 26 | +}; |
| 27 | + |
| 28 | +TEST(json, test_short_write_and_read) { |
| 29 | + write_and_read( |
| 30 | + ShortConfig{8080, true, "localhost"}, |
| 31 | + R"({"port":8080,"verbose":true,"host":"localhost"})"); |
| 32 | +} |
| 33 | + |
| 34 | +struct CombinedConfig { |
| 35 | + rfl::Positional<std::string> input_file; |
| 36 | + rfl::Short<"o", std::string> output_dir; |
| 37 | + rfl::Short<"v", bool> verbose; |
| 38 | + int count; |
| 39 | + std::vector<std::string> tags; |
| 40 | +}; |
| 41 | + |
| 42 | +TEST(json, test_positional_and_short_combined_write_and_read) { |
| 43 | + write_and_read( |
| 44 | + CombinedConfig{"data.csv", "/tmp/out", true, 10, {"dev", "prod"}}, |
| 45 | + R"({"input_file":"data.csv","output_dir":"/tmp/out","verbose":true,"count":10,"tags":["dev","prod"]})"); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(json, test_positional_read_from_json) { |
| 49 | + const auto result = rfl::json::read<PositionalConfig>( |
| 50 | + R"({"input_file":"test.txt","count":7})"); |
| 51 | + ASSERT_TRUE(result) << result.error().what(); |
| 52 | + EXPECT_EQ(result.value().input_file(), "test.txt"); |
| 53 | + EXPECT_EQ(result.value().count(), 7); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(json, test_short_read_from_json) { |
| 57 | + const auto result = rfl::json::read<ShortConfig>( |
| 58 | + R"({"port":3000,"verbose":false,"host":"example.com"})"); |
| 59 | + ASSERT_TRUE(result) << result.error().what(); |
| 60 | + EXPECT_EQ(result.value().port(), 3000); |
| 61 | + EXPECT_FALSE(result.value().verbose()); |
| 62 | + EXPECT_EQ(result.value().host, "example.com"); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace test_positional_and_short_json |
0 commit comments