File: default_conversion.rs

package info (click to toggle)
rust-rstest 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 993 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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());
}

#[fixture]
fn byte_array(#[default(b"1234")] some: &[u8]) -> usize {
    some.len()
}

#[rstest]
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) {}