Skip to content

Commit d1009f6

Browse files
committed
#42 Error if args but no cases
1 parent 12c52ce commit d1009f6

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

playground/src/main.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
use rstest::*;
22

3-
#[fixture]
4-
fn first() -> u32 {
5-
42
6-
}
7-
8-
#[fixture]
9-
fn second() -> &'static str {
10-
"foo"
11-
}
12-
13-
#[fixture]
14-
fn double(first: u32, second: &str) -> u32 {
15-
0
16-
}
17-
18-
19-
#[rstest(double("pippo"))]
20-
fn should_show_type_error(double: u32) {}
3+
#[rstest(one, two, three)]
4+
fn should_show_error_for_no_case(one: u32, two: u32, three: u32) {}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use rstest::rstest;
2+
3+
#[rstest(one, two, three)]
4+
fn should_show_error_for_no_case(one: u32, two: u32, three: u32) {}

src/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,10 @@ pub fn rstest(args: proc_macro::TokenStream,
569569
let errors = errors_in_rstest(&test, &info);
570570

571571
if errors.is_empty() {
572-
if info.data.cases().next().is_none() {
573-
render_single_case(test, info)
574-
} else {
572+
if info.data.has_cases() {
575573
render_parametrize_cases(test, info)
574+
} else {
575+
render_single_case(test, info)
576576
}
577577
} else {
578578
errors
@@ -640,6 +640,17 @@ fn invalid_case_errors(params: &RsTestData) -> Errors {
640640
)
641641
}
642642

643+
fn case_args_without_cases(params: &RsTestData) -> Errors {
644+
if !params.has_cases() {
645+
return Box::new(params.case_args().map(|a|
646+
syn::Error::new(a.span(), "No cases for this argument."))
647+
);
648+
}
649+
Box::new(
650+
std::iter::empty()
651+
)
652+
}
653+
643654
fn errors_in_parametrize(test: &ItemFn, info: &RsTestInfo) -> TokenStream {
644655
missed_arguments_errors(test, info.data.case_args())
645656
.chain(missed_arguments_errors(test, info.data.fixtures()))
@@ -661,6 +672,7 @@ fn errors_in_rstest(test: &ItemFn, info: &parse::rstest::RsTestInfo) -> TokenStr
661672
missed_arguments_errors(test, info.data.items.iter())
662673
.chain(duplicate_arguments_errors(info.data.items.iter()))
663674
.chain(invalid_case_errors(&info.data))
675+
.chain(case_args_without_cases(&info.data))
664676
.map(|e| e.to_compile_error())
665677
.collect()
666678
}

src/parse/rstest.rs

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ impl RsTestData {
4747
)
4848
}
4949

50+
#[allow(dead_code)]
51+
pub(crate) fn has_case_args(&self) -> bool {
52+
self.case_args().next().is_some()
53+
}
54+
5055
pub(crate) fn cases(&self) -> impl Iterator<Item=&TestCase> {
5156
self.items.iter()
5257
.filter_map(|it|
@@ -57,6 +62,10 @@ impl RsTestData {
5762
)
5863
}
5964

65+
pub(crate) fn has_cases(&self) -> bool {
66+
self.cases().next().is_some()
67+
}
68+
6069
pub(crate) fn fixtures(&self) -> impl Iterator<Item=&Fixture> {
6170
self.items.iter().filter_map(|it|
6271
match it {
@@ -65,6 +74,11 @@ impl RsTestData {
6574
}
6675
)
6776
}
77+
78+
#[allow(dead_code)]
79+
pub(crate) fn has_fixtures(&self) -> bool {
80+
self.fixtures().next().is_some()
81+
}
6882
}
6983

7084
impl Parse for RsTestData {

tests/rstest/mod.rs

+42
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,48 @@ mod cases {
232232
}
233233
}
234234

235+
mod not_compile_if_args_but_no_cases {
236+
use super::*;
237+
238+
use lazy_static::lazy_static;
239+
use std::process::Output;
240+
241+
//noinspection RsTypeCheck
242+
fn execute() -> &'static (Output, String) {
243+
lazy_static! {
244+
static ref OUTPUT: (Output, String) =
245+
run_test("args_with_no_cases.rs");
246+
}
247+
assert_ne!(Some(0), OUTPUT.0.status.code(), "Should not compile");
248+
&OUTPUT
249+
}
250+
251+
#[test]
252+
fn report_error() {
253+
let (output, name) = execute();
254+
let stderr = output.stderr.str();
255+
256+
assert_in!(stderr, format!("
257+
error: No cases for this argument.
258+
--> {}/src/lib.rs:3:10
259+
|
260+
3 | #[rstest(one, two, three)]
261+
| ^^^
262+
", name).unindent());
263+
264+
}
265+
266+
#[test]
267+
fn and_reports_all_errors() {
268+
let (output, _) = execute();
269+
let stderr = output.stderr.str();
270+
271+
// Exactly 3 cases are wrong
272+
assert_eq!(3, stderr.count("No cases for this argument."),
273+
"Should contain message exactly 3 occurrences in error message:\n{}", stderr);
274+
}
275+
}
276+
235277
mod dump_input_values {
236278
use super::*;
237279

0 commit comments

Comments
 (0)