目录
- 五、采用测试驱动开发完善库的功能
- 5.1 编写失败测试用例
- 5.2 编写成功测试用例
- 5.3 在run函数中打印搜索到的行
- 六、添加大小写不敏感功能
- 七、将错误信息输出到标准错误
- 八、附录完整代码
五、采用测试驱动开发完善库的功能
5.1 编写失败测试用例
- 在lib.rs中写一个简单的
search
函数
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str>{vec![]
}
- 目前该函数是一个总是返回空的vector函数;
- 函数的参数是
&str
型,因此需要显式地定义生命周期; - 生命周期指定contents的生命周期与返回值的相关连;
- 在lib.rs中编写测试代码
#[cfg(test)]
mod tests{use super::*;#[test]fn one_result(){let query = "duct";let contents = "\
Rust:
safe, fast, productive.
Pick there.";assert_eq!(vec!["safe, fast, productive."],search(query, contents))}
}
- 运行测试会如预期的失败了;
5.2 编写成功测试用例
失败的原因在于search返回空,因此完善它就行
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
}
- 创建一个可变的Vector;
- 使用字符串类的迭代方法一次读取一行;
- 在此行中查找,找到后 push进入Vector即可;
编译测试用例
5.3 在run函数中打印搜索到的行
- 修改run函数,将搜索到的行内容打印出来
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {let contents = fs::read_to_string(config.filename)?;for line in search(&config.query, &contents) {println!("{}", line);}Ok(())
}
运行命令cargo run body poem.txt
,旨在poem.txt文件内查找body字符串
六、添加大小写不敏感功能
- 编写大小写不敏感的测试函数
search_case_insensitive
; - 将旧的
one_result
改名为大小写敏感函数case_sensitive
;
大小写不敏感的search函数
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
}
- 大小写不敏感的做法就是将读取到的字母以及目标子串全部改为大写或小写;
编写测试函数
#[cfg(test)]
mod tests {use super::*;#[test]fn case_sensitive() {let query = "duct";let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";assert_eq!(vec!["safe, fast, productive."],search(query, contents));}#[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));}
}
在Config中增加case_sensitive并修改构造函数
use std::env;pub struct Config{query: String,filename: String,case_sensitive: bool,
}impl Config{pub fn new(args: &[String]) -> Result<Config, &'static str>{if args.len() < 3{return Err("Not enough arguments!");}let query = args[1].clone();let filename = args[2].clone();let case_sensitive = env::var("CASE_INSENSITIVE").is_err();Ok(Config {query, filename, case_sensitive})}
}
- 在Config中添加了
case_sensitive
的布尔值,用来说明是否大小写敏感; - 在Config的
new
函数中使用env::var
读取CASE_INSENSITIVE
的环境变量设置情况; - 如果设置了
CASE_INSENSITIVE
的值is_err
返回True就是大小写不敏感的,否则是敏感的; - 注意:不论CASE_INSENSITIVE设置为多少,都是大小写不敏感的;
修改run函数,以判断是否起用大小写敏感
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {let contents = fs::read_to_string(config.filename)?;let results = if config.case_sensitive {search(&config.query, &contents)}else{search_case_insensitive(&config.query, &contents)};for line in results{println!("{}", line);}Ok(())
}
测试,先不加环境变量测试大小写敏感的,再测试不敏感的。
七、将错误信息输出到标准错误
- 通过
println!
打印的信息都输出到了终端; - 大部分终端都提供了两种输出:标准输出(standard output,stdout)和标准错误(standard error,stderr);
- 标准输出对应一般信息,标准错误用于错误信息;
- Rust提供
eprintln!
宏输出标准错误信息;
修改main的标准错误输出
fn main() {let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err| {eprintln!("Problem parsing arguments: {}", err);process::exit(1);});if let Err(e) = minigrep::run(config) {eprintln!("Application error: {}", e);process::exit(1);}
}
八、附录完整代码
main.rs
use std::env;
use std::process;
use minigrep::Config;fn main() {let args: Vec<String> = env::args().collect();let config = Config::new(&args).unwrap_or_else(|err| {eprintln!("Problem parsing arguments: {}", err);process::exit(1);});if let Err(e) = minigrep::run(config) {eprintln!("Application error: {}", e);process::exit(1);}
}
lib.rs
use std::fs;
use std::env;
use std::error::Error;pub struct Config{query: String,filename: String,case_sensitive: bool,
}impl Config{pub fn new(args: &[String]) -> Result<Config, &'static str>{if args.len() < 3{return Err("Not enough arguments!");}let query = args[1].clone();let filename = args[2].clone();let case_sensitive = env::var("CASE_INSENSITIVE").is_err();Ok(Config {query, filename, case_sensitive})}
}pub fn run(config: Config) -> Result<(), Box<dyn Error>> {let contents = fs::read_to_string(config.filename)?;let results = if config.case_sensitive {search(&config.query, &contents)}else{search_case_insensitive(&config.query, &contents)};for line in results{println!("{}", line);}Ok(())
}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
}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
}#[cfg(test)]
mod tests {use super::*;#[test]fn case_sensitive() {let query = "duct";let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";assert_eq!(vec!["safe, fast, productive."],search(query, contents));}#[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));}
}