使用测试驱动开发来开发库的功能

现在,我们已经将逻辑提取到 src/lib.rs 中,并将参数收集和错误处理留在 src/main.rs 中,因此更容易为我们代码的核心功能编写测试。我们可以使用各种参数直接调用函数并检查返回值,而无需从命令行调用我们的二进制文件。

在本节中,我们将使用以下步骤,通过测试驱动开发 (TDD) 过程将搜索逻辑添加到 minigrep 程序中

  1. 编写一个会失败的测试,并运行它以确保它因您期望的原因而失败。
  2. 编写或修改足够少的代码以使新测试通过。
  3. 重构您刚刚添加或更改的代码,并确保测试继续通过。
  4. 从第 1 步重复!

虽然它只是编写软件的众多方法之一,但 TDD 可以帮助驱动代码设计。在编写使测试通过的代码之前编写测试有助于在整个过程中保持高测试覆盖率。

我们将对实际执行在文件内容中搜索查询字符串并生成与查询匹配的行列表的功能进行测试驱动实现。我们将把这个功能添加到一个名为 search 的函数中。

编写一个失败的测试

因为我们不再需要它们了,所以让我们从 src/lib.rssrc/main.rs 中删除我们用来检查程序行为的 println! 语句。然后,在 src/lib.rs 中,我们将添加一个带有测试函数的 tests 模块,就像我们在第 11 章中所做的那样。该测试函数指定了我们希望 search 函数具有的行为:它将接受一个查询和要搜索的文本,并且它将仅返回文本中包含该查询的行。列表 12-15 显示了这个测试,它还无法编译。

文件名:src/lib.rs
use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    Ok(())
}

#[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));
    }
}
列表 12-15:为我们希望拥有的 search 函数创建一个失败的测试

此测试搜索字符串 "duct"。我们正在搜索的文本是三行,其中只有一行包含 "duct" (请注意,开头的双引号后的反斜杠告诉 Rust 不要在此字符串文字内容的开头放置换行符)。我们断言从 search 函数返回的值仅包含我们期望的行。

我们还不能运行此测试并观察它失败,因为该测试甚至无法编译:search 函数尚不存在!根据 TDD 原则,我们将添加足够少的代码以通过添加始终返回空向量的 search 函数的定义来使测试编译和运行,如列表 12-16 所示。然后,该测试应该会编译并失败,因为空向量与包含行 "safe, fast, productive." 的向量不匹配。

文件名:src/lib.rs
use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    Ok(())
}

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    vec![]
}

#[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));
    }
}
列表 12-16:定义足够少的 search 函数,以便我们的测试可以编译

请注意,我们需要在 search 的签名中定义一个显式的生命周期 'a,并将该生命周期与 contents 参数和返回值一起使用。回想一下第 10 章中的内容,生命周期参数指定哪个参数生命周期与返回值的生命周期相关联。在这种情况下,我们指示返回的向量应包含引用参数 contents 的切片的字符串切片(而不是参数 query)。

换句话说,我们告诉 Rust,search 函数返回的数据的生命周期将与传递给 search 函数的 contents 参数中的数据的生命周期一样长。这很重要!切片 _引用_ 的数据需要有效,该引用才有效;如果编译器假设我们正在制作 query 而不是 contents 的字符串切片,它将不正确地执行其安全检查。

如果我们忘记生命周期注释并尝试编译此函数,则会收到此错误

$ cargo build
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
error[E0106]: missing lifetime specifier
  --> src/lib.rs:28:51
   |
28 | pub fn search(query: &str, contents: &str) -> Vec<&str> {
   |                      ----            ----         ^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `query` or `contents`
help: consider introducing a named lifetime parameter
   |
28 | pub fn search<'a>(query: &'a str, contents: &'a str) -> Vec<&'a str> {
   |              ++++         ++                 ++              ++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `minigrep` (lib) due to 1 previous error

Rust 不可能知道我们需要两个参数中的哪一个,因此我们需要明确地告诉它。因为 contents 是包含我们所有文本的参数,并且我们希望返回与该文本匹配的部分,所以我们知道 contents 是应该使用生命周期语法连接到返回值的参数。

其他编程语言不要求您在签名中将参数连接到返回值,但是这种做法会随着时间的推移变得更容易。您可能想将此示例与“使用生命周期验证引用”章节中的示例进行比较。

现在让我们运行测试

$ cargo test
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.97s
     Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)

running 1 test
test tests::one_result ... FAILED

failures:

---- tests::one_result stdout ----
thread 'tests::one_result' panicked at src/lib.rs:44:9:
assertion `left == right` failed
  left: ["safe, fast, productive."]
 right: []
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::one_result

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--lib`

很好,测试失败了,正如我们预期的那样。让我们使测试通过!

编写代码以使测试通过

目前,我们的测试失败是因为我们始终返回一个空向量。要修复该问题并实现 search,我们的程序需要执行以下步骤

  1. 迭代内容的每一行。
  2. 检查该行是否包含我们的查询字符串。
  3. 如果包含,则将其添加到我们要返回的值列表中。
  4. 如果不包含,则不执行任何操作。
  5. 返回匹配的结果列表。

让我们逐步完成每个步骤,从迭代行开始。

使用 lines 方法迭代行

Rust 有一个有用的方法来处理字符串的逐行迭代,方便地命名为 lines,其工作方式如列表 12-17 所示。请注意,这还无法编译。

文件名:src/lib.rs
use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    Ok(())
}

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    for line in contents.lines() {
        // do something with line
    }
}

#[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));
    }
}
列表 12-17:迭代 contents 中的每一行

lines 方法返回一个迭代器。我们将在第 13 章中深入讨论迭代器,但请回想一下,您在列表 3-5中看到了这种使用迭代器的方式,我们在其中使用带有迭代器的 for 循环来对集合中的每个项运行一些代码。

在每行中搜索查询

接下来,我们将检查当前行是否包含我们的查询字符串。幸运的是,字符串有一个名为 contains 的有用方法可以为我们做到这一点!在 search 函数中添加对 contains 方法的调用,如列表 12-18 所示。请注意,这仍然无法编译。

文件名:src/lib.rs
use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    Ok(())
}

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    for line in contents.lines() {
        if line.contains(query) {
            // do something with line
        }
    }
}

#[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));
    }
}
列表 12-18:添加功能以查看该行是否包含 query 中的字符串

目前,我们正在构建功能。为了使代码能够编译,我们需要从正文中返回值,就像我们在函数签名中指示的那样。

存储匹配的行

要完成此函数,我们需要一种存储我们要返回的匹配行的方法。为此,我们可以在 for 循环之前创建一个可变向量,并调用 push 方法将 line 存储在向量中。在 for 循环之后,我们返回该向量,如列表 12-19 所示。

文件名:src/lib.rs
use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    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
}

#[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));
    }
}
列表 12-19:存储匹配的行,以便我们可以返回它们

现在,search 函数应该只返回包含 query 的行,并且我们的测试应该通过。让我们运行测试

$ cargo test
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 1.22s
     Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)

running 1 test
test tests::one_result ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running unittests src/main.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests minigrep

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

我们的测试通过了,所以我们知道它有效!

此时,我们可以考虑在保持测试通过以保持相同功能的同时,寻找重构搜索函数实现的机会。搜索函数中的代码还不错,但它没有利用迭代器的一些有用功能。我们将在第 13 章中返回到此示例,我们将在其中详细探讨迭代器,并了解如何改进它。

run 函数中使用 search 函数

现在,search 函数已经工作并通过测试,我们需要从我们的 run 函数中调用 search。我们需要将 config.query 值和 run 从文件中读取的 contents 传递给 search 函数。然后,run 将打印从 search 返回的每一行

文件名:src/lib.rs

use std::error::Error;
use std::fs;

pub struct Config {
    pub query: String,
    pub file_path: String,
}

impl Config {
    pub fn build(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            return Err("not enough arguments");
        }

        let query = args[1].clone();
        let file_path = args[2].clone();

        Ok(Config { query, file_path })
    }
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path)?;

    for line in search(&config.query, &contents) {
        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
}

#[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));
    }
}

我们仍然使用 for 循环从 search 返回每一行并打印它。

现在整个程序应该可以工作了!让我们试一下,首先使用一个应该从艾米丽·狄金森的诗中返回一行的单词:frog

$ cargo run -- frog poem.txt
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/minigrep frog poem.txt`
How public, like a frog

太棒了!现在让我们尝试一个将匹配多行的单词,例如 body

$ cargo run -- body poem.txt
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
     Running `target/debug/minigrep body poem.txt`
I'm nobody! Who are you?
Are you nobody, too?
How dreary to be somebody!

最后,让我们确保当我们搜索诗中任何地方都没有的单词(例如 monomorphization)时,我们不会得到任何行

$ cargo run -- monomorphization poem.txt
   Compiling minigrep v0.1.0 (file:///projects/minigrep)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.0s
     Running `target/debug/minigrep monomorphization poem.txt`

太棒了!我们已经构建了我们自己版本的经典工具,并学习了很多关于如何构建应用程序的知识。我们还了解了一些关于文件输入和输出、生命周期、测试和命令行解析的知识。

为了完成这个项目,我们将简要演示如何使用环境变量以及如何打印到标准错误,这在您编写命令行程序时都很有用。