class_init.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * clsas_init.cpp - class init tests
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <catch.hpp>
  19. #include <core/class_init.h>
  20. using namespace class_init;
  21. bool& foo_registered() {
  22. static bool instance = false;
  23. return instance;
  24. }
  25. class Foo : Initialized<Foo> {
  26. public:
  27. static void init() {
  28. foo_registered() = true;
  29. }
  30. };
  31. TEST_CASE("Automatic class initialization", "") {
  32. CHECK(foo_registered());
  33. }
  34. class Bar {
  35. };
  36. class ClassName {
  37. public:
  38. virtual string operator()() const = 0;
  39. };
  40. class BarName : public ClassName, Registered<BarName, Bar, ClassName>, ReverseRegistered<BarName, Bar, string> {
  41. public:
  42. string operator()() const override {
  43. return "Bar";
  44. }
  45. };
  46. TEST_CASE("Automatic class registration", "") {
  47. CHECK((type_info<ClassName, string>(typeid(Bar)) == "Bar"));
  48. CHECK_THROWS_AS((type_info<ClassName, string>(typeid(Foo))), UnknownTypeError);
  49. }
  50. TEST_CASE("Reverse class registration", "") {
  51. CHECK(find_type<string>("Bar") == typeid(Bar));
  52. CHECK_THROWS_AS(find_type<string>("Foo"), TypeLookupError);
  53. }
  54. class FooBar {
  55. public:
  56. virtual string name() const {
  57. return "FooBar";
  58. }
  59. virtual ~FooBar() {}
  60. };
  61. class FooBaz : public FooBar {
  62. public:
  63. string name() const override {
  64. return "FooBaz";
  65. }
  66. };
  67. class PolyClassName {
  68. public:
  69. virtual string operator()(any const& object) const = 0;
  70. };
  71. class FooBarName : public PolyClassName, Registered<FooBarName, FooBar*, PolyClassName> {
  72. public:
  73. string operator()(any const& object) const override {
  74. auto p = any_cast<FooBar*>(object);
  75. return p->name();
  76. }
  77. };
  78. TEST_CASE("Polymorphic class name", "") {
  79. FooBar* obj = new FooBar();
  80. CHECK((any_info<PolyClassName, string>(obj) == "FooBar"));
  81. delete obj;
  82. obj = new FooBaz();
  83. CHECK((any_info<PolyClassName, string>(obj) == "FooBaz"));
  84. CHECK_THROWS_AS((any_info<PolyClassName, string>(new FooBaz()) == "FooBaz"), UnknownTypeError);
  85. }