mod common;
use common::*;
use zenith_core::{DataContext, default_provider};
use zenith_scene::{compile, compile_page, ir::SceneCommand};
fn fixture(format_attr: &str, span_text: &str) -> String {
format!(
r##"zenith version=1 {{
project id="proj.md" name="MD"
tokens format="zenith-token-v1" {{
token id="color.ink" type="color" value="#111827"
token id="font.body" type="fontFamily" value="Noto Sans"
token id="size.body" type="dimension" value=(px)24
}}
styles {{}}
document id="doc.md" title="MD" {{
page id="page.md" w=(px)400 h=(px)200 {{
text id="t.md" x=(px)10 y=(px)20 w=(px)380 h=(px)80 fill=(token)"color.ink" font-family=(token)"font.body" font-size=(token)"size.body"{format_attr} {{
span "{span_text}"
}}
}}
}}
}}"##
)
}
#[test]
fn markdown_format_parses_inline_marks_into_styled_spans() {
let src = fixture(r#" format="markdown""#, r"**bold** and _italic_ and ==hi==");
let doc = parse(&src);
let result = compile(&doc, &default_provider());
assert!(
result
.diagnostics
.iter()
.all(|d| d.severity != zenith_core::Severity::Error),
"expected no errors; got: {:?}",
result.diagnostics
);
let glyph_runs: Vec<_> = result
.scene
.commands
.iter()
.filter(|c| matches!(c, SceneCommand::DrawGlyphRun { .. }))
.collect();
let fill_rects: Vec<_> = result
.scene
.commands
.iter()
.filter(|c| matches!(c, SceneCommand::FillRect { .. }))
.collect();
assert!(
glyph_runs.len() >= 3,
"expected >= 3 glyph runs for 'bold/italic/highlight' spans; got {}",
glyph_runs.len()
);
assert!(
!fill_rects.is_empty(),
"expected at least 1 FillRect for the ==hi== highlight; got 0"
);
}
#[test]
fn no_format_attribute_renders_literal_asterisks_verbatim() {
let src = fixture("", r"**bold** and _italic_ and ==hi==");
let doc = parse(&src);
let result = compile(&doc, &default_provider());
assert!(
result
.diagnostics
.iter()
.all(|d| d.severity != zenith_core::Severity::Error),
"expected no errors; got: {:?}",
result.diagnostics
);
let glyph_runs: Vec<_> = result
.scene
.commands
.iter()
.filter(|c| matches!(c, SceneCommand::DrawGlyphRun { .. }))
.collect();
let fill_rects: Vec<_> = result
.scene
.commands
.iter()
.filter(|c| matches!(c, SceneCommand::FillRect { .. }))
.collect();
assert_eq!(
glyph_runs.len(),
1,
"expected exactly 1 glyph run for literal text without format; got {}",
glyph_runs.len()
);
assert!(
fill_rects.is_empty(),
"expected 0 FillRects for literal text without format; got {}",
fill_rects.len()
);
}
#[test]
fn markdown_format_with_data_ref_parses_substituted_text() {
let src = r##"zenith version=1 {
project id="proj.mddr" name="MDDR"
tokens format="zenith-token-v1" {
token id="color.ink" type="color" value="#111827"
token id="font.body" type="fontFamily" value="Noto Sans"
token id="size.body" type="dimension" value=(px)24
}
styles {}
document id="doc.mddr" title="MDDR" {
page id="page.mddr" w=(px)400 h=(px)200 {
text id="t.mddr" x=(px)10 y=(px)20 w=(px)380 h=(px)80 fill=(token)"color.ink" font-family=(token)"font.body" font-size=(token)"size.body" format="markdown" {
span "" data-ref="article.body"
}
}
}
}"##;
let doc = parse(src);
let mut data = DataContext::default();
data.fields
.insert("article.body".to_owned(), "**Hi** there".to_owned());
let result = compile_page(&doc, &default_provider(), 0, Some(&data));
assert!(
result
.diagnostics
.iter()
.all(|d| d.severity != zenith_core::Severity::Error),
"expected no errors; got: {:?}",
result.diagnostics
);
let glyph_runs: Vec<_> = result
.scene
.commands
.iter()
.filter(|c| matches!(c, SceneCommand::DrawGlyphRun { .. }))
.collect();
assert!(
glyph_runs.len() >= 2,
"expected >= 2 glyph runs after data+markdown parse of '**Hi** there'; got {}",
glyph_runs.len()
);
}