This commit is contained in:
TimLai666 2024-06-18 16:37:49 +08:00 committed by GitHub
parent 5d717537fe
commit afad4b9601
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 30 deletions

View File

@ -1,7 +1,7 @@
# Tests # 測試
Going out of order from the book to cover tests -- many of the following exercises will ask you to make tests pass! 與官方教學的順序不同,我們會先介紹測試——接下來的許多練習會要求您通過測試!
## Further information ## 進一步了解
- [Writing Tests](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) - [撰寫測試](https://doc.rust-lang.org/book/ch11-01-writing-tests.html)

View File

@ -1,14 +1,10 @@
// tests1.rs // tests1.rs
// //
// Tests are important to ensure that your code does what you think it should // 測試對於確保您的程式碼執行您認為它應該執行的操作非常重要。可以使用以下命令運行此文件上的測試rustlings run tests1
// do. Tests can be run on this file with the following command: rustlings run
// tests1
// //
// This test has a problem with it -- make the test compile! Make the test pass! // 這個測試有問題——請先讓測試編譯通過,然後讓測試成功,再故意讓測試失敗,以熟悉測試操作。
// Make the test fail!
// //
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a // 執行 `rustlings hint tests1` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE

View File

@ -1,10 +1,8 @@
// tests2.rs // tests2.rs
// //
// This test has a problem with it -- make the test compile! Make the test pass! // 這個測試有問題——請先讓測試編譯通過,然後讓測試成功,再故意讓測試失敗,以熟悉測試操作。
// Make the test fail!
// //
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a // 執行 `rustlings hint tests2` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE

View File

@ -1,11 +1,8 @@
// tests3.rs // tests3.rs
// //
// This test isn't testing our function -- make it do that in such a way that // 這個測試沒有測試我們的函數——讓它以一種通過測試的方式來測試函數。然後寫第二個測試,測試當我們調用 `is_even(5)` 時是否得到預期的結果。
// the test passes. Then write a second test that tests whether we get the
// result we expect to get when we call `is_even(5)`.
// //
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a // 執行 `rustlings hint tests3` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE

View File

@ -1,9 +1,8 @@
// tests4.rs // tests4.rs
// //
// Make sure that we're testing for the correct conditions! // 確保我們正在測試正確的條件!
// //
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a // 執行 `rustlings hint tests4` 或使用 `hint` 子命令獲取提示。
// hint.
// I AM NOT DONE // I AM NOT DONE
@ -13,10 +12,10 @@ struct Rectangle {
} }
impl Rectangle { impl Rectangle {
// Only change the test functions themselves // 只需更改測試函數本身
pub fn new(width: i32, height: i32) -> Self { pub fn new(width: i32, height: i32) -> Self {
if width <= 0 || height <= 0 { if width <= 0 || height <= 0 {
panic!("Rectangle width and height cannot be negative!") panic!("矩形的寬度和高度不能為負數!")
} }
Rectangle {width, height} Rectangle {width, height}
} }
@ -28,21 +27,21 @@ mod tests {
#[test] #[test]
fn correct_width_and_height() { fn correct_width_and_height() {
// This test should check if the rectangle is the size that we pass into its constructor // 這個測試應該檢查矩形的大小是否與我們傳遞給構造函數的大小相同
let rect = Rectangle::new(10, 20); let rect = Rectangle::new(10, 20);
assert_eq!(???, 10); // check width assert_eq!(???, 10); // 檢查寬度
assert_eq!(???, 20); // check height assert_eq!(???, 20); // 檢查高度
} }
#[test] #[test]
fn negative_width() { fn negative_width() {
// This test should check if program panics when we try to create rectangle with negative width // 這個測試應該檢查當我們嘗試創建寬度為負數的矩形時程式是否會恐慌
let _rect = Rectangle::new(-10, 10); let _rect = Rectangle::new(-10, 10);
} }
#[test] #[test]
fn negative_height() { fn negative_height() {
// This test should check if program panics when we try to create rectangle with negative height // 這個測試應該檢查當我們嘗試創建高度為負數的矩形時程式是否會恐慌
let _rect = Rectangle::new(10, -10); let _rect = Rectangle::new(10, -10);
} }
} }