Make environment generic and adapt closures
Added unit test
This commit is contained in:
Regular → Executable
+188
@@ -322,3 +322,191 @@ impl Lexer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::f64;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Lex `input` and return the resulting tokens, panicking on lexical errors.
|
||||
fn lex(input: &str) -> Vec<Token> {
|
||||
Lexer::new(input.to_string(), 0)
|
||||
.scans_tokens()
|
||||
.expect("expected input to lex without errors")
|
||||
}
|
||||
|
||||
/// Lex `input` and collect just the token types (including the trailing EOF).
|
||||
fn token_types(input: &str) -> Vec<TokenType> {
|
||||
lex(input).into_iter().map(|t| t.token_type).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_single_character_tokens() {
|
||||
assert_eq!(
|
||||
token_types("(){}[],.-+;:*%"),
|
||||
vec![
|
||||
TokenType::LeftParen,
|
||||
TokenType::RightParen,
|
||||
TokenType::LeftBrace,
|
||||
TokenType::RightBrace,
|
||||
TokenType::LeftBracket,
|
||||
TokenType::RightBracket,
|
||||
TokenType::Comma,
|
||||
TokenType::Dot,
|
||||
TokenType::Minus,
|
||||
TokenType::Plus,
|
||||
TokenType::Semicolon,
|
||||
TokenType::Colon,
|
||||
TokenType::Star,
|
||||
TokenType::Percent,
|
||||
TokenType::Eof,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_one_and_two_char_operators() {
|
||||
assert_eq!(
|
||||
token_types("! != = == < <= > >= /"),
|
||||
vec![
|
||||
TokenType::Bang,
|
||||
TokenType::BangEqual,
|
||||
TokenType::Equal,
|
||||
TokenType::EqualEqual,
|
||||
TokenType::Less,
|
||||
TokenType::LessEqual,
|
||||
TokenType::Greater,
|
||||
TokenType::GreaterEqual,
|
||||
TokenType::Slash,
|
||||
TokenType::Eof,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keyword_lookup_matches_known_words() {
|
||||
assert_eq!(get_keyword_token("and"), Some(TokenType::And));
|
||||
assert_eq!(get_keyword_token("if"), Some(TokenType::If));
|
||||
assert_eq!(get_keyword_token("then"), Some(TokenType::Then));
|
||||
assert_eq!(get_keyword_token("while"), Some(TokenType::While));
|
||||
assert_eq!(get_keyword_token("do"), Some(TokenType::StartBlock));
|
||||
assert_eq!(get_keyword_token("end"), Some(TokenType::EndBlock));
|
||||
assert_eq!(get_keyword_token("not_a_keyword"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_keywords_in_a_stream() {
|
||||
assert_eq!(
|
||||
token_types("if then else while print return"),
|
||||
vec![
|
||||
TokenType::If,
|
||||
TokenType::Then,
|
||||
TokenType::Else,
|
||||
TokenType::While,
|
||||
TokenType::Print,
|
||||
TokenType::Return,
|
||||
TokenType::Eof,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_integer_number() {
|
||||
let tokens = lex("42");
|
||||
assert_eq!(tokens[0].token_type, TokenType::Number);
|
||||
assert_eq!(tokens[0].literal, Some(BaseValue::Number(Number::I32(42))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_float_number() {
|
||||
let tokens = lex("3.141592653589793");
|
||||
assert_eq!(
|
||||
tokens[0].literal,
|
||||
Some(BaseValue::Number(Number::F64(f64::consts::PI)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_number_with_float_suffix() {
|
||||
let tokens = lex("5f");
|
||||
assert_eq!(tokens[0].literal, Some(BaseValue::Number(Number::F64(5.0))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_number_with_unsigned_suffix() {
|
||||
let tokens = lex("7u");
|
||||
assert_eq!(tokens[0].literal, Some(BaseValue::Number(Number::U128(7))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_string_literal_including_quotes() {
|
||||
// The lexer slices from the opening quote through the closing quote,
|
||||
// so the literal currently retains the surrounding quotes.
|
||||
let tokens = lex("\"hello\"");
|
||||
assert_eq!(tokens[0].token_type, TokenType::String);
|
||||
assert_eq!(
|
||||
tokens[0].literal,
|
||||
Some(BaseValue::String("\"hello\"".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_identifier() {
|
||||
let tokens = lex("foo_bar");
|
||||
assert_eq!(tokens[0].token_type, TokenType::Identifier);
|
||||
assert_eq!(
|
||||
tokens[0].literal,
|
||||
Some(BaseValue::String("foo_bar".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scans_boolean_and_nil_literals() {
|
||||
let tokens = lex("true false Nil");
|
||||
assert_eq!(tokens[0].literal, Some(BaseValue::Boolean(true)));
|
||||
assert_eq!(tokens[1].literal, Some(BaseValue::Boolean(false)));
|
||||
assert_eq!(tokens[2].literal, Some(BaseValue::Nil));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_line_comments() {
|
||||
assert_eq!(
|
||||
token_types("1 // a comment\n2"),
|
||||
vec![TokenType::Number, TokenType::Number, TokenType::Eof]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_block_comments() {
|
||||
assert_eq!(
|
||||
token_types("1 /* multi\nline */ 2"),
|
||||
vec![TokenType::Number, TokenType::Number, TokenType::Eof]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn always_appends_eof_even_for_empty_input() {
|
||||
let tokens = lex("");
|
||||
assert_eq!(tokens.len(), 1);
|
||||
assert_eq!(tokens[0].token_type, TokenType::Eof);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unterminated_string_is_a_lexical_error() {
|
||||
let result = Lexer::new("\"oops".to_string(), 0).scans_tokens();
|
||||
assert!(matches!(result, Err(LoxError::LexicalError { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unterminated_block_comment_is_a_lexical_error() {
|
||||
let result = Lexer::new("/* never ends".to_string(), 0).scans_tokens();
|
||||
assert!(matches!(result, Err(LoxError::LexicalError { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unexpected_character_is_a_lexical_error() {
|
||||
let result = Lexer::new("@".to_string(), 0).scans_tokens();
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user