From 666b931ebadeb4bc0b79813e5bae88b05517f3a0 Mon Sep 17 00:00:00 2001 From: michele Date: Mon, 5 Apr 2021 17:09:27 +0200 Subject: [PATCH] #111: Test type conversion and error message if implement FromStr but not Debug --- .../rstest/convert_string_literal.rs | 20 +++++++++++++++++++ tests/rstest/mod.rs | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/tests/resources/rstest/convert_string_literal.rs b/tests/resources/rstest/convert_string_literal.rs index 0d8f512..3e7989f 100644 --- a/tests/resources/rstest/convert_string_literal.rs +++ b/tests/resources/rstest/convert_string_literal.rs @@ -53,3 +53,23 @@ fn not_convert_impl(#[case] that_impl: impl MyTrait, #[case] s: &str) { fn not_convert_generics>(#[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 { + 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); +} diff --git a/tests/rstest/mod.rs b/tests/rstest/mod.rs index 8a1bdd4..3af01af 100644 --- a/tests/rstest/mod.rs +++ b/tests/rstest/mod.rs @@ -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") @@ -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); }