Skip to content

Commit

Permalink
Implementation and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Aug 4, 2024
1 parent fcf732d commit 40087a7
Show file tree
Hide file tree
Showing 26 changed files with 1,316 additions and 457 deletions.
81 changes: 59 additions & 22 deletions rstest/tests/fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,9 @@ mod should {
output.stderr.str(),
format!(
r#"
--> {}/src/lib.rs:12:33
--> {name}/src/lib.rs:14:33
|
12 | fn error_cannot_resolve_fixture(no_fixture: u32) {{"#,
name
14 | fn error_cannot_resolve_fixture(no_fixture: u32) {{"#
)
.unindent()
);
Expand All @@ -260,11 +259,10 @@ mod should {
format!(
r#"
error[E0308]: mismatched types
--> {}/src/lib.rs:8:18
|
8 | let a: u32 = "";
"#,
name
--> {name}/src/lib.rs:10:18
|
10 | let a: u32 = "";
"#
)
.unindent()
);
Expand All @@ -277,20 +275,19 @@ mod should {
assert_in!(
output.stderr.str(),
format!(
"
r#"
error[E0308]: mismatched types
--> {}/src/lib.rs:16:29
",
name
--> {name}/src/lib.rs:17:29
"#
)
.unindent()
);

assert_in!(
output.stderr.str(),
"
16 | fn error_fixture_wrong_type(fixture: String) {
| ^^^^^^"
r#"
17 | fn error_fixture_wrong_type(fixture: String) {}
| ^^^^^^"#
.unindent()
);
}
Expand All @@ -304,12 +301,11 @@ mod should {
format!(
"
error: Missed argument: 'not_a_fixture' should be a test function argument.
--> {}/src/lib.rs:19:11
--> {name}/src/lib.rs:19:11
|
19 | #[fixture(not_a_fixture(24))]
| ^^^^^^^^^^^^^
",
name
"
)
.unindent()
);
Expand All @@ -324,15 +320,56 @@ mod should {
format!(
r#"
error: Duplicate argument: 'f' is already defined.
--> {}/src/lib.rs:33:23
--> {name}/src/lib.rs:32:23
|
33 | #[fixture(f("first"), f("second"))]
32 | #[fixture(f("first"), f("second"))]
| ^
"#,
name
"#
)
.unindent()
);
}

#[rstest]
fn on_destruct_implicit_fixture(errors_rs: &(Output, String)) {
let (output, name) = errors_rs.clone();

assert_in!(
output.stderr.str(),
format!(
r#"
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
--> {name}/src/lib.rs:48:35
|
48 | fn error_destruct_without_resolve(T(a): T) {{}}
| ^^^^^^^
"#
)
.unindent()
);
}

#[rstest]
fn on_destruct_explicit_fixture_without_from(errors_rs: &(Output, String)) {
let (output, name) = errors_rs.clone();

assert_in!(
output.stderr.str(),
format!(
r#"
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
--> {name}/src/lib.rs:51:57
|
51 | fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {{}}
| ^^^^^^^
"#
)
.unindent()
);
assert_eq!(
1,
output.stderr.str().count("51 | fn error_destruct_without")
)
}

#[fixture]
Expand Down
32 changes: 24 additions & 8 deletions rstest/tests/resources/fixture/errors.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use rstest::*;

#[fixture]
pub fn fixture() -> u32 { 42 }
pub fn fixture() -> u32 {
42
}

#[fixture]
fn error_inner(fixture: u32) {
let a: u32 = "";
}

#[fixture]
fn error_cannot_resolve_fixture(no_fixture: u32) {
}
fn error_cannot_resolve_fixture(no_fixture: u32) {}

#[fixture]
fn error_fixture_wrong_type(fixture: String) {
}
fn error_fixture_wrong_type(fixture: String) {}

#[fixture(not_a_fixture(24))]
fn error_inject_an_invalid_fixture(fixture: String) {
}
fn error_inject_an_invalid_fixture(fixture: String) {}

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

#[fixture(f("first"), f("second"))]
fn error_inject_a_fixture_more_than_once(f: String) {
fn error_inject_a_fixture_more_than_once(f: String) {}

struct T(u32);

#[fixture]
fn structed() -> T {
T(42)
}

#[fixture]
fn structed_injectd(fixture: u32) -> T {
T(fixture)
}

#[fixture]
fn error_destruct_without_resolve(T(a): T) {}

#[fixture]
fn error_destruct_without_resolve_also_with(#[with(21)] T(a): T) {}
79 changes: 79 additions & 0 deletions rstest/tests/resources/rstest/destruct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use rstest::*;

struct T {
a: u32,
b: u32,
}

impl T {
fn new(a: u32, b: u32) -> Self {
Self { a, b }
}
}

struct S(u32, u32);

#[fixture]
fn fix() -> T {
T::new(1, 42)
}

#[fixture]
fn named() -> S {
S(1, 42)
}

#[fixture]
fn tuple() -> (u32, u32) {
(1, 42)
}

#[fixture]
fn swap(#[from(fix)] T { a, b }: T) -> T {
T::new(b, a)
}

#[rstest]
fn swapped(#[from(swap)] T { a, b }: T) {
assert_eq!(a, 42);
assert_eq!(b, 1);
}

#[rstest]
#[case::two_times_twenty_one(T::new(2, 21))]
#[case::six_times_seven(T{ a: 6, b: 7 })]
fn cases_destruct(
#[from(fix)] T { a, b }: T,
#[case] T { a: c, b: d }: T,
#[values(T::new(42, 1), T{ a: 3, b: 14})] T { a: e, b: f }: T,
) {
assert_eq!(a * b, 42);
assert_eq!(c * d, 42);
assert_eq!(e * f, 42);
}

#[rstest]
#[case::two_times_twenty_one(S(2, 21))]
#[case::six_times_seven(S(6, 7))]
fn cases_destruct_named_tuple(
#[from(named)] S(a, b): S,
#[case] S(c, d): S,
#[values(S(42, 1), S(3, 14))] S(e, f): S,
) {
assert_eq!(a * b, 42);
assert_eq!(c * d, 42);
assert_eq!(e * f, 42);
}

#[rstest]
#[case::two_times_twenty_one((2, 21))]
#[case::six_times_seven((6, 7))]
fn cases_destruct_tuple(
#[from(tuple)] (a, b): (u32, u32),
#[case] (c, d): (u32, u32),
#[values((42, 1), (3, 14))] (e, f): (u32, u32),
) {
assert_eq!(a * b, 42);
assert_eq!(c * d, 42);
assert_eq!(e * f, 42);
}
6 changes: 6 additions & 0 deletions rstest/tests/resources/rstest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ fn error_timeout_without_duration() {}

#[rstest]
fn error_absolute_path_files(#[files("/tmp/tmp.Q81idVZYAV/*.txt")] path: std::path::PathBuf) {}

struct T(u32, u32);

#[rstest]
#[case(T(3, 4))]
fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {}
55 changes: 55 additions & 0 deletions rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,27 @@ fn happy_path() {
.assert(output);
}

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

TestResults::new()
.ok("cases_destruct::case_1_two_times_twenty_one::__destruct_3_1_T__new_42_1_")
.ok("cases_destruct::case_1_two_times_twenty_one::__destruct_3_2_T_a_3_b_14_")
.ok("cases_destruct::case_2_six_times_seven::__destruct_3_1_T__new_42_1_")
.ok("cases_destruct::case_2_six_times_seven::__destruct_3_2_T_a_3_b_14_")
.ok("cases_destruct_named_tuple::case_1_two_times_twenty_one::__destruct_3_1_S_42_1_")
.ok("cases_destruct_named_tuple::case_1_two_times_twenty_one::__destruct_3_2_S_3_14_")
.ok("cases_destruct_named_tuple::case_2_six_times_seven::__destruct_3_1_S_42_1_")
.ok("cases_destruct_named_tuple::case_2_six_times_seven::__destruct_3_2_S_3_14_")
.ok("cases_destruct_tuple::case_1_two_times_twenty_one::__destruct_3_1__42_1_")
.ok("cases_destruct_tuple::case_1_two_times_twenty_one::__destruct_3_2__3_14_")
.ok("cases_destruct_tuple::case_2_six_times_seven::__destruct_3_1__42_1_")
.ok("cases_destruct_tuple::case_2_six_times_seven::__destruct_3_2__3_14_")
.ok("swapped")
.assert(output);
}

#[test]
fn rename() {
let (output, _) = run_test("rename.rs");
Expand Down Expand Up @@ -1727,4 +1748,38 @@ mod should_show_correct_errors {
.unindent()
);
}

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

assert_in!(
output.stderr.str(),
format!(
r#"
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
--> {name}/src/lib.rs:126:27
|
126 | fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {{}}
| ^^^^^^^^^^"#,
)
.unindent()
);

assert_in!(
output.stderr.str(),
format!(
r#"
error: To destruct a fixture you should provide a path to resolve it by '#[from(...)]' attribute.
--> {name}/src/lib.rs:126:51
|
126 | fn wrong_destruct_fixture(T(a, b): T, #[with(42)] T(c, d): T) {{}}
| ^^^^^^^^^^"#,
)
.unindent()
);

assert_not_in!(output.stderr.str(), "#[case] T(e, f): T");
assert_not_in!(output.stderr.str(), "#[values(T(1, 2))] T(g, h): T");
}
}
2 changes: 1 addition & 1 deletion rstest_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ actix-rt = "2.7.0"
async-std = { version = "1.12.0", features = ["attributes"] }
maplit = "1.0.2"
pretty_assertions = "1.2.1"
rstest = { path = "../rstest", default-features = false }
rstest = { version = "0.20.0", default-features = false }
rstest_reuse = { path = "../rstest_reuse" }
rstest_test = { path = "../rstest_test" }

Expand Down
Loading

0 comments on commit 40087a7

Please sign in to comment.