Skip to content

Commit

Permalink
More TOML write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Feb 11, 2025
1 parent e6d5dd9 commit 24768b1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/glaze/core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ namespace glz
*t
};
};

template <class T>
concept nullable_like = nullable_t<T> && (!is_expected<T> && !std::is_array_v<T>);

// For optional like types that cannot overload `operator bool()`
template <class T>
Expand Down
3 changes: 0 additions & 3 deletions include/glaze/json/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ namespace glz
}
};

template <class T>
concept nullable_like = nullable_t<T> && (!is_expected<T> && !std::is_array_v<T>);

// Returns 0 if we cannot determine the required padding,
// in which case the `to` specialization must allocate buffer space
// Some types like numbers must have space to be quoted
Expand Down
12 changes: 12 additions & 0 deletions include/glaze/toml/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ namespace glz
std::forward<Ctx>(ctx), std::forward<B>(b), std::forward<IX>(ix));
}
};

template <nullable_like T>
struct to<TOML, T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& b, auto&& ix)
{
if (value) {
write<TOML>::op<Opts>(*value, ctx, b, ix);
}
}
};

template <boolean_like T>
struct to<TOML, T>
Expand Down
87 changes: 79 additions & 8 deletions tests/toml_test/toml_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ struct container {
double value = 5.5;
};

suite starter = [] {
struct optional_struct {
std::optional<int> maybe = 99;
};

suite starter = [] {

// Original test for a reflectable object.
"example"_test = [] {
my_struct s{};
Expand All @@ -34,37 +38,37 @@ d = 3.14
hello = "Hello World"
arr = [1, 2, 3])");
};

// Test writing a simple scalar value.
"scalar_int"_test = [] {
int i = 42;
std::string buffer{};
expect(not glz::write_toml(i, buffer));
expect(buffer == "42");
};

"simple_array"_test = [] {
std::vector<int> v = {1, 2, 3, 4};
std::string buffer{};
expect(not glz::write_toml(v, buffer));
expect(buffer == "[1, 2, 3, 4]");
};

"writable_map"_test = [] {
std::map<std::string, int> m = { {"a", 1}, {"b", 2} };
std::string buffer{};
expect(not glz::write_toml(m, buffer));
// std::map orders keys lexicographically, so we expect:
expect(buffer == "a = 1\nb = 2");
};

"tuple_test"_test = [] {
std::tuple<int, std::string> t = {100, "hello"};
std::string buffer{};
expect(not glz::write_toml(t, buffer));
expect(buffer == R"([100, "hello"])");
};

// Test writing a string that contains quotes and backslashes.
"escape_string"_test = [] {
std::string s = "Line \"quoted\" and \\ backslash";
Expand All @@ -73,7 +77,7 @@ arr = [1, 2, 3])");
// The expected output escapes the quote and backslash, and encloses the result in quotes.
expect(buffer == R"("Line \"quoted\" and \\ backslash")");
};

// Test writing a nested structure.
"nested_struct"_test = [] {
container c{};
Expand All @@ -85,7 +89,74 @@ y = "test"
value = 5.5)");
};


// Test writing a boolean value.
"boolean_value"_test = [] {
bool b = true;
std::string buffer{};
expect(not glz::write_toml(b, buffer));
expect(buffer == "true");
};

// Test writing an empty array.
"empty_array"_test = [] {
std::vector<int> empty{};
std::string buffer{};
expect(not glz::write_toml(empty, buffer));
expect(buffer == "[]");
};

// Test writing an empty map.
"empty_map"_test = [] {
std::map<std::string, int> empty{};
std::string buffer{};
expect(not glz::write_toml(empty, buffer));
expect(buffer == "");
};

// Test writing a vector of booleans.
"vector_of_bool"_test = [] {
std::vector<bool> vb = { true, false, true };
std::string buffer{};
expect(not glz::write_toml(vb, buffer));
expect(buffer == "[true, false, true]");
};

// Test writing an optional that contains a value.
"optional_present"_test = [] {
std::optional<int> opt = 42;
std::string buffer{};
expect(not glz::write_toml(opt, buffer));
expect(buffer == "42");
};

// Test writing an optional that is null.
"optional_null"_test = [] {
std::optional<int> opt = std::nullopt;
std::string buffer{};
expect(not glz::write_toml(opt, buffer));
// Assuming that a null optional is skipped and produces an empty output.
expect(buffer == "");
};

// Test writing a structure with an optional member (present).
"optional_struct_present"_test = [] {
optional_struct os{};
std::string buffer{};
expect(not glz::write_toml(os, buffer));
expect(buffer == "maybe = 99");
};

// Test writing a structure with an optional member (null).
"optional_struct_null"_test = [] {
optional_struct os{};
os.maybe = std::nullopt;
std::string buffer{};
expect(not glz::write_toml(os, buffer));
// If all members are null (or skipped) then the output is empty.
expect(buffer == "");
};

};

int main() { return 0; }

0 comments on commit 24768b1

Please sign in to comment.