1# -*- tcl -*-
2# idx_import_json.test:  tests for the doctools::idx::import::json package/plugin.
3#
4# Copyright (c) 2009 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
5# All rights reserved.
6#
7# RCS: @(#) $Id: import_json.test,v 1.1 2009/04/01 04:28:37 andreas_kupries Exp $
8
9# -------------------------------------------------------------------------
10
11source [file join \
12	[file dirname [file dirname [file join [pwd] [info script]]]] \
13	devtools testutilities.tcl]
14
15testsNeedTcl     8.4
16testsNeedTcltest 2.0
17
18support {
19    use fileutil/fileutil.tcl  fileutil
20    use struct/list.tcl        struct::list
21
22    # Copy of code from idx_import_json.tcl, to define dict support
23    # even where dict is not really present on the system.
24
25    if {[package vcompare [package present Tcl] 8.5] < 0} {
26	if {[catch {
27	    package require dict
28	}]} {
29	    # Create a pure Tcl implementation of the dict methods
30	    # required by json, and fake the presence of the dict package.
31	    proc dict {cmd args} { return [uplevel 1 [linsert $args 0 dict/$cmd]] }
32	    proc dict/create {} { return {} }
33	    proc dict/set {var key val} {
34		upvar 1 $var a
35		array set x $a
36		set x($key) $val
37		set a [array get x]
38		return
39	    }
40	    package provide dict 1
41	}
42    }
43    use json/json.tcl          json
44
45    useLocal structure.tcl doctools::idx::structure
46
47    #msgcat::mclocale C
48}
49testing {
50    package provide doctools::idx::import::plugin 1
51    # The above fakes plugin environment. Well, not completely. By
52    # leaving out a definition for the 'include' alias the plugin is
53    # signaled that there is no need to overwrite the GetFile command
54    # of doctools::idx::parse with a version calling out to the plugin
55    # manager, i.e. that it can still use the regular file operations.
56
57    useLocal import_json.tcl doctools::idx::import::json
58}
59
60source [tcllibPath doctools2base/tests/common]
61set mytestdir tests/data
62
63# -------------------------------------------------------------------------
64
65# General set of error cases regarding the number of arguments.
66
67test doctools-idx-import-json-1.0 {import, wrong#args} -body {
68    import
69} -returnCodes error -result {wrong # args: should be "import text configuration"}
70
71test doctools-idx-import-json-1.1 {import, wrong#args} -body {
72    import T
73} -returnCodes error -result {wrong # args: should be "import text configuration"}
74
75test doctools-idx-import-json-1.2 {import, wrong#args} -body {
76    import T C XXX
77} -returnCodes error -result {wrong # args: should be "import text configuration"}
78
79# idx_import_json tests, numbering starts at 2
80# -------------------------------------------------------------------------
81
82# We are checking that the various forms of json markup, as can be
83# generated by doctools::idx(::export(::json)) are valid input to the
84# json parser.
85#
86# section {} holds the non-canonical input we have to accept and make
87# canonical to higher layers.
88
89foreach {k section} {
90    0 {}
91    1 -ultracompact
92    2 -indented
93    3 -indalign
94} {
95    TestFilesProcess $mytestdir ok json$section serial-print -> n label input data expected {
96	test doctools-idx-import-json-2.$k.$n "doctools::idx::import::json, $label$section, ok" -body {
97	    doctools::idx::structure print [import $data {}]
98	} -result $expected
99    }
100}
101
102# -------------------------------------------------------------------------
103
104# We test the error messages and codes thrown by the parser for a
105# variety of failure possibilities.
106
107TestFilesProcess $mytestdir fail json json-emsg -> n label input data expected {
108    test doctools-idx-import-json-3.$n "doctools::idx::import::json, $label, error message" -body {
109	import $data {}
110    } -returnCodes error -result $expected
111}
112
113#----------------------------------------------------------------------
114testsuiteCleanup
115return
116