test(parser): add tests for parser crate

This commit is contained in:
Orhun Parmaksız 2021-10-30 02:19:31 +03:00
parent da334a9732
commit 14abb58f17
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 100 additions and 9 deletions

View file

@ -3,7 +3,7 @@ use regex::Captures;
use std::path::PathBuf;
/// Representation of a paragraph in a [`Document`].
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Paragraph {
/// Paragraph title.
pub title: String,
@ -55,7 +55,7 @@ impl Paragraph {
}
/// Representation of a parsed document which consists of paragraphs.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Document {
/// Paragraphs in the document.
pub paragraphs: Vec<Paragraph>,
@ -69,3 +69,32 @@ impl Document {
Self { paragraphs, path }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::reader;
use regex::RegexBuilder;
#[test]
fn test_paragraph() -> Result<(), Error> {
let input =
reader::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))?;
let captures = RegexBuilder::new(r#"^\[[a-zA-Z]+\]\n"#)
.multi_line(true)
.build()?
.captures_iter(&input)
.collect::<Vec<_>>();
let paragraphs = Paragraph::from_captures(captures, &input)?;
assert!(paragraphs.len() >= 2);
assert_eq!("[package]", paragraphs[0].title);
assert!(paragraphs[0]
.contents
.contains(&format!("version = \"{}\"", env!("CARGO_PKG_VERSION"))));
assert_eq!("[dependencies]", paragraphs[1].title);
assert!(paragraphs[1].contents.contains("regex = "));
Ok(())
}
}

View file

@ -50,3 +50,33 @@ impl<'a> Parser<'a> {
Ok(documents)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_document_parser() -> Result<(), Error> {
let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let parser = Parser::new("Cargo.*", r#"^\[package\]\n"#)?;
let mut documents = parser.parse(base_path.as_path())?;
assert!(documents[0].paragraphs[0]
.contents
.contains(&format!("name = \"{}\"", env!("CARGO_PKG_NAME"))));
documents[0].paragraphs[0].contents = String::new();
assert_eq!(
Document {
paragraphs: vec![Paragraph {
title: String::from("[package]"),
contents: String::new(),
}],
path: base_path.join("Cargo.toml")
},
documents[0]
);
Ok(())
}
}

View file

@ -71,16 +71,13 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> IoResult<String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Error;
use std::path::PathBuf;
#[test]
fn test_file_reader() {
fn test_file_reader() -> Result<(), Error> {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
println!("{:?}", path);
assert!(read_to_string(path)
.expect("cannot read Cargo.toml")
.lines()
.collect::<String>()
.contains(&format!("name = \"{}\"", env!("CARGO_PKG_NAME"))));
assert!(read_to_string(path)?.contains(&format!("name = \"{}\"", env!("CARGO_PKG_NAME"))));
Ok(())
}
}

View file

@ -0,0 +1,35 @@
use std::path::PathBuf;
use systeroid_parser::error::Error;
use systeroid_parser::parser::Parser;
#[test]
fn test_parser() -> Result<(), Error> {
let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let parser = Parser::new("src/*.rs", r#"^#\[cfg\(test\)\]$\n"#)?;
let documents = parser.parse(base_path.as_path())?;
assert!(documents
.iter()
.find(|d| d.path == PathBuf::from(base_path.join("src").join("lib.rs")))
.unwrap()
.paragraphs
.is_empty());
assert!(documents
.iter()
.find(|d| d.path == PathBuf::from(base_path.join("src").join("reader.rs")))
.unwrap()
.paragraphs[0]
.contents
.contains("fn test_file_reader()"));
documents.iter().for_each(|document| {
document.paragraphs.iter().for_each(|paragraph| {
assert_eq!("#[cfg(test)]", paragraph.title);
assert!(paragraph.contents.contains("mod tests"));
assert!(paragraph.contents.contains("use super::*;"));
});
});
Ok(())
}