Skip to content

Commit 402d7d4

Browse files
committed
#42 Integration tests in rstest for matrix case.
1 parent 6e7607d commit 402d7d4

File tree

6 files changed

+240
-31
lines changed

6 files changed

+240
-31
lines changed

resources/rstest/dump_debug.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ fn single_fail(fu32: u32, fstring: String, ftuple: (A, String, i32)) {
2020
case(24, "trs", ("tt", -24))
2121
::trace
2222
)]
23-
fn should_fail(u: u32, s: &str, t: (&str, i32)) {
23+
fn cases_fail(u: u32, s: &str, t: (&str, i32)) {
24+
assert!(false);
25+
}
26+
27+
#[rstest(
28+
u => [1, 2],
29+
s => ["rst", "srt"],
30+
t => [("SS", -12), ("TT", -24)]
31+
::trace
32+
)]
33+
fn matrix_fail(u: u32, s: &str, t: (&str, i32)) {
2434
assert!(false);
2535
}

resources/rstest/dump_exclude_some_fixtures.rs resources/rstest/dump_exclude_some_inputs.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@ fn fa() -> A { A {} }
1818
#[rstest(
1919
::trace::notrace(fa,fb))
2020
]
21-
fn single_fail(fu32: u32, fa: A, fb: B, fd: D) {
21+
fn simple(fu32: u32, fa: A, fb: B, fd: D) {
2222
assert!(false);
2323
}
2424

2525
#[rstest(a,b,d,
2626
case(A{}, B{}, D{})
2727
::trace::notrace(a,b))
2828
]
29-
fn should_fail(fu32: u32, a: A, b: B, d: D) {
29+
fn cases(fu32: u32, a: A, b: B, d: D) {
30+
assert!(false);
31+
}
32+
33+
#[rstest(
34+
a => [A{}],
35+
b => [B{}],
36+
dd => [D{}],
37+
::trace::notrace(a,b))
38+
]
39+
fn matrix(fu32: u32, a: A, b: B, dd: D) {
3040
assert!(false);
3141
}

resources/rstest/dump_not_debug.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ fn single(fixture: S) {}
1313
::trace
1414
)]
1515
fn cases(s: S) {}
16+
17+
#[rstest(
18+
s => [S{}]
19+
::trace
20+
)]
21+
fn matrix(s: S) {}

resources/rstest/errors.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ fn error_cannot_resolve_fixture(no_fixture: u32, f: u32) {}
1414
fn error_fixture_wrong_type(fixture: String, f: u32) {}
1515

1616
#[rstest(f, case(42))]
17-
fn error_param_wrong_type(f: &str) {}
17+
fn error_case_wrong_type(f: &str) {}
1818

1919
#[rstest(condition,
2020
case(vec![1,2,3].contains(2)))
2121
]
22-
fn error_in_arbitrary_rust_code(condition: bool) {
22+
fn error_in_arbitrary_rust_code_cases(condition: bool) {
2323
assert!(condition)
2424
}
2525

@@ -48,3 +48,26 @@ fn error_define_case_that_is_already_an_injected_fixture(f: u32) {
4848
#[rstest(v, f(42), f(42), case(12))]
4949
fn error_inject_a_fixture_more_than_once(v: u32, f: u32) {
5050
}
51+
52+
#[rstest(f => [42])]
53+
fn error_matrix_wrong_type(f: &str) {}
54+
55+
#[rstest(condition => [vec![1,2,3].contains(2)] )]
56+
fn error_arbitrary_rust_code_matrix(condition: bool) {
57+
assert!(condition)
58+
}
59+
60+
#[rstest(empty => [])]
61+
fn error_empty_list(empty: &str) {}
62+
63+
#[rstest(not_exist_1 => [42],
64+
not_exist_2 => [42])]
65+
fn error_no_match_args() {}
66+
67+
#[rstest(f => [41, 42], f(42))]
68+
fn error_inject_a_fixture_that_is_already_a_value_list(f: u32) {
69+
}
70+
71+
#[rstest(f(42), f => [41, 42])]
72+
fn error_define_a_value_list_that_is_already_an_injected_fixture(f: u32) {
73+
}

