Refactor Support.

This commit is contained in:
ykiko
2024-12-04 01:12:40 +08:00
parent ac7ce3aa6a
commit 2baea3800a
26 changed files with 1173 additions and 600 deletions

View File

@@ -1,11 +1,11 @@
#include <gtest/gtest.h>
#include <Support/JSON.h>
#include <Support/Struct.h>
namespace clice {
namespace {
using namespace clice;
TEST(JSON, Point) {
TEST(Support, JSON) {
json::Object object;
object["x"] = 1;
object["y"] = 2;
@@ -24,3 +24,34 @@ TEST(JSON, Point) {
}
} // namespace
struct ValueRef {
int index;
};
template <>
struct json::Serde<ValueRef> {
constexpr inline static bool state = true;
std::vector<int>& decoder;
json::Value serialize(const ValueRef& ref) {
return json::Value(decoder[ref.index]);
}
};
TEST(Support, StatefulSerde) {
std::vector<ValueRef> refs;
refs.emplace_back(4);
refs.emplace_back(3);
refs.emplace_back(2);
refs.emplace_back(1);
refs.emplace_back(0);
std::vector<int> decoder = {1, 2, 3, 4, 5};
json::Serde<ValueRef> serde{decoder};
auto result = json::serialize(refs, serde);
ASSERT_EQ(result, json::Value(json::Array{5, 4, 3, 2, 1}));
}
} // namespace clice