Class.tests.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Created by Phil on 09/11/2010.
  3. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch.hpp"
  9. #include <array>
  10. namespace{ namespace ClassTests {
  11. #ifndef CLASS_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  12. #define CLASS_TEST_HELPERS_INCLUDED
  13. class TestClass
  14. {
  15. std::string s;
  16. public:
  17. TestClass()
  18. : s( "hello" )
  19. {}
  20. void succeedingCase()
  21. {
  22. REQUIRE( s == "hello" );
  23. }
  24. void failingCase()
  25. {
  26. REQUIRE( s == "world" );
  27. }
  28. };
  29. struct Fixture
  30. {
  31. Fixture() : m_a( 1 ) {}
  32. int m_a;
  33. };
  34. template< typename T >
  35. struct Template_Fixture {
  36. Template_Fixture(): m_a(1) {}
  37. T m_a;
  38. };
  39. template<typename T>
  40. struct Template_Fixture_2 {
  41. Template_Fixture_2() {}
  42. T m_a;
  43. };
  44. template< typename T>
  45. struct Template_Foo {
  46. size_t size() { return 0; }
  47. };
  48. template< typename T, size_t V>
  49. struct Template_Foo_2 {
  50. size_t size() { return V; }
  51. };
  52. template <int V>
  53. struct Nttp_Fixture{
  54. int value = V;
  55. };
  56. #endif
  57. METHOD_AS_TEST_CASE( TestClass::succeedingCase, "A METHOD_AS_TEST_CASE based test run that succeeds", "[class]" )
  58. METHOD_AS_TEST_CASE( TestClass::failingCase, "A METHOD_AS_TEST_CASE based test run that fails", "[.][class][failing]" )
  59. TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that succeeds", "[class]" )
  60. {
  61. REQUIRE( m_a == 1 );
  62. }
  63. TEMPLATE_TEST_CASE_METHOD(Template_Fixture, "A TEMPLATE_TEST_CASE_METHOD based test run that succeeds", "[class][template]", int, float, double) {
  64. REQUIRE( Template_Fixture<TestType>::m_a == 1 );
  65. }
  66. 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) {
  67. REQUIRE(Nttp_Fixture<V>::value > 0);
  68. }
  69. 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))
  70. {
  71. REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 0 );
  72. }
  73. 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)))
  74. {
  75. REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() >= 2);
  76. }
  77. using MyTypes = std::tuple<int, char, double>;
  78. TEMPLATE_LIST_TEST_CASE_METHOD(Template_Fixture, "Template test case method with test types specified inside std::tuple", "[class][template][list]", MyTypes)
  79. {
  80. REQUIRE( Template_Fixture<TestType>::m_a == 1 );
  81. }
  82. // We should be able to write our tests within a different namespace
  83. namespace Inner
  84. {
  85. TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that fails", "[.][class][failing]" )
  86. {
  87. REQUIRE( m_a == 2 );
  88. }
  89. TEMPLATE_TEST_CASE_METHOD(Template_Fixture,"A TEMPLATE_TEST_CASE_METHOD based test run that fails", "[.][class][template][failing]", int, float, double)
  90. {
  91. REQUIRE( Template_Fixture<TestType>::m_a == 2 );
  92. }
  93. 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) {
  94. REQUIRE(Nttp_Fixture<V>::value == 0);
  95. }
  96. 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))
  97. {
  98. REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 1 );
  99. }
  100. 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)))
  101. {
  102. REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() < 2);
  103. }
  104. }
  105. }} // namespace ClassTests