Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:A bit trickery to only run tests with valid json data if testing asTypedList / createDocumentFromTypedList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | asTypedList
Files: files | file ages | folders
SHA3-256: 2493c54646e063d9d7358b85802e90573b0f736da6bea2d984c82bddd7118e9d
User & Date: rolf 2024-09-23 18:45:45
Context
2024-09-23
18:49
Fixed syntax error. check-in: ba093cc945 user: rolf tags: asTypedList
18:45
A bit trickery to only run tests with valid json data if testing asTypedList / createDocumentFromTypedList. check-in: 2493c54646 user: rolf tags: asTypedList
2024-09-22
00:15
Used the valid json data of the various tests in domjson.test for tests for asTypedList and createDocumentFromTypedList. check-in: 43f6e49157 user: rolf tags: asTypedList
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to tests/domjson.test.

18
19
20
21
22
23
24





25
26
27
28
29
30
31
32

source [file join [file dir [info script]] loadtdom.tcl]

testConstraint needExpand 1
if {$tcl_version < 8.5} {
    testConstraint needExpand 0
}






namespace eval nodeCmds {
    dom createNodeCmd elementNode e1
    dom createNodeCmd -jsonType ARRAY elementNode jae1
    dom createNodeCmd elementNode e2
    dom createNodeCmd commentNode c
    dom createNodeCmd textNode    t
    dom createNodeCmd -jsonType TRUE textNode true







>
>
>
>
>
|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

source [file join [file dir [info script]] loadtdom.tcl]

testConstraint needExpand 1
if {$tcl_version < 8.5} {
    testConstraint needExpand 0
}
# See below the comment in proc dom for explanation
if {[info commands _dom] eq ""} {
    testConstraint failtest 1
} else {
    testConstraint failtest 0
}
namespace eval nodeCmds {
    dom createNodeCmd elementNode e1
    dom createNodeCmd -jsonType ARRAY elementNode jae1
    dom createNodeCmd elementNode e2
    dom createNodeCmd commentNode c
    dom createNodeCmd textNode    t
    dom createNodeCmd -jsonType TRUE textNode true
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
test json-1.8 {Parse JSON - true, false, null} {
    set doc [dom parse -json -jsonroot "JSONObject" {{"a":true,"b":false,"c":null,"d":"true","e":""}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<JSONObject><a>true</a><b>false</b><c>null</c><d>true</d><e></e></JSONObject>}

test json-1.9 {JSON syntax error} {
    set result [catch {dom parse -json {{"a" "a value"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a" " <--Error-- a value"}"}}

test json-1.10 {JSON syntax error} {
    set result [catch {dom parse -json {{"a":00.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 6
"{"a":00 <--Error-- .23}"}}

test json-1.11 {JSON syntax error} {
    set result [catch {dom parse -json {{"a":-00.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 7
"{"a":-00 <--Error-- .23}"}}

test json-1.12 {JSON syntax error} {
    set result [catch {dom parse -json {{"a":.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a":. <--Error-- 23}"}}

test json-1.13 {JSON syntax error} {
    set result [catch {dom parse -json {{"a":-.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 6
"{"a":-. <--Error-- 23}"}}

test json-1.14 {JSON syntax error} {
    set result [catch {dom parse -json {{"a":-}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a":- <--Error-- }"}}

test json-1.15 {Parse JSON - nested object} {
    set doc [dom parse -json {["a",["aa","bb"],"b"]}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "a<arraycontainer>aabb</arraycontainer>b"

set notJsons {
    {{null}}
    {{1.23}}
    {{"string"}}
    {{"e":}}
}
test json-1.16 {Invalid input} {
    set result ""
    set ind 0
    foreach notJson $notJsons {
        if {![catch {dom parse -json $notJson docNode} errMsg]} {
            lappend result $errMsg
        }
    }
    set result
} ""

test json-1.17 {Literal binary 0 (NUL, '\0') is not allowed in input} {
    catch {dom parse -json "\"a\u0000\""}
} 1

test json-1.18 {Escaped binary 0 (NUL, '\0') is OK} {
    dom parse -json "\"a\\u0000\"" doc
    set result [$doc asJSON]
    $doc delete
    set result
} "\"a\\u0000\""

test json-1.19 {Invalid input - incomplete \u escape} {
    set result 1
    foreach jsonstr {
        "ab\u00"
        "ab\ua"
        "ab\u12"
        "ab\u123"
        "ab\u123g"







|





|





|





|





|





|


















|










|










|







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
test json-1.8 {Parse JSON - true, false, null} {
    set doc [dom parse -json -jsonroot "JSONObject" {{"a":true,"b":false,"c":null,"d":"true","e":""}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<JSONObject><a>true</a><b>false</b><c>null</c><d>true</d><e></e></JSONObject>}

test json-1.9 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a" "a value"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a" " <--Error-- a value"}"}}

test json-1.10 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a":00.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 6
"{"a":00 <--Error-- .23}"}}

test json-1.11 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a":-00.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 7
"{"a":-00 <--Error-- .23}"}}

test json-1.12 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a":.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a":. <--Error-- 23}"}}

test json-1.13 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a":-.23}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 6
"{"a":-. <--Error-- 23}"}}

test json-1.14 {JSON syntax error} {failtest} {
    set result [catch {dom parse -json {{"a":-}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 5
"{"a":- <--Error-- }"}}

test json-1.15 {Parse JSON - nested object} {
    set doc [dom parse -json {["a",["aa","bb"],"b"]}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "a<arraycontainer>aabb</arraycontainer>b"

set notJsons {
    {{null}}
    {{1.23}}
    {{"string"}}
    {{"e":}}
}
test json-1.16 {Invalid input} {failtest} {
    set result ""
    set ind 0
    foreach notJson $notJsons {
        if {![catch {dom parse -json $notJson docNode} errMsg]} {
            lappend result $errMsg
        }
    }
    set result
} ""

test json-1.17 {Literal binary 0 (NUL, '\0') is not allowed in input} {failtest} {
    catch {dom parse -json "\"a\u0000\""}
} 1

test json-1.18 {Escaped binary 0 (NUL, '\0') is OK} {
    dom parse -json "\"a\\u0000\"" doc
    set result [$doc asJSON]
    $doc delete
    set result
} "\"a\\u0000\""

test json-1.19 {Invalid input - incomplete \u escape} {failtest} {
    set result 1
    foreach jsonstr {
        "ab\u00"
        "ab\ua"
        "ab\u12"
        "ab\u123"
        "ab\u123g"
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
test json-3.4.1 {unescaping} {
    set doc [dom parse -jsonroot json -json {["\\a","\u0071"]}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<json>\aq</json>}

test json-3.5 {unescaping} {
    set result [catch {dom parse -json {{"this":"a\lb"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 11
"{"this":"a\l <--Error-- b"}"}}

test json-3.6 {unescaping} {
    set doc [dom parse -json {{"this":"a\nbc"}}]







|







264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
test json-3.4.1 {unescaping} {
    set doc [dom parse -jsonroot json -json {["\\a","\u0071"]}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<json>\aq</json>}

test json-3.5 {unescaping} {failtest} {
    set result [catch {dom parse -json {{"this":"a\lb"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON syntax error" at position 11
"{"this":"a\l <--Error-- b"}"}}

test json-3.6 {unescaping} {
    set doc [dom parse -json {{"this":"a\nbc"}}]
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
    }
    set result
} {1 1 1 1 1}

if {[info commands _dom] eq ""} {
    rename dom _dom
    proc dom {args} {
        if {[lindex $args 0] != "parse" || [lsearch $args "-json"] < 0} {
            return [uplevel 1 [linsert $args 0 _dom]]
        }
        set haveData 0
        set uplevelVar ""
        for {set x 1} {$x < [llength $args]} {incr x} {
            switch -- [lindex $args $x] {
                "-jsonroot" -







|







3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
    }
    set result
} {1 1 1 1 1}

if {[info commands _dom] eq ""} {
    rename dom _dom
    proc dom {args} {
        if {[lindex $args 0] != "parse" || [lsearch -exact $args "-json"] < 0} {
            return [uplevel 1 [linsert $args 0 _dom]]
        }
        set haveData 0
        set uplevelVar ""
        for {set x 1} {$x < [llength $args]} {incr x} {
            switch -- [lindex $args $x] {
                "-jsonroot" -
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119

3120
3121



3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
                        set data [lindex $args $x]
                        set haveData 1
                    }
                }                
            }
        }
        if {[catch {
            _dom parse -json $data $doc
        } errMsg]} {
            # Some tests check if invalid json data is detected. Since
            # we need valid json data for our test there is nothing we

            # can do here.
            return [uplevel 1 [linsert $args 0 _dom]]



        }
        set njson [$doc asJSON]
        set typedList [$doc asTypedList]
        _dom createDocumentFromTypedList $typedList docFromTyped
        if {$njson ne [$docFromTyped asJSON]} {
            error "Normalized json '$data' differs from normalized json created with createDocumentFromTypedList"
        }
        return [uplevel 1 [linsert $args 0 _dom]]
    }
    source [file join [file dir [info script]] domjson.test]
    rename dom {}
    rename _dom dom
}







|


|
>
|
<
>
>
>













3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126

3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
                        set data [lindex $args $x]
                        set haveData 1
                    }
                }                
            }
        }
        if {[catch {
            _dom parse -json $data doc
        } errMsg]} {
            # Some tests check if invalid json data is detected. Since
            # we need valid json data for what is tested here that
            # tests should be marked with constraint failtest. To
            # raise error here does not help much because if the test

            # data is expected to be invalid the command will be
            # [catch]ed. Therefore the puts to catch attention.
            puts  "Unexpeced invalid json data '$data'"
        }
        set njson [$doc asJSON]
        set typedList [$doc asTypedList]
        _dom createDocumentFromTypedList $typedList docFromTyped
        if {$njson ne [$docFromTyped asJSON]} {
            error "Normalized json '$data' differs from normalized json created with createDocumentFromTypedList"
        }
        return [uplevel 1 [linsert $args 0 _dom]]
    }
    source [file join [file dir [info script]] domjson.test]
    rename dom {}
    rename _dom dom
}