if {"::tcltest" ni [namespace children]} {
	package require tcltest
	namespace import ::tcltest::*
}

package require rl_json
package require parse_args
namespace path {::rl_json ::parse_args}

test pretty-1.1 {Basic pretty-print} -body { #<<<
	json pretty {{"foo":null,"empty":{},"emptyarr":[],"hello, world":"bar","This is a much longer key":["str",123,123.4,true,false,null,{"inner": "obj"}]}}
} -cleanup {
	unset -nocomplain o
} -result {{
    "foo":          null,
    "empty":        {},
    "emptyarr":     [],
    "hello, world": "bar",
    "This is a much longer key": [
        "str",
        123,
        123.4,
        true,
        false,
        null,
        {
            "inner": "obj"
        }
    ]
}}
#>>>
test pretty-1.2 {Basic pretty-print, different indent} -body { #<<<
	json pretty -indent "  " {{"foo":null,"empty":{},"emptyarr":[],"hello, world":"bar","This is a much longer key":["str",123,123.4,true,false,null,{"inner": "obj"}]}}
} -result {{
  "foo":          null,
  "empty":        {},
  "emptyarr":     [],
  "hello, world": "bar",
  "This is a much longer key": [
    "str",
    123,
    123.4,
    true,
    false,
    null,
    {
      "inner": "obj"
    }
  ]
}}
#>>>
test pretty-2.1 {too few args} -body { #<<<
	json pretty
} -returnCodes error -result {wrong # args: should be "pretty pretty ?-indent indent? json_val ?key ...?"}
#>>>
test pretty-3.1 {path} -body { #<<<
	json pretty {{"foo":null,"empty":{},"emptyarr":[],"hello, world":"bar","This is a much longer key":["str",123,123.4,true,false,null,{"inner": "obj"}]}} {This is a much longer key}
} -result {[
    "str",
    123,
    123.4,
    true,
    false,
    null,
    {
        "inner": "obj"
    }
]}
#>>>
test pretty-3.2 {bad path} -body { #<<<
	json pretty {{"foo":null,"empty":{},"emptyarr":[],"hello, world":"bar","This is a much longer key":["str",123,123.4,true,false,null,{"inner": "obj"}]}} bad
} -returnCodes error -result {Path element 2: "bad" not found} -errorCode NONE
#>>>

test debug-1.1 {Basic debug pretty-print} -body { #<<<
	json debug {{"foo":null,"empty":{},"emptyarr":[],"hello, world":"bar","This is a much longer key":["str",123,123.4,true,false,null,{"inner": "obj"}]}}
} -match regexp -result {^\(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\){
    "foo":          \(0x[0-9a-fA-F]+\[[0-9]+\]+/NULL\)null,
    "empty":        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\){},
    "emptyarr":     \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)\[\],
    "hello, world": \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)"bar",
    "This is a much longer key": \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)\[
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)"str",
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)123,
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)123.4,
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)true,
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)false,
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/NULL\)null,
        \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\){
            "inner": \(0x[0-9a-fA-F]+\[[0-9]+\]+/0x[0-9a-fA-F]+\[[0-9]+\]+ [a-z ]+\)"obj"
        }
    \]
}$}
#>>>

# Coverage golf
test pretty-jsonPretty-1.1 {} -body {json pretty -indent} -returnCodes error -result {missing argument to "-indent"} -errorCode {TCL ARGUMENT MISSING}
test pretty-jsonPretty-2.1 {} -body {json pretty -- 1} -result 1
test pretty-jsonPretty-3.1 {} -body {json pretty -indent {  }} -returnCodes error -result {wrong # args: should be "pretty ?-default defaultValue? json_val ?key ...?"} -errorCode {TCL WRONGARGS}
test pretty-jsonPretty-4.1 {} -body {json pretty bad} -returnCodes error -result {Error parsing JSON value: Illegal character at offset 0} -errorCode {RL JSON PARSE {Illegal character} bad 0}
test pretty-jsonPretty-5.1 {} -body {json pretty -bad} -returnCodes error -result {bad option "-bad": must be *} -errorCode {TCL LOOKUP INDEX option -bad} -match glob

json free_cache

::tcltest::cleanupTests
return

# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
