Class.tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #if defined( __GNUC__ ) || defined( __clang__ )
  7. # pragma GCC diagnostic ignored "-Wfloat-equal"
  8. #endif
  9. #include <catch2/catch_test_macros.hpp>
  10. #include <catch2/catch_template_test_macros.hpp>
  11. #include <array>
  12. namespace {
  13. class TestClass {
  14. std::string s;
  15. public:
  16. TestClass(): s( "hello" ) {}
  17. void succeedingCase() { REQUIRE( s == "hello" ); }
  18. void failingCase() { REQUIRE( s == "world" ); }
  19. };
  20. struct Fixture {
  21. Fixture(): m_a( 1 ) {}
  22. int m_a;
  23. };
  24. template <typename T> struct Template_Fixture {
  25. Template_Fixture(): m_a( 1 ) {}
  26. T m_a;
  27. };
  28. template <typename T> struct Template_Fixture_2 {
  29. Template_Fixture_2() {}
  30. T m_a;
  31. };
  32. template <typename T> struct Template_Foo {
  33. size_t size() { return 0; }
  34. };
  35. template <typename T, size_t V> struct Template_Foo_2 {
  36. size_t size() { return V; }
  37. };
  38. template <int V> struct Nttp_Fixture { int value = V; };
  39. } // end unnamed namespace
  40. METHOD_AS_TEST_CASE( TestClass::succeedingCase, "A METHOD_AS_TEST_CASE based test run that succeeds", "[class]" )
  41. METHOD_AS_TEST_CASE( TestClass::failingCase, "A METHOD_AS_TEST_CASE based test run that fails", "[.][class][failing]" )
  42. TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that succeeds", "[class]" )
  43. {
  44. REQUIRE( m_a == 1 );
  45. }
  46. TEMPLATE_TEST_CASE_METHOD(Template_Fixture, "A TEMPLATE_TEST_CASE_METHOD based test run that succeeds", "[class][template]", int, float, double) {
  47. REQUIRE( Template_Fixture<TestType>::m_a == 1 );
  48. }
  49. TEMPLATE_TEST_CASE_METHOD_SIG(Nttp_Fixture, "A TEMPLATE_TEST_CASE_METHOD_SIG based test run that succeeds", "[class][template][nttp]",((int V), V), 1, 3, 6) {
  50. REQUIRE(Nttp_Fixture<V>::value > 0);
  51. }
  52. TEMPLATE_PRODUCT_TEST_CASE_METHOD(Template_Fixture_2, "A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that succeeds","[class][template][product]",(std::vector,Template_Foo),(int,float))
  53. {
  54. REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 0 );
  55. }
  56. TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG(Template_Fixture_2, "A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that succeeds", "[class][template][product][nttp]", ((typename T, size_t S), T, S),(std::array, Template_Foo_2), ((int,2), (float,6)))
  57. {
  58. REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() >= 2);
  59. }
  60. using MyTypes = std::tuple<int, char, double>;
  61. TEMPLATE_LIST_TEST_CASE_METHOD(Template_Fixture, "Template test case method with test types specified inside std::tuple", "[class][template][list]", MyTypes)
  62. {
  63. REQUIRE( Template_Fixture<TestType>::m_a == 1 );
  64. }
  65. // We should be able to write our tests within a different namespace
  66. namespace Inner
  67. {
  68. TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that fails", "[.][class][failing]" )
  69. {
  70. REQUIRE( m_a == 2 );
  71. }
  72. TEMPLATE_TEST_CASE_METHOD(Template_Fixture,"A TEMPLATE_TEST_CASE_METHOD based test run that fails", "[.][class][template][failing]", int, float, double)
  73. {
  74. REQUIRE( Template_Fixture<TestType>::m_a == 2 );
  75. }
  76. TEMPLATE_TEST_CASE_METHOD_SIG(Nttp_Fixture, "A TEMPLATE_TEST_CASE_METHOD_SIG based test run that fails", "[.][class][template][nttp][failing]", ((int V), V), 1, 3, 6) {
  77. REQUIRE(Nttp_Fixture<V>::value == 0);
  78. }
  79. TEMPLATE_PRODUCT_TEST_CASE_METHOD(Template_Fixture_2, "A TEMPLATE_PRODUCT_TEST_CASE_METHOD based test run that fails","[.][class][template][product][failing]",(std::vector,Template_Foo),(int,float))
  80. {
  81. REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 1 );
  82. }
  83. TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG(Template_Fixture_2, "A TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG based test run that fails", "[.][class][template][product][nttp][failing]", ((typename T, size_t S), T, S), (std::array, Template_Foo_2), ((int, 2), (float, 6)))
  84. {
  85. REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() < 2);
  86. }
  87. } // namespace
  88. // We want a class in nested namespace so we can test JUnit's classname normalization.
  89. namespace {
  90. namespace A {
  91. namespace B {
  92. class TestClass {};
  93. }
  94. }
  95. } // namespace
  96. TEST_CASE_METHOD( A::B::TestClass,
  97. "A TEST_CASE_METHOD testing junit classname normalization",
  98. "[class][approvals]" ) {
  99. SUCCEED();
  100. }