reflection_test.cxx 2.2 KB

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