[Easy to understand series | rustlang language | zero-based | Getting Started | (25) | combat 2: command-line tool minigrep (2)]
Project combat
Combat 2: command-line tool minigrep
We continue to develop our minigrep.
We are now in a TDD mode test-driven development, to develop new features search functions.
Go ahead, let in src / lib. rs file, increase test code:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}
Also then write an empty function:
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
vec![]
}
Then, we have to run the following command:
cargo test
The results show:
$ cargo test
Compiling minigrep v0.1.0 (file:///projects/minigrep)
--warnings--
Finished dev [unoptimized + debuginfo] target(s) in 0.43 secs
Running target/debug/deps/minigrep-abcabcabc
running 1 test
test tests::one_result ... FAILED
failures:
---- tests::one_result stdout ----
thread 'tests::one_result' panicked at 'assertion failed: `(left ==
right)`
left: `["safe, fast, productive."]`,
right: `[]`)', src/lib.rs:48:8
note: Run with `RUST_BACKTRACE=1` for a backtrace.
failures:
tests::one_result
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
error: test failed, to rerun pass '--lib'
The test is not passed.
Our implementation function search, simply return an empty vector. Of course, not through.
How to do?
Refactor search function:
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
We then run the following command:
cargo test
The results show:
running 1 test
test tests::one_result ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Testing by! Pretty!
This is the basic process of TDD.
We look at the diagram:
Okay, now we can directly call the search function, and put it in src / lib.rs in run functions in:
//重构从文件中读取内容的业务逻辑
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.filename)?;
println!("With text:\n{}", contents);
for line in search(&config.query, &contents) {
println!("-----search result ------{}", line);
}
Ok(())
}
We then run the command:
cargo run frog poem.txt
The results are:
.......
-----search result ------How public, like a frog
We find the line containing the word frog! perfect!
We find the next body of words:
cargo run body poem.txt
The results are:
......
-----search result ------I'm nobody! Who are you?
-----search result ------Are you nobody, too?
-----search result ------How dreary to be somebody!
The result is correct!
Of course, we can try a word that does not exist:
cargo run monomorphization poem.txt
The results are:
E:\code\rustProject\minigrep> cargo run monomorphization poem.txt
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\minigrep.exe monomorphization poem.txt`
With text:
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!
With "----- search result ------" does not appear in the result information, indicating not find the words: monomorphization.
The results in line with expectations.
Now we TDD to develop ways to develop new features function search_case_insensitive, the new test code:
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
Of course, we can simply implement what search_case_insensitive function, let it fail, because the logic is simple, direct it to achieve it:
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
let mut results = Vec::new();
for line in contents.lines() {
if line.to_lowercase().contains(&query) {
results.push(line);
}
}
results
}
Well, we ran the following command:
cargo test
result:
$cargo test
Compiling minigrep v0.1.0 (E:\code\rustProject\minigrep)
Finished dev [unoptimized + debuginfo] target(s) in 1.38s
Running target\debug\deps\minigrep-07da7ef1bffd9ef9.exe
running 2 tests
test case_insensitive ... ok
test tests::one_result ... ok
By testing, beautiful!
Above, I hope useful to you.
如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
Reference article:
https://doc.rust-lang.org/stable/book/ch12-05-working-with-environment-variables.html