NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxtestCatchExamples.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "catch.hpp"
34 
36 // CATCH TUTORIAL FOR CUSTUSX
38 
71 //included for testing purposes
72 #include <exception>
73 
74 //===========================================================================================
78 TEST_CASE( "TEST_CASE: Name and tags", "[hide][tutorial]" ) {
79  //both name and tags are visible
80  int a = 1, b = a;
81  REQUIRE( a == b );
82 }
83 
84 TEST_CASE( "TEST_CASE: Name, description and tags", "Description. [hide][tutorial]" ) {
85  //both name and tags are visible, but NOT the description.
86  int a = 1, b = a;
87  REQUIRE( a == b );
88 }
89 
90 //===========================================================================================
99 TEST_CASE( "ASSERTIONS: Natural expressions", "[hide][tutorial]" ) {
100  //Evaluates the expression and records the result.
101  //If an exception is thrown it is caught, reported, and counted as a failure.
102  //These are the macros you will use most of the time
103  int a = 1, b = a, c = 2;
104  REQUIRE(a == b);
105  CHECK(a == b);
106 
107  //Evaluates the expression and records the logical NOT of the result.
108  //If an exception is thrown it is caught, reported, and counted as a failure.
109  //(these forms exist as a workaround for the fact that ! prefixed expressions cannot be decomposed).
110  REQUIRE_FALSE( a == c);
111  CHECK_FALSE( a == c);
112 }
113 
114 TEST_CASE( "ASSERTIONS: Exceptions", "[hide][tutorial]" ) {
115  //Expects that an exception (of any type) is be thrown during evaluation of the expression.
116  REQUIRE_THROWS(throw std::exception());
117  CHECK_THROWS(throw std::exception());
118 
119  //Expects that an exception of the specified type is thrown during evaluation of the expression.
120  REQUIRE_THROWS_AS(throw std::exception(), std::exception);
121  CHECK_THROWS_AS(throw std::exception(), std::exception);
122 
123  //Expects that no exception is thrown during evaluation of the expression.
124  int a = 1, b = a;
125  REQUIRE_NOTHROW( a == b);
126  CHECK_NOTHROW( a == b);
127 }
128 
129 TEST_CASE( "ASSERTIONS: Matcher expressions: Equals", "[hide][tutorial]" ) {
130  CHECK_THAT( "1", Equals("1"));
131  REQUIRE_THAT( "1", Equals("1"));
132 }
133 
134 TEST_CASE( "ASSERTIONS: Matcher expressions: Contains", "[hide][tutorial]" ) {
135  CHECK_THAT( "onion", Contains("io"));
136  REQUIRE_THAT( "onion", Contains("io"));
137 }
138 
139 TEST_CASE( "ASSERTIONS: Matcher expressions: StartsWith", "[hide][tutorial]" ) {
140  CHECK_THAT( "the start", StartsWith("the"));
141  REQUIRE_THAT( "the start", StartsWith("the"));
142 }
143 
144 TEST_CASE( "ASSERTIONS: Matcher expressions: EndsWith", "[hide][tutorial]" ) {
145  CHECK_THAT( "the end", EndsWith("end"));
146  REQUIRE_THAT( "the end", EndsWith("end"));
147 }
148 
149 //===========================================================================================
154 TEST_CASE ("Float vs double precision", "[hide][tutorial]") {
155 
156  //Demonstrating the Approx class
157  Approx approx(0.1);
158  approx.epsilon(0.01); // precision can be specified
159 
160  float f_number = 0.1;
161  double d_number = 0.1;
162  CHECK_FALSE( f_number == d_number);
163  CHECK( f_number == Approx(d_number));
164 
165 }
166 
167 //===========================================================================================
173 TEST_CASE( "LOGGING: INFO", "[hide][tutorial]" ) {
174  std::string info = " logged.";
175  INFO("The info is " << info);
176 }
177 
178 TEST_CASE( "LOGGING: WARN", "[hide][tutorial]" ) {
179  std::string warning = " not important at all.";
180  WARN("The warning is " << warning);
181 }
182 
183 TEST_CASE( "LOGGING: FAIL", "[hide][tutorial]" ) {
184  //this will be recorded as a failure
185  std::string failure = " not a failure at all, it is supposed to fail.";
186  FAIL("The failure is " << failure);
187 }
188 
189 TEST_CASE( "LOGGING: SCOPED_INFO", "[hide][tutorial]" ) {
190  std::string scoped_info = " will only be logged if a test fails in the current scope.";
191  SCOPED_INFO("The scoped info is " << scoped_info);
192 }
193 
194 TEST_CASE( "LOGGING: CAPTURE", "[hide][tutorial]" ) {
195  int theAnswere = 42;
196  CAPTURE( theAnswere );
197 }
198 
199 //===========================================================================================
205 //BDD style testing
206 SCENARIO("BDD scenario", "Behavior-driven development scenario. [hide][tutorial][BDD]"){
207  GIVEN("we create a new std::vector"){
208  std::vector<int> vector;
209  int i = 1;
210  WHEN("we add a int to the vector"){
211  vector.push_back(1);
212  THEN("we can require that the vectors lenght is 1"){
213  REQUIRE( vector.size() == 1);
214  }
215  AND_WHEN("we add another int to the vector"){
216  vector.push_back(i);
217  THEN("we can require that the vectors lenght is 2"){
218  REQUIRE( vector.size() == 2);
219  }
220  }
221  THEN("we can require that the vectors lenght is still 1"){
222  REQUIRE( vector.size() == 1);
223  }
224  }
225  THEN("we can require that the vector is empty"){
226  REQUIRE(vector.empty());
227  }
228  }
229 }
230 
231 
232 
SCENARIO("BDD scenario","Behavior-driven development scenario. [hide][tutorial][BDD]")
TEST_CASE("TEST_CASE: Name and tags","[hide][tutorial]")