Skip to content

Commit

Permalink
#42 Integration tests in rstest for matrix case.
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Oct 18, 2019
1 parent 6e7607d commit 402d7d4
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 31 deletions.
12 changes: 11 additions & 1 deletion resources/rstest/dump_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ fn single_fail(fu32: u32, fstring: String, ftuple: (A, String, i32)) {
case(24, "trs", ("tt", -24))
::trace
)]
fn should_fail(u: u32, s: &str, t: (&str, i32)) {
fn cases_fail(u: u32, s: &str, t: (&str, i32)) {
assert!(false);
}

#[rstest(
u => [1, 2],
s => ["rst", "srt"],
t => [("SS", -12), ("TT", -24)]
::trace
)]
fn matrix_fail(u: u32, s: &str, t: (&str, i32)) {
assert!(false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ fn fa() -> A { A {} }
#[rstest(
::trace::notrace(fa,fb))
]
fn single_fail(fu32: u32, fa: A, fb: B, fd: D) {
fn simple(fu32: u32, fa: A, fb: B, fd: D) {
assert!(false);
}

#[rstest(a,b,d,
case(A{}, B{}, D{})
::trace::notrace(a,b))
]
fn should_fail(fu32: u32, a: A, b: B, d: D) {
fn cases(fu32: u32, a: A, b: B, d: D) {
assert!(false);
}

#[rstest(
a => [A{}],
b => [B{}],
dd => [D{}],
::trace::notrace(a,b))
]
fn matrix(fu32: u32, a: A, b: B, dd: D) {
assert!(false);
}
6 changes: 6 additions & 0 deletions resources/rstest/dump_not_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ fn single(fixture: S) {}
::trace
)]
fn cases(s: S) {}

#[rstest(
s => [S{}]
::trace
)]
fn matrix(s: S) {}
27 changes: 25 additions & 2 deletions resources/rstest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ fn error_cannot_resolve_fixture(no_fixture: u32, f: u32) {}
fn error_fixture_wrong_type(fixture: String, f: u32) {}

#[rstest(f, case(42))]
fn error_param_wrong_type(f: &str) {}
fn error_case_wrong_type(f: &str) {}

#[rstest(condition,
case(vec![1,2,3].contains(2)))
]
fn error_in_arbitrary_rust_code(condition: bool) {
fn error_in_arbitrary_rust_code_cases(condition: bool) {
assert!(condition)
}

Expand Down Expand Up @@ -48,3 +48,26 @@ fn error_define_case_that_is_already_an_injected_fixture(f: u32) {
#[rstest(v, f(42), f(42), case(12))]
fn error_inject_a_fixture_more_than_once(v: u32, f: u32) {
}

#[rstest(f => [42])]
fn error_matrix_wrong_type(f: &str) {}

#[rstest(condition => [vec![1,2,3].contains(2)] )]
fn error_arbitrary_rust_code_matrix(condition: bool) {
assert!(condition)
}

#[rstest(empty => [])]
fn error_empty_list(empty: &str) {}

#[rstest(not_exist_1 => [42],
not_exist_2 => [42])]
fn error_no_match_args() {}

#[rstest(f => [41, 42], f(42))]
fn error_inject_a_fixture_that_is_already_a_value_list(f: u32) {
}

#[rstest(f(42), f => [41, 42])]
fn error_define_a_value_list_that_is_already_an_injected_fixture(f: u32) {
}
31 changes: 31 additions & 0 deletions resources/rstest/matrix/partial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use rstest::*;

#[fixture]
fn f1() -> u32 { 0 }
#[fixture]
fn f2() -> u32 { 0 }
#[fixture]
fn f3() -> u32 { 0 }

#[fixture]
fn fixture(f1: u32, f2: u32, f3: u32) -> u32 { f1 + f2 + 2 * f3 }

#[rstest(a => [0, 1], b => [0, 2])]
fn default(fixture: u32, a: u32, b: u32) {
assert_eq!(fixture, a * b);
}

