test errors

This commit is contained in:
ConnorSkees 2020-03-21 14:39:16 -04:00
parent 90fb2e6112
commit 502da79d42
3 changed files with 49 additions and 8 deletions

View File

@ -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 {

9
tests/error.rs Normal file
View File

@ -0,0 +1,9 @@
#![cfg(test)]
#[macro_use]
mod macros;
error!(
nothing_after_decimal,
"a {\n color: 1.;\n}\n", "Error: Expected digit."
);

View File

@ -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()),
}
}
};
}