mirror of
https://github.com/orhun/systeroid
synced 2024-07-22 02:45:19 +00:00
test(parser): add tests for parser crate
This commit is contained in:
parent
da334a9732
commit
14abb58f17
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
35
systeroid-parser/tests/integration_test.rs
Normal file
35
systeroid-parser/tests/integration_test.rs
Normal 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(())
|
||||
}
|
Loading…
Reference in a new issue