Go Back | Previous Test: Test 6 | Next Test: Test 8

Test 7

The changed lines are in bold.


tfc-part2.test (102 lines, 2656 bytes)

package require tcltest
namespace import ::tcltest::*

source tfc-part2.tcl

saveState

test thatCellsAreEmptyByDefault-1.1 \
  {test that cells are not empty by default} {} {
    set sheet [Sheet::new]
    list [$sheet get A1] [$sheet get ZX347]
  } {{} {}}

restoreState

test thatTextCellsAreStored-1.1 \
  {test that text cells are stored} {} {
    set sheet [Sheet::new]
    set theCell A21
    $sheet put $theCell {A string}
    lappend results [$sheet get $theCell]
    $sheet put $theCell {A different string}
    lappend results [$sheet get $theCell]
    $sheet put $theCell {}
    lappend results [$sheet get $theCell]
    set results
  } {{A string} {A different string} {}}

restoreState

test thatManyCellsExist-1.1 \
  {test that many cells exist} {} {
    set sheet [Sheet::new]
    $sheet put A1 First
    $sheet put X27 Second
    $sheet put ZX901 Third
    lappend results [$sheet get A1]
    lappend results [$sheet get X27]
    lappend results [$sheet get ZX901]
    $sheet put A1 Fourth
    lappend results [$sheet get A1]
    lappend results [$sheet get X27]
    lappend results [$sheet get ZX901]
    set results
  } {First Second Third Fourth Second Third}

restoreState

test thatNumericCellsAreIdentifiedAndStored-1.1 \
  {test that numeric cells are identified and stored} {} {
    set sheet [Sheet::new]
    set theCell A21
    $sheet put $theCell X99
    lappend results [$sheet get $theCell]
    $sheet put $theCell 14
    lappend results [$sheet get $theCell]
    $sheet put $theCell { 99 X}
    lappend results [$sheet get $theCell]
    $sheet put $theCell { 1234 }
    lappend results [$sheet get $theCell]
    $sheet put $theCell { }
    lappend results [$sheet get $theCell]
    set results
  } {X99 14 { 99 X} 1234 { }}

restoreState

test thatWeHaveAccessToCellLiteralValuesForEditing-1.1 \
  {test that we have access to cell literal values for editing} {} {
    set sheet [Sheet::new]
    set theCell A21
    $sheet put $theCell {Some string}
    lappend results [$sheet getLiteral $theCell]
    $sheet put $theCell { 1234 }
    lappend results [$sheet getLiteral $theCell]
    $sheet put $theCell =7
    lappend results [$sheet getLiteral $theCell]
  } {{Some string} { 1234 } =7}

restoreState

test formulaSpec-1.1 \
  {test formula spec} {} {
    set sheet [Sheet::new]
    $sheet put B1 { =7}
    lappend results [$sheet get B1]
    lappend results [$sheet getLiteral B1]
  } {{ =7} { =7}}

restoreState

test constantFormula-1.1 \
  {test formula spec} {} {
    set sheet [Sheet::new]
    $sheet put A1 =7
    lappend results [$sheet getLiteral A1]
    lappend results [$sheet get A1]
  } {=7 7}

cleanupTests



tfc-part2.tcl (65 lines, 1520 bytes)

namespace eval Sheet {
  variable count
  set count 0

  proc new {} {
    variable count
    incr count

    namespace eval [namespace current]::$count {
      proc get {key} {
        variable cells
        if {[info exists cells($key)]} {
          if {[string match = [string index $cells($key) 0]]} {
            return [string range $cells($key) 1 end]
          } elseif {[string is integer $cells($key)]} {
            return [string trim $cells($key)]
          } else {
            return $cells($key)
          }
        }
      }

      proc put {key value} {
        variable cells
        set cells($key) $value
      }

      proc getLiteral {key} {
        variable cells
        if {[info exists cells($key)]} {
          return $cells($key)
        }
      }
    }

    proc [namespace current]::$count {args} {
      variable count
      set procName [namespace current]::$count
      set command [lindex $args 0]

      switch -exact $command {
        get {
          ${procName}::$command [lindex $args 1]
        }
        put {
          ${procName}::$command [lindex $args 1] [lindex $args 2]
        }
        getLiteral {
          ${procName}::$command [lindex $args 1]
        }
        default {
          if {[llength $args] == 0} {
            error "wrong # args: should be \"$procName option ?arg arg ...?"
          } else {
            error "bad option \"$command\": must be get, getLiteral, or put"
          }
        }
      }
    }

    return [namespace current]::$count
  }
}