-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add a more generic version of the variants checker I wrote for the Node -> MdqNode conversions - Add unit tests for all the MdqNode -> markdown renderings - Various fixes to the prod code that shook out from those tests
- Loading branch information
Showing
7 changed files
with
1,579 additions
and
100 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[cfg(test)] | ||
mod test_utils { | ||
|
||
#[macro_export] | ||
macro_rules! mdq_node { | ||
($node_type:tt {$($attr:ident: $val:expr),*}) => { | ||
MdqNode::$node_type($node_type{$($attr: $val),*}) | ||
}; | ||
($paragraph_text:literal) => { | ||
crate::mdq_node!(Paragraph{body: vec![crate::mdq_inline!($paragraph_text)]}) | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! mdq_nodes { | ||
[$($node_type:tt {$($attr:ident: $val:expr),*$(,)?}),*$(,)?] => { | ||
vec![$( | ||
crate::mdq_node!($node_type { | ||
$($attr: $val),* | ||
}) | ||
),* | ||
] | ||
}; | ||
[$($paragraph_text:literal),*$(,)?] => { | ||
vec![$( | ||
crate::mdq_node!($paragraph_text) | ||
),* | ||
] | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! mdq_inline { | ||
(span $which:ident [$($contents:expr),*$(,)?]) => { | ||
crate::tree::Inline::Span { | ||
variant: crate::tree::SpanVariant::$which, | ||
children: vec![$($contents),*], | ||
} | ||
}; | ||
($text:literal) => { | ||
crate::tree::Inline::Text { | ||
variant: crate::tree::InlineVariant::Text, | ||
value: $text.to_string(), | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#[cfg(test)] | ||
pub use test_utils::*; | ||
|
||
// We this file's contents from prod by putting them in a submodule guarded by cfg(test), but then "pub use" it to | ||
// export its contents. | ||
#[cfg(test)] | ||
mod test_utils { | ||
use std::{thread, time}; | ||
|
||
// TODO unify this with the one in tree.rs. | ||
pub struct VariantsChecker<E> { | ||
require: std::sync::Arc<std::sync::Mutex<std::collections::HashSet<String>>>, | ||
resolver: fn(&E) -> &str, | ||
} | ||
|
||
impl<E> VariantsChecker<E> { | ||
pub fn new<I>(require: I, resolver: fn(&E) -> &str) -> Self | ||
where | ||
I: IntoIterator<Item = String>, | ||
{ | ||
Self { | ||
require: std::sync::Arc::new(std::sync::Mutex::new(require.into_iter().collect())), | ||
resolver, | ||
} | ||
} | ||
|
||
pub fn see(&self, node: &E) { | ||
let node_str = (self.resolver)(node); | ||
self.require.lock().map(|mut set| set.remove(node_str)).unwrap(); | ||
} | ||
|
||
pub fn wait_for_all(&self) { | ||
let timeout = time::Duration::from_millis(500); | ||
let retry_delay = time::Duration::from_millis(50); | ||
let start = time::Instant::now(); | ||
loop { | ||
if self.require.lock().map(|set| set.is_empty()).unwrap() { | ||
break; | ||
} | ||
if start.elapsed() >= timeout { | ||
let mut remaining: Vec<String> = self | ||
.require | ||
.lock() | ||
.map(|set| set.iter().map(|s| s.to_owned()).collect()) | ||
.unwrap(); | ||
remaining.sort(); | ||
panic!( | ||
"Timed out, and missing {} variants:\n- {}", | ||
remaining.len(), | ||
remaining.join("\n- ") | ||
) | ||
} | ||
thread::sleep(retry_delay); | ||
} | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! new_variants_checker { | ||
{$enum_type:ty : $($variant:pat),* $(,)?} => { | ||
{ | ||
use $enum_type::*; | ||
|
||
VariantsChecker::new( | ||
vec![$(stringify!($variant).to_string(),)*], | ||
{|elem| match elem { | ||
$($variant => stringify!($variant),)* | ||
}} | ||
) | ||
} | ||
}; | ||
} | ||
} |