Skip to content

Commit 9dbe79b

Browse files
committed
test(platform): Switch to snapbox
1 parent 872225e commit 9dbe79b

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cargo-platform/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ description = "Cargo's representation of a target platform."
1212
[dependencies]
1313
serde.workspace = true
1414

15+
[dev-dependencies]
16+
snapbox.workspace = true
17+
1518
[lints]
1619
workspace = true

crates/cargo-platform/tests/test_cfg.rs

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use cargo_platform::{Cfg, CfgExpr, Ident, Platform};
21
use std::fmt;
32
use std::str::FromStr;
43

4+
use cargo_platform::{Cfg, CfgExpr, Ident, Platform};
5+
use snapbox::assert_data_eq;
6+
use snapbox::prelude::*;
7+
use snapbox::str;
8+
59
macro_rules! c {
610
($a:ident) => {
711
Cfg::Name(Ident {
@@ -57,23 +61,18 @@ where
5761
assert_eq!(c, expected);
5862
}
5963

60-
fn bad<T>(s: &str, err: &str)
64+
#[track_caller]
65+
fn bad<T>(input: &str, expected: impl IntoData)
6166
where
6267
T: FromStr + fmt::Display,
6368
T::Err: fmt::Display,
6469
{
65-
let e = match T::from_str(s) {
66-
Ok(cfg) => panic!("expected `{}` to not parse but got {}", s, cfg),
70+
dbg!(input);
71+
let actual = match T::from_str(input) {
72+
Ok(cfg) => panic!("expected `{input}` to not parse but got {cfg}"),
6773
Err(e) => e.to_string(),
6874
};
69-
assert!(
70-
e.contains(err),
71-
"when parsing `{}`,\n\"{}\" not contained \
72-
inside: {}",
73-
s,
74-
err,
75-
e
76-
);
75+
assert_data_eq!(actual, expected.raw());
7776
}
7877

7978
#[test]
@@ -93,24 +92,38 @@ fn cfg_syntax() {
9392

9493
#[test]
9594
fn cfg_syntax_bad() {
96-
bad::<Cfg>("", "but cfg expression ended");
97-
bad::<Cfg>(" ", "but cfg expression ended");
98-
bad::<Cfg>("\t", "unexpected character");
99-
bad::<Cfg>("7", "unexpected character");
100-
bad::<Cfg>("=", "expected identifier");
101-
bad::<Cfg>(",", "expected identifier");
102-
bad::<Cfg>("(", "expected identifier");
103-
bad::<Cfg>("foo (", "unexpected content `(` found after cfg expression");
104-
bad::<Cfg>("bar =", "expected a string");
105-
bad::<Cfg>("bar = \"", "unterminated string");
10695
bad::<Cfg>(
107-
"foo, bar",
108-
"unexpected content `, bar` found after cfg expression",
96+
"",
97+
str![
98+
"failed to parse `` as a cfg expression: expected identifier, but cfg expression ended"
99+
],
100+
);
101+
bad::<Cfg>(" ", str!["failed to parse ` ` as a cfg expression: expected identifier, but cfg expression ended"]);
102+
bad::<Cfg>("\t", str!["failed to parse ` ` as a cfg expression: unexpected character ` ` in cfg, expected parens, a comma, an identifier, or a string"]);
103+
bad::<Cfg>("7", str!["failed to parse `7` as a cfg expression: unexpected character `7` in cfg, expected parens, a comma, an identifier, or a string"]);
104+
bad::<Cfg>(
105+
"=",
106+
str!["failed to parse `=` as a cfg expression: expected identifier, found `=`"],
107+
);
108+
bad::<Cfg>(
109+
",",
110+
str!["failed to parse `,` as a cfg expression: expected identifier, found `,`"],
111+
);
112+
bad::<Cfg>(
113+
"(",
114+
str!["failed to parse `(` as a cfg expression: expected identifier, found `(`"],
109115
);
110-
bad::<Cfg>("r# foo", "unexpected character");
111-
bad::<Cfg>("r #foo", "unexpected content");
112-
bad::<Cfg>("r#\"foo\"", "unexpected character");
113-
bad::<Cfg>("foo = r#\"\"", "unexpected character");
116+
bad::<Cfg>("foo (", str!["failed to parse `foo (` as a cfg expression: unexpected content `(` found after cfg expression"]);
117+
bad::<Cfg>("bar =", str!["failed to parse `bar =` as a cfg expression: expected a string, but cfg expression ended"]);
118+
bad::<Cfg>(
119+
"bar = \"",
120+
str![[r#"failed to parse `bar = "` as a cfg expression: unterminated string in cfg"#]],
121+
);
122+
bad::<Cfg>("foo, bar", str!["failed to parse `foo, bar` as a cfg expression: unexpected content `, bar` found after cfg expression"]);
123+
bad::<Cfg>("r# foo", str!["failed to parse `r# foo` as a cfg expression: unexpected character ` ` in cfg, expected parens, a comma, an identifier, or a string"]);
124+
bad::<Cfg>("r #foo", str!["failed to parse `r #foo` as a cfg expression: unexpected content `#foo` found after cfg expression"]);
125+
bad::<Cfg>("r#\"foo\"", str![[r#"failed to parse `r#"foo"` as a cfg expression: unexpected character `"` in cfg, expected parens, a comma, an identifier, or a string"#]]);
126+
bad::<Cfg>("foo = r#\"\"", str![[r#"failed to parse `foo = r#""` as a cfg expression: unexpected character `"` in cfg, expected parens, a comma, an identifier, or a string"#]]);
114127
}
115128

116129
#[test]
@@ -137,17 +150,29 @@ fn cfg_expr() {
137150

138151
#[test]
139152
fn cfg_expr_bad() {
140-
bad::<CfgExpr>(" ", "but cfg expression ended");
141-
bad::<CfgExpr>(" all", "expected `(`");
142-
bad::<CfgExpr>("all(a", "expected `)`");
143-
bad::<CfgExpr>("not", "expected `(`");
144-
bad::<CfgExpr>("not(a", "expected `)`");
145-
bad::<CfgExpr>("a = ", "expected a string");
146-
bad::<CfgExpr>("all(not())", "expected identifier");
153+
bad::<CfgExpr>(" ", str!["failed to parse ` ` as a cfg expression: expected start of a cfg expression, but cfg expression ended"]);
154+
bad::<CfgExpr>(
155+
" all",
156+
str!["failed to parse ` all` as a cfg expression: expected `(`, but cfg expression ended"],
157+
);
158+
bad::<CfgExpr>(
159+
"all(a",
160+
str!["failed to parse `all(a` as a cfg expression: expected `)`, but cfg expression ended"],
161+
);
162+
bad::<CfgExpr>(
163+
"not",
164+
str!["failed to parse `not` as a cfg expression: expected `(`, but cfg expression ended"],
165+
);
166+
bad::<CfgExpr>(
167+
"not(a",
168+
str!["failed to parse `not(a` as a cfg expression: expected `)`, but cfg expression ended"],
169+
);
170+
bad::<CfgExpr>("a = ", str!["failed to parse `a = ` as a cfg expression: expected a string, but cfg expression ended"]);
147171
bad::<CfgExpr>(
148-
"foo(a)",
149-
"unexpected content `(a)` found after cfg expression",
172+
"all(not())",
173+
str!["failed to parse `all(not())` as a cfg expression: expected identifier, found `)`"],
150174
);
175+
bad::<CfgExpr>("foo(a)", str!["failed to parse `foo(a)` as a cfg expression: unexpected content `(a)` found after cfg expression"]);
151176
}
152177

153178
#[test]

0 commit comments

Comments
 (0)