Skip to content

Commit a034041

Browse files
committed
---
yaml --- r: 6139 b: refs/heads/master c: 2c033f8 h: refs/heads/master i: 6137: 878c3e4 6135: 516de31 v: v3
1 parent 2b8454d commit a034041

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: af1ce1f3ded07833d8c5d5d455285fa4a5c800d3
2+
refs/heads/master: 2c033f83ef0819931566462642253fc89ee47189

trunk/doc/tutorial/syntax.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ exists, convert the result of the expression to the given type.
281281

282282
## Attributes
283283

284+
<a name="conditional></a>
285+
284286
Every definition can be annotated with attributes. Attributes are meta
285287
information that can serve a variety of purposes. One of those is
286288
conditional compilation:
@@ -289,14 +291,20 @@ conditional compilation:
289291
fn register_win_service() { /* ... */ }
290292

291293
This will cause the function to vanish without a trace during
292-
compilation on a non-Windows platform. Attributes always look like
293-
`#[attr]`, where `attr` can be simply a name (as in `#[test]`, which
294-
is used by the [built-in test framework](test.html)), a name followed
295-
by `=` and then a literal (as in `#[license = "BSD"]`, which is a
296-
valid way to annotate a Rust program as being released under a
297-
BSD-style license), or a name followed by a comma-separated list of
298-
nested attributes, as in the `cfg` example above, or in this
299-
[crate](mod.html) metadata declaration:
294+
compilation on a non-Windows platform, much like `#ifdef` in C (it
295+
allows `cfg(flag=value)` and `cfg(flag)` forms, where the second
296+
simply checks whether the configuration flag is defined at all). Flags
297+
for `target_os` and `target_arch` are set by the compiler. It is
298+
possible to set additional flags with the `--cfg` command-line option.
299+
300+
Attributes always look like `#[attr]`, where `attr` can be simply a
301+
name (as in `#[test]`, which is used by the [built-in test
302+
framework](test.html)), a name followed by `=` and then a literal (as
303+
in `#[license = "BSD"]`, which is a valid way to annotate a Rust
304+
program as being released under a BSD-style license), or a name
305+
followed by a comma-separated list of nested attributes, as in the
306+
`cfg` example above, or in this [crate](mod.html) metadata
307+
declaration:
300308

301309
#[link(name = "std",
302310
vers = "0.1",

trunk/doc/tutorial/test.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
11
# Testing
22

3-
FIXME to be written
3+
The Rust language has a facility for testing built into the language.
4+
Tests can be interspersed with other code, and annotated with the
5+
`#[test]` attribute.
6+
7+
use std;
8+
9+
fn twice(x: int) -> int { x + x }
10+
11+
#[test]
12+
fn test_twice() {
13+
let i = -100;
14+
while i < 100 {
15+
assert twice(i) == 2 * i;
16+
i += 1;
17+
}
18+
}
19+
20+
When you compile the program normally, the `test_twice` function will
21+
not be used. To actually run the tests, compile with the `--test`
22+
flag:
23+
24+
> rustc --lib twice.rs
25+
> ./twice
26+
running 1 tests
27+
test test_twice ... ok
28+
result: ok. 1 passed; 0 failed; 0 ignored
29+
30+
Or, if we change the file to fail, for example by replacing `x + x`
31+
with `x + 1`:
32+
33+
running 1 tests
34+
test test_twice ... FAILED
35+
failures:
36+
test_twice
37+
result: FAILED. 0 passed; 1 failed; 0 ignored
38+
39+
You can pass a command-line argument to a program compiled with
40+
`--test` to run only the tests whose name matches the given string. If
41+
we had, for example, test functions `test_twice`, `test_once_1`, and
42+
`test_once_2`, running our program with `./twice test_once` would run
43+
the latter two, and running it with `./twice test_once_2` would run
44+
only the last.
45+
46+
To indicate that a test is supposed to fail instead of pass, you can
47+
give it a `#[should_fail]` attribute.
48+
49+
use std;
50+
51+
fn divide(a: float, b: float) -> float {
52+
if b == 0f { fail; }
53+
a / b
54+
}
55+
56+
#[test]
57+
#[should_fail]
58+
fn divide_by_zero() { divide(1f, 0f); }
59+
60+
To disable a test completely, add an `#[ignore]` attribute. Running a
61+
test runner (the program compiled with `--test`) with an `--ignored`
62+
command-line flag will cause it to also run the tests labelled as
63+
ignored.
64+
65+
A program compiled as a test runner will have the configuration flag
66+
`test` defined, so that you can add code that won't be included in a
67+
normal compile with the `#[cfg(test)]` attribute (see [conditional
68+
compilation](syntax.md#conditional)).

0 commit comments

Comments
 (0)