Add automated parsing tests

pull/1/head
trivernis 5 years ago
parent cb8f469853
commit fc1d1ccb56

2
.gitignore vendored

@ -2,5 +2,5 @@
**/*.rs.bk **/*.rs.bk
.idea .idea
.ast .ast
test test-files
perf.data perf.data

2
Cargo.lock generated

@ -566,7 +566,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "snekdown" name = "snekdown"
version = "0.12.3" version = "0.12.4"
dependencies = [ dependencies = [
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package] [package]
name = "snekdown" name = "snekdown"
version = "0.12.3" version = "0.12.4"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"
@ -16,6 +16,8 @@ crate-type = ["lib"]
name = "snekdown" name = "snekdown"
path = "src/main.rs" path = "src/main.rs"
[tests]
[dependencies] [dependencies]
crossbeam-utils = "0.7.2" crossbeam-utils = "0.7.2"
structopt = "0.3.14" structopt = "0.3.14"

@ -1,3 +1,4 @@
pub mod format; pub mod format;
pub mod parsing; pub mod parsing;
pub use parsing::parser::Parser; pub use parsing::parser::Parser;
pub use parsing::utils;

@ -50,7 +50,7 @@ pub enum Line {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Document { pub struct Document {
pub(crate) elements: Vec<Block>, pub elements: Vec<Block>,
pub(crate) is_root: bool, pub(crate) is_root: bool,
pub(crate) path: Option<String>, pub(crate) path: Option<String>,
pub(crate) placeholders: Vec<Arc<Mutex<Placeholder>>>, pub(crate) placeholders: Vec<Arc<Mutex<Placeholder>>>,
@ -80,7 +80,7 @@ pub struct Paragraph {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct List { pub struct List {
pub(crate) ordered: bool, pub(crate) ordered: bool,
pub(crate) items: Vec<ListItem>, pub items: Vec<ListItem>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

@ -4,4 +4,5 @@ pub mod inline;
pub mod parser; pub mod parser;
pub mod placeholders; pub mod placeholders;
pub mod tokens; pub mod tokens;
pub(crate) mod utils; #[macro_use]
pub mod utils;

@ -50,7 +50,11 @@ impl Parser {
is_child: bool, is_child: bool,
) -> Self { ) -> Self {
let text: Vec<char> = text.chars().collect(); let text: Vec<char> = text.chars().collect();
let current_char = text.get(0).unwrap().clone(); let current_char = if text.len() > 0 {
text.get(0).unwrap().clone()
} else {
' '
};
if let Some(path) = path.clone() { if let Some(path) = path.clone() {
let path_info = Path::new(&path); let path_info = Path::new(&path);
paths paths
@ -463,8 +467,10 @@ impl Parser {
self.revert_to(start_index)?; self.revert_to(start_index)?;
break; break;
} }
if !self.check_eof() {
self.revert_to(start_index)?; self.revert_to(start_index)?;
} }
}
if paragraph.elements.len() > 0 { if paragraph.elements.len() > 0 {
Ok(paragraph) Ok(paragraph)

@ -3,6 +3,13 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
#[macro_export]
macro_rules! parse {
($str:expr) => {
Parser::new($str.to_string(), None).parse()
};
}
pub type ParseResult<T> = Result<T, ParseError>; pub type ParseResult<T> = Result<T, ParseError>;
#[derive(Debug)] #[derive(Debug)]

@ -0,0 +1,97 @@
use snekdown::parse;
use snekdown::parsing::elements::Block;
use snekdown::Parser;
macro_rules! count_block_elements {
($document:expr, $filter:expr) => {
$document
.elements
.iter()
.filter($filter)
.collect::<Vec<&Block>>()
.len()
};
}
#[test]
fn it_inits() {
let _ = Parser::new("".to_string(), None);
}
#[test]
fn it_parses_sections() {
let document = parse!("# Section\n## Subsection\n# Section");
assert_eq!(
count_block_elements!(document, |e| if let Block::Section(_) = e {
true
} else {
false
}),
2
)
}
#[test]
fn it_parses_tables() {
let document = parse!("|header|header|\n|---|---|\n|col|col|");
assert_eq!(
count_block_elements!(document, |e| if let Block::Table(_) = e {
true
} else {
false
}),
1
)
}
#[test]
fn it_parses_paragraphs() {
let document = parse!("**Bold***Italic*_Underline_`Monospace`^super^~strike~");
assert_eq!(
count_block_elements!(document, |e| if let Block::Paragraph(_) = e {
true
} else {
false
}),
1
)
}
#[test]
fn it_parses_lists() {
let document = parse!("- item1\n- item2\n\n* item\n+ item\n\no item\n1. item");
assert_eq!(
count_block_elements!(document, |e| if let Block::List(l) = e {
l.items.len() == 2
} else {
false
}),
3
)
}
#[test]
fn it_parses_code_blocks() {
let document = parse!("```\ncode\n```\n```rust\ncode\n``````");
assert_eq!(
count_block_elements!(document, |e| if let Block::CodeBlock(_) = e {
true
} else {
false
}),
2
)
}
#[test]
fn it_parses_quotes() {
let document = parse!("> quote\n\n[meta]> quote\n>hm");
assert_eq!(
count_block_elements!(document, |e| if let Block::Quote(_) = e {
true
} else {
false
}),
2
)
}
Loading…
Cancel
Save