resources/rstest/matrix/partial.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use rstest::*;
2+
3+
#[fixture]
4+
fn f1() -> u32 { 0 }
5+
#[fixture]
6+
fn f2() -> u32 { 0 }
7+
#[fixture]
8+
fn f3() -> u32 { 0 }
9+
10+
#[fixture]
11+
fn fixture(f1: u32, f2: u32, f3: u32) -> u32 { f1 + f2 + 2 * f3 }
12+
13+
#[rstest(a => [0, 1], b => [0, 2])]
14+
fn default(fixture: u32, a: u32, b: u32) {
15+
assert_eq!(fixture, a * b);
16+
}
17+
18+
#[rstest(a => [0, 1], b => [0, 2], fixture(1))]
19+
fn partial_1(fixture: u32, a: u32, b: u32) {
20+
assert_eq!(fixture, a * b);
21+
}
22+
23+
#[rstest(a => [0, 1], b => [0, 2], fixture(0, 2))]
24+
fn partial_2(fixture: u32, a: u32, b: u32) {
25+
assert_eq!(fixture, a * b);
26+
}
27+
28+
#[rstest(a => [0, 1], b => [0, 2], fixture(0, 0, 1))]
29+
fn complete(fixture: u32, a: u32, b: u32) {
30+
assert_eq!(fixture, a * b);
31+
}

tests/rstest/mod.rs

