Skip to content

Commit 75ce34c

Browse files
committed
Update the fenced code for the C++ tutorial.
1 parent 2c0c8f0 commit 75ce34c

File tree

1 file changed

+79
-77
lines changed

1 file changed

+79
-77
lines changed

testing-with-tap/c-plus-plus.md

Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ title: Testing with C++
99

1010
### SYNOPSIS
1111

12-
```
13-
#include <tap++/tap++.h>
14-
#include <string>
12+
```cpp
13+
#include <tap++/tap++.h>
14+
#include <string>
1515

16-
using namespace TAP;
16+
using namespace TAP;
1717

18-
int foo() {
19-
return 1;
20-
}
18+
int foo() {
19+
return 1;
20+
}
2121

22-
std::string bar() {
23-
return "a string";
24-
}
22+
std::string bar() {
23+
return "a string";
24+
}
2525

26-
int main() {
27-
plan(3);
28-
ok(true, "This test passes");
29-
is(foo(), 1, "foo() should be 1");
30-
is(bar(), "a string", "bar() should be \"a string\"");
31-
return exit_status();
32-
}
26+
int main() {
27+
plan(3);
28+
ok(true, "This test passes");
29+
is(foo(), 1, "foo() should be 1");
30+
is(bar(), "a string", "bar() should be \"a string\"");
31+
return exit_status();
32+
}
3333
```
3434

3535
Remember to link with libtap++. For example: `g++ -ltap++ tap-synopsis.o -o tap-synopsis`
@@ -52,10 +52,10 @@ Before anything else, you need a testing plan. This basically declares how many
5252

5353
#### plan()
5454

55-
```
56-
void plan(int number_of_tests);
57-
void plan(skip_all, const std::string& reason="");
58-
void plan(no_plan);
55+
```cpp
56+
void plan(int number_of_tests);
57+
void plan(skip_all, const std::string& reason="");
58+
void plan(no_plan);
5959
```
6060
6161
The function plan is used to indicate the plan of your test run. Usually you will just give it the number of tests as argument.
@@ -64,9 +64,9 @@ Alternatively, you can give it the skip_all or no_plan constants as arguments. T
6464
6565
#### done_testing()
6666
67-
```
68-
void done_testing();
69-
void done_testing(int number_of_tests);
67+
```cpp
68+
void done_testing();
69+
void done_testing(int number_of_tests);
7070
```
7171

7272
If you don't know how many tests you're going to run, you can issue the plan when you're done running tests.
@@ -78,17 +78,17 @@ This is safer than and replaces the "no_plan" plan.
7878
By convention, each test is assigned a number in order. This is largely done automatically for you. However, it's often very useful to assign a name to each test. Which would you rather see:
7979

8080
```
81-
ok 4
82-
not ok 5
83-
ok 6
81+
ok 4
82+
not ok 5
83+
ok 6
8484
```
8585

8686
or
8787

8888
```
89-
ok 4 - basic multi-variable
90-
not ok 5 - simple exponential
91-
ok 6 - force == mass * acceleration
89+
ok 4 - basic multi-variable
90+
not ok 5 - simple exponential
91+
ok 6 - force == mass * acceleration
9292
```
9393

9494
The later gives you some idea of what failed. It also makes it easier to find the test in your script, simply search for "simple exponential".
@@ -101,59 +101,61 @@ All of the following print "ok" or "not ok" depending on if the test succeeded o
101101

102102
#### ok()
103103

104-
```
105-
bool ok(bool condition, const std::string& test_name = "");
104+
```cpp
105+
bool ok(bool condition, const std::string& test_name = "");
106106
```
107107
108108
ok is the basic test expression in TAP. It simply evaluates any expression, for example, got == expected, taking a true value to mean that the test passed and a false value to mean that the test failed.
109109
test_name is a very short description of the test that will be printed out. It makes it very easy to find a test in your script when it fails and gives others an idea of your intentions. test_name is optional, but we very strongly encourage its use.
110110
111-
####is(); isnt()
111+
#### is(); isnt()
112112
113-
```
114-
template<typename T, typename U> bool is(const T& got, const U& expected, std::string& test_name = "");
115-
template<typename T, typename U> bool isnt(const T& got, const U& expected, std::string& test_name = "");
113+
```cpp
114+
template<typename T, typename U> bool is(
115+
const T& got, const U& expected, std::string& test_name = "");
116+
template<typename T, typename U> bool isnt(
117+
const T& got, const U& expected, std::string& test_name = "");
116118
```
117119

118120
Similar to ok(), is() and isnt() compare their two arguments with == and != respectively and use the result of that to determine if the test succeeded or failed. So these:
119121

120-
```
121-
# Is the ultimate answer 42?
122-
is( ultimate_answer(), 42, "Meaning of Life" );
122+
```cpp
123+
# Is the ultimate answer 42?
124+
is(ultimate_answer(), 42, "Meaning of Life");
123125

124-
# foo isn't empty
125-
isnt( foo, "", "Got some foo" );
126+
# foo isn't empty
127+
isnt(foo, "", "Got some foo");
126128
```
127129
128130
are similar to these:
129131
130-
```
131-
ok( ultimate_answer() == 42, "Meaning of Life" );
132-
ok( foo != "", "Got some foo" );
132+
```cpp
133+
ok(ultimate_answer() == 42, "Meaning of Life");
134+
ok(foo != "", "Got some foo");
133135
```
134136

135137
(Mnemonic: "This is that." "This isn't that.")
136138
So why use these? They produce better diagnostics on failure. ok() cannot know what you are testing for (beyond the name), but is() and isnt() know what the test was and why it failed. For example this test:
137139

138-
```
139-
std::string foo("waffle"), bar("yarblokos");
140-
is( foo, bar, 'Is foo the same as bar?' );
140+
```cpp
141+
std::string foo("waffle"), bar("yarblokos");
142+
is(foo, bar, "Is foo the same as bar?");
141143
```
142144
143145
Will produce something like this:
144146
145147
```
146-
not ok 17 - Is foo the same as bar?
147-
# Failed test 'Is foo the same as bar?'
148-
# got: 'waffle'
149-
# expected: 'yarblokos'
148+
not ok 17 - Is foo the same as bar?
149+
# Failed test 'Is foo the same as bar?'
150+
# got: 'waffle'
151+
# expected: 'yarblokos'
150152
```
151153
152154
#### pass(); fail()
153155
154-
```
155-
bool pass(const std::string& test_name = "");
156-
bool fail(const std::string& test_name = "");
156+
```cpp
157+
bool pass(const std::string& test_name = "");
158+
bool fail(const std::string& test_name = "");
157159
```
158160

159161
Sometimes you just want to say that the tests have passed. Usually the case is you've got some complicated condition that is difficult to wedge into an ok(). In this case, you can simply use pass() (to declare the test ok) or fail (for not ok). They are synonyms for ok(true, test_name) and ok(false, test_name).
@@ -163,8 +165,8 @@ Use these very, very, very sparingly.
163165

164166
#### skip()
165167

166-
```
167-
void skip(int number, const std::string& reason = "");
168+
```cpp
169+
void skip(int number, const std::string& reason = "");
168170
```
169171
170172
skip tells the TAP harness that you're skipping a number of tests for the given reason. Note that you have to do the skipping yourself.
@@ -175,25 +177,25 @@ If you pick the right test function, you'll usually get a good idea of what went
175177
176178
#### diag
177179
178-
```
179-
diag(diagnostic_message...);
180+
```cpp
181+
diag(diagnostic_message...);
180182
```
181183

182184
Prints a diagnostic message which is guaranteed not to interfere with test output. The arguments are simply concatenated together.
183185
Returns false, so as to preserve failure.
184186
Handy for this sort of thing:
185187

186-
```
187-
ok( has_user("foo"), "There's a foo user" ) or
188-
diag("Since there's no foo, check that /etc/bar is set up right");
188+
```cpp
189+
ok(has_user("foo"), "There's a foo user") or
190+
diag("Since there's no foo, check that /etc/bar is set up right");
189191
```
190192
191193
which would produce:
192194
193195
```
194-
not ok 42 - There's a foo user
195-
# Failed test 'There's a foo user'
196-
# Since there's no foo, check that /etc/bar is set up right.
196+
not ok 42 - There's a foo user
197+
# Failed test 'There's a foo user'
198+
# Since there's no foo, check that /etc/bar is set up right.
197199
```
198200
199201
You might remember ok() or diag() with the mnemonic open() or die().
@@ -202,24 +204,24 @@ You might remember ok() or diag() with the mnemonic open() or die().
202204
203205
#### note
204206
205-
```
206-
note(diagnostic_message...);
207+
```cpp
208+
note(diagnostic_message...);
207209
```
208210

209211
Like diag(), except the message will not be seen when the test is run in a harness. It will only be visible in the verbose TAP stream.
210212
Handy for putting in notes which might be useful for debugging, but don't indicate a problem.
211213

212-
```
213-
note("Tempfile is ", tempfile);
214+
```cpp
215+
note("Tempfile is ", tempfile);
214216
```
215217
216218
diag simply catenates its arguments to the error output, while note prints diagnostics to the TAP stream.
217219
218220
#### set_output(); set_error()
219221
220-
```
221-
void set_output(std::ofstream& new_output);
222-
void set_error(std::ofstream& new_error);
222+
```cpp
223+
void set_output(std::ofstream& new_output);
224+
void set_error(std::ofstream& new_error);
223225
```
224226

225227
These set the filehandle of the TAP stream and the error stream. They default to std::cout and std::cerr, respectively. These can only be set before any output is written to them.
@@ -232,18 +234,18 @@ If all your tests passed, Test::Builder will exit with zero (which is normal). I
232234
So the exit codes are...
233235

234236
```
235-
0 all tests successful
236-
255 test died or all passed but wrong # of tests run
237-
any other number how many failed (including missing or extras)
237+
0 all tests successful
238+
255 test died or all passed but wrong # of tests run
239+
any other number how many failed (including missing or extras)
238240
```
239241

240242
If you fail more than 254 tests, it will be reported as 254.
241243

242244
#### bail_out()
243245

244-
```
245-
int exit_status();
246-
void bail_out(const std::string& reason);
246+
```cpp
247+
int exit_status();
248+
void bail_out(const std::string& reason);
247249
```
248250
249251
**bail_out** terminates the current test program with exit code 255, indicating to the test harness that all subsequent testing should halt. Typically this is used to indicate that testing cannot continue at all.

0 commit comments

Comments
 (0)