# -*- tcl -*-
# docidx.testsuite:  tests for the docidx management.
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: container,v 1.2 2009/04/17 05:42:07 andreas_kupries Exp $

# -------------------------------------------------------------------------

set mytestdir tests/data

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# I. Handling regular serialization of indices, import and export.
#    Import serialization, then re-export.

TestFilesProcess $mytestdir ok serial serial-print -> n label input data expected {
    test doctools-idx-40.$n "doctools::idx deserialize serial, $label, ok" -setup {
	doctools::idx I
    } -body {
	I deserialize = $data
	I invalidate
	doctools::idx::structure print [I serialize]
    } -cleanup {
	I destroy
    } -result $expected
}

# Testing the errors thrown for invalid serializations, at the level
# of index objects. Underneath are the doctools::idx::structure
# commands, so we can and are re-using the test cases which were
# written for them.

foreach {n badserial expected} {
    0 {}
    {error in serialization: dictionary too short, expected exactly one key}
    1 {FOO {}}
    {error in serialization: bad type tag "FOO"}
    2 {doctools::idx {}}
    {error in serialization: dictionary too short, expected exactly four keys}
    3 {doctools::idx {a . b . c . d .}}
    {error in serialization: missing expected key "label"}
    4 {doctools::idx {label . b . c . d .}}
    {error in serialization: missing expected key "title"}
    5 {doctools::idx {label . title . c . d .}}
    {error in serialization: missing expected key "keywords"}
    6 {doctools::idx {label . title . keywords . d .}}
    {error in serialization: missing expected key "references"}
    8 {doctools::idx {label . keywords {k {} k {}} references {} title .}}
    {error in serialization: duplicate keywords}
    10 {doctools::idx {label . keywords {} references {a {url x}} title .}}
    {error in serialization: use and declaration of references not matching}
    11 {doctools::idx {label . keywords {a {b c}} references {} title .}}
    {error in serialization: use and declaration of references not matching}
    12 {doctools::idx {label . keywords {a {b}} references {b {url}} title .}}
    {error in serialization: reference list wrong, need exactly 2}
    13 {doctools::idx {label . keywords {a {b}} references {b {foo B}} title .}}
    {error in serialization: bad reference tag "foo"}
    14 {doctools::idx {label . keywords {a {b}} references {b {url B} b {url A}} title .}}
    {error in serialization: duplicate reference identifiers}
} {
    test doctools-idx-41.$n "doctools::idx deserialize, error" -setup {
	doctools::idx I
    } -body {
	I deserialize = $badserial
    } -cleanup {
	I destroy
    } -returnCodes error -result $expected
}

