summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/json/json_write.test
blob: 78d745a1b1203c4af972a1cf3b3b50e122f3b3a6 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# json_write.test - Copyright (C) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# Tests for the Tcllib json::write package
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: json_write.test,v 1.1 2009/11/25 04:41:01 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.0

testing {
    useLocal json_write.tcl json::write
}

# -------------------------------------------------------------------------

set data {grammar {
    rules {
        A {is {/ {t +} {t -}}                                         mode value}
        D {is {/ {t 0} {t 1} }                                        mode value}
        E {is {/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}} mode value}
        F {is {x {n T} {* {x {n A} {n T}}}}                           mode value}
        M {is {/ {t *} {t /}}                                         mode value}
        N {is {x {? {n S}} {+ {n D}}}                                 mode value}
        S {is {/ {t +} {t -}}                                         mode value}
        T {is  {n N}                                                  mode value}
    }
    start {n Expression}
}}

proc gen {serial} {
    array set g $serial
    array set g $g(grammar)
    unset     g(grammar)

    # Assemble the rules ...
    set rules {}
    foreach {symbol def} $g(rules) {
        lassign $def _ is _ mode
        lappend rules $symbol \
            [json::write object \
                 is   [json::write string $is] \
                 mode [json::write string $mode]]
    }

    # Assemble the final result ...
    return [json::write object grammar \
                [json::write object \
                     rules [json::write object {*}$rules] \
                     start [json::write string $g(start)]]]
}

# -------------------------------------------------------------------------
# Tests
# -------------------------------------------------------------------------

test json-write-1.0 {default configuration} -body {
    list [json::write indented] [json::write aligned]
} -result {1 1}

test json-write-1.1 {implied configurations} -body {
    json::write indented 0
    list [json::write indented] [json::write aligned]
} -result {0 0}

test json-write-1.2 {implied configurations} -body {
    json::write indented 0
    json::write aligned  0
    json::write aligned  1
    list [json::write indented] [json::write aligned]
} -result {1 1}

# -------------------------------------------------------------------------

test json-write-2.0 {argument errors} -body {
    json::write indented X Y
} -returnCodes 1 -result {wrong # args: should be "json::write indented ?bool?"}

test json-write-2.1 {argument errors} -body {
    json::write aligned X Y
} -returnCodes 1 -result {wrong # args: should be "json::write aligned ?bool?"}

test json-write-2.2 {argument errors} -body {
    json::write string
} -returnCodes 1 -result {wrong # args: should be "json::write string s"}

test json-write-2.3 {argument errors} -body {
    json::write string A B
} -returnCodes 1 -result {wrong # args: should be "json::write string s"}

test json-write-2.4 {argument errors} -body {
    json::write object A
} -returnCodes 1 -result {wrong # args, expected an even number of arguments}

# -------------------------------------------------------------------------

test json-write-3.0 {indented, aligned} -setup {
    json::write indented 1
    json::write aligned  1
} -body {
    gen $data
} -result {{
    "grammar" : {
        "rules" : {
            "A" : {
                "is"   : "/ {t +} {t -}",
                "mode" : "value"
            },
            "D" : {
                "is"   : "/ {t 0} {t 1} ",
                "mode" : "value"
            },
            "E" : {
                "is"   : "/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}",
                "mode" : "value"
            },
            "F" : {
                "is"   : "x {n T} {* {x {n A} {n T}}}",
                "mode" : "value"
            },
            "M" : {
                "is"   : "/ {t *} {t /}",
                "mode" : "value"
            },
            "N" : {
                "is"   : "x {? {n S}} {+ {n D}}",
                "mode" : "value"
            },
            "S" : {
                "is"   : "/ {t +} {t -}",
                "mode" : "value"
            },
            "T" : {
                "is"   : "n N",
                "mode" : "value"
            }
        },
        "start" : "n Expression"
    }
}}

test json-write-3.1 {indented, !aligned} -setup {
    json::write indented 1
    json::write aligned  0
} -body {
    gen $data
} -result {{
    "grammar" : {
        "rules" : {
            "A" : {
                "is" : "/ {t +} {t -}",
                "mode" : "value"
            },
            "D" : {
                "is" : "/ {t 0} {t 1} ",
                "mode" : "value"
            },
            "E" : {
                "is" : "/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}",
                "mode" : "value"
            },
            "F" : {
                "is" : "x {n T} {* {x {n A} {n T}}}",
                "mode" : "value"
            },
            "M" : {
                "is" : "/ {t *} {t /}",
                "mode" : "value"
            },
            "N" : {
                "is" : "x {? {n S}} {+ {n D}}",
                "mode" : "value"
            },
            "S" : {
                "is" : "/ {t +} {t -}",
                "mode" : "value"
            },
            "T" : {
                "is" : "n N",
                "mode" : "value"
            }
        },
        "start" : "n Expression"
    }
}}

test json-write-3.1 {!indented, !aligned} -setup {
    json::write indented 0
    json::write aligned  0
} -body {
    gen $data
} -result {{"grammar":{"rules":{"A":{"is":"/ {t +} {t -}","mode":"value"},"D":{"is":"/ {t 0} {t 1} ","mode":"value"},"E":{"is":"/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}","mode":"value"},"F":{"is":"x {n T} {* {x {n A} {n T}}}","mode":"value"},"M":{"is":"/ {t *} {t /}","mode":"value"},"N":{"is":"x {? {n S}} {+ {n D}}","mode":"value"},"S":{"is":"/ {t +} {t -}","mode":"value"},"T":{"is":"n N","mode":"value"}},"start":"n Expression"}}}


# -------------------------------------------------------------------------

test json-write-4.0 {string quoting} -body {
    json::write string "a\"b\\c\bd\fe\nf\rg\th\0i\1j\2k\3l\177m"
} -result "\"a\\\"b\\\\c\\bd\\fe\\nf\\rg\\th\\u0000i\\u0001j\\u0002k\\u0003l\\u007fm\""

# -------------------------------------------------------------------------
unset data
rename gen {}
testsuiteCleanup

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End: