#单元测试 #断言 #匹配器 #测试驱动开发 #单元

hamcrest2

Hamcrest 测试库的移植版本。基于未维护的 hamcrest-rust(原版)增加了额外的匹配器、更好的文档等。

9 个版本

0.3.0 2019年11月4日
0.2.6 2019年4月28日
0.2.4 2019年3月10日
0.2.3 2018年7月5日
0.1.6 2018年5月12日

#169 in 测试

Download history 2671/week @ 2024-03-14 1567/week @ 2024-03-21 1209/week @ 2024-03-28 1619/week @ 2024-04-04 2038/week @ 2024-04-11 1651/week @ 2024-04-18 1779/week @ 2024-04-25 1782/week @ 2024-05-02 1884/week @ 2024-05-09 1999/week @ 2024-05-16 2343/week @ 2024-05-23 1633/week @ 2024-05-30 2348/week @ 2024-06-06 1615/week @ 2024-06-13 1001/week @ 2024-06-20 1129/week @ 2024-06-27

6,654 每月下载量
40 个 Crates(13 个直接) 中使用

MIT/Apache

51KB
1K SLoC

Build Status

Hamcrest2

Hamcrest 移植到 Rust。基于未维护的 hamcrest-rust(原版)增加了额外的匹配器、更好的文档、支持 Rust 2018 版本等。

安装

要使用 Hamcrest,请将以下内容添加到您的 Cargo.toml

[dev-dependencies]
hamcrest2 = "*"

快速执行 cargo build 后,您应该可以使用了!

使用方法

Hamcrest2 支持许多匹配器。最简单的方法是将它们全部 use,如下所示

use hamcrest2::prelude::*;

如果您想更具有选择性,请确保您还导入了 HamcrestMatcher 特性。

通用匹配器

eq, not

assert_that!(1, eq(1));  // also equal_to()
assert_that!(1, not(eq(2)));

compared_to

assert_that!(1, lt(2));   // also less_than()
assert_that!(1, leq(1));  // also less_than_or_equal_to()
assert_that!(2, gt(1));   // also greater_than()
assert_that!(2, geq(2));  // also greater_than_or_equal_to()

type_of

assert_that!(123usize, type_of::<usize>());
assert_that!("test", type_of::<&str>());

matches_regex

assert_that!("1234", matches_regex(r"\d"));
assert_that!("abc", does_not(match_regex(r"\d")));

数值匹配器

close_to

assert_that!(1e-40f32, close_to(0.0, 0.01));
assert_that!(1e-40f32, not(close_to(0.0, 0.000001)));

文件系统匹配器

path_exists, file_exists, dir_exists

let path = Path::new("./README.md");
assert_that!(path, path_exists());
assert_that!(path, file_exists());
assert_that!(path, not(dir_exists()));

选项和结果

has

let var: Option<i8> = Some(5);
assert_that!(var, has(5));

let var: Result<i8, String> = Ok(5);
assert_that!(var, has(5));

ok

let var: Result<i8, String> = Ok(5);
assert_that!(var, ok());
assert_that!(&var, ok());

assert_that!(Ok(5), ok::<i8, String>());
assert_that!(&Ok(5), ok::<i8, String>());

let var: Result<i8, String> = Err("bad!".to_string());
assert_that!(var, not(ok()));
assert_that!(&var, not(ok()));

err

let var: Result<i8, String> = Err("bad!".to_string());
assert_that!(var, err());
assert_that!(&var, err());

assert_that!(Err("bad!".to_string()), err::<i8, String>());
assert_that!(&Err("bad!".to_string()), err::<i8, String>());

let var: Result<i8, String> = Ok(5);
assert_that!(var, not(err()));
assert_that!(&var, not(err()));

some

let var: Option<i8> = Some(5);
assert_that!(var, some());
assert_that!(&var, some());

assert_that!(Some(1), some::<u8>());
assert_that!(&Some(1), some::<u8>());

let var: Option<i8> = None;
assert_that!(var, not(some()));
assert_that!(&var, not(some()));

none

let var: Option<i8> = None;
assert_that!(var, none());
assert_that!(&var, none());

assert_that!(None, none::<u8>());
assert_that!(&None, none::<u8>());
assert_that!(Some(1), not(none::<u8>()));
assert_that!(&Some(1), not(none::<u8>()));

集合匹配器

contains, contains exactly, contains in order

assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2)));
assert_that!(&vec!(1, 2, 3), contains(1));
assert_that!(&vec!(1, 2, 3), not(contains(4i)));

assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2, 3)).exactly());
assert_that!(&vec!(1, 2, 3), not(contains(vec!(1, 2)).exactly()));

assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2)).in_order());
assert_that!(&vec!(1, 2, 3), not(contains(vec!(1, 3)).in_order()));

len

assert_that!(&vec!(1, 2, 3), len(3));
assert_that!(&vec!(1, 2, 3), not(len(4)));

empty

assert_that!(&Vec::<i32>::new(), empty());
assert_that!(&vec![1, 2, 3], not(empty()));

复合匹配器

all

assert_that!(4, all!(lt(5), gt(3)));  // also and!()
assert_that!(
    &vec![1, 2, 3],
    all!(contains(vec![1, 2]), not(contains(vec![4])))
);

any

assert_that!(4, any!(less_than(2), greater_than(3)));  // also or!()
assert_that!(
    &vec![1, 2, 3],
    any!(contains(vec![1, 2, 5]), not(contains(vec![4])))
);

杂项匹配器

is(bool)

assert_that!(true, is(true));
assert_that!(false, is(false));

anything

assert_that!(42, anything());
assert_that!("test", is(anything()));

许可

许可方式为以下之一

根据您的选择。

贡献

除非您明确表示,否则根据 Apache-2.0 许可证定义的,您有意提交以包含在作品中的任何贡献,均将按上述方式双许可,无需附加条款或条件。

依赖项

~2.6–3.5MB
~63K SLoC