Go Back |
Previous Test: Test 2 |
Next Test: Test 4
Test 3
The changed lines are in bold.
| tfc-part1.test (49 lines, 1196 bytes) |
package require tcltest
namespace import ::tcltest::*
source tfc-part1.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}
cleanupTests
| tfc-part1.tcl (48 lines, 1032 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)]} {
return $cells($key)
}
}
proc put {key value} {
variable cells
set cells($key) $value
}
}
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]
}
default {
if {[llength $args] == 0} {
error "wrong # args: should be \"$procName option ?arg arg ...?"
} else {
error "bad option \"$command\": must be get, or put"
}
}
}
}
return [namespace current]::$count
}
}