From 1b0d4b1edb05cc90cdedb1885705c7219599ae22 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Wed, 11 Jul 2012 13:51:25 -0400 Subject: [PATCH] Finished unit tests for lexer --- source/lexer.scm | 18 ++++++---- tests/test_lexer.scm | 85 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/source/lexer.scm b/source/lexer.scm index 5e0eaf7..11ea599 100644 --- a/source/lexer.scm +++ b/source/lexer.scm @@ -83,7 +83,7 @@ (define (dlang/exponent in) (string-append - ;(string (char-match-one-of in '(#\e #\E))) + ;(string (char-match-one-of in "eE")) (string (if (char=? (buf-lookahead! in 1) #\e) (char-match in #\e) (char-match in #\E))) @@ -95,15 +95,19 @@ (token 'character (string-append (string (char-match in #\')) - (string (buf-consume! in)) + (if (eof-object? (buf-lookahead! in 1)) + (error "Unexpected EOF while parsing character literal") + (string (buf-consume! in))) (string (char-match in #\')) ))) (define (dlang/string in) - (token 'string - (string-append - (string (char-match in #\")) - ;(accumulate-till in string-append "" #\") - (string (char-match in #\")) ))) + (define text (string (char-match in #\"))) + (while (and (not (eof-object? (buf-lookahead! in 1))) + (not (char=? #\newline (buf-lookahead! in 1))) + (not (char=? #\" (buf-lookahead! in 1)))) + (set! text (string-append text (string (buf-consume! in))))) + (set! text (string-append text (string (char-match in #\")))) + (token 'string text)) (define (dlang/symbol in) (token 'symbol diff --git a/tests/test_lexer.scm b/tests/test_lexer.scm index 80a7c96..03dfe8c 100644 --- a/tests/test_lexer.scm +++ b/tests/test_lexer.scm @@ -385,9 +385,94 @@ ; dlang/character ;------------------------------------------------------------------------------ +(def-test "dlang/character should recognize a character" + (call-with-input-string "'a'" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/character buffer)) + (and (token? result) + (equal? 'character (token-type result)) + (equal? "'a'" (token-text result)))))) + +(def-test "dlang/character should error when missing first single quote" + (call-with-input-string "a'" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected ''', received 'a' instead" + (dlang/character buffer))))) + +(def-test "dlang/character should error when missing second single quote" + (call-with-input-string "'a" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected ''', received EOF instead" + (dlang/character buffer))))) + +(def-test "dlang/character should error when EOF reached" + (call-with-input-string "'" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Unexpected EOF while parsing character literal" + (dlang/character buffer))))) ; dlang/string ;------------------------------------------------------------------------------ +(def-test "dlang/string should recognize an empty string" + (call-with-input-string "\"\"" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/string buffer)) + (and (token? result) + (equal? 'string (token-type result)) + (equal? "\"\"" (token-text result)))))) + +(def-test "dlang/string should recognize a string of length 1" + (call-with-input-string "\"a\"" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/string buffer)) + (and (token? result) + (equal? 'string (token-type result)) + (equal? "\"a\"" (token-text result)))))) + +(def-test "dlang/string should recognize a string of length 2" + (call-with-input-string "\"ab\"" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/string buffer)) + (and (token? result) + (equal? 'string (token-type result)) + (equal? "\"ab\"" (token-text result)))))) + +(def-test "dlang/string should recognize a string of length 3" + (call-with-input-string "\"abc\"" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/string buffer)) + (and (token? result) + (equal? 'string (token-type result)) + (equal? "\"abc\"" (token-text result)))))) + +(def-test "dlang/string should error when missing first double quote" + (call-with-input-string "a\"" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected '\"', received 'a' instead" + (dlang/string buffer))))) + +(def-test "dlang/string should error when missing second double quote" + (call-with-input-string "\"a" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected '\"', received EOF instead" + (dlang/string buffer))))) + +(def-test "dlang/string should error when EOF reached" + (call-with-input-string "\"" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected '\"', received EOF instead" + (dlang/string buffer))))) ; dlang/symbol ;------------------------------------------------------------------------------ -- 2.52.0