reflection_test.cxx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. int d;
  12. int get() const { return d; }
  13. int & get() { return d; }
  14. };
  15. }
  16. namespace reflection {
  17. INSTANTIATE_REFLECTION(Example);
  18. }
  19. REFLECTION(Example)
  20. .bind("a", &Example::a)
  21. .bind("c", &Example::b)
  22. .bind("d", &Example::get, &Example::get);
  23. TEST(ReflectionTest, AquiresGetter) {
  24. EXPECT_NO_THROW(Reflection<Example>::getter("a"));
  25. }
  26. TEST(ReflectionTest, AquiresAccessor) {
  27. EXPECT_NO_THROW(Reflection<Example>::accessor("a"));
  28. }
  29. TEST(ReflectionTest, ThrowsOnFakeGetter) {
  30. EXPECT_THROW(Reflection<Example>::getter("b"), std::out_of_range);
  31. }
  32. TEST(ReflectionTest, ThrowsOnFakeAccessor) {
  33. EXPECT_THROW(Reflection<Example>::accessor("b"), std::out_of_range);
  34. }
  35. TEST(ReflectionTest, UseOnPrimitiveThrows) {
  36. EXPECT_THROW(Reflection<int>::getter("a"), std::logic_error);
  37. EXPECT_THROW(Reflection<int>::accessor("a"), std::logic_error);
  38. }
  39. TEST(ReflectionTest, UseOnStringThrows) {
  40. EXPECT_THROW(Reflection<std::string>::getter("a"), std::logic_error);
  41. EXPECT_THROW(Reflection<std::string>::accessor("a"), std::logic_error);
  42. }
  43. // TEST(ReflectionTest, BindsParameterByName) {
  44. // EXPECT_TRUE(reflection<example>::exists<int>("a"));
  45. // }
  46. //
  47. // TEST(ReflectionTest, CanRenameParameter) {
  48. // EXPECT_FALSE(reflection<example>::exists<int>("b"));
  49. // EXPECT_TRUE(reflection<example>::exists<int>("c"));
  50. // }
  51. //
  52. // TEST(ReflectionTest, CanAccessMember) {
  53. // auto p = reflection<example>::get_pointer<int>("a");
  54. // EXPECT_THAT(p, NotNull());
  55. // EXPECT_THAT(p, Eq(&example::a));
  56. //
  57. // example ex = { 5, 6 };
  58. // EXPECT_THAT(&(ex.*p), Eq(&ex.a));
  59. // EXPECT_THAT(ex.*p, Eq(5));
  60. // }
  61. //
  62. // TEST(ReflectionTest, CanGetNameFromMemPtr) {
  63. // EXPECT_THAT(reflection<example>::name(&example::a), Eq("a"));
  64. // EXPECT_THAT(reflection<example>::name(&example::b), Eq("c"));
  65. // }
  66. //
  67. // TEST(ReflectionTest, CanGetNameFromThis) {
  68. // example ex = { 5, 6 };
  69. // EXPECT_THAT(reflection<example>::name(ex, ex.a), Eq("a"));
  70. // EXPECT_THAT(reflection<example>::name(ex, ex.b), Eq("c"));
  71. // }
  72. //
  73. // TEST(ReflectionTest, NameForMissingIsEmpty) {
  74. // example ex = { 5, 6 };
  75. // EXPECT_THAT(reflection<example>::name(ex, 5), Eq(""));
  76. // }