Skip to content

Commit 40087a7

Browse files
committed
Implementation and integration tests
1 parent fcf732d commit 40087a7

26 files changed

+1316
-457
lines changed

rstest/tests/fixture/mod.rs

+59-22
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,9 @@ mod should {
242242
output.stderr.str(),
243243
format!(
244244
r#"
245-
--> {}/src/lib.rs:12:33
245+
--> {name}/src/lib.rs:14:33
246246
|
247-
12 | fn error_cannot_resolve_fixture(no_fixture: u32) {{"#,
248-
name
247+
14 | fn error_cannot_resolve_fixture(no_fixture: u32) {{"#
249248
)
250249
.unindent()
251250
);
@@ -260,11 +259,10 @@ mod should {
260259
format!(
261260
r#"
262261
error[E0308]: mismatched types
263-
--> {}/src/lib.rs:8:18
264-
|
265-
8 | let a: u32 = "";
266-
"#,
267-
name
262+
--> {name}/src/lib.rs:10:18
263+
|
264+
10 | let a: u32 = "";
265+
"#
268266
)
269267
.unindent()
270268
);
@@ -277,20 +275,19 @@ mod should {
277275
assert_in!(
278276
output.stderr.str(),
279277
format!(
280-
"
278+
r#"
281279
error[E0308]: mismatched types
282-
--> {}/src/lib.rs:16:29
283-
",
284-
name
280+
--> {name}/src/lib.rs:17:29
281+
"#
285282
)
286283
.unindent()
287284
);
288285

289286
assert_in!(
290287
output.stderr.str(),
291-
"
292-
16 | fn error_fixture_wrong_type(fixture: String) {
293-
| ^^^^^^"
288+
r#"
289+
17 | fn error_fixture_wrong_type(fixture: String) {}
290+
| ^^^^^^"#
294291
.unindent()
295292
);
296293
}
@@ -304,12 +301,11 @@ mod should {
304301
format!(
305302
"
306303
error: Missed argument: 'not_a_fixture' should be a test function argument.
307-
--> {}/src/lib.rs:19:11
304+
--> {name}/src/lib.rs:19:11
308305
|
309306
19 | #[fixture(not_a_fixture(24))]
310307
| ^^^^^^^^^^^^^
311-
",
312-
name
308+
"
313309
)
314310
.unindent()
315311
);
@@ -324,15 +320,56 @@ mod should {
324320
format!(
325321
r#"
326322
error: Duplicate argument: 'f' is already defined.
327-
--> {}/src/lib.rs:33:23
323+
--> {name}/src/lib.rs:32:23
328324
|
329-
33 | #[fixture(f("first"), f("second"))]
325+
32 | #[fixture(f("first"), f("second"))]
330326
| ^
331-
"#,
332-
name
327+
"#
328+
)
329+
.unindent()
330+
);
331+
}
332+
333+
#[rstest]
334+
fn on_destruct_implicit_fixture(errors_rs: &(Output, String)) {
335+
let (output, name) = errors_rs.clone();
336+
337+
assert_in!(
338+
output.stderr.str(),
339+
format!(
340+
r#"
341+
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
342+
--> {name}/src/lib.rs:48:35
343+
|
344+
48 | fn error_destruct_without_resolve(T(a): T) {{}}
345+
| ^^^^^^^
346+
"#
347+
)
348+
.unindent()
349+
);
350+
}
351+
352+
#[rstest]
353+
fn on_destruct_explicit_fixture_without_from(errors_rs: &(Output, String)) {
354+
let (output, name) = errors_rs.clone();
355+
356+
assert_in!(
357+
output.stderr.str(),
358+
format!(
359+
r#"
360+
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
361+
--> {name}/src/lib.rs:51:57
362+
|
363+
51 | fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {{}}
364+
| ^^^^^^^
365+
"#
333366
)
334367
.unindent()
335368
);
369+
assert_eq!(
370+
1,
371+
output.stderr.str().count("51 | fn error_destruct_without")
372+
)
336373
}
337374

338375
#[fixture]
+24-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
use rstest::*;
22

33
#[fixture]
4-
pub fn fixture() -> u32 { 42 }
4+
pub fn fixture() -> u32 {
5+
42
6+
}
57

68
#[fixture]
79
fn error_inner(fixture: u32) {
810
let a: u32 = "";
911
}
1012

1113
#[fixture]
12-
fn error_cannot_resolve_fixture(no_fixture: u32) {
13-
}
14+
fn error_cannot_resolve_fixture(no_fixture: u32) {}
1415

1516
#[fixture]
16-
fn error_fixture_wrong_type(fixture: String) {
17-
}
17+
fn error_fixture_wrong_type(fixture: String) {}
1818

1919
#[fixture(not_a_fixture(24))]
20-
fn error_inject_an_invalid_fixture(fixture: String) {
21-
}
20+
fn error_inject_an_invalid_fixture(fixture: String) {}
2221

2322
#[fixture]
2423
fn name() -> &'static str {
@@ -31,5 +30,22 @@ fn f(name: &str) -> String {
3130
}
3231

3332
#[fixture(f("first"), f("second"))]
34-
fn error_inject_a_fixture_more_than_once(f: String) {
33+
fn error_inject_a_fixture_more_than_once(f: String) {}
34+
35+
struct T(u32);
36+
37+
#[fixture]
38+
fn structed() -> T {
39+
T(42)
3540
}
41+
42+
#[fixture]
43+
fn structed_injectd(fixture: u32) -> T {
44+
T(fixture)
45+
}
46+
47+
#[fixture]
48+
fn error_destruct_without_resolve(T(a): T) {}
49+
50+
#[fixture]
51+
fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use rstest::*;
2+
3+
struct T {
4+
a: u32,
5+
b: u32,
6+
}
7+
8+
impl T {
9+
fn new(a: u32, b: u32) -> Self {
10+
Self { a, b }
11+
}
12+
}
13+
14+
struct S(u32, u32);
15+
16+
#[fixture]
17+
fn fix() -> T {
18+
T::new(1, 42)
19+
}
20+
21+
#[fixture]
22+
fn named() -> S {
23+
S(1, 42)
24+
}
25+
26+
#[fixture]
27+
fn tuple() -> (u32, u32) {
28+
(1, 42)
29+
}
30+
31+
#[fixture]
32+
fn swap(#[from(fix)] T { a, b }: T) -> T {
33+
T::new(b, a)
34+
}
35+
36+
#[rstest]
37+
fn swapped(#[from(swap)] T { a, b }: T) {
38+
assert_eq!(a, 42);
39+
assert_eq!(b, 1);
40+
}
41+
42+
#[rstest]
43+
#[case::two_times_twenty_one(T::new(2, 21))]
44+
#[case::six_times_seven(T{ a: 6, b: 7 })]
45+
fn cases_destruct(
46+
#[from(fix)] T { a, b }: T,
47+
#[case] T { a: c, b: d }: T,
48+
#[values(T::new(42, 1), T{ a: 3, b: 14})] T { a: e, b: f }: T,
49+
) {
50+
assert_eq!(a * b, 42);
51+
assert_eq!(c * d, 42);
52+
assert_eq!(e * f, 42);
53+
}
54+
55+
#[rstest]
56+
#[case::two_times_twenty_one(S(2, 21))]
57+
#[case::six_times_seven(S(6, 7))]
58+
fn cases_destruct_named_tuple(
59+
#[from(named)] S(a, b): S,
60+
#[case] S(c, d): S,
61+
#[values(S(42, 1), S(3, 14))] S(e, f): S,
62+
) {
63+
assert_eq!(a * b, 42);
64+
assert_eq!(c * d, 42);
65+
assert_eq!(e * f, 42);
66+
}
67+
68+
#[rstest]
69+
#[case::two_times_twenty_one((2, 21))]
70+
#[case::six_times_seven((6, 7))]
71+
fn cases_destruct_tuple(
72+
#[from(tuple)] (a, b): (u32, u32),
73+
#[case] (c, d): (u32, u32),
74+
#[values((42, 1), (3, 14))] (e, f): (u32, u32),
75+
) {
76+
assert_eq!(a * b, 42);
77+
assert_eq!(c * d, 42);
78+
assert_eq!(e * f, 42);
79+
}

rstest/tests/resources/rstest/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,9 @@ fn error_timeout_without_duration() {}
118118

119119
#[rstest]
120120
fn error_absolute_path_files(#[files("/tmp/tmp.Q81idVZYAV/*.txt")] path: std::path::PathBuf) {}
121+
122+
struct T(u32, u32);
123+
124+
#[rstest]
125+
#[case(T(3, 4))]
126+
fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {}

rstest/tests/rstest/mod.rs

+55
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,27 @@ fn happy_path() {
973973
.assert(output);
974974
}
975975

976+
#[test]
977+
fn destruct() {
978+
let (output, _) = run_test("destruct.rs");
979+
980+
TestResults::new()
981+
.ok("cases_destruct::case_1_two_times_twenty_one::__destruct_3_1_T__new_42_1_")
982+
.ok("cases_destruct::case_1_two_times_twenty_one::__destruct_3_2_T_a_3_b_14_")
983+
.ok("cases_destruct::case_2_six_times_seven::__destruct_3_1_T__new_42_1_")
984+
.ok("cases_destruct::case_2_six_times_seven::__destruct_3_2_T_a_3_b_14_")
985+
.ok("cases_destruct_named_tuple::case_1_two_times_twenty_one::__destruct_3_1_S_42_1_")
986+
.ok("cases_destruct_named_tuple::case_1_two_times_twenty_one::__destruct_3_2_S_3_14_")
987+
.ok("cases_destruct_named_tuple::case_2_six_times_seven::__destruct_3_1_S_42_1_")
988+
.ok("cases_destruct_named_tuple::case_2_six_times_seven::__destruct_3_2_S_3_14_")
989+
.ok("cases_destruct_tuple::case_1_two_times_twenty_one::__destruct_3_1__42_1_")
990+
.ok("cases_destruct_tuple::case_1_two_times_twenty_one::__destruct_3_2__3_14_")
991+
.ok("cases_destruct_tuple::case_2_six_times_seven::__destruct_3_1__42_1_")
992+
.ok("cases_destruct_tuple::case_2_six_times_seven::__destruct_3_2__3_14_")
993+
.ok("swapped")
994+
.assert(output);
995+
}
996+
976997
#[test]
977998
fn rename() {
978999
let (output, _) = run_test("rename.rs");
@@ -1727,4 +1748,38 @@ mod should_show_correct_errors {
17271748
.unindent()
17281749
);
17291750
}
1751+
1752+
#[test]
1753+
fn try_to_destruct_implicit_fixture() {
1754+
let (output, name) = execute();
1755+
1756+
assert_in!(
1757+
output.stderr.str(),
1758+
format!(
1759+
r#"
1760+
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
1761+
--> {name}/src/lib.rs:126:27
1762+
|
1763+
126 | fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {{}}
1764+
| ^^^^^^^^^^"#,
1765+
)
1766+
.unindent()
1767+
);
1768+
1769+
assert_in!(
1770+
output.stderr.str(),
1771+
format!(
1772+
r#"
1773+
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
1774+
--> {name}/src/lib.rs:126:51
1775+
|
1776+
126 | fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {{}}
1777+
| ^^^^^^^^^^"#,
1778+
)
1779+
.unindent()
1780+
);
1781+
1782+
assert_not_in!(output.stderr.str(), "#[case] T(e, f): T");
1783+
assert_not_in!(output.stderr.str(), "#[values(T(1, 2))] T(g, h): T");
1784+
}
17301785
}

rstest_macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ actix-rt = "2.7.0"
4444
async-std = { version = "1.12.0", features = ["attributes"] }
4545
maplit = "1.0.2"
4646
pretty_assertions = "1.2.1"
47-
rstest = { path = "../rstest", default-features = false }
47+
rstest = { version = "0.20.0", default-features = false }
4848
rstest_reuse = { path = "../rstest_reuse" }
4949
rstest_test = { path = "../rstest_test" }
5050

0 commit comments

Comments
 (0)