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 
9 #include "catch.hpp"
10 #include <array>
11 
12 namespace{ namespace ClassTests {
13 
14 #ifndef CLASS_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
15 #define CLASS_TEST_HELPERS_INCLUDED
16 
17 class TestClass
18 {
19     std::string s;
20 
21 public:
TestClass()22     TestClass()
23     : s( "hello" )
24     {}
25 
succeedingCase()26     void succeedingCase()
27     {
28         REQUIRE( s == "hello" );
29     }
failingCase()30     void failingCase()
31     {
32         REQUIRE( s == "world" );
33     }
34 };
35 
36 struct Fixture
37 {
Fixture__anon162716860111::ClassTests::Fixture38     Fixture() : m_a( 1 ) {}
39 
40     int m_a;
41 };
42 
43 template< typename T >
44 struct Template_Fixture {
Template_Fixture__anon162716860111::ClassTests::Template_Fixture45     Template_Fixture(): m_a(1) {}
46 
47     T m_a;
48 };
49 
50 template<typename T>
51 struct Template_Fixture_2 {
Template_Fixture_2__anon162716860111::ClassTests::Template_Fixture_252     Template_Fixture_2() {}
53 
54     T m_a;
55 };
56 
57 template< typename T>
58 struct Template_Foo {
size__anon162716860111::ClassTests::Template_Foo59     size_t size() { return 0; }
60 };
61 
62 template< typename T, size_t V>
63 struct Template_Foo_2 {
size__anon162716860111::ClassTests::Template_Foo_264     size_t size() { return V; }
65 };
66 
67 template <int V>
68 struct Nttp_Fixture{
69     int value = V;
70 };
71 #endif
72 
73 
74 
75 METHOD_AS_TEST_CASE( TestClass::succeedingCase, "A METHOD_AS_TEST_CASE based test run that succeeds", "[class]" )
76 METHOD_AS_TEST_CASE( TestClass::failingCase, "A METHOD_AS_TEST_CASE based test run that fails", "[.][class][failing]" )
77 
78 TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that succeeds", "[class]" )
79 {
80     REQUIRE( m_a == 1 );
81 }
82 
83 TEMPLATE_TEST_CASE_METHOD(Template_Fixture, "A TEMPLATE_TEST_CASE_METHOD based test run that succeeds", "[class][template]", int, float, double) {
84     REQUIRE( Template_Fixture<TestType>::m_a == 1 );
85 }
86 
87 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) {
88     REQUIRE(Nttp_Fixture<V>::value > 0);
89 }
90 
91 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))
92 {
93     REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 0 );
94 }
95 
96 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)))
97 {
98     REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() >= 2);
99 }
100 
101 using MyTypes = std::tuple<int, char, double>;
102 TEMPLATE_LIST_TEST_CASE_METHOD(Template_Fixture, "Template test case method with test types specified inside std::tuple", "[class][template][list]", MyTypes)
103 {
104     REQUIRE( Template_Fixture<TestType>::m_a == 1 );
105 }
106 
107 // We should be able to write our tests within a different namespace
108 namespace Inner
109 {
110     TEST_CASE_METHOD( Fixture, "A TEST_CASE_METHOD based test run that fails", "[.][class][failing]" )
111     {
112         REQUIRE( m_a == 2 );
113     }
114 
115     TEMPLATE_TEST_CASE_METHOD(Template_Fixture,"A TEMPLATE_TEST_CASE_METHOD based test run that fails", "[.][class][template][failing]", int, float, double)
116     {
117         REQUIRE( Template_Fixture<TestType>::m_a == 2 );
118     }
119 
120     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) {
121         REQUIRE(Nttp_Fixture<V>::value == 0);
122     }
123 
124     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))
125     {
126         REQUIRE( Template_Fixture_2<TestType>::m_a.size() == 1 );
127     }
128 
129     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)))
130     {
131         REQUIRE(Template_Fixture_2<TestType>{}.m_a.size() < 2);
132     }
133 }
134 
135 
136 
137 }} // namespace ClassTests
138