From 502da79d424517d55553952c860188a545b1c1f1 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 21 Mar 2020 14:39:16 -0400 Subject: [PATCH] test errors --- src/lexer.rs | 34 ++++++++++++++++++++++++++-------- tests/error.rs | 9 +++++++++ tests/macros.rs | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 tests/error.rs diff --git a/src/lexer.rs b/src/lexer.rs index e118d15..c7e4901 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -250,20 +250,38 @@ impl<'a> Lexer<'a> { } fn lex_num(&mut self) -> TokenKind { - let mut string = String::with_capacity(99); + let mut whole = String::new(); while let Some(c) = self.buf.peek() { - if !c.is_numeric() && c != &'.' { + if !c.is_numeric() { break; } - let tok = self - .buf - .next() - .expect("this is impossible because we have already peeked"); + let tok = self.buf.next().unwrap(); self.pos.next_char(); - string.push(tok); + whole.push(tok); } - TokenKind::Number(string) + let mut dec = String::new(); + + if self.buf.peek() == Some(&'.') { + self.buf.next(); + dec.push('.'); + while let Some(c) = self.buf.peek() { + if !c.is_numeric() { + break; + } + let tok = self.buf.next().unwrap(); + self.pos.next_char(); + dec.push(tok); + } + } + + if dec.len() == 1 { + return TokenKind::Error("Expected digit.".into()); + } + + whole.push_str(&dec); + + TokenKind::Number(whole) } fn lex_hash(&mut self) -> TokenKind { diff --git a/tests/error.rs b/tests/error.rs new file mode 100644 index 0000000..3ce0e79 --- /dev/null +++ b/tests/error.rs @@ -0,0 +1,9 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +error!( + nothing_after_decimal, + "a {\n color: 1.;\n}\n", "Error: Expected digit." +); diff --git a/tests/macros.rs b/tests/macros.rs index 447250a..8d7b0bc 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -33,3 +33,17 @@ macro_rules! test { } }; } + +#[macro_export] +macro_rules! error { + ($func:ident, $input:expr, $err:expr) => { + #[test] + #[allow(non_snake_case)] + fn $func() { + match grass::StyleSheet::new($input) { + Ok(..) => panic!("did not fail"), + Err(e) => assert_eq!($err, e.to_string().as_str()), + } + } + }; +}