reflection_test.cxx 2.3 KB

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