foreach {n noncanonserial expected} {
    7 {doctools::idx {label . title . keywords {} references {}}}
    {doctools::idx {label . keywords {} references {} title .}}
    9 {doctools::idx {label . keywords {b {} a {}} references {} title .}}
    {doctools::idx {label . keywords {a {} b {}} references {} title .}}
    15 {doctools::idx {label . keywords {a {b c}} references {c {url C} b {url B}} title .}}
    {doctools::idx {label . keywords {a {b c}} references {b {url B} c {url C}} title .}}
} {
    test doctools-idx-42.$n "doctools::idx deserialize, regular to canonical" -setup {
	doctools::idx I
	I deserialize = $noncanonserial
    } -body {
	I serialize
    } -cleanup {
	I destroy
    } -result $expected
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# II. Handling docidx markup as serialization format, import and export.

# Checking that the various forms of docidx markup as generated by the
# export plugin doctools::idx(::export::docidx) are valid input for
# the docidx import plugin. Actually testing that using an import
# manager from the index is working.

foreach {k section} {
    0 {}
    1 -ultracompact
    2 -compact
    3 -indented
    4 -aligned
    5 -indalign
} {
    TestFilesProcess $mytestdir ok docidx$section serial-print -> n label input data expected {
	test doctools-idx-50.$k.$n "doctools::idx deserialize = docidx, $label$section, ok" -setup {
	    doctools::idx I
	    doctools::idx::import IN
	    I importer IN
	} -body {
	    I deserialize = $data docidx
	    doctools::idx::structure print [I serialize]
	} -cleanup {
	    I destroy
	    IN destroy
	} -result $expected
    }
}

# We test the error messages and codes thrown during import for a
# variety of failure possibilities

TestFilesProcess $mytestdir fail docidx emsg -> n label input data expected {
    test doctools-idx-51.$n "doctools::idx deserialize = docidx, $label, error message" -setup {
	# Basic variables and include search paths for use by the tests
	doctools::idx::import IN
	IN config set fox dog
	IN config set lazy jump
	IN include add [TestFilesGlob $mytestdir]
	doctools::idx I
	I importer IN
    } -body {
	I deserialize = $data docidx
    } -cleanup {
	I destroy
	IN destroy
    } -returnCodes error -result $expected
}

TestFilesProcess $mytestdir fail docidx ecode -> n label input data expected {
    test doctools-idx-52.$n "doctools::idx deserialize = docidx, $label, error code" -setup {
	# Basic variables and include search paths for use by the tests
	doctools::idx::import IN
	IN config set fox dog
	IN config set lazy jump
	IN include add [TestFilesGlob $mytestdir]
	doctools::idx I
	I importer IN
    } -body {
	catch { I deserialize = $data docidx }
	set ::errorCode
    } -cleanup {
	I destroy
	IN destroy
    } -result $expected
}

# Testing the export of docidx markup through attached exporter management, for all possible configurations.

foreach {k nl in al section} {
    0 0 0 0 -ultracompact
    1 1 0 0 -compact
    2 1 1 0 -indented
    3 1 0 1 -aligned
    4 1 1 1 -indalign
    5 0 1 0 -indented
    6 0 0 1 -aligned
    7 0 1 1 -indalign
} {
    TestFilesProcess $mytestdir ok serial docidx$section -> n label input data expected {
	test doctools-idx-53.$k.$n "doctools::idx serialize docidx, ${label}$section, ok" -setup {
	    doctools::idx::export OUT
	    OUT config set newlines $nl
	    OUT config set indented $in
	    OUT config set aligned  $al
	    doctools::idx I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    stripcomments [I serialize docidx]
	} -cleanup {
	    I destroy
	    OUT destroy
	} -result $expected
    }
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# III. Handling text markup as serialization format, export only

TestFilesProcess $mytestdir ok serial text -> n label input data expected {
    test doctools-idx-54.$n "doctools::idx serialize text, $label, ok" -setup {
	doctools::idx::export OUT
	doctools::idx I
	I exporter OUT
    } -body {
	I deserialize = $data
	I serialize text
    } -cleanup {
	I destroy
	OUT destroy
    } -result $expected
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# IV. Handling json markup as serialization format, import and export.

# We are checking that the various forms of json markup, as can be
# generated by doctools::idx(::export(::json)) are valid input to the
# json parser.
#
# section {} holds the non-canonical input we have to accept and make
# canonical to higher layers.

foreach {k section} {
    0 {}
    1 -ultracompact
    2 -indented
    3 -indalign
} {
    TestFilesProcess $mytestdir ok json$section serial-print -> n label input data expected {
	test doctools-idx-55.$k.$n "doctools::idx deserialize = json, $label$section, ok" -setup {
	    doctools::idx::import IN
	    doctools::idx I
	    I importer IN
	} -body {
	    I deserialize = $data json
	    doctools::idx::structure print [I serialize]
	} -cleanup {
	    I destroy
	    IN destroy
	} -result $expected
    }
}

TestFilesProcess $mytestdir fail json json-emsg -> n label input data expected {
    test doctools-idx-56.$n "doctools::idx deserialize = json, $label, error message" -setup {
	doctools::idx::import IN
	doctools::idx I
	I importer IN
    } -body {
	I deserialize = $data json
    } -cleanup {
	I destroy
	IN destroy
    } -returnCodes error -result $expected
}

foreach {k in al section} {
    0 0 0 -ultracompact
    1 1 0 -indented
    2 0 1 -indalign
    3 1 1 -indalign
} {
    TestFilesProcess $mytestdir ok serial json$section -> n label input data expected {
	test doctools-idx-57.$k.$n "doctools::idx serialize json, $label$section, ok" -setup {
	    doctools::idx::export OUT
	    OUT config set indented $in
	    OUT config set aligned  $al
	    doctools::idx I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    I serialize json
	} -cleanup {
	    I destroy
	    OUT destroy
	} -result $expected
    }
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# V. Handling html markup as serialization format, export only

foreach {k nl in section} {
    0 0 0 -ultracompact
    1 0 1 -indented
    2 1 0 -compact
    3 1 1 -indented
} {
    TestFilesProcess $mytestdir ok serial html$section -> n label input data expected {
	test doctools-idx-58.$k.$n "doctools::idx serialize html, $label$section, ok" -setup {
	    doctools::idx::export OUT
	    OUT config set newlines $nl
	    OUT config set indented $in
	    OUT config set user _dummy_
	    doctools::idx I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    striphtmlcomments [I serialize html] 3
	} -cleanup {
	    I destroy
	    OUT destroy
	} -result $expected
    }
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# VI. Handling wiki markup as serialization format, export only

foreach {k style section} {
    0 list  -list
    1 table -table
} {
    TestFilesProcess $mytestdir ok serial wiki$section -> n label input data expected {
	test doctools-idx-59.$k.$n "doctools::idx serialize wiki, $label$section, ok" -setup {
	    doctools::idx::export OUT
	    OUT config set style $style
	    doctools::idx I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    I serialize wiki
	} -cleanup {
	    I destroy
	    OUT destroy
	} -result $expected
    }
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# VII. Handling nroff markup as serialization format, export only

foreach {k inline section} {
    0 0 -external
    1 1 -inlined
} {
    TestFilesProcess $mytestdir ok serial nroff$section -> n label input data expected {
	test doctools-idx-60.$k.$n "doctools::idx serialize nroff, $label$section, ok" -setup {
	    doctools::idx::export OUT
	    OUT config set inline $inline
	    doctools::idx I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    stripnroffcomments [stripmanmacros [I serialize nroff]]
	} -cleanup {
	    I destroy
	    OUT destroy
	} -result $expected
    }
}

# -------------------------------------------------------------------------
return

# TODO :: Test the merging of indices (copy from idx_structure.test)

# -------------------------------------------------------------------------
return
