Skip to content

Commit 5f6ed18

Browse files
committed
Add some unit tests for core api
1 parent b553df1 commit 5f6ed18

File tree

1 file changed

+198
-10
lines changed

1 file changed

+198
-10
lines changed

src/lib.rs

Lines changed: 198 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
html_logo_url = "https://raw.githubusercontent.com/wooorm/markdown-rs/8924580/media/logo-monochromatic.svg?sanitize=true"
2020
)]
2121

22-
// ^-- Would be nice to use `logo-chromatic`, but that looks horrible on Safari
23-
// at relatively small sizes currently. 😢
24-
2522
extern crate alloc;
2623

2724
mod construct;
@@ -41,15 +38,12 @@ use alloc::{boxed::Box, fmt, string::String};
4138
use mdast::Node;
4239
use parser::parse;
4340

44-
// Do not use: exported for quick prototyping, will be removed.
4541
#[doc(hidden)]
4642
pub use util::identifier::{id_cont, id_start};
4743

48-
// Do not use: exported for quick prototyping, will be removed.
4944
#[doc(hidden)]
5045
pub use util::sanitize_uri::sanitize;
5146

52-
// Do not use: exported for quick prototyping, will be removed.
5347
#[doc(hidden)]
5448
pub use util::location::Location;
5549

