#
# Library functions for the ic_xml library
#
# File: ic_xml/lib/testbench_report.tcl
#
# $Revision: 394838 $ on $Date: 2004-05-12 20:56:36 -0400 (Wed, 12 May 2004) $
# 
# Copyright (c) 1993-2004 ICEM CFD Engineering
#
# Author: Jason Aftosmis (jason@icemcfd.com)
#
# TODO:
#   o
#
#---------------------------------------------------------------------
#

namespace eval TestBenchReport {
    
    namespace import ::IcXml::*
    
    variable _xsl_file [file join [ic_xml_lib_path] .. style testbench_report.xsl]
    variable _css_file [file join [ic_xml_lib_path] .. style generic_report.css]
    
    proc err {text}   { return [ic_xml_err   "TestBenchReport: $text"] }
    proc debug {text} { return [ic_xml_debug "TestBenchReport: $text"] }
    
    #
    # --> not sure about these anymore...fixme --jka
    #    
    proc ic_xml_testbench_create_document {args} {
        # e.g.: set args "TITLE testG NAME something TYPE speed"
        return [TestBenchReport::createDocument $args]
    }
    proc ic_xml_testbench_add_testcase {title namesvals} {
        return [TestBenchReport::addTestcase $title $namesvals]
    }
    proc ic_xml_testbench_write_report {output_file args} {
        return [TestBenchReport::writeReport $output_file $args]
    }
}


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


proc TestBenchReport::test {} {
    
    IcXmlReport::checkEmpty
    
    foreach test {testset1} {
	set dom_key [TestBenchReport::createDocument "NAME $test TITLE Hello"]
	TestBenchReport::addTestcase $dom_key first "START today RESULT good"
	TestBenchReport::addTestcase $dom_key second "START late RESULT bad"
	TestBenchReport::writeReport $dom_key ${test}a.html html
	TestBenchReport::closeReport $dom_key ${test}a.xml xml
    }
    foreach test {testset1} {
	set dom_key [TestBenchReport::openReport ${test}a.xml]
	TestBenchReport::addTestcase $dom_key third "START today RESULT good"
	TestBenchReport::addTestcase $dom_key fourth "START late RESULT bad"
	TestBenchReport::closeReport $dom_key ${test}b.xml xml
    }
    foreach test {testset1} {
	catch { file delete -force ${test}a.html }
	catch { file delete -force ${test}a.xml }
	catch { file delete -force ${test}b.xml }
    }
    
    return [IcXmlReport::checkEmpty]
}

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


## Create a new document with a root <TESTSET> and attributes <em>attributes</em>.
## Returns the domKey.

proc TestBenchReport::createDocument { {attributes ""} } {
    
    return [IcXmlReport::createDocument "TESTSET" $attributes]
}


## Add a <TESTCASE> element to the current <TESTSET>
## e.g.:
##
## <TESTCASE TITLE="$title">
##    <TESTPATH>%TBROOTDIR%products/DS81/Scripts\msh11-7.js</TESTPATH>
##    <START>$date</START> 
##    ...
##    <ELAPSED>$runtime</ELAPSED>
##    <RESULT>$result</RESULT>
##    <DUMP>$logfile</DUMP>
## </TESTCASE>
##
## Returns the actually nodeObject, for better or worse. ??FIXME??

proc TestBenchReport::addTestcase { domKey title children } {
    
    set testsetObj [IcXmlReport::getReportObject $domKey]
    if {"" == $testsetObj} {
	err "Warning: there is no TESTSET element in the given DOM"
    }
    
    #
    # --> Create the <TESTCASE> element with the TITLE attribute
    #
    
    set elName "TESTCASE"
    set attributes "TITLE $title"
    set testcaseObj [IcXmlReport::addElement $domKey $testsetObj $elName $attributes $children]
    
    #
    # --> testcaseObj is not tracked right now  fixme --jka FIXME FIXME ??
    #
    
    return $testcaseObj
}
 
  
## Read the given XML file <em>xml_file</em> into a TestBenchReport.
## Returns the domKey.

proc TestBenchReport::openReport { xml_file } {
    # this should be smarter, verifying, etc. --jka
    return [IcXmlReport::openReport $xml_file]
}


## Write out the TestBenchReport associated with the given <em>domKey</em>
## to the path <em>file</em> in the format <em>format</em>.

proc TestBenchReport::writeReport { domKey file format } {
    variable _css_file
    variable _xsl_file
    
    set args ""
    
    if {("" != $_xsl_file) && [file exists $_xsl_file]} {
	append args "-xsl_file {$_xsl_file} "
    } elseif {"" != $_xsl_file}  {
	err "Warning: could not find XSL file $_xsl_file"
    }
    if {("" != $_css_file) && [file exists $_css_file]} {
	append args "-css_file {$_css_file} "
    } elseif {"" != $_css_file}  {
	err "Warning: could not find CSS file $_css_file"
    }
    
    return [IcXmlReport::writeReport $domKey "$file" $format $args]
}


## Close the TestBenchReport associated with the given <em>domKey</em>
## and, if specified, to write it to the path <em>file</em> in the
## format <em>format</em>.

proc TestBenchReport::closeReport { domKey {file ""} {format ""} } {
    
    if {"" != $file} {
	set stat [TestBenchReport::writeReport $domKey $file $format]
    }
    return [IcXmlReport::closeDocument $domKey]
}
