Skip to content

Commit

Permalink
Merge pull request #498 from SamWilsn/sourcepos-tests
Browse files Browse the repository at this point in the history
Write a sourcepos test for each `NodeValue` variant
  • Loading branch information
kivikakk authored Feb 25, 2025
2 parents 7ce7578 + 2055a9e commit 58c8cc4
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 12 deletions.
31 changes: 30 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ caseless = "0.2.1"

[dev-dependencies]
ntest = "0.9"
strum = { version = "0.26.3", features = ["derive"] }
toml = "0.7.3"

[features]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub use parser::parse_document_with_broken_link_callback;
pub use parser::{
parse_document, BrokenLinkCallback, BrokenLinkReference, ExtensionOptions, ListStyleType,
Options, ParseOptions, Plugins, RenderOptions, RenderPlugins, ResolvedReference, URLRewriter,
WikiLinksMode,
};
pub use typed_arena::Arena;
pub use xml::format_document as format_xml;
Expand Down
11 changes: 8 additions & 3 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub use crate::parser::multiline_block_quote::NodeMultilineBlockQuote;

/// The core AST node enum.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(strum::EnumDiscriminants))]
#[cfg_attr(
test,
strum_discriminants(vis(pub(crate)), derive(strum::VariantArray, Hash))
)]
pub enum NodeValue {
/// The root of every CommonMark document. Contains **blocks**.
Document,
Expand Down Expand Up @@ -255,7 +260,7 @@ pub struct NodeTable {
}

/// An inline [code span](https://github.github.com/gfm/#code-spans).
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeCode {
/// The number of backticks
pub num_backticks: usize,
Expand All @@ -268,7 +273,7 @@ pub struct NodeCode {
}

/// The details of a link's destination, or an image's source.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeLink {
/// The URL for the link destination or image source.
pub url: String,
Expand All @@ -281,7 +286,7 @@ pub struct NodeLink {
}

/// The details of a wikilink's destination.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeWikiLink {
/// The URL for the link destination.
pub url: String,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// An inline math span
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeMath {
/// Whether this is dollar math (`$` or `$$`).
/// `false` indicates it is code math
Expand All @@ -8,7 +8,7 @@ pub struct NodeMath {
/// Whether this is display math (using `$$`)
pub display_math: bool,

/// The literal contents of the math span.
/// The literal contents of the math span.
/// As the contents are not interpreted as Markdown at all,
/// they are contained within this structure,
/// rather than inserted into a child inline of any kind.
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ where
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Selects between wikilinks with the title first or the URL first.
pub(crate) enum WikiLinksMode {
pub enum WikiLinksMode {
/// Indicates that the URL precedes the title. For example: `[[http://example.com|link
/// title]]`.
UrlFirst,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/multiline_block_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// The metadata of a multiline blockquote.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct NodeMultilineBlockQuote {
/// The length of the fence.
pub fence_length: usize,
Expand Down
32 changes: 28 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod raw;
mod regressions;
mod rewriter;
mod shortcodes;
#[path = "tests/sourcepos.rs"]
mod sourcepos_;
mod spoiler;
mod strikethrough;
mod subscript;
Expand Down Expand Up @@ -129,20 +131,35 @@ macro_rules! html_opts {
([$($optclass:ident.$optname:ident),*], $lhs:expr, $rhs:expr) => {
html_opts!([$($optclass.$optname),*], $lhs, $rhs,)
};
([$($optclass:ident.$optname:ident = $val:expr),*], $lhs:expr, $rhs:expr) => {
html_opts!([$($optclass.$optname = $val),*], $lhs, $rhs,)
};
([$($optclass:ident.$optname:ident),*], $lhs:expr, $rhs:expr,) => {
html_opts!([$($optclass.$optname),*], $lhs, $rhs, roundtrip)
};
([$($optclass:ident.$optname:ident = $val:expr),*], $lhs:expr, $rhs:expr,) => {
html_opts!([$($optclass.$optname = $val),*], $lhs, $rhs, roundtrip)
};
([$($optclass:ident.$optname:ident),*], $lhs:expr, $rhs:expr, $rt:ident) => {
html_opts!([$($optclass.$optname),*], $lhs, $rhs, $rt,)
};
([$($optclass:ident.$optname:ident = $val:expr),*], $lhs:expr, $rhs:expr, $rt:ident) => {
html_opts!([$($optclass.$optname = $val),*], $lhs, $rhs, $rt,)
};
([$($optclass:ident.$optname:ident),*], $lhs:expr, $rhs:expr, roundtrip,) => {
html_opts!([$($optclass.$optname = true),*], $lhs, $rhs, roundtrip,)
};
([$($optclass:ident.$optname:ident = $val:expr),*], $lhs:expr, $rhs:expr, roundtrip,) => {
$crate::tests::html_opts_i($lhs, $rhs, true, |opts| {
$(opts.$optclass.$optname = true;)*
$(opts.$optclass.$optname = $val;)*
});
};
([$($optclass:ident.$optname:ident),*], $lhs:expr, $rhs:expr, no_roundtrip,) => {
html_opts!([$($optclass.$optname = true),*], $lhs, $rhs, no_roundtrip,)
};
([$($optclass:ident.$optname:ident = $val:expr),*], $lhs:expr, $rhs:expr, no_roundtrip,) => {
$crate::tests::html_opts_i($lhs, $rhs, false, |opts| {
$(opts.$optclass.$optname = true;)*
$(opts.$optclass.$optname = $val;)*
});
};
}
Expand Down Expand Up @@ -314,13 +331,20 @@ macro_rules! assert_ast_match {
$amt
)
};
([ $( $optclass:ident.$optname:ident ),* ], $( $md:literal )+, $amt:tt) => {
([ $( $optclass:ident.$optname:ident = $val:expr ),* ], $( $md:literal )+, $amt:tt) => {
crate::tests::assert_ast_match_i(
concat!( $( $md ),+ ),
ast!($amt),
|#[allow(unused_variables)] opts| {$(opts.$optclass.$optname = true;)*},
|#[allow(unused_variables)] opts| {$(opts.$optclass.$optname = $val;)*},
);
};
([ $( $optclass:ident.$optname:ident ),* ], $( $md:literal )+, $amt:tt) => {
assert_ast_match!(
[ $( $optclass.$optname = true),* ],
$( $md )+,
$amt
)
};
}

pub(crate) use assert_ast_match;
Expand Down
Loading

0 comments on commit 58c8cc4

Please sign in to comment.