@@ -115,11 +109,11 @@ impl LineEnding {
115109
///
116110
/// Panics if `code` is not `\r\n`, `\r`, or `\n`.
117111
fn from_str(str: &str) -> LineEnding {
112+
debug_assert!(matches!(str, "\r\n" | "\r" | "\n"), "expected eol");
118113
match str {
119114
"\r\n" => LineEnding::CarriageReturnLineFeed,
120115
"\r" => LineEnding::CarriageReturn,
121-
"\n" => LineEnding::LineFeed,
122-
_ => unreachable!("invalid str"),
116+
_ => LineEnding::LineFeed,
123117
}
124118
}
125119
}
@@ -1538,6 +1532,7 @@ pub fn to_mdast(value: &str, options: &ParseOptions) -> Result<Node, String> {
15381532
mod tests {
15391533
extern crate std;
15401534
use super::*;
1535+
use alloc::format;
15411536

15421537
#[test]
15431538
fn test_line_ending() {
@@ -1576,10 +1571,203 @@ mod tests {
15761571
}
15771572

15781573
#[test]
1579-
#[should_panic = "invalid str"]
1574+
#[should_panic = "expected eol"]
15801575
fn test_line_ending_broken() {
15811576
// Hide stack trace.
1582-
std::panic::set_hook(Box::new(|_| {}));
15831577
LineEnding::from_str("a");
15841578
}
1579+
1580+
#[test]
1581+
fn test_constructs() {
1582+
let constructs = Constructs::default();
1583+
assert!(constructs.attention, "should default to `CommonMark` (1)");
1584+
assert!(
1585+
!constructs.gfm_autolink_literal,
1586+
"should default to `CommonMark` (2)"
1587+
);
1588+
assert!(
1589+
!constructs.mdx_jsx_flow,
1590+
"should default to `CommonMark` (3)"
1591+
);
1592+
assert!(
1593+
!constructs.frontmatter,
1594+
"should default to `CommonMark` (4)"
1595+
);
1596+
1597+
let constructs = Constructs::gfm();
1598+
assert!(constructs.attention, "should support `gfm` shortcut (1)");
1599+
assert!(
1600+
constructs.gfm_autolink_literal,
1601+
"should support `gfm` shortcut (2)"
1602+
);
1603+
assert!(
1604+
!constructs.mdx_jsx_flow,
1605+
"should support `gfm` shortcut (3)"
1606+
);
1607+
assert!(!constructs.frontmatter, "should support `gfm` shortcut (4)");
1608+
1609+
let constructs = Constructs::mdx();
1610+
assert!(constructs.attention, "should support `gfm` shortcut (1)");
1611+
assert!(
1612+
!constructs.gfm_autolink_literal,
1613+
"should support `mdx` shortcut (2)"
1614+
);
1615+
assert!(constructs.mdx_jsx_flow, "should support `mdx` shortcut (3)");
1616+
assert!(!constructs.frontmatter, "should support `mdx` shortcut (4)");
1617+
}
1618+
1619+
#[test]
1620+
fn test_parse_options() {
1621+
let options = ParseOptions::default();
1622+
assert!(
1623+
options.constructs.attention,
1624+
"should default to `CommonMark` (1)"
1625+
);
1626+
assert!(
1627+
!options.constructs.gfm_autolink_literal,
1628+
"should default to `CommonMark` (2)"
1629+
);
1630+
assert!(
1631+
!options.constructs.mdx_jsx_flow,
1632+
"should default to `CommonMark` (3)"
1633+
);
1634+
1635+
let options = ParseOptions::gfm();
1636+
assert!(
1637+
options.constructs.attention,
1638+
"should support `gfm` shortcut (1)"
1639+
);
1640+
assert!(
1641+
options.constructs.gfm_autolink_literal,
1642+
"should support `gfm` shortcut (2)"
1643+
);
1644+
assert!(
1645+
!options.constructs.mdx_jsx_flow,
1646+
"should support `gfm` shortcut (3)"
1647+
);
1648+
1649+
let options = ParseOptions::mdx();
1650+
assert!(
1651+
options.constructs.attention,
1652+
"should support `mdx` shortcut (1)"
1653+
);
1654+
assert!(
1655+
!options.constructs.gfm_autolink_literal,
1656+
"should support `mdx` shortcut (2)"
1657+
);
1658+
assert!(
1659+
options.constructs.mdx_jsx_flow,
1660+
"should support `mdx` shortcut (3)"
1661+
);
1662+
1663+
assert_eq!(
1664+
format!("{:?}", ParseOptions::default()),
1665+
"ParseOptions { constructs: Constructs { attention: true, autolink: true, block_quote: true, character_escape: true, character_reference: true, code_indented: true, code_fenced: true, code_text: true, definition: true, frontmatter: false, gfm_autolink_literal: false, gfm_footnote_definition: false, gfm_label_start_footnote: false, gfm_strikethrough: false, gfm_table: false, gfm_task_list_item: false, hard_break_escape: true, hard_break_trailing: true, heading_atx: true, heading_setext: true, html_flow: true, html_text: true, label_start_image: true, label_start_link: true, label_end: true, list_item: true, math_flow: false, math_text: false, mdx_esm: false, mdx_expression_flow: false, mdx_expression_text: false, mdx_jsx_flow: false, mdx_jsx_text: false, thematic_break: true }, gfm_strikethrough_single_tilde: true, math_text_single_dollar: true, mdx_expression_parse: None, mdx_esm_parse: None }",
1666+
"should support `Debug` trait"
1667+
);
1668+
assert_eq!(
1669+
format!("{:?}", ParseOptions {
1670+
mdx_esm_parse: Some(Box::new(|_value| {
1671+
MdxSignal::Ok
1672+
})),
1673+
mdx_expression_parse: Some(Box::new(|_value, _kind| {
1674+
MdxSignal::Ok
1675+
})),
1676+
..Default::default()
1677+
}),
1678+
"ParseOptions { constructs: Constructs { attention: true, autolink: true, block_quote: true, character_escape: true, character_reference: true, code_indented: true, code_fenced: true, code_text: true, definition: true, frontmatter: false, gfm_autolink_literal: false, gfm_footnote_definition: false, gfm_label_start_footnote: false, gfm_strikethrough: false, gfm_table: false, gfm_task_list_item: false, hard_break_escape: true, hard_break_trailing: true, heading_atx: true, heading_setext: true, html_flow: true, html_text: true, label_start_image: true, label_start_link: true, label_end: true, list_item: true, math_flow: false, math_text: false, mdx_esm: false, mdx_expression_flow: false, mdx_expression_text: false, mdx_jsx_flow: false, mdx_jsx_text: false, thematic_break: true }, gfm_strikethrough_single_tilde: true, math_text_single_dollar: true, mdx_expression_parse: Some(\"[Function]\"), mdx_esm_parse: Some(\"[Function]\") }",
1679+
"should support `Debug` trait on mdx functions"
1680+
);
1681+
}
1682+
1683+
#[test]
1684+
fn test_compile_options() {
1685+
let options = CompileOptions::default();
1686+
assert!(
1687+
!options.allow_dangerous_html,
1688+
"should default to safe `CommonMark` (1)"
1689+
);
1690+
assert!(
1691+
!options.gfm_tagfilter,
1692+
"should default to safe `CommonMark` (2)"
1693+
);
1694+
1695+
let options = CompileOptions::gfm();
1696+
assert!(
1697+
!options.allow_dangerous_html,
1698+
"should support safe `gfm` shortcut (1)"
1699+
);
1700+
assert!(
1701+
options.gfm_tagfilter,
1702+
"should support safe `gfm` shortcut (1)"
1703+
);
1704+
}
1705+
1706+
#[test]
1707+
fn test_options() {
1708+
let options = Options::default();
1709+
assert!(
1710+
options.parse.constructs.attention,
1711+
"should default to safe `CommonMark` (1)"
1712+
);
1713+
assert!(
1714+
!options.parse.constructs.gfm_autolink_literal,
1715+
"should default to safe `CommonMark` (2)"
1716+
);
1717+
assert!(
1718+
!options.parse.constructs.mdx_jsx_flow,
1719+
"should default to safe `CommonMark` (3)"
1720+
);
1721+
assert!(
1722+
!options.compile.allow_dangerous_html,
1723+
"should default to safe `CommonMark` (4)"
1724+
);
1725+
1726+
let options = Options::gfm();
1727+
assert!(
1728+
options.parse.constructs.attention,
1729+
"should support safe `gfm` shortcut (1)"
1730+
);
1731+
assert!(
1732+
options.parse.constructs.gfm_autolink_literal,
1733+
"should support safe `gfm` shortcut (2)"
1734+
);
1735+
assert!(
1736+
!options.parse.constructs.mdx_jsx_flow,
1737+
"should support safe `gfm` shortcut (3)"
1738+
);
1739+
assert!(
1740+
!options.compile.allow_dangerous_html,
1741+
"should support safe `gfm` shortcut (4)"
1742+
);
1743+
}
1744+
1745+
#[test]
1746+
fn test_to_html() {
1747+
assert_eq!(
1748+
to_html("a"),
1749+
"<p>a</p>",
1750+
"should support turning markdown into html with `to_html`"
1751+
);
1752+
}
1753+
1754+
#[test]
1755+
fn test_to_html_with_options() {
1756+
assert_eq!(
1757+
to_html_with_options("a", &Options::default()).unwrap(),
1758+
"<p>a</p>",
1759+
"should support turning markdown into html with `to_html_with_options`"
1760+
);
1761+
}
1762+
1763+
#[test]
1764+
fn test_to_mdast() {
1765+
assert!(
1766+
matches!(
1767+
to_mdast("a", &ParseOptions::default()).unwrap(),
1768+
mdast::Node::Root(_)
1769+
),
1770+
"should support turning markdown into mdast with `to_mdast`"
1771+
);
1772+
}
15851773
}

0 commit comments

Comments
 (0)