#[rstest(a => [0, 1], b => [0, 2], fixture(1))]
fn partial_1(fixture: u32, a: u32, b: u32) {
assert_eq!(fixture, a * b);
}

#[rstest(a => [0, 1], b => [0, 2], fixture(0, 2))]
fn partial_2(fixture: u32, a: u32, b: u32) {
assert_eq!(fixture, a * b);
}

#[rstest(a => [0, 1], b => [0, 2], fixture(0, 0, 1))]
fn complete(fixture: u32, a: u32, b: u32) {
assert_eq!(fixture, a * b);
}
181 changes: 155 additions & 26 deletions tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,16 @@ mod dump_input_values {

TestResults::new()
.fail("single_fail")
.fail("should_fail::case_1")
.fail("should_fail::case_2")
.fail("cases_fail::case_1")
.fail("cases_fail::case_2")
.fail("matrix_fail::case_1_1_1")
.fail("matrix_fail::case_1_1_2")
.fail("matrix_fail::case_1_2_1")
.fail("matrix_fail::case_1_2_2")
.fail("matrix_fail::case_2_1_1")
.fail("matrix_fail::case_2_1_2")
.fail("matrix_fail::case_1_2_1")
.fail("matrix_fail::case_2_2_2")
.assert(output);

assert_in!(out, "fu32 = 42");
Expand All @@ -128,6 +136,14 @@ mod dump_input_values {
assert_in!(out, "u = 24");
assert_in!(out, r#"s = "trs""#);
assert_in!(out, r#"t = ("tt", -24)"#);

assert_in!(out, "u = 1");
assert_in!(out, r#"s = "rst""#);
assert_in!(out, r#"t = ("SS", -12)"#);

assert_in!(out, "u = 2");
assert_in!(out, r#"s = "srt""#);
assert_in!(out, r#"t = ("TT", -24)"#);
}

#[test]
Expand All @@ -147,21 +163,29 @@ mod dump_input_values {
15 | fn cases(s: S) {{}}
| ^ `S` cannot be formatted using `{{:?}}`", name)
.unindent());

assert_in!(output.stderr.str().to_string(), format!("
--> {}/src/lib.rs:21:11
|
21 | fn matrix(s: S) {{}}
| ^ `S` cannot be formatted using `{{:?}}`", name).unindent());
}

#[test]
fn can_exclude_some_inputs() {
let (output, _) = run_test("dump_exclude_some_fixtures.rs");
let (output, _) = run_test("dump_exclude_some_inputs.rs");
let out = output.stdout.str().to_string();

TestResults::new()
.fail("single_fail")
.fail("should_fail::case_1")
.fail("simple")
.fail("cases::case_1")
.fail("matrix::case_1_1_1")
.assert(output);

assert_in!(out, "fu32 = 42");
assert_in!(out, "d = D");
assert_in!(out, "fd = D");
assert_in!(out, "dd = D");
}

#[test]
Expand Down Expand Up @@ -442,6 +466,59 @@ mod cases {
}
}

mod matrix {
use super::*;

fn res(name: impl AsRef<Path>) -> impl AsRef<Path> {
Path::new("matrix").join(name.as_ref())
}

#[test]
fn should_compile() {
let output = prj(res("simple.rs"))
.compile()
.unwrap();

assert_eq!(Some(0), output.status.code(), "Compile error due: {}", output.stderr.str())
}

#[test]
fn happy_path() {
let (output, _) = run_test(res("simple.rs"));

TestResults::new()
.ok("strlen_test::case_1_1")
.ok("strlen_test::case_1_2")
.ok("strlen_test::case_2_1")
.ok("strlen_test::case_2_2")
.assert(output);
}

#[test]
fn should_apply_partial_fixture() {
let (output, _) = run_test(res("partial.rs"));

TestResults::new()
.ok("default::case_1_1")
.ok("default::case_1_2")
.ok("default::case_2_1")
.ok("partial_2::case_2_2")
.ok("complete::case_2_2")
.fail("default::case_2_2")
.fail("partial_1::case_1_1")
.fail("partial_1::case_1_2")
.fail("partial_1::case_2_1")
.fail("partial_1::case_2_2")
.fail("partial_2::case_1_1")
.fail("partial_2::case_1_2")
.fail("partial_2::case_2_1")
.fail("complete::case_1_1")
.fail("complete::case_1_2")
.fail("complete::case_2_1")
.assert(output);
}
}

mod should_show_correct_errors {
use std::process::Output;

Expand Down Expand Up @@ -518,14 +595,25 @@ mod should_show_correct_errors {
}

#[test]
fn if_wrong_type_param() {
fn if_wrong_type_case_param() {
let (output, name) = execute();

assert_in!(output.stderr.str(), format!("
error[E0308]: mismatched types
--> {}/src/lib.rs:17:26
|
17 | fn error_case_wrong_type(f: &str) {{}}", name).unindent());
}

#[test]
fn if_wrong_type_matrix_param() {
let (output, name) = execute();

assert_in!(output.stderr.str(), format!("
error[E0308]: mismatched types
--> {}/src/lib.rs:17:27
--> {}/src/lib.rs:53:28
|
17 | fn error_param_wrong_type(f: &str) {{}}", name).unindent());
53 | fn error_matrix_wrong_type(f: &str) {{}}", name).unindent());
}

#[test]
Expand All @@ -540,6 +628,16 @@ mod should_show_correct_errors {
| ^
| |",
name).unindent());

assert_in!(output.stderr.str(), format!("
error[E0308]: mismatched types
--> {}/src/lib.rs:55:45
|
55 | #[rstest(condition => [vec![1,2,3].contains(2)] )]
| ^
| |",
name).unindent());

}

#[test]
Expand Down Expand Up @@ -580,34 +678,65 @@ mod should_show_correct_errors {
| ^",
name).unindent());
}
}

mod matrix {
use super::*;
#[test]
fn if_list_argument_dont_match_function_signature() {
let (output, name) = execute();

assert_in!(output.stderr.str(), format!("
error: Missed argument: 'not_exist_1' should be a test function argument.
--> {}/src/lib.rs:63:10
|
63 | #[rstest(not_exist_1 => [42],
| ^^^^^^^^^^^",
name).unindent());

assert_in!(output.stderr.str(), format!("
error: Missed argument: 'not_exist_2' should be a test function argument.
--> {}/src/lib.rs:64:10
|
64 | not_exist_2 => [42])]
| ^^^^^^^^^^^",
name).unindent());

fn res(name: impl AsRef<Path>) -> impl AsRef<Path> {
Path::new("matrix").join(name.as_ref())
}

#[test]
fn should_compile() {
let output = prj(res("simple.rs"))
.compile()
.unwrap();
fn if_inject_a_fixture_that_is_already_a_value_list() {
let (output, name) = execute();

assert_eq!(Some(0), output.status.code(), "Compile error due: {}", output.stderr.str())
assert_in!(output.stderr.str(), format!("
error: Duplicate argument: 'f' is already defined.
--> {}/src/lib.rs:67:25
|
67 | #[rstest(f => [41, 42], f(42))]
| ^",
name).unindent());
}

#[test]
fn happy_path() {
let (output, _) = run_test(res("simple.rs"));
fn if_define_value_list_that_is_already_an_injected_fixture() {
let (output, name) = execute();

TestResults::new()
.ok("strlen_test::case_1_1")
.ok("strlen_test::case_1_2")
.ok("strlen_test::case_2_1")
.ok("strlen_test::case_2_2")
.assert(output);
assert_in!(output.stderr.str(), format!("
error: Duplicate argument: 'f' is already defined.
--> {}/src/lib.rs:71:17
|
71 | #[rstest(f(42), f => [41, 42])]
| ^",
name).unindent());
}

#[test]
fn if_a_value_contains_empty_list() {
let (output, name) = execute();

assert_in!(output.stderr.str(), format!("
error: Values list should not be empty
--> {}/src/lib.rs:60:19
|
60 | #[rstest(empty => [])]
| ^^",
name).unindent());
}
}

0 comments on commit 402d7d4

Please sign in to comment.