aboutsummaryrefslogtreecommitdiffstats
path: root/format-test.csc
blob: b7b2c999eef2437f46bca7ad73b343b2c2cd8253 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(import (scheme base)
        (only (csc strings) str-quote)
        (only (csc testing) define-test errorf subtest)
        (csc format))


(define-test (test-vsprintf t)
  (define-record-type <test-case>
    (test-case name fmt args want)
    test-case?
    (name name)
    (fmt fmt)
    (args args)
    (want want))
  (let ((tests (list
                 (test-case
                   "single-string"
                   "test-string"
                   '()
                   "test-string")
                 (test-case
                   "list"
                   "{}"
                   '((1 2 3))
                   "(1 2 3)")
                 (test-case
                   "complex"
                   "this {} is {} a {} test"
                   '((1 2 3) 1 "bbb")
                   "this (1 2 3) is 1 a bbb test")
                 (test-case
                   "escape open"
                   "{{"
                   '()
                   "{")
                 (test-case
                   "escape close"
                   "}}"
                   '()
                   "}"))))
    (for-each
      (lambda (tc)
        (subtest t (name tc)
                 (let ((got (vsprintf (fmt tc) (args tc))))
                   (unless (equal? got (want tc))
                     (errorf t "(vsprintf {} {}) = {}, want {}." (str-quote fmt) (args tc) got (want tc))))))
      tests)))