# -*- tcl -*-
# doctoc.testsuite:  tests for the doctoc management.
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: container,v 1.2 2009/11/15 05:50:03 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-toc-${impl}-40.$n "doctools::toc deserialize serial, $label, ok" -setup {
	doctools::toc I
    } -body {
	I deserialize = $data
	I invalidate
	doctools::toc::structure print [I serialize]
    } -cleanup {
	I destroy
    } -result $expected
}

# Testing the errors thrown for invalid serializations, at the level
# of toc objects. Underneath are the doctools::toc::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::toc {}}
    {error in serialization: dictionary too short, expected exactly three keys}
    3 {doctools::toc {a . b . c .}}
    {error in serialization: missing expected key "label"}
    4 {doctools::toc {label . b . c .}}
    {error in serialization: missing expected key "title"}
    5 {doctools::toc {label . title . c .}}
    {error in serialization: missing expected key "items"}
    7 {doctools::toc {items {{}} label . title .}}
    {error in serialization: element list wrong, need exactly 2}
    8 {doctools::toc {items {{FOO {}}} label . title .}}
    {error in serialization: bad element tag "FOO"}
    9 {doctools::toc {items {{reference {}}} label . title .}}
    {error in serialization: dictionary too short, expected exactly three keys}
    10 {doctools::toc {items {
	{reference {a . b . c .}}} label . title .}}
    {error in serialization: missing expected key "id"}
    11 {doctools::toc {items {
	{reference {id . b . c .}}} label . title .}}
    {error in serialization: missing expected key "label"}
    12 {doctools::toc {items {
	{reference {id . label . c .}}} label . title .}}
    {error in serialization: missing expected key "desc"}
    14 {doctools::toc {items {
	{division {}}} label . title .}}
    {error in serialization: dictionary too short, expected two or three keys}
    15 {doctools::toc {items {
	{division {a . b .}}} label . title .}}
    {error in serialization: missing expected key "label"}
    16 {doctools::toc {items {
	{division {label . b .}}} label . title .}}
    {error in serialization: missing expected key "items"}
    19 {doctools::toc {items {
	{reference {desc . id . label .}}
	{reference {desc . id . label .}}} label . title .}}
    {error in serialization: duplicate labels}
    20 {doctools::toc {items {
	{division {id . items {} label .}}
	{division {id . items {} label .}}} label . title .}}
    {error in serialization: duplicate labels}
    21 {doctools::toc {items {
	{division {id . items {} label .}}
        {reference {desc . id . label .}}} label . title .}}
    {error in serialization: duplicate labels}
} {
    test doctools-toc-${impl}-41.$n "doctools::toc deserialize, error" -setup {
	doctools::toc I
    } -body {
	I deserialize = $badserial
    } -cleanup {
	I destroy
    } -returnCodes error -result $expected
}

foreach {n noncanonserial expected} {
    6 {doctools::toc {label . title . items {}}}
    {doctools::toc {items {} label . title .}}
    13 {doctools::toc {items {
	{reference {id . label . desc .}}} label . title .}}
    {doctools::toc {items {{reference {desc . id . label .}}} label . title .}}
    17 {doctools::toc {items {
	{division {label . items {}}}} label . title .}}
    {doctools::toc {items {{division {items {} label .}}} label . title .}}
    18 {doctools::toc {items {
	{division {items {} label . id .}}} label . title .}}
    {doctools::toc {items {{division {id . items {} label .}}} label . title .}}
} {
    test doctools-toc-${impl}-42.$n "doctools::toc deserialize, regular to canonical" -setup {
	doctools::toc I
	I deserialize = $noncanonserial
    } -body {
	I serialize
    } -cleanup {
	I destroy
    } -result $expected
}

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

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

foreach {k section} {
    0 {}
    1 -ultracompact
    2 -compact
    3 -indented
    4 -aligned
    5 -indalign
} {
    TestFilesProcess $mytestdir ok doctoc$section serial-print -> n label input data expected {
	test doctools-toc-${impl}-50.$k.$n "doctools::toc deserialize = doctoc, $label$section, ok" -setup {
	    doctools::toc I
	    doctools::toc::import IN
	    I importer IN
	} -body {
	    I deserialize = $data doctoc
	    doctools::toc::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 doctoc emsg -> n label input data expected {
    test doctools-toc-${impl}-51.$n "doctools::toc deserialize = doctoc, $label, error message" -setup {
	# Basic variables and include search paths for use by the tests
	doctools::toc::import IN
	IN config set fox dog
	IN config set lazy jump
	IN include add [TestFilesGlob $mytestdir]
	doctools::toc I
	I importer IN
    } -body {
	I deserialize = $data doctoc
    } -cleanup {
	I destroy
	IN destroy
    } -returnCodes error -result $expected
}

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

# Testing the export of doctoc 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 doctoc$section -> n label input data expected {
	test doctools-toc-${impl}-53.$k.$n "doctools::toc serialize doctoc, ${label}$section, ok" -setup {
	    doctools::toc::export OUT
	    OUT config set newlines $nl
	    OUT config set indented $in
	    OUT config set aligned  $al
	    doctools::toc I
	    I exporter OUT
	} -body {
	    I deserialize = $data
	    stripcomments [I serialize doctoc]
	} -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-toc-${impl}-54.$n "doctools::toc serialize text, $label, ok" -setup {
	doctools::toc::export OUT
	doctools::toc 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::toc(::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-toc-${impl}-55.$k.$n "doctools::toc deserialize = json, $label$section, ok" -setup {
	    doctools::toc::import IN
	    doctools::toc I
	    I importer IN
	} -body {
	    I deserialize = $data json
	    doctools::toc::structure print [I serialize]
	} -cleanup {
	    I destroy
	    IN destroy
	} -result $expected
    }
}

TestFilesProcess $mytestdir fail json json-emsg -> n label input data expected {
    test doctools-toc-${impl}-56.$n "doctools::toc deserialize = json, $label, error message" -setup {
	doctools::toc::import IN
	doctools::toc 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-toc-${impl}-57.$k.$n "doctools::toc serialize json, $label$section, ok" -setup {
	    doctools::toc::export OUT
	    OUT config set indented $in
	    OUT config set aligned  $al
	    doctools::toc 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-toc-${impl}-58.$k.$n "doctools::toc serialize html, $label$section, ok" -setup {
	    doctools::toc::export OUT
	    OUT config set newlines $nl
	    OUT config set indented $in
	    OUT config set user _dummy_
	    doctools::toc 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

TestFilesProcess $mytestdir ok serial wiki -> n label input data expected {
    test doctools-toc-${impl}-59.$n "doctools::toc serialize wiki, $label, ok" -setup {
	doctools::toc::export OUT
	doctools::toc 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-toc-${impl}-60.$k.$n "doctools::toc serialize nroff, $label$section, ok" -setup {
	    doctools::toc::export OUT
	    OUT config set inline $inline
	    doctools::toc 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 toc_structure.test)

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