Skip to content

Commit ea37a0c

Browse files
Merge branch 'devel' into docs_includes
2 parents 17c11e9 + 77f7c01 commit ea37a0c

27 files changed

+511
-223
lines changed

.github/workflows/mac-builds.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ on: [push, pull_request]
44

55
jobs:
66
build:
7-
runs-on: macos-12
7+
# macos-12 updated to a toolchain that crashes when linking the
8+
# test binary. This seems to be a known bug in that version,
9+
# and will eventually get fixed in an update. After that, we can go
10+
# back to newer macos images.
11+
runs-on: macos-11
812
strategy:
913
matrix:
1014
cxx:

CMake/CatchMiscFunctions.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
include(CheckCXXCompilerFlag)
1010
function(add_cxx_flag_if_supported_to_targets flagname targets)
11-
check_cxx_compiler_flag("${flagname}" HAVE_FLAG_${flagname})
11+
string(MAKE_C_IDENTIFIER ${flagname} flag_identifier )
12+
check_cxx_compiler_flag("${flagname}" HAVE_FLAG_${flag_identifier})
1213

13-
if (HAVE_FLAG_${flagname})
14+
if (HAVE_FLAG_${flag_identifier})
1415
foreach(target ${targets})
1516
target_compile_options(${target} PUBLIC ${flagname})
1617
endforeach()
@@ -112,9 +113,8 @@ endfunction()
112113
# Adds flags required for reproducible build to the target
113114
# Currently only supports GCC and Clang
114115
function(add_build_reproducibility_settings target)
115-
# Make the build reproducible on versions of g++ and clang that supports -ffile-prefix-map
116-
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND NOT ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 8) OR
117-
("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND NOT ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 10))
118-
target_compile_options(${target} PRIVATE "-ffile-prefix-map=${CATCH_DIR}=.")
116+
# Make the build reproducible on versions of g++ and clang that supports -ffile-prefix-map
117+
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
118+
add_cxx_flag_if_supported_to_targets("-ffile-prefix-map=${CATCH_DIR}/=" "${target}")
119119
endif()
120120
endfunction()

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3131
endif()
3232

3333
project(Catch2
34-
VERSION 3.1.0 # CML version placeholder, don't delete
34+
VERSION 3.1.1 # CML version placeholder, don't delete
3535
LANGUAGES CXX
3636
# HOMEPAGE_URL is not supported until CMake version 3.12, which
3737
# we do not target yet.

README.md

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,77 @@
1111
[![Join the chat in Discord: https://discord.gg/4CWS9zD](https://img.shields.io/badge/Discord-Chat!-brightgreen.svg)](https://discord.gg/4CWS9zD)
1212

1313

14-
## What's the Catch2?
14+
## What is Catch2?
1515

1616
Catch2 is mainly a unit testing framework for C++, but it also
1717
provides basic micro-benchmarking features, and simple BDD macros.
1818

1919
Catch2's main advantage is that using it is both simple and natural.
20-
Tests autoregister themselves and do not have to be named with valid
21-
identifiers, assertions look like normal C++ code, and sections provide
22-
a nice way to share set-up and tear-down code in tests.
20+
Test names do not have to be valid identifiers, assertions look like
21+
normal C++ boolean expressions, and sections provide a nice and local way
22+
to share set-up and tear-down code in tests.
2323

24+
**Example unit test**
25+
```cpp
26+
#include <catch2/catch_test_macros.hpp>
2427

25-
## Catch2 v3 is being developed!
28+
#include <cstdint>
2629

27-
You are on the `devel` branch, where the next major version, v3, of
28-
Catch2 is being developed. As it is a significant rework, you will
29-
find that parts of this documentation are likely still stuck on v2.
30+
uint32_t factorial( uint32_t number ) {
31+
return number <= 1 ? number : factorial(number-1) * number;
32+
}
3033

31-
For stable (and documentation-matching) version of Catch2, [go to the
32-
`v2.x` branch](https://github.com/catchorg/Catch2/tree/v2.x).
34+
TEST_CASE( "Factorials are computed", "[factorial]" ) {
35+
REQUIRE( factorial( 1) == 1 );
36+
REQUIRE( factorial( 2) == 2 );
37+
REQUIRE( factorial( 3) == 6 );
38+
REQUIRE( factorial(10) == 3'628'800 );
39+
}
40+
```
41+
42+
**Example microbenchmark**
43+
```cpp
44+
#include <catch2/benchmark/catch_benchmark.hpp>
45+
46+
#include <cstdint>
47+
48+
uint64_t fibonacci(uint64_t number) {
49+
return number < 2 ? 1 : fibonacci(number - 1) + fibonacci(number - 2);
50+
}
51+
52+
TEST_CASE("Benchmark Fibonacci", "[!benchmark]") {
53+
REQUIRE(Fibonacci(5) == 5);
54+
55+
REQUIRE(Fibonacci(20) == 6'765);
56+
BENCHMARK("Fibonacci 20") {
57+
return Fibonacci(20);
58+
};
59+
60+
REQUIRE(Fibonacci(25) == 75'025);
61+
BENCHMARK("Fibonacci 25") {
62+
return Fibonacci(25);
63+
};
64+
}
65+
```
66+
67+
## Catch2 v3 has been released!
68+
69+
You are on the `devel` branch, where the v3 version is being developed.
70+
v3 brings a bunch of significant changes, the big one being that Catch2
71+
is no longer a single-header library. Catch2 now behaves as a normal
72+
library, with multiple headers and separately compiled implementation.
73+
74+
The documentation is slowly being updated to take these changes into
75+
account, but this work is currently still ongoing.
3376

3477
For migrating from the v2 releases to v3, you should look at [our
3578
documentation](docs/migrate-v2-to-v3.md#top). It provides a simple
3679
guidelines on getting started, and collects most common migration
3780
problems.
3881

82+
For the previous major version of Catch2 [look into the `v2.x` branch
83+
here on GitHub](https://github.com/catchorg/Catch2/tree/v2.x).
84+
3985

4086
## How to use it
4187
This documentation comprises these three parts:

docs/Readme.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@
44
To get the most out of Catch2, start with the [tutorial](tutorial.md#top).
55
Once you're up and running consider the following reference material.
66

7-
Writing tests:
7+
**Writing tests:**
88
* [Assertion macros](assertions.md#top)
9-
* [Matchers](matchers.md#top)
9+
* [Matchers (asserting complex properties)](matchers.md#top)
10+
* [Comparing floating point numbers](comparing-floating-point-numbers.md#top)
1011
* [Logging macros](logging.md#top)
1112
* [Test cases and sections](test-cases-and-sections.md#top)
1213
* [Test fixtures](test-fixtures.md#top)
13-
* [Reporters](reporters.md#top)
14+
* [Reporters (output customization)](reporters.md#top)
1415
* [Event Listeners](event-listeners.md#top)
15-
* [Data Generators](generators.md#top)
16+
* [Data Generators (value parameterized tests)](generators.md#top)
1617
* [Other macros](other-macros.md#top)
1718
* [Micro benchmarking](benchmarks.md#top)
1819

19-
Fine tuning:
20+
**Fine tuning:**
2021
* [Supplying your own main()](own-main.md#top)
2122
* [Compile-time configuration](configuration.md#top)
2223
* [String Conversions](tostring.md#top)
2324

24-
Running:
25+
**Running:**
2526
* [Command line](command-line.md#top)
2627

27-
Odds and ends:
28+
**Odds and ends:**
2829
* [Frequently Asked Questions (FAQ)](faq.md#top)
2930
* [Best practices and other tips](usage-tips.md#top)
3031
* [CMake integration](cmake-integration.md#top)
3132
* [CI and other miscellaneous pieces](ci-and-misc.md#top)
3233
* [Known limitations](limitations.md#top)
33-
34-
Other:
34+
35+
**Other:**
3536
* [Why Catch2?](why-catch.md#top)
3637
* [Migrating from v2 to v3](migrate-v2-to-v3.md#top)
3738
* [Open Source Projects using Catch2](opensource-users.md#top)

docs/assertions.md

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
**Contents**<br>
55
[Natural Expressions](#natural-expressions)<br>
6+
[Floating point comparisons](#floating-point-comparisons)<br>
67
[Exceptions](#exceptions)<br>
78
[Matcher expressions](#matcher-expressions)<br>
89
[Thread Safety](#thread-safety)<br>
@@ -22,7 +23,7 @@ Most of these macros come in two forms:
2223
The ```REQUIRE``` family of macros tests an expression and aborts the test case if it fails.
2324
The ```CHECK``` family are equivalent but execution continues in the same test case even if the assertion fails. This is useful if you have a series of essentially orthogonal assertions and it is useful to see all the results rather than stopping at the first failure.
2425

25-
* **REQUIRE(** _expression_ **)** and
26+
* **REQUIRE(** _expression_ **)** and
2627
* **CHECK(** _expression_ **)**
2728

2829
Evaluates the expression and records the result. If an exception is thrown, it is caught, reported, and counted as a failure. These are the macros you will use most of the time.
@@ -34,101 +35,82 @@ CHECK( thisReturnsTrue() );
3435
REQUIRE( i == 42 );
3536
```
3637

37-
* **REQUIRE_FALSE(** _expression_ **)** and
38+
Expressions prefixed with `!` cannot be decomposed. If you have a type
39+
that is convertible to bool and you want to assert that it evaluates to
40+
false, use the two forms below:
41+
42+
43+
* **REQUIRE_FALSE(** _expression_ **)** and
3844
* **CHECK_FALSE(** _expression_ **)**
3945

40-
Evaluates the expression and records the _logical NOT_ of the result. If an exception is thrown it is caught, reported, and counted as a failure.
41-
(these forms exist as a workaround for the fact that ! prefixed expressions cannot be decomposed).
46+
Note that there is no reason to use these forms for plain bool variables,
47+
because there is no added value in decomposing them.
4248

4349
Example:
50+
```cpp
51+
Status ret = someFunction();
52+
REQUIRE_FALSE(ret); // ret must evaluate to false, and Catch2 will print
53+
// out the value of ret if possibly
4454
```
45-
REQUIRE_FALSE( thisReturnsFalse() );
46-
```
47-
48-
Do note that "overly complex" expressions cannot be decomposed and thus will not compile. This is done partly for practical reasons (to keep the underlying expression template machinery to minimum) and partly for philosophical reasons (assertions should be simple and deterministic).
49-
50-
Examples:
51-
* `CHECK(a == 1 && b == 2);`
52-
This expression is too complex because of the `&&` operator. If you want to check that 2 or more properties hold, you can either put the expression into parenthesis, which stops decomposition from working, or you need to decompose the expression into two assertions: `CHECK( a == 1 ); CHECK( b == 2);`
53-
* `CHECK( a == 2 || b == 1 );`
54-
This expression is too complex because of the `||` operator. If you want to check that one of several properties hold, you can put the expression into parenthesis (unlike with `&&`, expression decomposition into several `CHECK`s is not possible).
55-
5655
5756
### Floating point comparisons
5857
```cpp
5958
#include <catch2/catch_approx.hpp>
6059
```
6160

62-
When comparing floating point numbers - especially if at least one of them has been computed - great care must be taken to allow for rounding errors and inexact representations.
61+
### Other limitations
6362

64-
Catch provides a way to perform tolerant comparisons of floating point values through use of a wrapper class called `Approx`. `Approx` can be used on either side of a comparison expression. It overloads the comparisons operators to take a tolerance into account. Here's a simple example:
63+
Note that expressions containing either of the binary logical operators,
64+
`&&` or `||`, cannot be decomposed and will not compile. The reason behind
65+
this is that it is impossible to overload `&&` and `||` in a way that
66+
keeps their short-circuiting semantics, and expression decomposition
67+
relies on overloaded operators to work.
6568

66-
```cpp
67-
REQUIRE( performComputation() == Approx( 2.1 ) );
68-
```
69+
Simple example of an issue with overloading binary logical operators
70+
is a common pointer idiom, `p && p->foo == 2`. Using the built-in `&&`
71+
operator, `p` is only dereferenced if it is not null. With overloaded
72+
`&&`, `p` is always dereferenced, thus causing a segfault if
73+
`p == nullptr`.
6974

70-
Catch also provides a user-defined literal for `Approx`; `_a`. It resides in
71-
the `Catch::literals` namespace and can be used like so:
72-
```cpp
73-
using namespace Catch::literals;
74-
REQUIRE( performComputation() == 2.1_a );
75-
```
75+
If you want to test expression that contains `&&` or `||`, you have two
76+
options.
7677

77-
`Approx` is constructed with defaults that should cover most simple cases.
78-
For the more complex cases, `Approx` provides 3 customization points:
78+
1) Enclose it in parentheses. Parentheses force evaluation of the expression
79+
before the expression decomposition can touch it, and thus it cannot
80+
be used.
7981

80-
* __epsilon__ - epsilon serves to set the coefficient by which a result
81-
can differ from `Approx`'s value before it is rejected.
82-
_By default set to `std::numeric_limits<float>::epsilon()*100`._
83-
* __margin__ - margin serves to set the the absolute value by which
84-
a result can differ from `Approx`'s value before it is rejected.
85-
_By default set to `0.0`._
86-
* __scale__ - scale is used to change the magnitude of `Approx` for relative check.
87-
_By default set to `0.0`._
82+
2) Rewrite the expression. `REQUIRE(a == 1 && b == 2)` can always be split
83+
into `REQUIRE(a == 1); REQUIRE(b == 2);`. Alternatively, if this is a
84+
common pattern in your tests, think about using [Matchers](#matcher-expressions).
85+
instead. There is no simple rewrite rule for `||`, but I generally
86+
believe that if you have `||` in your test expression, you should rethink
87+
your tests.
8888

89-
#### epsilon example
90-
```cpp
91-
Approx target = Approx(100).epsilon(0.01);
92-
100.0 == target; // Obviously true
93-
200.0 == target; // Obviously still false
94-
100.5 == target; // True, because we set target to allow up to 1% difference
95-
```
9689

97-
#### margin example
98-
```cpp
99-
Approx target = Approx(100).margin(5);
100-
100.0 == target; // Obviously true
101-
200.0 == target; // Obviously still false
102-
104.0 == target; // True, because we set target to allow absolute difference of at most 5
103-
```
90+
## Floating point comparisons
10491

105-
#### scale
106-
Scale can be useful if the computation leading to the result worked
107-
on different scale than is used by the results. Since allowed difference
108-
between Approx's value and compared value is based primarily on Approx's value
109-
(the allowed difference is computed as
110-
`(Approx::scale + Approx::value) * epsilon`), the resulting comparison could
111-
need rescaling to be correct.
92+
Comparing floating point numbers is complex, and [so it has its own
93+
documentation page](comparing-floating-point-numbers.md#top).
11294

11395

11496
## Exceptions
11597

116-
* **REQUIRE_NOTHROW(** _expression_ **)** and
98+
* **REQUIRE_NOTHROW(** _expression_ **)** and
11799
* **CHECK_NOTHROW(** _expression_ **)**
118100

119101
Expects that no exception is thrown during evaluation of the expression.
120102

121-
* **REQUIRE_THROWS(** _expression_ **)** and
103+
* **REQUIRE_THROWS(** _expression_ **)** and
122104
* **CHECK_THROWS(** _expression_ **)**
123105

124106
Expects that an exception (of any type) is be thrown during evaluation of the expression.
125107

126-
* **REQUIRE_THROWS_AS(** _expression_, _exception type_ **)** and
108+
* **REQUIRE_THROWS_AS(** _expression_, _exception type_ **)** and
127109
* **CHECK_THROWS_AS(** _expression_, _exception type_ **)**
128110

129111
Expects that an exception of the _specified type_ is thrown during evaluation of the expression. Note that the _exception type_ is extended with `const&` and you should not include it yourself.
130112

131-
* **REQUIRE_THROWS_WITH(** _expression_, _string or string matcher_ **)** and
113+
* **REQUIRE_THROWS_WITH(** _expression_, _string or string matcher_ **)** and
132114
* **CHECK_THROWS_WITH(** _expression_, _string or string matcher_ **)**
133115
```cpp
134116
#include <catch2/matchers/catch_matchers.hpp>
@@ -166,8 +148,8 @@ REQUIRE_NOTHROW([&](){
166148

167149
To support Matchers a slightly different form is used. Matchers have [their own documentation](matchers.md#top).
168150

169-
* **REQUIRE_THAT(** _lhs_, _matcher expression_ **)** and
170-
* **CHECK_THAT(** _lhs_, _matcher expression_ **)**
151+
* **REQUIRE_THAT(** _lhs_, _matcher expression_ **)** and
152+
* **CHECK_THAT(** _lhs_, _matcher expression_ **)**
171153

172154
Matchers can be composed using `&&`, `||` and `!` operators.
173155

docs/ci-and-misc.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<a id="top"></a>
22
# CI and other odd pieces
33

4-
This page talks about how Catch integrates with Continuous Integration
4+
This page talks about how Catch integrates with Continuous Integration
55
Build Systems may refer to low-level tools, like CMake, or larger systems that run on servers, like Jenkins or TeamCity. This page will talk about both.
66

77
## Continuous Integration systems
@@ -11,9 +11,9 @@ Probably the most important aspect to using Catch with a build server is the use
1111
Two of these reporters are built in (XML and JUnit) and the third (TeamCity) is included as a separate header. It's possible that the other two may be split out in the future too - as that would make the core of Catch smaller for those that don't need them.
1212

1313
### XML Reporter
14-
```-r xml```
14+
```-r xml```
1515

16-
The XML Reporter writes in an XML format that is specific to Catch.
16+
The XML Reporter writes in an XML format that is specific to Catch.
1717

1818
The advantage of this format is that it corresponds well to the way Catch works (especially the more unusual features, such as nested sections) and is a fully streaming format - that is it writes output as it goes, without having to store up all its results before it can start writing.
1919

docs/command-line.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ starting at 0. The tests in the set given by
548548
`--shard-index <#shard index to run>` will be executed. The default shard
549549
count is `1`, and the default index to run is `0`.
550550

551-
_It is an error to specify a shard index greater than the number of shards._
551+
_Shard index must be less than number of shards. As the name suggests,
552+
it is treated as an index of the shard to run._
552553

553554
Sharding is useful when you want to split test execution across multiple
554555
processes, as is done with the [Bazel test sharding](https://docs.bazel.build/versions/main/test-encyclopedia.html#test-sharding).

0 commit comments

Comments
 (0)