+155-26
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ mod dump_input_values {
113113

114114
TestResults::new()
115115
.fail("single_fail")
116-
.fail("should_fail::case_1")
117-
.fail("should_fail::case_2")
116+
.fail("cases_fail::case_1")
117+
.fail("cases_fail::case_2")
118+
.fail("matrix_fail::case_1_1_1")
119+
.fail("matrix_fail::case_1_1_2")
120+
.fail("matrix_fail::case_1_2_1")
121+
.fail("matrix_fail::case_1_2_2")
122+
.fail("matrix_fail::case_2_1_1")
123+
.fail("matrix_fail::case_2_1_2")
124+
.fail("matrix_fail::case_1_2_1")
125+
.fail("matrix_fail::case_2_2_2")
118126
.assert(output);
119127

120128
assert_in!(out, "fu32 = 42");
@@ -128,6 +136,14 @@ mod dump_input_values {
128136
assert_in!(out, "u = 24");
129137
assert_in!(out, r#"s = "trs""#);
130138
assert_in!(out, r#"t = ("tt", -24)"#);
139+
140+
assert_in!(out, "u = 1");
141+
assert_in!(out, r#"s = "rst""#);
142+
assert_in!(out, r#"t = ("SS", -12)"#);
143+
144+
assert_in!(out, "u = 2");
145+
assert_in!(out, r#"s = "srt""#);
146+
assert_in!(out, r#"t = ("TT", -24)"#);
131147
}
132148

133149
#[test]
@@ -147,21 +163,29 @@ mod dump_input_values {
147163
15 | fn cases(s: S) {{}}
148164
| ^ `S` cannot be formatted using `{{:?}}`", name)
149165
.unindent());
166+
167+
assert_in!(output.stderr.str().to_string(), format!("
168+
--> {}/src/lib.rs:21:11
169+
|
170+
21 | fn matrix(s: S) {{}}
171+
| ^ `S` cannot be formatted using `{{:?}}`", name).unindent());
150172
}
151173

152174
#[test]
153175
fn can_exclude_some_inputs() {
154-
let (output, _) = run_test("dump_exclude_some_fixtures.rs");
176+
let (output, _) = run_test("dump_exclude_some_inputs.rs");
155177
let out = output.stdout.str().to_string();
156178

157179
TestResults::new()
158-
.fail("single_fail")
159-
.fail("should_fail::case_1")
180+
.fail("simple")
181+
.fail("cases::case_1")
182+
.fail("matrix::case_1_1_1")
160183
.assert(output);
161184

162185
assert_in!(out, "fu32 = 42");
163186
assert_in!(out, "d = D");
164187
assert_in!(out, "fd = D");
188+
assert_in!(out, "dd = D");
165189
}
166190

167191
#[test]
@@ -442,6 +466,59 @@ mod cases {
442466
}
443467
}
444468

469+
mod matrix {
470+
use super::*;
471+
472+
fn res(name: impl AsRef<Path>) -> impl AsRef<Path> {
473+
Path::new("matrix").join(name.as_ref())
474+
}
475+
476+
#[test]
477+
fn should_compile() {
478+
let output = prj(res("simple.rs"))
479+
.compile()
480+
.unwrap();
481+
482+
assert_eq!(Some(0), output.status.code(), "Compile error due: {}", output.stderr.str())
483+
}
484+
485+
#[test]
486+
fn happy_path() {
487+
let (output, _) = run_test(res("simple.rs"));
488+
489+
TestResults::new()
490+
.ok("strlen_test::case_1_1")
491+
.ok("strlen_test::case_1_2")
492+
.ok("strlen_test::case_2_1")
493+
.ok("strlen_test::case_2_2")
494+
.assert(output);
495+
}
496+
497+
#[test]
498+
fn should_apply_partial_fixture() {
499+
let (output, _) = run_test(res("partial.rs"));
500+
501+
TestResults::new()
502+
.ok("default::case_1_1")
503+
.ok("default::case_1_2")
504+
.ok("default::case_2_1")
505+
.ok("partial_2::case_2_2")
506+
.ok("complete::case_2_2")
507+
.fail("default::case_2_2")
508+
.fail("partial_1::case_1_1")
509+
.fail("partial_1::case_1_2")
510+
.fail("partial_1::case_2_1")
511+
.fail("partial_1::case_2_2")
512+
.fail("partial_2::case_1_1")
513+
.fail("partial_2::case_1_2")
514+
.fail("partial_2::case_2_1")
515+
.fail("complete::case_1_1")
516+
.fail("complete::case_1_2")
517+
.fail("complete::case_2_1")
518+
.assert(output);
519+
}
520+
}
521+
445522
mod should_show_correct_errors {
446523
use std::process::Output;
447524

@@ -518,14 +595,25 @@ mod should_show_correct_errors {
518595
}
519596

520597
#[test]
521-
fn if_wrong_type_param() {
598+
fn if_wrong_type_case_param() {
599+
let (output, name) = execute();
600+
601+
assert_in!(output.stderr.str(), format!("
602+
error[E0308]: mismatched types
603+
--> {}/src/lib.rs:17:26
604+
|
605+
17 | fn error_case_wrong_type(f: &str) {{}}", name).unindent());
606+
}
607+
608+
#[test]
609+
fn if_wrong_type_matrix_param() {
522610
let (output, name) = execute();
523611

524612
assert_in!(output.stderr.str(), format!("
525613
error[E0308]: mismatched types
526-
--> {}/src/lib.rs:17:27
614+
--> {}/src/lib.rs:53:28
527615
|
528-
17 | fn error_param_wrong_type(f: &str) {{}}", name).unindent());
616+
53 | fn error_matrix_wrong_type(f: &str) {{}}", name).unindent());
529617
}
530618

531619
#[test]
@@ -540,6 +628,16 @@ mod should_show_correct_errors {
540628
| ^
541629
| |",
542630
name).unindent());
631+
632+
assert_in!(output.stderr.str(), format!("
633+
error[E0308]: mismatched types
634+
--> {}/src/lib.rs:55:45
635+
|
636+
55 | #[rstest(condition => [vec![1,2,3].contains(2)] )]
637+
| ^
638+
| |",
639+
name).unindent());
640+
543641
}
544642

545643
#[test]
@@ -580,34 +678,65 @@ mod should_show_correct_errors {
580678
| ^",
581679
name).unindent());
582680
}
583-
}
584681

585-
mod matrix {
586-
use super::*;
682+
#[test]
683+
fn if_list_argument_dont_match_function_signature() {
684+
let (output, name) = execute();
685+
686+
assert_in!(output.stderr.str(), format!("
687+
error: Missed argument: 'not_exist_1' should be a test function argument.
688+
--> {}/src/lib.rs:63:10
689+
|
690+
63 | #[rstest(not_exist_1 => [42],
691+
| ^^^^^^^^^^^",
692+
name).unindent());
693+
694+
assert_in!(output.stderr.str(), format!("
695+
error: Missed argument: 'not_exist_2' should be a test function argument.
696+
--> {}/src/lib.rs:64:10
697+
|
698+
64 | not_exist_2 => [42])]
699+
| ^^^^^^^^^^^",
700+
name).unindent());
587701

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

592704
#[test]
593-
fn should_compile() {
594-
let output = prj(res("simple.rs"))
595-
.compile()
596-
.unwrap();
705+
fn if_inject_a_fixture_that_is_already_a_value_list() {
706+
let (output, name) = execute();
597707

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

601717
#[test]
602-
fn happy_path() {
603-
let (output, _) = run_test(res("simple.rs"));
718+
fn if_define_value_list_that_is_already_an_injected_fixture() {
719+
let (output, name) = execute();
604720

605-
TestResults::new()
606-
.ok("strlen_test::case_1_1")
607-
.ok("strlen_test::case_1_2")
608-
.ok("strlen_test::case_2_1")
609-
.ok("strlen_test::case_2_2")
610-
.assert(output);
721+
assert_in!(output.stderr.str(), format!("
722+
error: Duplicate argument: 'f' is already defined.
723+
--> {}/src/lib.rs:71:17
724+
|
725+
71 | #[rstest(f(42), f => [41, 42])]
726+
| ^",
727+
name).unindent());
611728
}
612729

730+
#[test]
731+
fn if_a_value_contains_empty_list() {
732+
let (output, name) = execute();
733+
734+
assert_in!(output.stderr.str(), format!("
735+
error: Values list should not be empty
736+
--> {}/src/lib.rs:60:19
737+
|
738+
60 | #[rstest(empty => [])]
739+
| ^^",
740+
name).unindent());
741+
}
613742
}

0 commit comments

Comments
 (0)