reflection_test.cxx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "reflection/reflection.h"
  2. #include "xcode_gtest_helper.h"
  3. using reflection::Object;
  4. using reflection::Reflection;
  5. using testing::Eq;
  6. using testing::NotNull;
  7. namespace {
  8. struct Example {
  9. int a;
  10. int b;
  11. };
  12. bool _ = Reflection<Example>()
  13. .bind("a", &Example::a)
  14. .bind("c", &Example::b);
  15. }
  16. TEST(ReflectionTest, AquiresGetter) {
  17. EXPECT_NO_THROW(Reflection<Example>::getter("a"));
  18. }
  19. TEST(ReflectionTest, AquiresAccessor) {
  20. EXPECT_NO_THROW(Reflection<Example>::accessor("a"));
  21. }
  22. TEST(ReflectionTest, ThrowsOnFakeGetter) {
  23. EXPECT_THROW(Reflection<Example>::getter("b"), std::out_of_range);
  24. }
  25. TEST(ReflectionTest, ThrowsOnFakeAccessor) {
  26. EXPECT_THROW(Reflection<Example>::accessor("b"), std::out_of_range);
  27. }
  28. TEST(ReflectionTest, UseOnPrimitiveThrows) {
  29. EXPECT_THROW(Reflection<int>::getter("a"), std::logic_error);
  30. EXPECT_THROW(Reflection<int>::accessor("a"), std::logic_error);
  31. }
  32. TEST(ReflectionTest, UseOnStringThrows) {
  33. EXPECT_THROW(Reflection<std::string>::getter("a"), std::logic_error);
  34. EXPECT_THROW(Reflection<std::string>::accessor("a"), std::logic_error);
  35. }
  36. //TEST(ReflectionTest, BindsParameterByName) {
  37. // EXPECT_TRUE(reflection<example>::exists<int>("a"));
  38. //}
  39. //
  40. //TEST(ReflectionTest, CanRenameParameter) {
  41. // EXPECT_FALSE(reflection<example>::exists<int>("b"));
  42. // EXPECT_TRUE(reflection<example>::exists<int>("c"));
  43. //}
  44. //
  45. //TEST(ReflectionTest, CanAccessMember) {
  46. // auto p = reflection<example>::get_pointer<int>("a");
  47. // EXPECT_THAT(p, NotNull());
  48. // EXPECT_THAT(p, Eq(&example::a));
  49. //
  50. // example ex = { 5, 6 };
  51. // EXPECT_THAT(&(ex.*p), Eq(&ex.a));
  52. // EXPECT_THAT(ex.*p, Eq(5));
  53. //}
  54. //
  55. //TEST(ReflectionTest, CanGetNameFromMemPtr) {
  56. // EXPECT_THAT(reflection<example>::name(&example::a), Eq("a"));
  57. // EXPECT_THAT(reflection<example>::name(&example::b), Eq("c"));
  58. //}
  59. //
  60. //TEST(ReflectionTest, CanGetNameFromThis) {
  61. // example ex = { 5, 6 };
  62. // EXPECT_THAT(reflection<example>::name(ex, ex.a), Eq("a"));
  63. // EXPECT_THAT(reflection<example>::name(ex, ex.b), Eq("c"));
  64. //}
  65. //
  66. //TEST(ReflectionTest, NameForMissingIsEmpty) {
  67. // example ex = { 5, 6 };
  68. // EXPECT_THAT(reflection<example>::name(ex, 5), Eq(""));
  69. //}