|
| 1 | +use acdc_parser::{Block, InlineMacro, InlineNode, Options, parse, parse_inline}; |
| 2 | + |
| 3 | +type TestResult = Result<(), Box<dyn std::error::Error>>; |
| 4 | + |
| 5 | +fn unexpected(message: &str, actual: impl std::fmt::Debug) -> Box<dyn std::error::Error> { |
| 6 | + std::io::Error::other(format!("{message}, got {actual:?}")).into() |
| 7 | +} |
| 8 | + |
| 9 | +fn assert_plain_text(text: &[InlineNode<'_>], expected: &str) -> TestResult { |
| 10 | + let [InlineNode::PlainText(text)] = text else { |
| 11 | + return Err(unexpected("expected plain macro text", text)); |
| 12 | + }; |
| 13 | + assert_eq!(text.content, expected); |
| 14 | + Ok(()) |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn xref_accepts_non_ascii_path_characters() -> TestResult { |
| 19 | + for (input, target, text) in [ |
| 20 | + ( |
| 21 | + "xref:die_straße.adoc[my street]", |
| 22 | + "die_straße.adoc", |
| 23 | + "my street", |
| 24 | + ), |
| 25 | + ( |
| 26 | + "xref:die_straße.adoc#section[street section]", |
| 27 | + "die_straße.adoc#section", |
| 28 | + "street section", |
| 29 | + ), |
| 30 | + ] { |
| 31 | + let parsed = parse_inline(input, &Options::default())?; |
| 32 | + let [InlineNode::Macro(InlineMacro::CrossReference(xref))] = parsed.inlines() else { |
| 33 | + return Err(unexpected( |
| 34 | + "expected one cross-reference macro", |
| 35 | + parsed.inlines(), |
| 36 | + )); |
| 37 | + }; |
| 38 | + |
| 39 | + assert_eq!(xref.target, target); |
| 40 | + assert_plain_text(&xref.text, text)?; |
| 41 | + } |
| 42 | + |
| 43 | + Ok(()) |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn other_inline_macros_accept_non_ascii_path_characters() -> TestResult { |
| 48 | + let parsed = parse_inline("link:die_straße.adoc[street]", &Options::default())?; |
| 49 | + let [InlineNode::Macro(InlineMacro::Link(link))] = parsed.inlines() else { |
| 50 | + return Err(unexpected("expected one link macro", parsed.inlines())); |
| 51 | + }; |
| 52 | + assert_eq!(link.target.to_string(), "die_straße.adoc"); |
| 53 | + |
| 54 | + let parsed = parse_inline("image:straße.png[street]", &Options::default())?; |
| 55 | + let [InlineNode::Macro(InlineMacro::Image(image))] = parsed.inlines() else { |
| 56 | + return Err(unexpected( |
| 57 | + "expected one inline image macro", |
| 58 | + parsed.inlines(), |
| 59 | + )); |
| 60 | + }; |
| 61 | + assert_eq!(image.source.to_string(), "straße.png"); |
| 62 | + |
| 63 | + let parsed = parse_inline("icon:straße[]", &Options::default())?; |
| 64 | + let [InlineNode::Macro(InlineMacro::Icon(icon))] = parsed.inlines() else { |
| 65 | + return Err(unexpected("expected one icon macro", parsed.inlines())); |
| 66 | + }; |
| 67 | + assert_eq!(icon.target.to_string(), "straße"); |
| 68 | + |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn block_media_macros_accept_non_ascii_path_characters() -> TestResult { |
| 74 | + let parsed = parse( |
| 75 | + "image::straße.png[]\n\naudio::straße.mp3[]\n\nvideo::straße.mp4[]\n", |
| 76 | + &Options::default(), |
| 77 | + )?; |
| 78 | + let [ |
| 79 | + Block::Image(image), |
| 80 | + Block::Audio(audio), |
| 81 | + Block::Video(video), |
| 82 | + ] = parsed.document().blocks.as_slice() |
| 83 | + else { |
| 84 | + return Err(unexpected( |
| 85 | + "expected image, audio, and video blocks", |
| 86 | + &parsed.document().blocks, |
| 87 | + )); |
| 88 | + }; |
| 89 | + |
| 90 | + assert_eq!(image.source.to_string(), "straße.png"); |
| 91 | + assert_eq!(audio.source.to_string(), "straße.mp3"); |
| 92 | + let [video_source] = video.sources.as_slice() else { |
| 93 | + return Err(unexpected("expected one video source", &video.sources)); |
| 94 | + }; |
| 95 | + assert_eq!(video_source.to_string(), "straße.mp4"); |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments