Merge pull request #4 from fosdickio/add-lexer-unit-tests

Adding additional lexer unit tests
This commit is contained in:
Nicolás Hatcher Andrés
2024-02-14 22:41:49 +01:00
committed by GitHub
3 changed files with 69 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ use crate::language::get_language;
use crate::locale::get_locale;
use crate::expressions::{
lexer::{Lexer, LexerMode},
lexer::{Lexer, LexerError, LexerMode},
token::TokenType::*,
token::{Error, OpCompare, OpProduct, OpSum},
types::ParsedReference,
@@ -411,6 +411,20 @@ fn test_name_r1c1p() {
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_reference_r1c1_error() {
let mut lx = new_lexer("$A$4", false);
lx.mode = LexerMode::R1C1;
assert_eq!(
lx.next_token(),
Illegal(LexerError {
position: 1,
message: "Cannot parse A1 reference in R1C1 mode".to_string(),
})
);
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_name_wrong_ref() {
let mut lx = new_lexer("Sheet1!2", false);
@@ -629,6 +643,22 @@ fn test_ampersand() {
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_comma() {
// Used for testing locales where the comma is not a decimal separator
let mut lx = new_lexer("12,34", false);
assert_eq!(lx.next_token(), Number(12.0));
assert_eq!(lx.next_token(), Comma);
assert_eq!(lx.next_token(), Number(34.0));
assert_eq!(lx.next_token(), EOF);
// Used for testing locales where the comma is the decimal separator
let mut lx = new_lexer("12,34", false);
lx.locale.numbers.symbols.decimal = ",".to_string();
assert_eq!(lx.next_token(), Number(12.34));
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_semicolon() {
let mut lx = new_lexer("FALSE;", false);

View File

@@ -112,6 +112,39 @@ fn test_range_with_sheet_with_space() {
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_range_error() {
let mut lx = new_lexer("'Sheet 1'!3.4:5");
assert_eq!(
lx.next_token(),
Illegal(LexerError {
position: 10,
message: "Expecting reference in range".to_string(),
})
);
assert_eq!(lx.next_token(), EOF);
let mut lx = new_lexer("'Sheet 1'!3:A2");
assert_eq!(
lx.next_token(),
Illegal(LexerError {
position: 14,
message: "Error parsing Range".to_string()
})
);
assert_eq!(lx.next_token(), EOF);
let mut lx = new_lexer("'Sheet 1'!3:");
assert_eq!(
lx.next_token(),
Illegal(LexerError {
position: 12,
message: "Error parsing Range".to_string()
})
);
assert_eq!(lx.next_token(), EOF);
}
#[test]
fn test_range_column() {
let mut lx = new_lexer("C:D");

View File

@@ -10,6 +10,11 @@ fn test_error_codes() {
Error::NA,
Error::NUM,
Error::ERROR,
Error::NIMPL,
Error::SPILL,
Error::CALC,
Error::CIRC,
Error::NULL
];
for (i, error) in errors.iter().enumerate() {
let s = format!("{}", error);