class_init.cpp 2.6 KB

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