Skip to content

Commit

Permalink
#111 Test default conversion fail error message
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Apr 23, 2021
1 parent bcb9c8c commit 2fefbf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/fixture/mod.rs
Expand Up @@ -179,9 +179,13 @@ mod should {
fn convert_literal_string_for_default_values() {
let (output, _) = run_test("default_conversion.rs");

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

TestResults::new()
.ok("test_base")
.ok("test_byte_array")
.ok("test_convert_custom")
.fail("test_fail_conversion")
.assert(output);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/resources/fixture/default_conversion.rs
@@ -1,11 +1,34 @@
use rstest::{fixture, rstest};
use std::net::{Ipv4Addr, SocketAddr};

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())),
}
}
}

#[fixture]
fn base(#[default = "1.2.3.4"] ip: Ipv4Addr, #[default = r#"8080"#] port: u16) -> SocketAddr {
SocketAddr::new(ip.into(), port)
}

#[fixture]
fn fail(#[default = "error"] t: MyType) -> MyType {
t
}

#[fixture]
fn valid(#[default = "some"] t: MyType) -> MyType {
t
}

#[rstest]
fn test_base(base: SocketAddr) {
assert_eq!(base, "1.2.3.4:8080".parse().unwrap());
Expand All @@ -20,3 +43,9 @@ fn byte_array(#[default = b"1234"] some: &[u8]) -> usize {
fn test_byte_array(byte_array: usize) {
assert_eq!(4, byte_array);
}

#[rstest]
fn test_convert_custom(valid: MyType) {}

#[rstest]
fn test_fail_conversion(fail: MyType) {}

0 comments on commit 2fefbf3

Please sign in to comment.