From 0a7ea142030afd2e9fa0b6e41cd59de13f42b31d Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sat, 16 Mar 2019 01:10:52 -0700 Subject: [PATCH] Add test to make sure that string slices print correctly. --- tests/runtime/test-string.scm | 43 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/tests/runtime/test-string.scm b/tests/runtime/test-string.scm index 311e41459..da7788586 100644 --- a/tests/runtime/test-string.scm +++ b/tests/runtime/test-string.scm @@ -3093,17 +3093,20 @@ USA. (= "aaafoo" (string-trim-right "aaafooaaa" (char-set #\f #\o))))) (define (string-copy!:all-indices to from) - (let ((to-length (string-length to)) - (from-limit (+ (string-length from) 1))) + (let ((to-length (string-length to))) (append-map (lambda (s+e) (map (lambda (at) (cons at s+e)) (iota (- to-length (- (cadr s+e) (car s+e)))))) - (append-map (lambda (start) - (map (lambda (end) - (list start end)) - (iota (- from-limit start) start))) - (iota from-limit))))) + (substring:all-indices from)))) + +(define (substring:all-indices string) + (let ((limit (+ (string-length string) 1))) + (append-map (lambda (start) + (map (lambda (end) + (list start end)) + (iota (- limit start) start))) + (iota limit)))) (define-test 'string-copy!:different-strings (let ((s1 "abcdefghijklmnopqrstuvwxyz") @@ -3136,15 +3139,29 @@ USA. 'expression expr)))))) (string-copy!:all-indices s1 s1)))) -(define (maybe-expect-failure expect-failure? body) - (if expect-failure? - (expect-failure body) - (body))) - (define (string-copy!:predict s1 at s2 #!optional start end) (list->string (let ((l1 (string->list s1)) (l2 (string->list s2 start end))) (append (list-head l1 at) l2 - (list-tail l1 (+ at (length l2))))))) \ No newline at end of file + (list-tail l1 (+ at (length l2))))))) + +(define-test 'string-slice + (let ((s "abcdefghijklmnopqrstuvwxyz")) + (map (lambda (indices) + (let ((start (car indices)) + (end (cadr indices))) + (let ((expr `(string-slice ,s ,start ,end))) + (lambda () + (let ((sut (string-slice s start end)) + (expected (substring:predict s start end))) + (assert-string= sut expected 'expression expr) + (assert-string= (call-with-output-string + (lambda (port) + (write sut port))) + (string-append "\"" expected "\""))))))) + (substring:all-indices s)))) + +(define (substring:predict s i j) + (list->string (sublist (string->list s) i j))) \ No newline at end of file -- 2.25.1