Skip to content

Commit 2ea0d97

Browse files
committed
test(toml): Separate manifest expansion from code fence tests
1 parent d769cb5 commit 2ea0d97

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,55 @@ fn split_source(input: &str) -> CargoResult<Source<'_>> {
268268
#[cfg(test)]
269269
mod test_expand {
270270
use snapbox::assert_data_eq;
271+
use snapbox::prelude::*;
271272
use snapbox::str;
272273

273274
use super::*;
274275

276+
#[track_caller]
277+
fn assert_source(source: &str, expected: impl IntoData) {
278+
use std::fmt::Write as _;
279+
280+
let actual = match split_source(source) {
281+
Ok(actual) => actual,
282+
Err(err) => panic!("unexpected err: {err}"),
283+
};
284+
285+
let mut rendered = String::new();
286+
write_optional_field(&mut rendered, "shebang", actual.shebang);
287+
write_optional_field(&mut rendered, "info", actual.info);
288+
write_optional_field(&mut rendered, "frontmatter", actual.frontmatter);
289+
writeln!(&mut rendered, "content: {:?}", actual.content).unwrap();
290+
assert_data_eq!(rendered, expected.raw());
291+
}
292+
293+
fn write_optional_field(writer: &mut dyn std::fmt::Write, field: &str, value: Option<&str>) {
294+
if let Some(value) = value {
295+
writeln!(writer, "{field}: {value:?}").unwrap();
296+
} else {
297+
writeln!(writer, "{field}: None").unwrap();
298+
}
299+
}
300+
301+
#[test]
302+
fn split_dependencies() {
303+
assert_source(
304+
r#"---
305+
[dependencies]
306+
time="0.1.25"
307+
---
308+
fn main() {}
309+
"#,
310+
str![[r#"
311+
shebang: None
312+
info: None
313+
frontmatter: "[dependencies]\ntime=\"0.1.25\"\n"
314+
content: "fn main() {}\n"
315+
316+
"#]],
317+
);
318+
}
319+
275320
#[track_caller]
276321
fn expand(source: &str) -> String {
277322
let shell = crate::Shell::from_write(Box::new(Vec::new()));
@@ -283,7 +328,7 @@ mod test_expand {
283328
}
284329

285330
#[test]
286-
fn test_default() {
331+
fn expand_default() {
287332
assert_data_eq!(
288333
expand(r#"fn main() {}"#),
289334
str![[r#"
@@ -311,7 +356,7 @@ strip = true
311356
}
312357

313358
#[test]
314-
fn test_dependencies() {
359+
fn expand_dependencies() {
315360
assert_data_eq!(
316361
expand(
317362
r#"---cargo
@@ -344,44 +389,6 @@ strip = true
344389
345390
[workspace]
346391
347-
"#]]
348-
);
349-
}
350-
351-
#[test]
352-
fn test_no_infostring() {
353-
assert_data_eq!(
354-
expand(
355-
r#"---
356-
[dependencies]
357-
time="0.1.25"
358-
---
359-
fn main() {}
360-
"#
361-
),
362-
str![[r#"
363-
[[bin]]
364-
name = "test-"
365-
path = [..]
366-
367-
[dependencies]
368-
time = "0.1.25"
369-
370-
[package]
371-
autobenches = false
372-
autobins = false
373-
autoexamples = false
374-
autolib = false
375-
autotests = false
376-
build = false
377-
edition = "2021"
378-
name = "test-"
379-
380-
[profile.release]
381-
strip = true
382-
383-
[workspace]
384-
385392
"#]]
386393
);
387394
}

0 commit comments

Comments
 (0)