From 774f1d65ca3b82490d2e4b54fcc5b1f8b91932c8 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 18 Mar 2024 17:21:02 -0500 Subject: [PATCH] Add tests --- src/lexic/mod.rs | 8 ++++++++ src/lexic/scanner/number.rs | 32 +++++++++++++++++++++++++++++ src/lexic/scanner/string.rs | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/src/lexic/mod.rs b/src/lexic/mod.rs index 2ab9b07..46f6de3 100755 --- a/src/lexic/mod.rs +++ b/src/lexic/mod.rs @@ -467,4 +467,12 @@ mod indentation_tests { assert_eq!(TokenType::Comment, tokens[0].token_type); } + + #[test] + fn should_emit_error_on_incorrect_indentation() { + let input = String::from("1\n 2\n 3"); + let tokens = get_tokens(&input); + + assert!(tokens.is_err()); + } } diff --git a/src/lexic/scanner/number.rs b/src/lexic/scanner/number.rs index 40059b2..a0d48ac 100755 --- a/src/lexic/scanner/number.rs +++ b/src/lexic/scanner/number.rs @@ -436,4 +436,36 @@ mod tests { panic!("Expected some value") }; } + + #[test] + fn should_not_scan_invalid_scientific_notation() { + let input = str_to_vec("1e"); + let start_pos = 0; + + match scan(&input, start_pos) { + LexResult::Err(reason) => { + assert_eq!( + "The characters after 'e' are not + or -, or are not followed by a number", + reason.reason + ) + } + _ => panic!("Expected an error"), + } + } + + #[test] + fn should_not_scan_invalid_scientific_notation_2() { + let input = str_to_vec("1e+f"); + let start_pos = 0; + + match scan(&input, start_pos) { + LexResult::Err(reason) => { + assert_eq!( + "The characters after 'e' are not + or -, or are not followed by a number", + reason.reason + ) + } + _ => panic!("Expected an error"), + } + } } diff --git a/src/lexic/scanner/string.rs b/src/lexic/scanner/string.rs index 06b4cd4..2bc0deb 100755 --- a/src/lexic/scanner/string.rs +++ b/src/lexic/scanner/string.rs @@ -179,4 +179,45 @@ mod tests { panic!() } } + + #[test] + fn should_scan_non_escape_characters_preceded_by_bsls() { + let input = str_to_vec("\"Sample\\atext\""); + let start_pos = 1; + if let LexResult::Some(token, next) = scan(&input, start_pos) { + assert_eq!(14, next); + assert_eq!(TokenType::String, token.token_type); + assert_eq!("\"Sample\\atext\"", token.value); + assert_eq!(0, token.position); + } else { + panic!() + } + } + + #[test] + fn shouldnt_panic_when_encountering_eof_after_bsls() { + let input = str_to_vec("\"Sample\\"); + let start_pos = 1; + let result = scan(&input, start_pos); + + match result { + LexResult::Err(reason) => { + assert_eq!("Incomplete string found", reason.reason) + }, + _ => panic!("expected an error") + } + } + + #[test] + fn should_not_scan_an_unfinished_string() { + let input = str_to_vec("\"Hello, world!"); + let result = scan(&input, 1); + + match result { + LexResult::Err(reason) => { + assert_eq!("Incomplete string found", reason.reason) + }, + _ => panic!("expected an error") + } + } }