diff --git a/src/test/literals.rs b/src/test/literals.rs index 8181fb3..fbfc5a1 100644 --- a/src/test/literals.rs +++ b/src/test/literals.rs @@ -1,71 +1,90 @@ -use crate::{test::parse_inner_token, Rule}; +use pest::consumes_to; +use pest::parses_to; + +use crate::Rule; +use crate::SnekParser; #[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 - ); + parses_to!(parser: SnekParser, input: "0", rule: Rule::literal, tokens: [ + literal(0, 1, [ + integer(0, 1) + ]) + ]); + parses_to!(parser: SnekParser, input: "100", rule: Rule::literal, tokens: [ + literal(0, 3, [ + integer(0, 3) + ]) + ]); } #[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 - ); + parses_to!(parser: SnekParser, input: "0.0", rule: Rule::literal, tokens: [ + literal(0, 3, [ + float(0, 3) + ]) + ]); + parses_to!(parser: SnekParser, input: ".0", rule: Rule::literal, tokens: [ + literal(0, 2, [ + float(0, 2) + ]) + ]); + parses_to!(parser: SnekParser, input: "0.", rule: Rule::literal, tokens: [ + literal(0, 2, [ + float(0, 2) + ]) + ]); + parses_to!(parser: SnekParser, input: "11.2", rule: Rule::literal, tokens: [ + literal(0, 4, [ + float(0, 4) + ]) + ]); } #[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 - ); + parses_to!(parser: SnekParser, input: "0xFF", rule: Rule::literal, tokens: [ + literal(0, 4, [ + byte(0, 4) + ]) + ]); + parses_to!(parser: SnekParser, input: "0b0010", rule: Rule::literal, tokens: [ + literal(0, 6, [ + byte(0, 6) + ]) + ]); } #[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 - ); + parses_to!(parser: SnekParser, input: r#""Hello world""#, rule: Rule::literal, tokens: [ + literal(0, 13, [ + string(0, 13) + ]) + ]); + parses_to!(parser: SnekParser, input: r#""""#, rule: Rule::literal, tokens: [ + literal(0, 2, [ + string(0, 2) + ]) + ]); + parses_to!(parser: SnekParser, input: r#""\"""#, rule: Rule::literal, tokens: [ + literal(0, 3, [ + string(0, 3) + ]) + ]); } #[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"); + parses_to!(parser: SnekParser, input: "true", rule: Rule::literal, tokens: [ + literal(0, 4, [ + boolean(0, 4) + ]) + ]); + parses_to!(parser: SnekParser, input: "false", rule: Rule::literal, tokens: [ + literal(0, 5, [ + boolean(0, 5) + ]) + ]); } diff --git a/src/test/mod.rs b/src/test/mod.rs index c8c2ab2..8c1e65b 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1,17 +1,7 @@ -use pest::iterators::Pair; mod infix_expressions; 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() -} +use crate::parse; #[test] fn it_parses() {