diff --git a/docs/ch03-05-control-flow.html b/docs/ch03-05-control-flow.html
index a9c724f..5ae5344 100644
--- a/docs/ch03-05-control-flow.html
+++ b/docs/ch03-05-control-flow.html
@@ -284,8 +284,7 @@ the value is: 50
#[test]
+fn greeting_contains_name() {
+ let result = greeting("Carol");
+ assert!(
+ result.contains("Carol"),
+ "Greeting did not contain name, value was `{}`", result
+ );
+}
---- tests::greeting_contains_name stdout ----
+ thread 'tests::greeting_contains_name' panicked at 'Result did not contain
+ name, value was `Hello`', src/lib.rs:12
+note: Run with `RUST_BACKTRACE=1` for a backtrace.
+
#[test]
-fn a_simple_case() {
- let result = "hello"; // this value would come from running your code
- assert_eq!(result, "world", "greeting operation failed");
+
Listing 11-8: Testing that a condition will cause a
+panic!
struct Guess {
+ value: u32,
+}
+
+impl Guess {
+ pub fn new(value: u32) -> Guess {
+ if value < 1 {
+ panic!("Guess value must be greater than or equal to 1, got {}.",
+ value);
+ } else if value > 100 {
+ panic!("Guess value must be less than or equal to 100, got {}.",
+ value);
+ }
+
+ Guess {
+ value: value,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ #[should_panic(expected = "Guess value must be less than or equal to 100")]
+ fn greater_than_100() {
+ Guess::new(200);
+ }
}
#[test]
-#[should_panic(expected = "do not lie on character boundary")]
-fn slice_not_on_char_boundaries() {
- let s = "Здравствуйте";
- &s[0..1];
+
Listing 11-9: Testing that a condition will cause a
+panic! with a particular panic message
+
这个测试会通过,因为should_panic属性中expected参数提供的值是Guess::new函数 panic 信息的子字符串。我们可以指定期望的整个 panic 信息,在这个例子中是Guess value must be less than or equal to 100, got 200.。这依赖于 panic 有多独特或动态和你希望测试有多准确。在这个例子中,错误信息的子字符串足以确保函数在else if value > 100的情况下运行。
+
为了观察带有expected信息的should_panic测试失败时会发生什么,让我们再次引入一个 bug 来将if value < 1和else if value > 100的代码块对换:
+
if value < 1 {
+ panic!("Guess value must be less than or equal to 100, got {}.", value);
+} else if value > 100 {
+ panic!("Guess value must be greater than or equal to 1, got {}.", value);
}
-
-
-
Listing 11-2: A test expecting a panic! with a particular message
running 1 test
+test tests::greater_than_100 ... FAILED
+
+failures:
+
+---- tests::greater_than_100 stdout ----
+ thread 'tests::greater_than_100' panicked at 'Guess value must be greater
+ than or equal to 1, got 200.', src/lib.rs:10
+note: Run with `RUST_BACKTRACE=1` for a backtrace.
+note: Panic did not include expected string 'Guess value must be less than or
+equal to 100'
+
+failures:
+ tests::greater_than_100
+
+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
+
+
错误信息表明测试确实如期望 panic 了,不过 panic 信息did not include expected string 'Guess value must be less than or equal to 100'。可以看到我们的到的 panic 信息,在这个例子中是Guess value must be greater than or equal to 1, got 200.。这样就可以开始寻找 bug 在哪了!
diff --git a/docs/print.html b/docs/print.html
index 967a5f7..4e99b8d 100644
--- a/docs/print.html
+++ b/docs/print.html
@@ -1545,8 +1545,7 @@ the value is: 50
#[test]
+fn greeting_contains_name() {
+ let result = greeting("Carol");
+ assert!(
+ result.contains("Carol"),
+ "Greeting did not contain name, value was `{}`", result
+ );
+}
---- tests::greeting_contains_name stdout ----
+ thread 'tests::greeting_contains_name' panicked at 'Result did not contain
+ name, value was `Hello`', src/lib.rs:12
+note: Run with `RUST_BACKTRACE=1` for a backtrace.
#[test]
-fn a_simple_case() {
- let result = "hello"; // this value would come from running your code
- assert_eq!(result, "world", "greeting operation failed");
+
Listing 11-8: Testing that a condition will cause a
+panic!
struct Guess {
+ value: u32,
+}
+
+impl Guess {
+ pub fn new(value: u32) -> Guess {
+ if value < 1 {
+ panic!("Guess value must be greater than or equal to 1, got {}.",
+ value);
+ } else if value > 100 {
+ panic!("Guess value must be less than or equal to 100, got {}.",
+ value);
+ }
+
+ Guess {
+ value: value,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ #[should_panic(expected = "Guess value must be less than or equal to 100")]
+ fn greater_than_100() {
+ Guess::new(200);
+ }
}
#[test]
-#[should_panic(expected = "do not lie on character boundary")]
-fn slice_not_on_char_boundaries() {
- let s = "Здравствуйте";
- &s[0..1];
+
Listing 11-9: Testing that a condition will cause a
+panic! with a particular panic message
+
这个测试会通过,因为should_panic属性中expected参数提供的值是Guess::new函数 panic 信息的子字符串。我们可以指定期望的整个 panic 信息,在这个例子中是Guess value must be less than or equal to 100, got 200.。这依赖于 panic 有多独特或动态和你希望测试有多准确。在这个例子中,错误信息的子字符串足以确保函数在else if value > 100的情况下运行。
+
为了观察带有expected信息的should_panic测试失败时会发生什么,让我们再次引入一个 bug 来将if value < 1和else if value > 100的代码块对换:
+
if value < 1 {
+ panic!("Guess value must be less than or equal to 100, got {}.", value);
+} else if value > 100 {
+ panic!("Guess value must be greater than or equal to 1, got {}.", value);
}
-
-
-
Listing 11-2: A test expecting a panic! with a particular message
running 1 test
+test tests::greater_than_100 ... FAILED
+
+failures:
+
+---- tests::greater_than_100 stdout ----
+ thread 'tests::greater_than_100' panicked at 'Guess value must be greater
+ than or equal to 1, got 200.', src/lib.rs:10
+note: Run with `RUST_BACKTRACE=1` for a backtrace.
+note: Panic did not include expected string 'Guess value must be less than or
+equal to 100'
+
+failures:
+ tests::greater_than_100
+
+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
+
+
错误信息表明测试确实如期望 panic 了,不过 panic 信息did not include expected string 'Guess value must be less than or equal to 100'。可以看到我们的到的 panic 信息,在这个例子中是Guess value must be greater than or equal to 1, got 200.。这样就可以开始寻找 bug 在哪了!
ch11-02-running-tests.md
diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md
index 0c17184..6fb1f6d 100644
--- a/src/ch03-05-control-flow.md
+++ b/src/ch03-05-control-flow.md
@@ -305,7 +305,6 @@ the value is: 50
可以使用`for`循环来对一个集合的每个元素执行一些代码,来作为一个更有效率替代。`for`循环看起来像这样:
-Filename: src/main.rs
```rust
@@ -318,12 +317,8 @@ fn main() {
}
```
-
-
-Listing 3-6: Looping through each element of a collection using a `for` loop
-
-
-
+Listing 3-6: Looping through each element of a collection
+using a `for` loop
当运行这段代码,将看到与列表 3-5 一样的输出。更为重要的是,我们增强了代码安全性并消除了出现可能会导致超出数组的结尾或遍历长度不够而缺少一些元素这类 bug 机会。
diff --git a/src/ch07-01-mod-and-the-filesystem.md b/src/ch07-01-mod-and-the-filesystem.md
index 945188c..1b7a958 100644
--- a/src/ch07-01-mod-and-the-filesystem.md
+++ b/src/ch07-01-mod-and-the-filesystem.md
@@ -230,7 +230,7 @@ mod server {
注意这个模块文件中我们也使用了一个`mod`声明;这是因为我们希望`server`成为`network`的一个子模块。
-现在再次运行`cargo build`。成功!不过我们还需要再提取出另一个模块:`server`。因为这是一个子模块————也就是模块中的模块————目前的将模块提取到对应名字的文件中的策略就不管用了。如果我们仍这么尝试则会出现错误。对 *src/network.rs* 的第一个修改是用`mod server;`替换`server`模块的内容:
+现在再次运行`cargo build`。成功!不过我们还需要再提取出另一个模块:`server`。因为这是一个子模块——也就是模块中的模块——目前的将模块提取到对应名字的文件中的策略就不管用了。如果我们仍这么尝试则会出现错误。对 *src/network.rs* 的第一个修改是用`mod server;`替换`server`模块的内容:
Filename: src/network.rs
diff --git a/src/ch11-01-writing-tests.md b/src/ch11-01-writing-tests.md
index d7fe508..a03b4b7 100644
--- a/src/ch11-01-writing-tests.md
+++ b/src/ch11-01-writing-tests.md
@@ -169,7 +169,6 @@ test fails
`assert!`宏由标准库提供,在希望确保测试中一些条件为`true`时非常有用。需要向`assert!`宏提供一个计算为布尔值的参数。如果值是`true`,`assert!`什么也不做同时测试会通过。如果值为`false`,`assert!`调用`panic!`宏,这会导致测试失败。这是一个帮助我们检查代码是否以期望的方式运行的宏。
-
+这一次运行`should_panic`测试,它会失败:
+
+```
+running 1 test
+test tests::greater_than_100 ... FAILED
+
+failures:
-
+---- tests::greater_than_100 stdout ----
+ thread 'tests::greater_than_100' panicked at 'Guess value must be greater
+ than or equal to 1, got 200.', src/lib.rs:10
+note: Run with `RUST_BACKTRACE=1` for a backtrace.
+note: Panic did not include expected string 'Guess value must be less than or
+equal to 100'
-Listing 11-2: A test expecting a `panic!` with a particular message
+failures:
+ tests::greater_than_100
+
+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
+```
-
-
+错误信息表明测试确实如期望 panic 了,不过 panic 信息`did not include expected string 'Guess value must be less than or equal to 100'`。可以看到我们的到的 panic 信息,在这个例子中是`Guess value must be greater than or equal to 1, got 200.`。这样就可以开始寻找 bug 在哪了!
-请自行尝试当`should_panic`的测试出现 panic 但并不符合期望的信息时会发生什么:在测试中因为不同原因造成`panic!`,或者将期望的 panic 信息改为并不与字母字节边界 panic 信息相匹配。
\ No newline at end of file
+现在我们讲完了编写测试的方法,让我们看看运行测试时会发生什么并讨论可以用于`cargo test`的不同选项。
\ No newline at end of file