Skip to content

Commit af86249

Browse files
Hard wrap tutorial doc for easier raw Markdown reading
Hard wrap text on tutorial doc to 72 column limit, to make it easier to be read the raw Mardown from text editor. Also, make consistent the usage of two spaces after period.
1 parent 6e79e68 commit af86249

File tree

1 file changed

+74
-58
lines changed

1 file changed

+74
-58
lines changed

docs/tutorial.md

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212

1313
## Getting Catch2
1414

15-
Ideally you should be using Catch2 through its [CMake integration](cmake-integration.md#top).
16-
Catch2 also provides pkg-config files and two file (header + cpp)
17-
distribution, but this documentation will assume you are using CMake. If
18-
you are using the two file distribution instead, remember to replace
19-
the included header with `catch_amalgamated.hpp`.
15+
Ideally you should be using Catch2 through its [CMake
16+
integration](cmake-integration.md#top). Catch2 also provides pkg-config
17+
files and two file (header + cpp) distribution, but this documentation
18+
will assume you are using CMake. If you are using the two file
19+
distribution instead, remember to replace the included header with
20+
`catch_amalgamated.hpp`.
2021

2122

2223
## Writing tests
2324

24-
Let's start with a really simple example ([code](../examples/010-TestCase.cpp)). Say you have written a function to calculate factorials and now you want to test it (let's leave aside TDD for now).
25+
Let's start with a really simple example
26+
([code](../examples/010-TestCase.cpp)). Say you have written a function
27+
to calculate factorials and now you want to test it (let's leave aside
28+
TDD for now).
2529

2630
```c++
2731
unsigned int Factorial( unsigned int number ) {
@@ -44,7 +48,12 @@ TEST_CASE( "Factorials are computed", "[factorial]" ) {
4448
}
4549
```
4650

47-
This will compile to a complete executable which responds to [command line arguments](command-line.md#top). If you just run it with no arguments it will execute all test cases (in this case there is just one), report any failures, report a summary of how many tests passed and failed and return the number of failed tests (useful for if you just want a yes/ no answer to: "did it work").
51+
This will compile to a complete executable which responds to [command
52+
line arguments](command-line.md#top). If you just run it with no
53+
arguments it will execute all test cases (in this case there is just
54+
one), report any failures, report a summary of how many tests passed and
55+
failed and return the number of failed tests (useful for if you just
56+
want a yes/ no answer to: "did it work").
4857

4958
Anyway, as the tests above as written will pass, but there is a bug.
5059
The problem is that `Factorial(0)` should return 1 (due to [its
@@ -61,8 +70,8 @@ TEST_CASE( "Factorials are computed", "[factorial]" ) {
6170
}
6271
```
6372
64-
After another compile & run cycle, we will see a test failure. The output
65-
will look something like:
73+
After another compile & run cycle, we will see a test failure. The
74+
output will look something like:
6675
6776
```
6877
Example.cpp:9: FAILED:
@@ -71,11 +80,12 @@ with expansion:
7180
0 == 1
7281
```
7382
74-
Note that the output contains both the original expression,
75-
`REQUIRE( Factorial(0) == 1 )` and the actual value returned by the call
76-
to the `Factorial` function: `0`.
83+
Note that the output contains both the original expression, `REQUIRE(
84+
Factorial(0) == 1 )` and the actual value returned by the call to the
85+
`Factorial` function: `0`.
7786
7887
We can fix this bug by slightly modifying the `Factorial` function to:
88+
7989
```c++
8090
unsigned int Factorial( unsigned int number ) {
8191
return number > 1 ? Factorial(number-1)*number : 1;
@@ -86,36 +96,38 @@ unsigned int Factorial( unsigned int number ) {
8696
### What did we do here?
8797

8898
Although this was a simple test it's been enough to demonstrate a few
89-
things about how Catch2 is used. Let's take a moment to consider those
99+
things about how Catch2 is used. Let's take a moment to consider those
90100
before we move on.
91101

92-
* We introduce test cases with the `TEST_CASE` macro. This macro takes
102+
* We introduce test cases with the `TEST_CASE` macro. This macro takes
93103
one or two string arguments - a free form test name and, optionally,
94-
one or more tags (for more see [Test cases and Sections](#test-cases-and-sections)).
104+
one or more tags (for more see [Test cases and
105+
Sections](#test-cases-and-sections)).
95106
* The test automatically self-registers with the test runner, and user
96-
does not have do anything more to ensure that it is picked up by the test
97-
framework. _Note that you can run specific test, or set of tests,
98-
through the [command line](command-line.md#top)._
107+
does not have do anything more to ensure that it is picked up by the
108+
test framework. _Note that you can run specific test, or set of
109+
tests, through the [command line](command-line.md#top)._
99110
* The individual test assertions are written using the `REQUIRE` macro.
100111
It accepts a boolean expression, and uses expression templates to
101-
internally decompose it, so that it can be individually stringified
102-
on test failure.
112+
internally decompose it, so that it can be individually stringified on
113+
test failure.
103114

104115
On the last point, note that there are more testing macros available,
105116
because not all useful checks can be expressed as a simple boolean
106-
expression. As an example, checking that an expression throws an exception
107-
is done with the `REQUIRE_THROWS` macro. More on that later.
117+
expression. As an example, checking that an expression throws an
118+
exception is done with the `REQUIRE_THROWS` macro. More on that later.
108119

109120

110121
## Test cases and sections
111122

112-
Like most test frameworks, Catch2 supports a class-based fixture mechanism,
113-
where individual tests are methods on class and setup/teardown can be
114-
done in constructor/destructor of the type.
123+
Like most test frameworks, Catch2 supports a class-based fixture
124+
mechanism, where individual tests are methods on class and
125+
setup/teardown can be done in constructor/destructor of the type.
115126

116127
However, their use in Catch2 is rare, because idiomatic Catch2 tests
117-
instead use _sections_ to share setup and teardown code between test code.
118-
This is best explained through an example ([code](../examples/100-Fix-Section.cpp)):
128+
instead use _sections_ to share setup and teardown code between test
129+
code. This is best explained through an example
130+
([code](../examples/100-Fix-Section.cpp)):
119131

120132
```c++
121133
TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
@@ -152,17 +164,18 @@ TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
152164
}
153165
```
154166
155-
For each `SECTION` the `TEST_CASE` is executed from the start. This means
156-
that each section is entered with a freshly constructed vector `v`, that
157-
we know has size 5 and capacity at least 5, because the two assertions
158-
are also checked before the section is entered. Each run through a test
159-
case will execute one, and only one, leaf section.
167+
For each `SECTION` the `TEST_CASE` is executed from the start. This
168+
means that each section is entered with a freshly constructed vector
169+
`v`, that we know has size 5 and capacity at least 5, because the two
170+
assertions are also checked before the section is entered. Each run
171+
through a test case will execute one, and only one, leaf section.
160172
161173
Section can also be nested, in which case the parent section can be
162-
entered multiple times, once for each leaf section. Nested sections are
174+
entered multiple times, once for each leaf section. Nested sections are
163175
most useful when you have multiple tests that share part of the set up.
164176
To continue on the vector example above, you could add a check that
165-
`std::vector::reserve` does not remove unused excess capacity, like this:
177+
`std::vector::reserve` does not remove unused excess capacity, like
178+
this:
166179
167180
```cpp
168181
SECTION( "reserving bigger changes capacity but not size" ) {
@@ -179,47 +192,50 @@ To continue on the vector example above, you could add a check that
179192
```
180193

181194
Another way to look at sections is that they are a way to define a tree
182-
of paths through the test. Each section represents a node, and the final
183-
tree is walked in depth-first manner, with each path only visiting only
184-
one leaf node.
195+
of paths through the test. Each section represents a node, and the
196+
final tree is walked in depth-first manner, with each path only visiting
197+
only one leaf node.
185198

186-
There is no practical limit on nesting sections, as long as your compiler
187-
can handle them, but keep in mind that overly nested sections can become
188-
unreadable. From experience, having section nest more than 3 levels is
189-
usually very hard to follow and not worth the removed duplication.
199+
There is no practical limit on nesting sections, as long as your
200+
compiler can handle them, but keep in mind that overly nested sections
201+
can become unreadable. From experience, having section nest more than 3
202+
levels is usually very hard to follow and not worth the removed
203+
duplication.
190204

191205

192206
## BDD style testing
193207

194-
Catch2 also provides some basic support for BDD-style testing. There are
195-
macro aliases for `TEST_CASE` and `SECTIONS` that you can use so that
196-
the resulting tests read as BDD spec. `SCENARIO` acts as a `TEST_CASE`
197-
with "Scenario: " name prefix. Then there are `GIVEN`, `WHEN`, `THEN`
198-
(and their variants with `AND_` prefix), which act as a `SECTION`,
199-
similarly prefixed with the macro name.
208+
Catch2 also provides some basic support for BDD-style testing. There
209+
are macro aliases for `TEST_CASE` and `SECTIONS` that you can use so
210+
that the resulting tests read as BDD spec. `SCENARIO` acts as a
211+
`TEST_CASE` with "Scenario: " name prefix. Then there are `GIVEN`,
212+
`WHEN`, `THEN` (and their variants with `AND_` prefix), which act as a
213+
`SECTION`, similarly prefixed with the macro name.
200214

201215
For more details on the macros look at the [test cases and
202-
sections](test-cases-and-sections.md#top) part of the reference docs,
203-
or at the [vector example done with BDD macros](../examples/120-Bdd-ScenarioGivenWhenThen.cpp).
216+
sections](test-cases-and-sections.md#top) part of the reference docs, or
217+
at the [vector example done with BDD
218+
macros](../examples/120-Bdd-ScenarioGivenWhenThen.cpp).
204219

205220

206221
## Data and Type driven tests
207222

208-
Test cases in Catch2 can also be driven by types, input data, or both
209-
at the same time.
223+
Test cases in Catch2 can also be driven by types, input data, or both at
224+
the same time.
210225

211-
For more details look into the Catch2 reference, either at the
212-
[type parametrized test cases](test-cases-and-sections.md#type-parametrised-test-cases),
213-
or [data generators](generators.md#top).
226+
For more details look into the Catch2 reference, either at the [type
227+
parametrized test
228+
cases](test-cases-and-sections.md#type-parametrised-test-cases), or
229+
[data generators](generators.md#top).
214230

215231

216232
## Next steps
217233

218234
This page is a brief introduction to get you up and running with Catch2,
219-
and to show the basic features of Catch2. The features mentioned here
220-
can get you quite far, but there are many more. However, you can read
221-
about these as you go, in the ever-growing [reference section](Readme.md#top)
222-
of the documentation.
235+
and to show the basic features of Catch2. The features mentioned here
236+
can get you quite far, but there are many more. However, you can read
237+
about these as you go, in the ever-growing [reference
238+
section](Readme.md#top) of the documentation.
223239

224240

225241
---

0 commit comments

Comments
 (0)