Skip to content

Commit 8ab3886

Browse files
epagemo8it
authored andcommitted
test(platform): Switch to snapbox
1 parent 2c8d020 commit 8ab3886

File tree

3 files changed

+65
-37
lines changed

3 files changed

+65
-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: 61 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,17 @@ 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+
let actual = match T::from_str(input) {
71+
Ok(cfg) => panic!("expected `{input}` to not parse but got {cfg}"),
6772
Err(e) => e.to_string(),
6873
};
69-
assert!(
70-
e.contains(err),
71-
"when parsing `{}`,\n\"{}\" not contained \
72-
inside: {}",
73-
s,
74-
err,
75-
e
76-
);
74+
assert_data_eq!(actual, expected.raw());
7775
}
7876

7977
#[test]
@@ -93,24 +91,38 @@ fn cfg_syntax() {
9391

9492
#[test]
9593
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");
10694
bad::<Cfg>(
107-
"foo, bar",
108-
"unexpected content `, bar` found after cfg expression",
95+
"",
96+
str![
97+
"failed to parse `` as a cfg expression: expected identifier, but cfg expression ended"
98+
],
99+
);
100+
bad::<Cfg>(" ", str!["failed to parse ` ` as a cfg expression: expected identifier, but cfg expression ended"]);
101+
bad::<Cfg>("\t", str!["failed to parse ` ` as a cfg expression: unexpected character ` ` in cfg, expected parens, a comma, an identifier, or a string"]);
102+
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"]);
103+
bad::<Cfg>(
104+
"=",
105+
str!["failed to parse `=` as a cfg expression: expected identifier, found `=`"],
106+
);
107+
bad::<Cfg>(
108+
",",
109+
str!["failed to parse `,` as a cfg expression: expected identifier, found `,`"],
110+
);
111+
bad::<Cfg>(
112+
"(",
113+
str!["failed to parse `(` as a cfg expression: expected identifier, found `(`"],
109114
);
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");
115+
bad::<Cfg>("foo (", str!["failed to parse `foo (` as a cfg expression: unexpected content `(` found after cfg expression"]);
116+
bad::<Cfg>("bar =", str!["failed to parse `bar =` as a cfg expression: expected a string, but cfg expression ended"]);
117+
bad::<Cfg>(
118+
"bar = \"",
119+
str![[r#"failed to parse `bar = "` as a cfg expression: unterminated string in cfg"#]],
120+
);
121+
bad::<Cfg>("foo, bar", str!["failed to parse `foo, bar` as a cfg expression: unexpected content `, bar` found after cfg expression"]);
122+
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"]);
123+
bad::<Cfg>("r #foo", str!["failed to parse `r #foo` as a cfg expression: unexpected content `#foo` found after cfg expression"]);
124+
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"#]]);
125+
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"#]]);
114126
}
115127

116128
#[test]
@@ -137,17 +149,29 @@ fn cfg_expr() {
137149

138150
#[test]
139151
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");
152+
bad::<CfgExpr>(" ", str!["failed to parse ` ` as a cfg expression: expected start of a cfg expression, but cfg expression ended"]);
153+
bad::<CfgExpr>(
154+
" all",
155+
str!["failed to parse ` all` as a cfg expression: expected `(`, but cfg expression ended"],
156+
);
157+
bad::<CfgExpr>(
158+
"all(a",
159+
str!["failed to parse `all(a` as a cfg expression: expected `)`, but cfg expression ended"],
160+
);
161+
bad::<CfgExpr>(
162+
"not",
163+
str!["failed to parse `not` as a cfg expression: expected `(`, but cfg expression ended"],
164+
);
165+
bad::<CfgExpr>(
166+
"not(a",
167+
str!["failed to parse `not(a` as a cfg expression: expected `)`, but cfg expression ended"],
168+
);
169+
bad::<CfgExpr>("a = ", str!["failed to parse `a = ` as a cfg expression: expected a string, but cfg expression ended"]);
147170
bad::<CfgExpr>(
148-
"foo(a)",
149-
"unexpected content `(a)` found after cfg expression",
171+
"all(not())",
172+
str!["failed to parse `all(not())` as a cfg expression: expected identifier, found `)`"],
150173
);
174+
bad::<CfgExpr>("foo(a)", str!["failed to parse `foo(a)` as a cfg expression: unexpected content `(a)` found after cfg expression"]);
151175
}
152176

153177
#[test]

0 commit comments

Comments
 (0)