// // typecast_test.cxx // reflection-test // // Created by Sam Jaffe on 7/5/22. // Copyright © 2022 Sam Jaffe. All rights reserved. // #define REFLECTION_TYPE_CAST_IMPLEMENTATION #include "reflection/typecast.h" #include "xcode_gtest_helper.h" using reflection::Object; using reflection::TypeCast; using testing::Eq; using testing::NotNull; enum class Status { OK, BAD_INPUT, INTERNAL_ERROR }; std::string status_to_string(Status status) { switch (status) { case Status::OK: return "OK"; case Status::BAD_INPUT: return "BAD"; case Status::INTERNAL_ERROR: return "ERROR"; } } Status status_from_string(std::string const & str) { if (str == "OK") return Status::OK; else if (str == "BAD") return Status::BAD_INPUT; else if (str == "ERROR") return Status::INTERNAL_ERROR; throw std::invalid_argument(str); } namespace reflection { INSTANTIATE_REFLECTION_TYPECAST(Status); REFLECTION_TYPE_CAST(Status).bind().bind(&status_from_string, &status_to_string); } TEST(TypeCastTest, CanPerformTypeCoercion) { Object stat(Status::OK); EXPECT_THAT(Status(stat), Eq(Status::OK)); EXPECT_THAT(int(stat), Eq(0)); std::string str(stat); EXPECT_THAT(str, Eq("OK")); }