diff options
| author | Tolmachev Igor <me@igorek.dev> | 2026-05-08 15:25:55 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2026-05-08 15:25:55 +0300 |
| commit | 58b937521f3e459089c0d475551bf9a49f930657 (patch) | |
| tree | 3c7e33c914445e3b6448ffc287cf70038c5e080c /compiler/src/lexer/tests.rs | |
| parent | 558c5dcaf7bcc32cfe5672c4113962e3bcd19188 (diff) | |
| download | crisp-58b937521f3e459089c0d475551bf9a49f930657.tar.gz crisp-58b937521f3e459089c0d475551bf9a49f930657.zip | |
Fold unclosed string error into Token variant
UnclosedString was the only error variant, making lexer::Error redundant. Also removes Result from
the Iterator impl.
Diffstat (limited to 'compiler/src/lexer/tests.rs')
| -rw-r--r-- | compiler/src/lexer/tests.rs | 49 |
1 files changed, 18 insertions, 31 deletions
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::*; | |||
| 4 | use super::*; | 4 | use super::*; |
| 5 | 5 | ||
| 6 | fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> { | 6 | fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> { |
| 7 | Lexer::new(input).map(|s| s.into_inner().unwrap()).collect() | 7 | Lexer::new(input).map(|s| s.into_inner()).collect() |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | #[test] | 10 | #[test] |
| @@ -103,6 +103,22 @@ fn test_string_escapes() { | |||
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | #[test] | 105 | #[test] |
| 106 | fn test_unclosed_strings() { | ||
| 107 | let cases = vec![ | ||
| 108 | (r#""abc"#, vec![UnclosedString("abc")]), | ||
| 109 | (r#""abc\""#, vec![UnclosedString(r#"abc\""#)]), | ||
| 110 | ("\"abc\n", vec![UnclosedString("abc")]), | ||
| 111 | ("\"abc\\\ndef", vec![UnclosedString("abc\\\ndef")]), | ||
| 112 | ("\"abc\n\"def\"", vec![UnclosedString("abc"), String("def")]), | ||
| 113 | (r#"""#, vec![UnclosedString("")]), | ||
| 114 | ("\"\n\"", vec![UnclosedString(""), UnclosedString("")]), | ||
| 115 | ]; | ||
| 116 | for (code, tokens) in cases { | ||
| 117 | assert_eq!(tokenize(code), tokens); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | #[test] | ||
| 106 | fn test_symbols() { | 122 | fn test_symbols() { |
| 107 | let cases = vec![ | 123 | let cases = vec![ |
| 108 | ("foo", vec![Symbol("foo")]), | 124 | ("foo", vec![Symbol("foo")]), |
| @@ -272,35 +288,6 @@ fn test_comments() { | |||
| 272 | } | 288 | } |
| 273 | } | 289 | } |
| 274 | 290 | ||
| 275 | fn first_error(input: &str) -> Error { | ||
| 276 | Lexer::new(input) | ||
| 277 | .find_map(|s| s.into_inner().err()) | ||
| 278 | .expect("error expected") | ||
| 279 | } | ||
| 280 | |||
| 281 | #[test] | ||
| 282 | fn test_unclosed_string_at_eof() { | ||
| 283 | assert_eq!(first_error(r#""abc"#), Error::UnclosedString); | ||
| 284 | assert_eq!(first_error(r#"""#), Error::UnclosedString); | ||
| 285 | } | ||
| 286 | |||
| 287 | #[test] | ||
| 288 | fn test_unclosed_string_with_trailing_escape() { | ||
| 289 | assert_eq!(first_error("\"abc\\"), Error::UnclosedString); | ||
| 290 | } | ||
| 291 | |||
| 292 | #[test] | ||
| 293 | fn test_unclosed_string_with_newline() { | ||
| 294 | assert_eq!(first_error("\"abc\ndef\""), Error::UnclosedString); | ||
| 295 | } | ||
| 296 | |||
| 297 | #[test] | ||
| 298 | fn test_lexer_stops_after_string_error() { | ||
| 299 | let mut lex = Lexer::new(r#""abc"#); | ||
| 300 | assert!(lex.next().unwrap().into_inner().is_err()); | ||
| 301 | assert!(lex.next().is_none()); | ||
| 302 | } | ||
| 303 | |||
| 304 | fn spans(input: &str) -> Vec<(Pos, Pos)> { | 291 | fn spans(input: &str) -> Vec<(Pos, Pos)> { |
| 305 | Lexer::new(input).map(|s| (s.start(), s.end())).collect() | 292 | Lexer::new(input).map(|s| (s.start(), s.end())).collect() |
| 306 | } | 293 | } |
| @@ -324,7 +311,7 @@ fn test_span_after_newline() { | |||
| 324 | } | 311 | } |
| 325 | 312 | ||
| 326 | #[test] | 313 | #[test] |
| 327 | fn test_span_multi_char_() { | 314 | fn test_span_multi_char() { |
| 328 | let s = spans("foo"); | 315 | let s = spans("foo"); |
| 329 | assert_eq!(s, vec![(Pos::new(1, 0, 0), Pos::new(1, 3, 3))]); | 316 | assert_eq!(s, vec![(Pos::new(1, 0, 0), Pos::new(1, 3, 3))]); |
| 330 | } | 317 | } |
