working hostname call
[scpubgit/TenDotTcl.git] / json / json_write.test
CommitLineData
458402ad 1# json_write.test - Copyright (C) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
2#
3# Tests for the Tcllib json::write package
4#
5# -------------------------------------------------------------------------
6# See the file "license.terms" for information on usage and redistribution
7# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8# -------------------------------------------------------------------------
9# RCS: @(#) $Id: json_write.test,v 1.1 2009/11/25 04:41:01 andreas_kupries Exp $
10
11# -------------------------------------------------------------------------
12
13source [file join \
14 [file dirname [file dirname [file join [pwd] [info script]]]] \
15 devtools testutilities.tcl]
16
17testsNeedTcl 8.5
18testsNeedTcltest 2.0
19
20testing {
21 useLocal json_write.tcl json::write
22}
23
24# -------------------------------------------------------------------------
25
26set data {grammar {
27 rules {
28 A {is {/ {t +} {t -}} mode value}
29 D {is {/ {t 0} {t 1} } mode value}
30 E {is {/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}} mode value}
31 F {is {x {n T} {* {x {n A} {n T}}}} mode value}
32 M {is {/ {t *} {t /}} mode value}
33 N {is {x {? {n S}} {+ {n D}}} mode value}
34 S {is {/ {t +} {t -}} mode value}
35 T {is {n N} mode value}
36 }
37 start {n Expression}
38}}
39
40proc gen {serial} {
41 array set g $serial
42 array set g $g(grammar)
43 unset g(grammar)
44
45 # Assemble the rules ...
46 set rules {}
47 foreach {symbol def} $g(rules) {
48 lassign $def _ is _ mode
49 lappend rules $symbol \
50 [json::write object \
51 is [json::write string $is] \
52 mode [json::write string $mode]]
53 }
54
55 # Assemble the final result ...
56 return [json::write object grammar \
57 [json::write object \
58 rules [json::write object {*}$rules] \
59 start [json::write string $g(start)]]]
60}
61
62# -------------------------------------------------------------------------
63# Tests
64# -------------------------------------------------------------------------
65
66test json-write-1.0 {default configuration} -body {
67 list [json::write indented] [json::write aligned]
68} -result {1 1}
69
70test json-write-1.1 {implied configurations} -body {
71 json::write indented 0
72 list [json::write indented] [json::write aligned]
73} -result {0 0}
74
75test json-write-1.2 {implied configurations} -body {
76 json::write indented 0
77 json::write aligned 0
78 json::write aligned 1
79 list [json::write indented] [json::write aligned]
80} -result {1 1}
81
82# -------------------------------------------------------------------------
83
84test json-write-2.0 {argument errors} -body {
85 json::write indented X Y
86} -returnCodes 1 -result {wrong # args: should be "json::write indented ?bool?"}
87
88test json-write-2.1 {argument errors} -body {
89 json::write aligned X Y
90} -returnCodes 1 -result {wrong # args: should be "json::write aligned ?bool?"}
91
92test json-write-2.2 {argument errors} -body {
93 json::write string
94} -returnCodes 1 -result {wrong # args: should be "json::write string s"}
95
96test json-write-2.3 {argument errors} -body {
97 json::write string A B
98} -returnCodes 1 -result {wrong # args: should be "json::write string s"}
99
100test json-write-2.4 {argument errors} -body {
101 json::write object A
102} -returnCodes 1 -result {wrong # args, expected an even number of arguments}
103
104# -------------------------------------------------------------------------
105
106test json-write-3.0 {indented, aligned} -body {
107 json::write indented 1
108 json::write aligned 1
109 gen $data
110} -result {{
111 "grammar" : {
112 "rules" : {
113 "A" : {
114 "is" : "\/ {t +} {t -}",
115 "mode" : "value"
116 },
117 "D" : {
118 "is" : "\/ {t 0} {t 1} ",
119 "mode" : "value"
120 },
121 "E" : {
122 "is" : "\/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}",
123 "mode" : "value"
124 },
125 "F" : {
126 "is" : "x {n T} {* {x {n A} {n T}}}",
127 "mode" : "value"
128 },
129 "M" : {
130 "is" : "\/ {t *} {t \/}",
131 "mode" : "value"
132 },
133 "N" : {
134 "is" : "x {? {n S}} {+ {n D}}",
135 "mode" : "value"
136 },
137 "S" : {
138 "is" : "\/ {t +} {t -}",
139 "mode" : "value"
140 },
141 "T" : {
142 "is" : "n N",
143 "mode" : "value"
144 }
145 },
146 "start" : "n Expression"
147 }
148}}
149
150test json-write-3.1 {indented, !aligned} -body {
151 json::write indented 1
152 json::write aligned 0
153 gen $data
154} -result {{
155 "grammar" : {
156 "rules" : {
157 "A" : {
158 "is" : "\/ {t +} {t -}",
159 "mode" : "value"
160 },
161 "D" : {
162 "is" : "\/ {t 0} {t 1} ",
163 "mode" : "value"
164 },
165 "E" : {
166 "is" : "\/ {x {t (} {n E} {t )}} {x {n F} {* {x {n M} {n F}}}}",
167 "mode" : "value"
168 },
169 "F" : {
170 "is" : "x {n T} {* {x {n A} {n T}}}",
171 "mode" : "value"
172 },
173 "M" : {
174 "is" : "\/ {t *} {t \/}",
175 "mode" : "value"
176 },
177 "N" : {
178 "is" : "x {? {n S}} {+ {n D}}",
179 "mode" : "value"
180 },
181 "S" : {
182 "is" : "\/ {t +} {t -}",
183 "mode" : "value"
184 },
185 "T" : {
186 "is" : "n N",
187 "mode" : "value"
188 }
189 },
190 "start" : "n Expression"
191 }
192}}
193
194test json-write-3.1 {!indented, !aligned} -body {
195 json::write indented 0
196 json::write aligned 0
197 gen $data
198} -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"}}}
199
200# -------------------------------------------------------------------------
201unset data
202rename gen {}
203testsuiteCleanup
204
205# Local Variables:
206# mode: tcl
207# indent-tabs-mode: nil
208# End: