#include #include #include #include #include #include "Test/Test.h" #include "Support/JSON.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/SmallString.h" namespace { struct ValueRef { std::size_t index; }; } // namespace template <> struct clice::json::Serde { constexpr inline static bool stateful = true; std::vector& encoder; std::vector& decoder; json::Value serialize(const ValueRef& ref) { return json::Value(decoder[ref.index]); } ValueRef deserialize(const json::Value& value) { encoder.emplace_back(*value.getAsInteger()); return ValueRef{encoder.size() - 1}; } }; namespace clice::testing { namespace { suite<"JSON"> json_tests = [] { test("String") = [&] { json::Value expected = "hello"; std::string input = "hello"; expect(json::serialize(input) == expected); expect(json::deserialize(expected) == input); std::string_view input2 = "hello"; expect(json::serialize(input2) == expected); expect(json::deserialize(expected) == input2); llvm::StringRef input3 = "hello"; expect(json::serialize(input3) == expected); expect(json::deserialize(expected) == input3); llvm::SmallString<5> input4 = {"hello"}; expect(json::serialize(input4) == expected); expect(json::deserialize>(expected) == input4); }; test("MapRange") = [&] { json::Value expected = json::Object{ {"1", 2}, {"3", 4}, {"5", 6} }; std::map input = { {1, 2}, {3, 4}, {5, 6} }; expect(json::serialize(input) == expected); expect(input == json::deserialize(expected)); std::unordered_map input2 = { {1, 2}, {3, 4}, {5, 6} }; expect(json::serialize(input2) == expected); expect(input2 == json::deserialize(expected)); llvm::DenseMap input4 = { {1, 2}, {3, 4}, {5, 6} }; expect(json::serialize(input4) == expected); expect(input4 == json::deserialize(expected)); }; test("SetRange") = [&] { json::Value expected = {1, 2, 3, 4, 5}; std::set input = {1, 2, 3, 4, 5}; expect(input == json::deserialize(expected)); std::unordered_set input2 = {1, 2, 3, 4, 5}; expect(input2 == json::deserialize(expected)); }; test("SequenceRange") = [&] { json::Value expected = {1, 2, 3, 4, 5}; std::vector input = {1, 2, 3, 4, 5}; expect(json::serialize(input) == expected); expect(json::deserialize>(expected) == input); llvm::ArrayRef input2 = input; expect(json::serialize(input2) == expected); llvm::SmallVector input3 = {1, 2, 3, 4, 5}; expect(json::serialize(input3) == expected); expect((json::deserialize>(expected)) == input3); }; test("Enum") = [&] { enum class E { A, B, C }; json::Value expected = json::Value(1); E input = E::B; expect(json::serialize(input) == expected); expect(json::deserialize(expected) == input); struct Color : refl::Enum { enum Kind { Red = 0, Green, Blue, Yellow, }; using Enum::Enum; }; json::Value expected2 = json::Value(2); Color input2 = Color::Blue; expect(json::serialize(input2) == expected2); expect(json::deserialize(expected2) == input2); }; test("Struct") = [&] { struct A { int x; int y; bool operator== (const A& other) const = default; }; json::Value expected = json::Object{ {"x", 1}, {"y", 2} }; A input = {1, 2}; expect(json::serialize(input) == expected); expect(json::deserialize(expected) == input); struct B { A a; std::string s; bool operator== (const B& other) const = default; }; json::Value expected2 = json::Object{ {"a", json::Object{{"x", 1}, {"y", 2}}}, {"s", "hello" } }; B input2 = { {1, 2}, "hello", }; expect(json::serialize(input2) == expected2); expect(json::deserialize(expected2) == input2); }; }; } // namespace } // namespace clice::testing