Add test module
This commit is contained in:
parent
918e3d042b
commit
3669d5d2cc
79
src/lib.rs
79
src/lib.rs
@ -6,85 +6,12 @@ mod parser;
|
||||
mod primitives;
|
||||
mod sequence;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
pub use choice::*;
|
||||
pub use combinators::*;
|
||||
pub use map::*;
|
||||
pub use parser::{ParseResult, Parser, ParserExtension};
|
||||
pub use primitives::*;
|
||||
pub use sequence::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parsing() {
|
||||
let (parsed, rest) = literal("a")("a yolo").unwrap();
|
||||
assert_eq!(parsed, "a");
|
||||
assert_eq!(rest, " yolo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sequence() {
|
||||
let parser = seq2(literal("bongo"), seq2(literal(" "), literal("jonzzz")));
|
||||
let output = parser.parse("bongo jonzzz").unwrap();
|
||||
assert_eq!(output.0 .0, "bongo");
|
||||
assert_eq!(output.0 .1, (" ", "jonzzz"));
|
||||
assert_eq!(output.1, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_choice() {
|
||||
let a = literal("bongo");
|
||||
let b = literal("sucy");
|
||||
let c = literal("ara");
|
||||
let inputs = [&a as &dyn Parser<&str, &str, ()>, &b, &c];
|
||||
let parser = choice(&inputs);
|
||||
|
||||
let output = parser.parse("ara hajimete").unwrap();
|
||||
assert_eq!(("ara", " hajimete"), output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map() {
|
||||
let parser =
|
||||
seq2(literal("a"), literal("b")).map(|(a, _b): (&str, &str)| (a.to_uppercase(), 59));
|
||||
let output = parser.parse("abcd").unwrap();
|
||||
assert_eq!((("A".to_owned(), 59), "cd"), output);
|
||||
|
||||
let spaces = repeated(literal_char(' ')).at_least(1);
|
||||
let parser = seq2(literal("lute"), spaces).to(500);
|
||||
assert_eq!(parser.parse("lute "), Ok((500, "")));
|
||||
assert_eq!(parser.parse("lute"), Err(((), "")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_combinators() {
|
||||
let parser = repeated(literal_char('a')).to(10).then(literal_char('b'));
|
||||
let output = parser.parse("aaaaaaaabcd").unwrap();
|
||||
assert_eq! {((10, 'b'), "cd"), output};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional() {
|
||||
let parser = seq2(
|
||||
optional(literal("alpha")),
|
||||
seq2(repeated(literal(" ")), literal("beta")),
|
||||
);
|
||||
|
||||
let output1 = parser.parse(" beta").unwrap();
|
||||
assert_eq!(output1.0 .0, None);
|
||||
let output2 = parser.parse("alpha beta").unwrap();
|
||||
assert_eq!(output2.0 .0, Some("alpha"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeated() {
|
||||
let spaces = repeated(literal_char(' ')).at_least(1);
|
||||
let bongo = literal("bongo");
|
||||
let parser = repeated(bongo).separated_by(map(spaces, |_| ()));
|
||||
let output = parser.parse("bongo bongo bongo bongo");
|
||||
let output = output.unwrap();
|
||||
assert_eq!(output.0, vec!["bongo", "bongo", "bongo", "bongo"]);
|
||||
}
|
||||
}
|
||||
|
72
src/test/mod.rs
Normal file
72
src/test/mod.rs
Normal file
@ -0,0 +1,72 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parsing() {
|
||||
let (parsed, rest) = literal("a")("a yolo").unwrap();
|
||||
assert_eq!(parsed, "a");
|
||||
assert_eq!(rest, " yolo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sequence() {
|
||||
let parser = seq2(literal("bongo"), seq2(literal(" "), literal("jonzzz")));
|
||||
let output = parser.parse("bongo jonzzz").unwrap();
|
||||
assert_eq!(output.0 .0, "bongo");
|
||||
assert_eq!(output.0 .1, (" ", "jonzzz"));
|
||||
assert_eq!(output.1, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_choice() {
|
||||
let a = literal("bongo");
|
||||
let b = literal("sucy");
|
||||
let c = literal("ara");
|
||||
let inputs = [&a as &dyn Parser<&str, &str, ()>, &b, &c];
|
||||
let parser = choice(&inputs);
|
||||
|
||||
let output = parser.parse("ara hajimete").unwrap();
|
||||
assert_eq!(("ara", " hajimete"), output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map() {
|
||||
let parser =
|
||||
seq2(literal("a"), literal("b")).map(|(a, _b): (&str, &str)| (a.to_uppercase(), 59));
|
||||
let output = parser.parse("abcd").unwrap();
|
||||
assert_eq!((("A".to_owned(), 59), "cd"), output);
|
||||
|
||||
let spaces = repeated(literal_char(' ')).at_least(1);
|
||||
let parser = seq2(literal("lute"), spaces).to(500);
|
||||
assert_eq!(parser.parse("lute "), Ok((500, "")));
|
||||
assert_eq!(parser.parse("lute"), Err(((), "")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_combinators() {
|
||||
let parser = repeated(literal_char('a')).to(10).then(literal_char('b'));
|
||||
let output = parser.parse("aaaaaaaabcd").unwrap();
|
||||
assert_eq! {((10, 'b'), "cd"), output};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional() {
|
||||
let parser = seq2(
|
||||
optional(literal("alpha")),
|
||||
seq2(repeated(literal(" ")), literal("beta")),
|
||||
);
|
||||
|
||||
let output1 = parser.parse(" beta").unwrap();
|
||||
assert_eq!(output1.0 .0, None);
|
||||
let output2 = parser.parse("alpha beta").unwrap();
|
||||
assert_eq!(output2.0 .0, Some("alpha"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repeated() {
|
||||
let spaces = repeated(literal_char(' ')).at_least(1);
|
||||
let bongo = literal("bongo");
|
||||
let parser = repeated(bongo).separated_by(map(spaces, |_| ()));
|
||||
let output = parser.parse("bongo bongo bongo bongo");
|
||||
let output = output.unwrap();
|
||||
assert_eq!(output.0, vec!["bongo", "bongo", "bongo", "bongo"]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user