From 0e0e4010d6bfcc15d4e7ffe34c123c728242ad8f Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Jul 2024 11:46:41 +0200 Subject: [PATCH] Reorder tests --- src/lib.rs | 91 ++------------------------------------------ src/test/literals.rs | 71 ++++++++++++++++++++++++++++++++++ src/test/mod.rs | 18 +++++++++ 3 files changed, 92 insertions(+), 88 deletions(-) create mode 100644 src/test/literals.rs create mode 100644 src/test/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 10938b5..916a700 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,9 @@ use pest::{iterators::Pairs, Parser}; use pest_derive::Parser; +#[cfg(test)] +mod test; + #[derive(Parser)] #[grammar = "./grammar.pest"] pub struct SnekParser; @@ -15,91 +18,3 @@ pub fn parse_rule<'a>( ) -> Result, pest::error::Error> { SnekParser::parse(rule, src) } - -#[cfg(test)] -mod test { - - use pest::iterators::Pair; - - use crate::{parse, parse_rule, Rule}; - - fn parse_token<'a>(rule: Rule, src: &'a str) -> Pair<'a, Rule> { - let mut pairs = parse_rule(rule, src).unwrap(); - pairs.next().unwrap() - } - - fn parse_inner_token<'a>(rule: Rule, src: &'a str) -> Pair<'a, Rule> { - parse_token(rule, src).into_inner().next().unwrap() - } - - #[test] - fn it_parses() { - parse("hello world").unwrap(); - } - - #[test] - fn it_parses_numbers() { - assert_eq!( - parse_inner_token(Rule::literal, "0").as_rule(), - Rule::integer - ); - assert_eq!( - parse_inner_token(Rule::literal, "100").as_rule(), - Rule::integer - ); - } - - #[test] - fn it_parses_floats() { - assert_eq!( - parse_inner_token(Rule::literal, "0.").as_rule(), - Rule::float - ); - assert_eq!( - parse_inner_token(Rule::literal, ".5").as_rule(), - Rule::float - ); - assert_eq!( - parse_inner_token(Rule::literal, "11.15").as_rule(), - Rule::float - ); - } - - #[test] - fn it_parses_bytes() { - assert_eq!( - parse_inner_token(Rule::literal, "0xFF").as_rule(), - Rule::byte - ); - assert_eq!( - parse_inner_token(Rule::literal, "0b0010").as_rule(), - Rule::byte - ); - } - - #[test] - fn it_parses_strings() { - assert_eq!( - parse_inner_token(Rule::literal, r#""Hello world""#).as_rule(), - Rule::string - ); - assert_eq!( - parse_inner_token(Rule::literal, r#""""#).as_rule(), - Rule::string - ); - } - - #[test] - fn it_parses_booleans() { - assert_eq!( - parse_inner_token(Rule::literal, "true").as_rule(), - Rule::boolean - ); - assert_eq!(parse_inner_token(Rule::literal, "true").as_str(), "true"); - assert_eq!( - parse_inner_token(Rule::literal, "false").as_rule(), - Rule::boolean - ); - assert_eq!(parse_inner_token(Rule::literal, "false").as_str(), "false"); - } -} diff --git a/src/test/literals.rs b/src/test/literals.rs new file mode 100644 index 0000000..8181fb3 --- /dev/null +++ b/src/test/literals.rs @@ -0,0 +1,71 @@ +use crate::{test::parse_inner_token, Rule}; + +#[test] +fn it_parses_numbers() { + assert_eq!( + parse_inner_token(Rule::literal, "0").as_rule(), + Rule::integer + ); + assert_eq!( + parse_inner_token(Rule::literal, "100").as_rule(), + Rule::integer + ); +} + +#[test] +fn it_parses_floats() { + assert_eq!( + parse_inner_token(Rule::literal, "0.").as_rule(), + Rule::float + ); + assert_eq!( + parse_inner_token(Rule::literal, ".5").as_rule(), + Rule::float + ); + assert_eq!( + parse_inner_token(Rule::literal, "11.15").as_rule(), + Rule::float + ); +} + +#[test] +fn it_parses_bytes() { + assert_eq!( + parse_inner_token(Rule::literal, "0xFF").as_rule(), + Rule::byte + ); + assert_eq!( + parse_inner_token(Rule::literal, "0b0010").as_rule(), + Rule::byte + ); +} + +#[test] +fn it_parses_strings() { + assert_eq!( + parse_inner_token(Rule::literal, r#""Hello world""#).as_rule(), + Rule::string + ); + assert_eq!( + parse_inner_token(Rule::literal, r#""""#).as_rule(), + Rule::string + ); + assert_eq!( + parse_inner_token(Rule::literal, r#""\"""#).as_rule(), + Rule::string + ); +} + +#[test] +fn it_parses_booleans() { + assert_eq!( + parse_inner_token(Rule::literal, "true").as_rule(), + Rule::boolean + ); + assert_eq!(parse_inner_token(Rule::literal, "true").as_str(), "true"); + assert_eq!( + parse_inner_token(Rule::literal, "false").as_rule(), + Rule::boolean + ); + assert_eq!(parse_inner_token(Rule::literal, "false").as_str(), "false"); +} diff --git a/src/test/mod.rs b/src/test/mod.rs new file mode 100644 index 0000000..ede3f4b --- /dev/null +++ b/src/test/mod.rs @@ -0,0 +1,18 @@ +use pest::iterators::Pair; +mod literals; + +use crate::{parse, parse_rule, Rule}; + +fn parse_token<'a>(rule: Rule, src: &'a str) -> Pair<'a, Rule> { + let mut pairs = parse_rule(rule, src).unwrap(); + pairs.next().unwrap() +} + +fn parse_inner_token<'a>(rule: Rule, src: &'a str) -> Pair<'a, Rule> { + parse_token(rule, src).into_inner().next().unwrap() +} + +#[test] +fn it_parses() { + parse("hello world").unwrap(); +}