Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "catch.hpp"
13 
15 // CATCH TUTORIAL FOR CUSTUSX
17 
50 //included for testing purposes
51 #include <exception>
52 
53 //===========================================================================================
57 TEST_CASE( "TEST_CASE: Name and tags", "[hide][tutorial]" ) {
58  //both name and tags are visible
59  int a = 1, b = a;
60  REQUIRE( a == b );
61 }
62 
63 TEST_CASE( "TEST_CASE: Name, description and tags", "Description. [hide][tutorial]" ) {
64  //both name and tags are visible, but NOT the description.
65  int a = 1, b = a;
66  REQUIRE( a == b );
67 }
68 
69 //===========================================================================================
78 TEST_CASE( "ASSERTIONS: Natural expressions", "[hide][tutorial]" ) {
79  //Evaluates the expression and records the result.
80  //If an exception is thrown it is caught, reported, and counted as a failure.
81  //These are the macros you will use most of the time
82  int a = 1, b = a, c = 2;
83  REQUIRE(a == b);
84  CHECK(a == b);
85 
86  //Evaluates the expression and records the logical NOT of the result.
87  //If an exception is thrown it is caught, reported, and counted as a failure.
88  //(these forms exist as a workaround for the fact that ! prefixed expressions cannot be decomposed).
89  REQUIRE_FALSE( a == c);
90  CHECK_FALSE( a == c);
91 }
92 
93 TEST_CASE( "ASSERTIONS: Exceptions", "[hide][tutorial]" ) {
94  //Expects that an exception (of any type) is be thrown during evaluation of the expression.
95  REQUIRE_THROWS(throw std::exception());
96  CHECK_THROWS(throw std::exception());
97 
98  //Expects that an exception of the specified type is thrown during evaluation of the expression.
99  REQUIRE_THROWS_AS(throw std::exception(), std::exception);
100  CHECK_THROWS_AS(throw std::exception(), std::exception);
101 
102  //Expects that no exception is thrown during evaluation of the expression.
103  int a = 1, b = a;
104  REQUIRE_NOTHROW( a == b);
105  CHECK_NOTHROW( a == b);
106 }
107 
108 TEST_CASE( "ASSERTIONS: Matcher expressions: Equals", "[hide][tutorial]" ) {
109  CHECK_THAT( "1", Equals("1"));
110  REQUIRE_THAT( "1", Equals("1"));
111 }
112 
113 TEST_CASE( "ASSERTIONS: Matcher expressions: Contains", "[hide][tutorial]" ) {
114  CHECK_THAT( "onion", Contains("io"));
115  REQUIRE_THAT( "onion", Contains("io"));
116 }
117 
118 TEST_CASE( "ASSERTIONS: Matcher expressions: StartsWith", "[hide][tutorial]" ) {
119  CHECK_THAT( "the start", StartsWith("the"));
120  REQUIRE_THAT( "the start", StartsWith("the"));
121 }
122 
123 TEST_CASE( "ASSERTIONS: Matcher expressions: EndsWith", "[hide][tutorial]" ) {
124  CHECK_THAT( "the end", EndsWith("end"));
125  REQUIRE_THAT( "the end", EndsWith("end"));
126 }
127 
128 //===========================================================================================
133 TEST_CASE ("Float vs double precision", "[hide][tutorial]") {
134 
135  //Demonstrating the Approx class
136  Approx approx(0.1);
137  approx.epsilon(0.01); // precision can be specified
138 
139  float f_number = 0.1;
140  double d_number = 0.1;
141  CHECK_FALSE( f_number == d_number);
142  CHECK( f_number == Approx(d_number));
143 
144 }
145 
146 //===========================================================================================
152 TEST_CASE( "LOGGING: INFO", "[hide][tutorial]" ) {
153  std::string info = " logged.";
154  INFO("The info is " << info);
155 }
156 
157 TEST_CASE( "LOGGING: WARN", "[hide][tutorial]" ) {
158  std::string warning = " not important at all.";
159  WARN("The warning is " << warning);
160 }
161 
162 TEST_CASE( "LOGGING: FAIL", "[hide][tutorial]" ) {
163  //this will be recorded as a failure
164  std::string failure = " not a failure at all, it is supposed to fail.";
165  FAIL("The failure is " << failure);
166 }
167 
168 TEST_CASE( "LOGGING: SCOPED_INFO", "[hide][tutorial]" ) {
169  std::string scoped_info = " will only be logged if a test fails in the current scope.";
170  SCOPED_INFO("The scoped info is " << scoped_info);
171 }
172 
173 TEST_CASE( "LOGGING: CAPTURE", "[hide][tutorial]" ) {
174  int theAnswere = 42;
175  CAPTURE( theAnswere );
176 }
177 
178 //===========================================================================================
184 //BDD style testing
185 SCENARIO("BDD scenario", "Behavior-driven development scenario. [hide][tutorial][BDD]"){
186  GIVEN("we create a new std::vector"){
187  std::vector<int> vector;
188  int i = 1;
189  WHEN("we add a int to the vector"){
190  vector.push_back(1);
191  THEN("we can require that the vectors lenght is 1"){
192  REQUIRE( vector.size() == 1);
193  }
194  AND_WHEN("we add another int to the vector"){
195  vector.push_back(i);
196  THEN("we can require that the vectors lenght is 2"){
197  REQUIRE( vector.size() == 2);
198  }
199  }
200  THEN("we can require that the vectors lenght is still 1"){
201  REQUIRE( vector.size() == 1);
202  }
203  }
204  THEN("we can require that the vector is empty"){
205  REQUIRE(vector.empty());
206  }
207  }
208 }
209 
210 
211 
#define CHECK_THROWS_AS(expr, exceptionType)
Definition: catch.hpp:7798
SCENARIO("BDD scenario","Behavior-driven development scenario. [hide][tutorial][BDD]")
#define REQUIRE_FALSE(expr)
Definition: catch.hpp:7785
#define WHEN(desc)
Definition: catch.hpp:7841
#define CHECK_THAT(arg, matcher)
Definition: catch.hpp:7801
#define REQUIRE_THROWS_AS(expr, exceptionType)
Definition: catch.hpp:7788
#define FAIL(msg)
Definition: catch.hpp:7806
#define REQUIRE(expr)
Definition: catch.hpp:7784
#define SCOPED_INFO(msg)
Definition: catch.hpp:7808
#define CHECK_NOTHROW(expr)
Definition: catch.hpp:7799
Impl::StdString::Equals Equals(std::string const &str)
Definition: catch.hpp:3503
Impl::StdString::Contains Contains(std::string const &substr)
Definition: catch.hpp:3509
#define AND_WHEN(desc)
Definition: catch.hpp:7842
#define CAPTURE(msg)
Definition: catch.hpp:7809
Impl::StdString::StartsWith StartsWith(std::string const &substr)
Definition: catch.hpp:3515
#define WARN(msg)
Definition: catch.hpp:7805
#define GIVEN(desc)
Definition: catch.hpp:7840
#define REQUIRE_THROWS(expr)
Definition: catch.hpp:7787
#define THEN(desc)
Definition: catch.hpp:7843
TEST_CASE("TEST_CASE: Name and tags","[hide][tutorial]")
#define REQUIRE_NOTHROW(expr)
Definition: catch.hpp:7789
#define REQUIRE_THAT(arg, matcher)
Definition: catch.hpp:7802
#define INFO(msg)
Definition: catch.hpp:7804
Approx & epsilon(double newEpsilon)
Definition: catch.hpp:3279
Impl::StdString::EndsWith EndsWith(std::string const &substr)
Definition: catch.hpp:3521
#define CHECK_THROWS(expr)
Definition: catch.hpp:7797
#define CHECK(expr)
Definition: catch.hpp:7791
#define CHECK_FALSE(expr)
Definition: catch.hpp:7792