focus/crates/focus-tui/tests/markdown_tests.rs

84 lines
2.6 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 极简 Markdown 渲染器的测试。
//! Tests for the minimal Markdown renderer.
use focus_tui::markdown::{render_markdown, MdBlockStyle, MdLine};
fn text_of(md: &MdLine) -> String {
md.spans.iter().map(|s| s.text.as_str()).collect()
}
fn join(lines: &[MdLine]) -> Vec<String> {
lines.iter().map(text_of).collect()
}
#[test]
fn renders_headers() {
let out = render_markdown("# Title\n## Sub\n### Deep\nplain", 80);
assert_eq!(out[0].style, MdBlockStyle::Header);
assert_eq!(out[0].level, 1);
assert_eq!(text_of(&out[0]), "Title");
assert_eq!(out[1].level, 2);
assert_eq!(out[2].level, 3);
assert_eq!(out[3].style, MdBlockStyle::Plain);
}
#[test]
fn renders_bold_italic_and_code() {
let out = render_markdown("**bold** and *italic* and `code`", 80);
let line = &out[0];
// 5 段bold, " and ", italic, " and ", code。
// 5 spans: bold, " and ", italic, " and ", code.
assert_eq!(line.spans.len(), 5);
let bold = line.spans.iter().find(|s| s.bold).expect("bold span");
assert_eq!(bold.text, "bold");
let italic = line.spans.iter().find(|s| s.italic).expect("italic span");
assert_eq!(italic.text, "italic");
let code = line.spans.iter().find(|s| s.code).expect("code span");
assert_eq!(code.text, "code");
}
#[test]
fn renders_fenced_code_blocks() {
let md = "```rust\nfn main() {}\n```\nafter";
let out = render_markdown(md, 80);
assert_eq!(out[0].style, MdBlockStyle::CodeBlock);
assert_eq!(text_of(&out[0]), "fn main() {}");
assert_eq!(out[1].style, MdBlockStyle::Plain);
assert_eq!(text_of(&out[1]), "after");
}
#[test]
fn renders_lists_and_quotes() {
let md = "- item a\n- item b\n1. first\n2. second\n> quote text";
let out = render_markdown(md, 80);
let joined = join(&out);
assert_eq!(joined[0], "• item a");
assert_eq!(joined[1], "• item b");
assert_eq!(joined[2], "1. first");
assert_eq!(joined[3], "2. second");
assert_eq!(out[4].style, MdBlockStyle::Quote);
assert_eq!(text_of(&out[4]), "quote text");
}
#[test]
fn renders_links_and_hr() {
let out = render_markdown("see [docs](https://example.com)\n---", 80);
let link = out[0].spans.iter().find(|s| s.link).expect("link span");
assert_eq!(link.text, "docs");
assert_eq!(out[1].style, MdBlockStyle::Hr);
}
#[test]
fn wraps_at_width() {
let out = render_markdown("aaaaaa bbbbbb cccccc", 8);
let joined = join(&out);
assert!(joined.len() >= 2, "got {:?}", joined);
for line in &joined {
assert!(
focus_tui::format::display_width(line) <= 8,
"line too wide: {:?}",
line
);
}
}