(module test-string-template mzscheme (require "string-template.ss" (lib "etc.ss") (planet "test.ss" ("schematics" "schemeunit.plt" 2)) (planet "text-ui.ss" ("schematics" "schemeunit.plt" 2))) (define template-tests (test-suite "simple-template tests" (test-case "construct empty string" (let ([tmpl (make-template "")]) (check-equal? (template->string tmpl (hash-table)) ""))) (test-case "simple, no interpolation" (let ([tmpl (make-template "epistemic logic is hard")]) (check-equal? (template->string tmpl (hash-table)) "epistemic logic is hard"))) (test-case "simple interpolation" (let ([tmpl (make-template "$foo$")]) (check-equal? (template->string tmpl (hash-table 'equal ("foo" "hello world"))) "hello world"))) (test-case "escaped delimiter" (let ([tmpl (make-template "You have \\$$amount$ dollars in your account")]) (check-equal? (template->string tmpl (hash-table 'equal ("amount" 42))) "You have $42 dollars in your account"))) (test-case "simple interpolation with space" (let ([tmpl (make-template "$ foo $")]) (check-equal? (template->string tmpl (hash-table 'equal ("foo" "hello world"))) "hello world"))) (test-case "two interpolations" (let ([tmpl (make-template "$name$ and $name$")]) (check-equal? (template->string tmpl (hash-table 'equal ("name" "jane doe"))) "jane doe and jane doe"))) (test-case "two people" (let ([tmpl (make-template "$name1$ and $name2$ went up the hill to fetch a pail of water")]) (check-equal? (template->string tmpl (hash-table 'equal ("name1" "jack") ("name2" "jill"))) "jack and jill went up the hill to fetch a pail of water"))) (test-case "simple separator" (let ([tmpl (make-template "$name; separator=\", \"$")]) (check-equal? (template->string tmpl (hash-table 'equal ("name" '("isis" "jaz")))) "isis, jaz"))) (test-case "missing an attribute should show an error message" (let ([tmpl (make-template "$oops$")]) (check-exn exn:template:missing-attribute? (lambda () (template->string tmpl (hash-table)))))) (test-case "simple separator with newlines" (let ([tmpl (make-template "$name; separator=\"\\n\"$")]) (check-equal? (template->string tmpl (hash-table 'equal ("name" '("isis" "jaz")))) "isis\njaz"))))) (test/text-ui template-tests))