1 # json.test - Copyright (C) 2006 ActiveState Software Inc.
3 # Tests for the Tcllib json package
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.test,v 1.8 2011/11/10 21:05:58 andreas_kupries Exp $
11 # -------------------------------------------------------------------------
14 [file dirname [file dirname [file join [pwd] [info script]]]] \
15 devtools testutilities.tcl]
17 testsNeedTcl [expr {[catch {package require dict}] ? "8.5" : "8.4"}]
21 useLocal json.tcl json
26 catch {unset DICTSORT}
28 proc dictsort3 {spec data} {
29 while [llength $spec] {
30 set type [lindex $spec 0]
31 set spec [lrange $spec 1 end]
38 foreach {key} [lsort [dict keys $data]] {
39 set val [dict get $data $key]
40 foreach {keymatch valtype} $spec {
41 if {[string match $keymatch $key]} {
42 lappend json $key [dictsort3 $valtype $val]
54 foreach {keymatch valtype} $spec {
55 if {$idx == $keymatch || $keymatch eq "*"} {
56 lappend json [dictsort3 $valtype $val]
67 default {error "Invalid type"}
77 "Longitude": -122.3959,
79 "City": "SAN FRANCISCO",
86 "Latitude": 37.371991,
87 "Longitude": -122.026020,
95 set TCL(array) {{precision zip Latitude 37.7668 Longitude -122.3959 Address {} City {SAN FRANCISCO} State CA Zip 94107 Country US} {precision zip Latitude 37.371991 Longitude -122.026020 Address {} City SUNNYVALE State CA Zip 94085 Country US}}
97 set DICTSORT(array) {list dict}
101 "title": "example glossary",
102 "mixlist": ["a \"\" str", -0.09, null, "", {"member":true}],
107 "GlossTerm": "Standard \\\" Language",
109 "Abbrev": "ISO 8879:1986",
111 "A meta-markup language, used ...",
112 "GlossSeeAlso": ["GML", "XML", "markup"]}]}}
114 set TCL(glossary) {glossary {title {example glossary} mixlist {{a "" str} -0.09 null {} {member true}} GlossDiv {title S GlossList {{ID SGML GlossTerm {Standard \" Language} Acronym SGML\\ Abbrev {ISO 8879:1986} GlossDef {A meta-markup language, used ...} GlossSeeAlso {GML XML markup}}}}}}
115 set DICTSORT(glossary) {dict * {dict GlossDiv {dict GlossList {list dict}}}}
117 set JSON(menu) {{"menu": {
123 {"value": "Open", "onclick": "OpenDoc()"},
124 {"value": "Close", "onclick": "CloseDoc()"}
129 set TCL(menu) [list menu [list id file value File: unival \u6021: popup {menuitem {{value Open onclick OpenDoc()} {value Close onclick CloseDoc()}}}]]
130 set DICTSORT(menu) {dict * {dict popup {dict * {list dict}}}}
132 set JSON(widget) {{"widget": {
135 "title":"Sample Widget",
136 "name": "main_window",
140 "data": "Click Here",
146 "alignment": "center",
147 "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
151 set TCL(widget) {widget {debug on window {title {Sample Widget} name main_window width 500 height 500} text {data {Click Here} size 36 style bold name null hOffset 250 vOffset 100 alignment center onMouseUp {sun1.opacity = (sun1.opacity / 100) * 90;}}}}
152 set DICTSORT(widget) {dict * {dict text dict window dict}}
154 set JSON(menu2) {{"menu": {
158 {"id": "OpenNew", "label": "Open New"},
160 {"id": "ZoomIn", "label": "Zoom In"},
161 {"id": "ZoomOut", "label": "Zoom Out"},
164 {"id": "About", "label": "About Viewer..."}
168 set TCL(menu2) {menu {header Viewer items {{id Open} {id OpenNew label {Open New}} null {id ZoomIn label {Zoom In}} {id ZoomOut label {Zoom Out}} null {id Help} {id About label {About Viewer...}}}}}
169 set DICTSORT(menu2) {dict * {dict items {list 0 dict 1 dict 3 dict 4 dict 6 dict 7 dict}}}
171 set JSON(emptyList) {[]}
172 set TCL(emptyList) {}
174 set JSON(emptyList2) {{"menu": []}}
175 set TCL(emptyList2) {menu {}}
177 set JSON(emptyList3) {["menu", []]}
178 set TCL(emptyList3) {menu {}}
180 set JSON(emptyList4) {[[]]}
181 set TCL(emptyList4) {{}}
183 # -------------------------------------------------------------------------
185 # -------------------------------------------------------------------------
188 foreach name [array names JSON] {
189 test json-1.[incr i] "test JSON $name" -body {
190 set res [json::json2dict $JSON($name)]
191 if {[info exists DICTSORT($name)]} {
192 return [string equal [dictsort3 $DICTSORT($name) $res] [dictsort3 $DICTSORT($name) $TCL($name)]]
194 return [string equal $res $TCL($name)]
199 # -------------------------------------------------------------------------
201 # -------------------------------------------------------------------------
203 test json-2.0 {list2json} -body {
204 json::list2json {{"a"} {"b"} {"c"}}
205 } -result {["a","b","c"]}
207 test json-2.1 {string2json} -body {
211 # -------------------------------------------------------------------------
214 catch {unset DICTSORT}
219 # indent-tabs-mode: nil