From 3669d5d2cc4b86e15c835c703ed006f5750ca1d1 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 28 Jan 2024 02:36:21 -0800 Subject: [PATCH] Add test module --- src/lib.rs | 79 ++----------------------------------------------- src/test/mod.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 76 deletions(-) create mode 100644 src/test/mod.rs diff --git a/src/lib.rs b/src/lib.rs index a60747e..bfe9e72 100644 --- a/src/lib.rs +++ b/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"]); - } -} diff --git a/src/test/mod.rs b/src/test/mod.rs new file mode 100644 index 0000000..0538e03 --- /dev/null +++ b/src/test/mod.rs @@ -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"]); +}