#
# Commands covered: docx
#
#
# (c) 2025 Rolf Ade
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

if {[lsearch [namespace children] ::tcltest] == -1} {
  package require tcltest 2
  namespace import -force ::tcltest::*
  loadTestedCommands
}

package require ooxml
namespace import ::ooxml::docx::docx



set docx [docx new]
test docx-1.1 {paragraph argument check} {
    catch {$docx paragraph}
} 1

test docx-1.2 {paragraph argument check} {
    catch {$docx paragraph "some text"}
} 0

test docx-1.3 {paragraph argument check} {
    catch {$docx paragraph "some text" -style} errMsg
} 1

test docx-1.4 {mutually exclusive options} {
    set result [catch {
        $docx math {
            $docx mrun -sty p -nor 1 "some" 
        }
    } errMsg]
    list $result $errMsg
} {1 {The options -scr -sty and -nor are mutually exclusive.}}

set counter 0
foreach {method fixedArguments postOptions} {
    append text ""
    comment "" {"<comment script>"}
    commentrangeend id script
    configure "" ""
    endnote "" script
    footnote "" script
    image "file type" ""
    jumpto "text mark" ""
    pagesetup "" ""
    paragraph text ""
    sectionstart "" ""
    settings "" ""
    simplecomment text ""
    simplecommentrangeend "id text" ""
    simpletable tabledata ""
    style paragraphdefault ""
    style characterdefault ""
    style "character name" ""
    style "paragraph name" ""
    style "table name" ""
    table "" script
    textbox "" script
    url "text url" ""
} {
    incr counter
    set bodyScript "$docx $method"
    set expectedResult "invalid arguments, expected:"
    foreach fixedArgument $fixedArguments {
        append bodyScript " $fixedArgument"
        append expectedResult " $fixedArgument"
    }
    append bodyScript " -optionWithoutValue"
    append expectedResult " ?-option value? ?-option value? .."
    foreach postOption $postOptions {
        append bodyScript " {}"
        append expectedResult " $postOption"
    }
    test docx-1.5.$counter "Method $method - not paired option value list" \
        -body $bodyScript \
        -returnCodes error \
        -result $expectedResult
}

set mathbodytmpl {$docx math {$bodyScript}}
foreach {mathmethod fixedArguments postOptions} {
    maccent "" baseScript
    mbar "" baseScript
    mdelim "" script
    mfrac "" {numScript denScript}
    mgroupchr "" baseScript
    mmatrix "" rowScripts
    mnary "" baseScript
    mrun "" text
} {
    set bodyScript "$docx $mathmethod"
    set expectedResult "invalid arguments, expected:"
    foreach fixedArgument $fixedArguments {
        append bodyScript " $fixedArgument"
        append expectedResult " $fixedArgument"
    }
    append bodyScript " -optionWithoutValue"
    append expectedResult " ?-option value? ?-option value? .."
    foreach postOption $postOptions {
        append bodyScript " {}"
        append expectedResult " $postOption"
    }
    test docx-1.5.$counter "Method $mathmethod - not paired option value list" \
        -body [subst $mathbodytmpl] \
        -returnCodes error \
        -result $expectedResult
}


set counter 0
set expectedResult "unknown option: -notExistingOption, known option.*"
foreach {method fixedArguments postOptions} {
    append text ""
    comment "" {"<comment script>"}
    configure "" ""
    endnote "" script
    footnote "" script
    jumpto "text mark" ""
    pagesetup "" ""
    paragraph text ""
    sectionstart "" ""
    settings "" ""
    simplecomment text ""
    simpletable tabledata ""
    style paragraphdefault ""
    style characterdefault ""
    style "character name" ""
    style "paragraph name" ""
    style "table name" ""
    table "" script
    url "text url" ""
} {
    incr counter
    set bodyScript "$docx $method"
    foreach fixedArgument $fixedArguments {
        append bodyScript " $fixedArgument"
    }
    append bodyScript " -notExistingOption dummyValue"
    foreach postOption $postOptions {
        append bodyScript " {}"
    }
    test docx-1.6.$counter "Method $method - unknown option" \
        -body $bodyScript \
        -returnCodes error \
        -match regexp \
        -result $expectedResult
}

set mathbodytmpl {$docx math {$bodyScript}}
foreach {mathmethod fixedArguments postOptions} {
    maccent "" baseScript
    mbar "" baseScript
    mdelim "" script
    mfrac "" {numScript denScript}
    mgroupchr "" baseScript
    mnary "" baseScript
    mrun "" text
} {
    incr counter
    set bodyScript "$docx $mathmethod"
    foreach fixedArgument $fixedArguments {
        append bodyScript " $fixedArgument"
    }
    append bodyScript " -notExistingOption dummyValue"
    foreach postOption $postOptions {
        append bodyScript " {}"
    }
    test docx-1.6.$counter "Method $mathmethod - unknown option" \
        -body [subst $mathbodytmpl] \
        -returnCodes error \
        -match regexp \
        -result $expectedResult
}
$docx destroy

test docx-2.1 {settings} {
    set docx [docx new]
    $docx settings -reset 1
    set result [$docx xpath count(*) settings]
    $docx destroy
    set result
} 0

test docx-2.2 {settings} {
    set docx [docx new]
    $docx settings -reset 1
    $docx settings -defaultTabStop 400 -zoom {type bestFit percent 100} -autoHyphenation off
    set result ""
    foreach node [$docx xpath * settings] {
        lappend result [$node localName]
    }
    $docx destroy
    set result
} {zoom defaultTabStop autoHyphenation}

test docx-2.3 {settings} {
    set docx [docx new]
    $docx settings -reset 1
    $docx settings -defaultTabStop 400 -zoom {type bestFit percent 100} -autoHyphenation off
    $docx settings -evenAndOddHeaders on -view outline -embedSystemFonts on \
        -doNotEmbedSmartTags on -zoom {type textFit percent 200}
    set result [$docx xpath {* local-name()} settings -list]
    $docx destroy
    set result
} {view zoom embedSystemFonts defaultTabStop autoHyphenation evenAndOddHeaders doNotEmbedSmartTags}

test docx-2.4 {settings} {
    set docx [docx new]
    $docx settings -reset 1
    $docx settings -defaultTabStop 400 -zoom {type bestFit percent 100} -autoHyphenation off
    $docx settings -evenAndOddHeaders on -view outline -embedSystemFonts on \
        -doNotEmbedSmartTags on -zoom {type textFit percent 200}
    set result [$docx xpath string(w:zoom/@w:val) settings]
    lappend result [$docx xpath string(w:zoom/@w:percent) settings]
    $docx destroy
    set result
} {textFit 200}


    # cleanup
::tcltest::cleanupTests
return
