;;;; string-utils-test.scm -*- Hen -*- ;;;; Kon Lovett, Oct '17 (use test) (use string-utils) (test-begin "String Utils") (test-group "Unicode" (test-assert (ascii-codepoint? #\a)) (test "abc" (unicode-string #\a #\b #\c)) (test "cebb" (string->hex (unicode-char->string #\U03BB))) (test "cebbcebbcebb" (string->hex (unicode-string #\U03BB #\U03BB #\U03BB))) (test "cebbcebb" (string->hex (unicode-make-string 2 #\U03BB))) ) (test-group "Memoized" (test "a" (make-string+ 1 #\a)) (define a5 (make-string+ 5 #\a)) (test "aaaaa" a5) (define spc5 (make-string+ 5)) (test " " spc5) (test-assert (eq? a5 (make-string+ 5 #\a))) (test-assert (eq? spc5 (make-string+ 5 #\space))) (define tststr1 (string+ #\我 #\你)) (test "(string+ #\\我 #\\你)" tststr1 "我你") (test-assert (eq? tststr1 (string+ #\我 #\你))) (define tststr2 "上海的东西很便宜") (test-assert (eq? tststr2 (global-string tststr2))) ) (use to-hex) (use srfi-4) (test-group "To Hex" (let ((t (make-string (* 2 3)))) (blob_to_hex t (string->blob "12abc34") 2 5) (test "616263" t) ) ; (let ((t (make-string (* 2 3)))) (u8vec_to_hex t (u8vector 1 2 #x61 #x62 #x63 3 4) 2 5) (test "616263" t) ) ; (let ((t (make-string (* 2 2)))) (s8vec_to_hex t (s8vector 1 2 -45 -54 3 4) 2 4) (test "d3ca" t) ) ) (use string-hexadecimal) (test-group "String -> Hex" (test "616263" (string->hex "12abc34" 2 5)) (test "414243444546" (string->hex "ABCDEF")) (test "4243444546" (string->hex "ABCDEF" 1)) (test "4243" (string->hex "ABCDEF" 1 3)) ) (test-group "Hex -> String" (test "abc" (hex->string "616263")) (test "ABCDEF" (hex->string "414243444546")) (test "BCDEF" (hex->string "4243444546")) (test "BC" (hex->string "4243")) (test "jkL]" (hex->string "6a6B4c5D")) (test-error (hex->string "424")) ) ;; (use string-interpolation) (import (prefix utf8-string-interpolation utf8::)) (require-library utf8-string-interpolation) (use string-interpolation-syntax) (test-group "String Interpolation" (test "foo 3 bar" (string-interpolate "foo #(+ 1 2) bar")) (test "foo 3 bar" (string-interpolate "foo #(+ 1 2) bar" eval-env: (scheme-report-environment 5))) (test "foo 3 bar" (string-interpolate "foo ${(+ 1 2)} bar" eval-tag: #\$)) (test "foo 3 bar" (string-interpolate "foo $(+ 1 2) bar" eval-tag: #\$ eval-env: (scheme-report-environment 5))) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵")) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵" eval-env: (scheme-report-environment 5))) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 ${(+ 1 2)} 东西很贵" eval-tag: #\$)) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 $(+ 1 2) 东西很贵" eval-tag: #\$ eval-env: (scheme-report-environment 5))) ) (use data-structures) ;must be "top level"; cannot be test-group (test-begin "String Interpolation SYNTAX") (set-sharp-string-interpolation-syntax string-interpolate) (test '("foo 3 bar") (list (call-with-input-string "#\"foo #{(+ 1 2)} bar\"" read))) (set-sharp-string-interpolation-syntax #f) (set-sharp-string-interpolation-syntax (cute string-interpolate <> eval-tag: #\$ eval-env: (scheme-report-environment 5))) (test '("foo 3 bar") (list (call-with-input-string "#\"foo ${(+ 1 2)} bar\"" read))) (set-sharp-string-interpolation-syntax #f) (test-end) (test-end) (test-exit)