Skip to content

Commit

Permalink
#111: Test type conversion and error message
Browse files Browse the repository at this point in the history
if implement FromStr but not Debug
  • Loading branch information
la10736 committed Apr 5, 2021
1 parent 6e3da77 commit 666b931
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/resources/rstest/convert_string_literal.rs
Expand Up @@ -53,3 +53,23 @@ fn not_convert_impl(#[case] that_impl: impl MyTrait, #[case] s: &str) {
fn not_convert_generics<S: AsRef<str>>(#[case] ip: S, #[case] addr: SocketAddr) {
assert_eq!(addr.ip().to_string(), ip.as_ref());
}

struct MyType(String);
struct E;
impl core::str::FromStr for MyType {
type Err = E;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"error" => Err(E),
inner => Ok(MyType(inner.to_owned())),
}
}
}

#[rstest]
#[case("hello", "hello")]
#[case("doesn't mater", "error")]
fn convert_without_debug(#[case] expected: &str, #[case] converted: MyType) {
assert_eq!(expected, converted.0);
}
4 changes: 4 additions & 0 deletions tests/rstest/mod.rs
Expand Up @@ -832,6 +832,8 @@ mod matrix {
fn convert_string_literal() {
let (output, _) = run_test("convert_string_literal.rs");

assert_in!(output.stdout.str(), "Cannot parse 'error' to get MyType");

TestResults::new()
.ok("cases::case_1")
.ok("cases::case_2")
Expand All @@ -847,6 +849,8 @@ fn convert_string_literal() {
.ok("not_convert_impl::case_1")
.ok("not_convert_generics::case_1")
.ok("not_convert_generics::case_2")
.ok("convert_without_debug::case_1")
.fail("convert_without_debug::case_2")
.assert(output);
}

Expand Down

0 comments on commit 666b931

Please sign in to comment.