From 58b937521f3e459089c0d475551bf9a49f930657 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Fri, 8 May 2026 15:25:55 +0300 Subject: Fold unclosed string error into Token variant UnclosedString was the only error variant, making lexer::Error redundant. Also removes Result from the Iterator impl. --- compiler/src/lexer/tests.rs | 49 +++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) (limited to 'compiler/src/lexer/tests.rs') diff --git a/compiler/src/lexer/tests.rs b/compiler/src/lexer/tests.rs index 65dd2f2..30be85a 100644 --- a/compiler/src/lexer/tests.rs +++ b/compiler/src/lexer/tests.rs @@ -4,7 +4,7 @@ use super::Token::*; use super::*; fn tokenize<'a>(input: &'a str) -> Vec> { - Lexer::new(input).map(|s| s.into_inner().unwrap()).collect() + Lexer::new(input).map(|s| s.into_inner()).collect() } #[test] @@ -102,6 +102,22 @@ fn test_string_escapes() { } } +#[test] +fn test_unclosed_strings() { + let cases = vec![ + (r#""abc"#, vec![UnclosedString("abc")]), + (r#""abc\""#, vec![UnclosedString(r#"abc\""#)]), + ("\"abc\n", vec![UnclosedString("abc")]), + ("\"abc\\\ndef", vec![UnclosedString("abc\\\ndef")]), + ("\"abc\n\"def\"", vec![UnclosedString("abc"), String("def")]), + (r#"""#, vec![UnclosedString("")]), + ("\"\n\"", vec![UnclosedString(""), UnclosedString("")]), + ]; + for (code, tokens) in cases { + assert_eq!(tokenize(code), tokens); + } +} + #[test] fn test_symbols() { let cases = vec![ @@ -272,35 +288,6 @@ fn test_comments() { } } -fn first_error(input: &str) -> Error { - Lexer::new(input) - .find_map(|s| s.into_inner().err()) - .expect("error expected") -} - -#[test] -fn test_unclosed_string_at_eof() { - assert_eq!(first_error(r#""abc"#), Error::UnclosedString); - assert_eq!(first_error(r#"""#), Error::UnclosedString); -} - -#[test] -fn test_unclosed_string_with_trailing_escape() { - assert_eq!(first_error("\"abc\\"), Error::UnclosedString); -} - -#[test] -fn test_unclosed_string_with_newline() { - assert_eq!(first_error("\"abc\ndef\""), Error::UnclosedString); -} - -#[test] -fn test_lexer_stops_after_string_error() { - let mut lex = Lexer::new(r#""abc"#); - assert!(lex.next().unwrap().into_inner().is_err()); - assert!(lex.next().is_none()); -} - fn spans(input: &str) -> Vec<(Pos, Pos)> { Lexer::new(input).map(|s| (s.start(), s.end())).collect() } @@ -324,7 +311,7 @@ fn test_span_after_newline() { } #[test] -fn test_span_multi_char_() { +fn test_span_multi_char() { let s = spans("foo"); assert_eq!(s, vec![(Pos::new(1, 0, 0), Pos::new(1, 3, 3))]); } -- cgit v1.3