tDOM

Artifact [1061d03659]
Login

Artifact [1061d03659]

Artifact 1061d0365930fe9b64b3ed46d8692349dd575914:


# Features covered: JSON parser
#
# This file contains a collection of tests for the JSON parser.
# Tested functionalities:
#    json-1.*: JSON syntax tests
#    json-2.*: Valid JSON, that could not parsed into DOM
#    json-3.*: JSON unescaping
#    json-4.*: Unicode
#    json-5.*: Max nesting
# Copyright (c) 2017 Rolf Ade.

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

test json-1.1 {Parse JSON} {
    set doc [dom parse -json {{"a":"avalue","b":"bvalue","c":0.123}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a>avalue</a><b>bvalue</b><c>0.123</c>"

test json-1.2 {Parse JSON} {
    set doc [dom parse -json { {"a" : [ "avalue" ] } }]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<a xmlns:json="http://tdom.org/json" json:typehint="array">avalue</a>}

test json-1.3 {Parse JSON} {
    set doc [dom parse -json {{"a":"a value","b":"1value"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a>a value</a><b>1value</b>"

test json-1.4 {Parse JSON - nested object} {
    set doc [dom parse -json {{"a":{"aa":"aavalue","bb":"bbvalue"},"b":"bvalue","c":0.123}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a><aa>aavalue</aa><bb>bbvalue</bb></a><b>bvalue</b><c>0.123</c>"

test json-1.5 {Parse JSON - array} {
    set doc [dom parse -json {{"a": [1,2,3,4,"abc"]}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a>1</a><a>2</a><a>3</a><a>4</a><a>abc</a>"

test json-1.6 {Parse JSON - true, false, null} {
    set doc [dom parse -json {{"a":true,"b":false,"c":null,"d":"true","e":""}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<a xmlns:json="http://tdom.org/json" json:typehint="boolean">true</a><b xmlns:json="http://tdom.org/json" json:typehint="boolean">false</b><c xmlns:json="http://tdom.org/json" json:typehint="null"/><d>true</d><e></e>}

test json-1.7 {Parse JSON - array in nested object} {
    set doc [dom parse -json {{"a":{"aa":[1,2,3,4,"abc"]},"b":"bvalue"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a><aa>1</aa><aa>2</aa><aa>3</aa><aa>4</aa><aa>abc</aa></a><b>bvalue</b>"

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 xmlns:json="http://tdom.org/json"><a json:typehint="boolean">true</a><b json:typehint="boolean">false</b><c json:typehint="null"/><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
} "<item>a</item><item><item>aa</item><item>bb</item></item><item>b</item>"

test json-2.1 {invalid xml name} {
    set result [catch {dom parse -json {{"a":"a value","1b":"1value"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON object name that cannot be an XML element name" at position 16
"{"a":"a value","1 <--Error-- b":"1value"}"}}

test json-2.2 {invalid xml name} {
    set result [catch {set doc [dom parse -json {{"a b":"a value","1b":"1value"}}]} errMsg]
    list $result $errMsg
} {1 {error "JSON object name that cannot be an XML element name" at position 3
"{"a  <--Error-- b":"a value","1b":"1value"}"}}

test json-3.1 {Unescaping} {
    set doc [dom parse -json {{"a":"a\nvalue","b":"value\tvalue"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<a>a
value</a><b>value\tvalue</b>"

test json-3.2 {Unescaping} {
    set doc [dom parse -json {{"a":"a\nvalue", "b":"12\u0077\u2221ec"}}]
    set result [$doc asXML -indent none -escapeNonASCII]
    $doc delete
    set result
} "<a>a
value</a><b>12w&#8737;ec</b>"

test json-3.3 {Unescaping} {
    set doc [dom parse -json {{"a":"\u0077\u2221"}}]
    set result [$doc asXML -indent none -escapeNonASCII]
    $doc delete
    set result
} "<a>w&#8737;</a>"

test json-3.4 {unescaping} {
    set doc [dom parse -jsonroot json -json {["\\a"]}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<json xmlns:json="http://tdom.org/json"><item json:typehint="array">\a</item></json>}

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><item>\a</item><item>q</item></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"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<this>a
bc</this>} 

test json-3.7 {unescaping} {
    set doc [dom parse -json {{"this":"a\u0077\t\u0078bc"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<this>aw\txbc</this>"

test json-3.8 {unescaping} {
    set result [catch {dom parse -json {{"this":"a\u0000b"}}} errMsg]
    list $result $errMsg
} {1 {error "JSON input includes characters not possible in an XML document." at position 15
"{"this":"a\u0000 <--Error-- b"}"}}

test json-3.9 {unescaping} {
    set doc [dom parse -json {{"this":"a\u0077","that":"\t\u0078bc"}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} "<this>aw</this><that>\txbc</that>"

test json-5.1 {-jsonmaxnesting 0} {
    set result [catch {dom parse -json -jsonmaxnesting 0 {{"this":"that"}}} errMsg]
    list $result $errMsg
} {1 {error "Maximum JSON object/array nesting depth exceeded" at position 0
"{ <--Error-- "this":"that"}"}}

test json-5.2 {-jsonmaxnesting 0} {
    set result [catch {dom parse -json -jsonmaxnesting 0 {["this","that"]}} errMsg]
    list $result $errMsg
} {1 {error "Maximum JSON object/array nesting depth exceeded" at position 0
"[ <--Error-- "this","that"]"}}

test json-5.3 {-jsonmaxnesting 0} {
    set result [catch {dom parse -jsonroot o -json -jsonmaxnesting 0 {["this","that"]}} errMsg]
    list $result $errMsg
} {1 {error "Maximum JSON object/array nesting depth exceeded" at position 0
"[ <--Error-- "this","that"]"}}

test json-5.4 {-jsonmaxnesting} {
    set doc [dom parse -json -jsonmaxnesting 2 {{"this":{"foo":"that"}}}]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<this><foo>that</foo></this>}

test json-5.5 {-jsonmaxnesting} {
    set result [catch {dom parse -json -jsonmaxnesting 1 \
                           {{"this":{"foo":"that"}}}} errMsg]
    list $result $errMsg
} {1 {error "Maximum JSON object/array nesting depth exceeded" at position 8
"{"this":{ <--Error-- "foo":"that"}}"}}

test json-5.6 {-jsonmaxnesting} {
    set doc [dom parse -json -jsonmaxnesting 2 {
        {
            "this": {
                "foo":"that",
                "bar":"grill"
            },
            "two": "value",
            "three": {
                "t1":"t1value",
                "t2":"t2value"
            },
            "four": ["a","b","c"]
        }}]
    set result [$doc asXML]
    $doc delete
    set result
} {<this>
    <foo>that</foo>
    <bar>grill</bar>
</this>
<two>value</two>
<three>
    <t1>t1value</t1>
    <t2>t2value</t2>
</three>
<four>a</four>
<four>b</four>
<four>c</four>
}