From f18b25135f9d1f6d9ad098d30a04a79424db6d85 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Wed, 1 Aug 2012 11:53:59 -0400 Subject: [PATCH] Added unit tests for charport initialize and charport-read --- source/parse-utils.scm | 4 ++-- tests/test.scm | 2 +- tests/test_parse_utils.scm | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/source/parse-utils.scm b/source/parse-utils.scm index 8c600cf..e0c2f53 100644 --- a/source/parse-utils.scm +++ b/source/parse-utils.scm @@ -27,14 +27,14 @@ (syntree-children=? (cdr ch1) (cdr ch2)))))) (define-record charport port line column) -(define (charport port) (make-charport port 0 0)) +(define (charport port) (make-charport port 1 1)) (define (charport-read chprt) (define ch (read-char (charport-port chprt))) (if (char=? ch #\newline) (begin (charport-line-set! chprt (+ 1 (charport-line chprt))) - (charport-column-set! chprt 0)) + (charport-column-set! chprt 1)) (charport-column-set! chprt (+ 1 (charport-column chprt)))) ch) diff --git a/tests/test.scm b/tests/test.scm index 48629ba..0b76944 100644 --- a/tests/test.scm +++ b/tests/test.scm @@ -3,7 +3,7 @@ (define unit-tests '()) (define (register-test! test) - (set! unit-tests (append unit-tests (list test)))) + (set! unit-tests (cons test unit-tests))) (define (print-summary pass fail) (if (zero? fail) diff --git a/tests/test_parse_utils.scm b/tests/test_parse_utils.scm index 1cfdff9..81304ec 100644 --- a/tests/test_parse_utils.scm +++ b/tests/test_parse_utils.scm @@ -79,6 +79,34 @@ (list (syntree 'foo "" '())) (list (syntree 'bar "" '()))))) +; charport +;------------------------------------------------------------------------------ +(def-test "charport should initialize a charport properly" + (call-with-input-string "a" + (lambda (input) + (define port (charport input)) + (and (equal? input (charport-port port)) + (equal? 1 (charport-line port)) + (equal? 1 (charport-column port)))))) + +; charport-read +;------------------------------------------------------------------------------ +(def-test "charport-read should increment column when character is not newline" + (call-with-input-string "a" + (lambda (input) + (define port (charport input)) + (and (equal? #\a (charport-read port)) + (equal? 1 (charport-line port)) + (equal? 2 (charport-column port)))))) + +(def-test "charport-read should increment line when character is newline" + (call-with-input-string "\n" + (lambda (input) + (define port (charport input)) + (and (equal? #\newline (charport-read port)) + (equal? 2 (charport-line port)) + (equal? 1 (charport-column port)))))) + ; char-match ;------------------------------------------------------------------------------ (def-test "char-match should consume and return char if the next char matches" -- 2.52.0