#
# Commands covered: simple excel sheet
#

#
# (c) 2019 Alexander Schoepe
#
# 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
#package require vfs::zip

test SimpleExcelWrite-1.1 {Write a Simple Excel Sheet} {
  set cDT {2019-10-31 15:10:23}
  set mCT {2019-10-31 17:20:45}

  set spreadsheet [::ooxml::xl_write new -creator {User A} -created $cDT -modifiedby {User B} -modified $mCT -application {Tcl Test}]
  if {[set sheet [$spreadsheet worksheet {Sheet 1}]] > -1} {
    $spreadsheet cell $sheet {Tcl Test - SimpleExcel} -index A1
  }
  if {[set sheet [$spreadsheet worksheet {Sheet 2}]] > -1} {
    $spreadsheet cell $sheet {Sheet 2} -index B2
  }
  $spreadsheet write simple.xlsx
  $spreadsheet destroy

  return [file exists simple.xlsx]
} 1


test SimpleExcelRead-1.2 {Read a Simple Excel Sheets} {
  array set workbook [ooxml::xl_read simple.xlsx]
  return [llength $workbook(sheets)]
} 2


test SimpleExcelRead-1.3 {Read a Simple Excel Sheet1 A1} {
  return $workbook(0,c,0,0)
} A1


test SimpleExcelRead-1.4 {Read a Simple Excel Sheet2 B2} {
  return $workbook(1,c,1,1)
} B2


if {[package vsatisfies [package present Tcl] 9]} {
  test MountXLSX-2.1 {Mount XLSX File} {
    catch {zipfs  mount simple.xlsx xlsx} mnt
  } 0
  variable zipfs "//zipfs:/"
} elseif {$::ooxml::zipAccessMethod eq "tcllib"} {
  test MountXLSX-2.1 {Mount XLSX File} {
    catch {
      ::zipfile::decode::open simple.xlsx
      variable zipdesc [::zipfile::decode::archive]
    }
  } 0
} else {
  test MountXLSX-2.1 {Mount XLSX File} {
    catch {vfs::zip::Mount simple.xlsx xlsx} mnt
  } 0
  variable zipfs ""
}

test DocPropsApp-3.1 {Parse xlsx/docProps/app.xml} {
  if {$::ooxml::zipAccessMethod eq "tcllib"} {
    set doc [dom parse [encoding convertfrom utf-8\
                            [::zipfile::decode::getfile $zipdesc docProps/app.xml]]]
  } else {
    set fd [open ${zipfs}xlsx/docProps/app.xml r]
    fconfigure $fd -encoding utf-8
    set doc [dom parse [read $fd]]
    close $fd
  }
  return [set appDoc [string match domDoc* $doc]]
} 1

set appXmlNs {
  x http://schemas.openxmlformats.org/officeDocument/2006/extended-properties
  vt http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes
}

testConstraint appCheck $appDoc

test DocPropsApp-3.2 {Check Application -application} appCheck {
  $doc selectNodesNamespaces $appXmlNs
  $doc selectNodes string(/x:Properties/x:Application)
} {Tcl Test}
 
test DocPropsApp-3.3 {Check Sheet Counter} appCheck {
  $doc selectNodesNamespaces $appXmlNs
  $doc selectNodes string(/x:Properties/x:HeadingPairs/vt:vector/vt:variant/vt:i4)
} $sheet
 

$doc delete


test DocPropsCore-4.1 {Parse xlsx/docProps/core.xml} {
  if {$::ooxml::zipAccessMethod eq "tcllib"} {
    set doc [dom parse [encoding convertfrom utf-8\
                            [::zipfile::decode::getfile $zipdesc docProps/core.xml]]]
  } else {
    set fd [open ${zipfs}xlsx/docProps/core.xml r]
    fconfigure $fd -encoding utf-8
    set doc [dom parse [read $fd]]
    close $fd
  }
  
  set coreXmlNs {
    cp http://schemas.openxmlformats.org/package/2006/metadata/core-properties
    dc http://purl.org/dc/elements/1.1/
    dcterms http://purl.org/dc/terms/
  }

  return [set coreDoc [string match domDoc* $doc]]
} 1

testConstraint coreCheck $coreDoc

test DocPropsCore-4.2 {Check Creator -creator} coreCheck {
  $doc selectNodesNamespaces $coreXmlNs
  $doc selectNodes string(/cp:coreProperties/dc:creator)
} {User A}
 
test DocPropsCore-4.3 {Check Modified By -modifiedby} coreCheck {
  $doc selectNodesNamespaces $coreXmlNs
  $doc selectNodes string(/cp:coreProperties/cp:lastModifiedBy)
} {User B}

test DocPropsCore-4.4 {Check Modified By -created} coreCheck {
  $doc selectNodesNamespaces $coreXmlNs
  $doc selectNodes string(/cp:coreProperties/dcterms:created)
} {2019-10-31T14:10:23Z}

test DocPropsCore-4.5 {Check Modified By -modified} coreCheck {
  $doc selectNodesNamespaces $coreXmlNs
  $doc selectNodes string(/cp:coreProperties/dcterms:modified)
} {2019-10-31T16:20:45Z}


$doc delete


if {[package vsatisfies [package present Tcl] 9]} {
  zipfs unmount $mnt
} elseif {$::ooxml::zipAccessMethod eq "tcllib"} {
  ::zipfile::decode::close
}  else {
  vfs::zip::Unmount $mnt xlsx
}
file delete -force simple.xlsx


# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# tcl-indent-level: 2
# End:
