parameterized icon indicating copy to clipboard operation
parameterized copied to clipboard

Add iterator input functions as arguments

Open foresterre opened this issue 5 years ago • 4 comments

Overview:

Add an input method based on any function which returns impl IntoIterator<Item=V> where the type of V is of the same type as a function argument.

For example for the following test case, the type of V would be either &str or u32:

#[parameterized(input = { "0", "1", "2" }, expected = { 0, 1, 2 })]
fn my_test(input: &str, expected: u32) {
    let parsed: Result<u32, ()> = input.parse().map_err(|_| ());
    assert_eq!(parsed.unwrap(), expected);
}

(Proposed) Syntax:

#[parameterized(fn my_inputs as input, expected = { 0, 1, 2 })]

or

#[parameterized(using my_inputs as input, expected = { 0, 1, 2 })]
fn my_test(input: &str, expected: u32) {
    let parsed: Result<u32, ()> = input.parse().map_err(|_| ());
    assert_eq!(parsed.unwrap(), expected);
}

fn my_inputs() -> impl IntoIterator<Item = &'static str> {
    vec!["0", "1", "2"]
}

or


#[parameterized(using v = my_provider)]
fn my_test(v: Option<i32>) {}

fn my_provider() -> impl IntoIterator<Option<i32>> {
    vec![Some(1)]
}

Considerations:

  • Always at least one finite iterator expected; or a finite expression based input from which we can take the max length as the amount of test cases
  • https://doc.rust-lang.org/std/iter/trait.ExactSizeIterator.html

foresterre avatar Nov 11 '19 17:11 foresterre