Make some unit tests for the lexer
This commit is contained in:
parent
7e784ffce1
commit
7f19c78127
2 changed files with 24 additions and 1 deletions
|
@ -170,3 +170,25 @@ impl Iterator for Lexer {
|
|||
lex
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// TESTING
|
||||
//
|
||||
|
||||
#[test]
|
||||
fn lexer_finds_parens() {
|
||||
let mut lexer = Lexer::new("()".to_string());
|
||||
assert_next_token(&mut lexer, &Token::LeftParen("(".to_string()));
|
||||
assert_next_token(&mut lexer, &Token::RightParen(")".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lexer_finds_identifiers() {
|
||||
let mut lexer = Lexer::new("abc".to_string());
|
||||
assert_next_token(&mut lexer, &Token::Identifier("abc".to_string()));
|
||||
}
|
||||
|
||||
fn assert_next_token(lexer: &mut Lexer, expected: &Token) {
|
||||
let lex = lexer.next().unwrap();
|
||||
assert_eq!(lex.token, *expected);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(PartialEq)]
|
||||
pub enum Token {
|
||||
LeftParen(String),
|
||||
RightParen(String),
|
||||
|
@ -15,7 +16,7 @@ pub enum Token {
|
|||
/// place.
|
||||
#[derive(Debug)]
|
||||
pub struct Lex {
|
||||
token: Token,
|
||||
pub token: Token,
|
||||
}
|
||||
|
||||
impl Lex {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue