From 72ceac05f1ed1f279f7348201044d665d088e57c Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 10 Jul 2012 01:29:04 -0400 Subject: [PATCH] Added tests for recognizing decimals --- source/lexer.scm | 5 +++-- tests/test_lexer.scm | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/source/lexer.scm b/source/lexer.scm index fda961b..cfea702 100644 --- a/source/lexer.scm +++ b/source/lexer.scm @@ -73,11 +73,12 @@ (define (dlang/decimal in) (string-append - (match in #\.) - (dlang/digits in ""))) + (string (char-match in #\.)) + (dlang/integer in))) (define (dlang/exponent in) (string-append + ;(string (char-match-one-of in '(#\e #\E))) (if (char=? (buf-lookahead! in 1) #\e) (match in #\e) (match in #\E)) (dlang/integer in ""))) diff --git a/tests/test_lexer.scm b/tests/test_lexer.scm index c0c1c76..460f0dc 100644 --- a/tests/test_lexer.scm +++ b/tests/test_lexer.scm @@ -48,7 +48,7 @@ (dlang/integer buffer))))) (def-test "dlang/integer should error when EOF" - (call-with-input-string "abc" + (call-with-input-string "" (lambda (input) (define buffer (buf input read-char)) (check-error "Expected an integer" @@ -56,6 +56,43 @@ ; dlang/decimal ;------------------------------------------------------------------------------ +(def-test "dlang/decimal should recognize an decimal of length one" + (call-with-input-string ".0" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/decimal buffer)) + (and (string? result) + (equal? ".0" result))))) + +(def-test "dlang/decimal should recognize an decimal of length two" + (call-with-input-string ".01" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/decimal buffer)) + (and (string? result) + (equal? ".01" result))))) + +(def-test "dlang/decimal should recognize an decimal of length three" + (call-with-input-string ".012" + (lambda (input) + (define buffer (buf input read-char)) + (define result (dlang/decimal buffer)) + (and (string? result) + (equal? ".012" result))))) + +(def-test "dlang/decimal should error when no integer portion in input" + (call-with-input-string ". " + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected an integer" + (dlang/decimal buffer))))) + +(def-test "dlang/decimal should error when EOF" + (call-with-input-string "" + (lambda (input) + (define buffer (buf input read-char)) + (check-error "Expected '.', received EOF instead" + (dlang/decimal buffer))))) ; dlang/exponent ;------------------------------------------------------------------------------ -- 2.52.0