summaryrefslogtreecommitdiffstats
path: root/tests/ioCmd.test
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2023-05-30 11:59:21 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2023-05-30 11:59:21 (GMT)
commitb98da201c52dbb581e9c9604e2f9d81ed0ec73af (patch)
tree350de38279114a5753f565d62df0f046b0840b22 /tests/ioCmd.test
parent07f73b212befdc5ca59e4c7c7582f86fbc2b50aa (diff)
downloadtcl-b98da201c52dbb581e9c9604e2f9d81ed0ec73af.zip
tcl-b98da201c52dbb581e9c9604e2f9d81ed0ec73af.tar.gz
tcl-b98da201c52dbb581e9c9604e2f9d81ed0ec73af.tar.bz2
start of test cases
Diffstat (limited to 'tests/ioCmd.test')
-rw-r--r--tests/ioCmd.test197
1 files changed, 196 insertions, 1 deletions
diff --git a/tests/ioCmd.test b/tests/ioCmd.test
index 678700f..1eedcb1 100644
--- a/tests/ioCmd.test
+++ b/tests/ioCmd.test
@@ -1,6 +1,7 @@
# -*- tcl -*-
# Commands covered: open, close, gets, read, puts, seek, tell, eof, flush,
-# fblocked, fconfigure, open, channel, fcopy
+# fblocked, fconfigure, open, channel, fcopy,
+# readFile, writeFile, foreachLine
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands. Sourcing this file into Tcl runs the tests and
@@ -3927,6 +3928,200 @@ test iocmd.tf-32.1 {origin thread of moved channel destroyed during access} -mat
} -constraints {testchannel thread notValgrind} \
-result {Owner lost}
+# Tests of readFile
+
+set BIN_DATA "\u0000\u0001\u0002\u0003\u0004\u001a\u001b\u000d\u000a\u0000"
+
+test iocmd.readFile-1.1 "readFile procedure: syntax" -body {
+ readFile
+} -returnCodes error -result {wrong # args: should be "readFile filename ?mode?"}
+test iocmd.readFile-1.2 "readFile procedure: syntax" -body {
+ readFile a b c
+} -returnCodes error -result {wrong # args: should be "readFile filename ?mode?"}
+test iocmd.readFile-1.3 "readFile procedure: syntax" -body {
+ readFile gorp gorp2
+} -returnCodes error -result {bad mode "gorp2": must be binary or text}
+
+test iocmd.readFile-2.1 "readFile procedure: behaviour" -setup {
+ set f [makeFile readFile21.txt "File\nContents"]
+} -body {
+ readFile $f
+} -cleanup {
+ removeFile $f
+} -result "File\nContents\n"
+test iocmd.readFile-2.2 "readFile procedure: behaviour" -setup {
+ set f [makeFile readFile22.txt "File\nContents"]
+} -body {
+ readFile $f text
+} -cleanup {
+ removeFile $f
+} -result "File\nContents\n"
+test iocmd.readFile-2.3 "readFile procedure: behaviour" -setup {
+ set f [makeFile readFile23.bin ""]
+ apply {filename {
+ set ff [open $filename wb]
+ puts -nonewline $ff $BIN_DATA
+ close $ff
+ }} $f
+} -body {
+ list [binary scan [readFile $f binary] c* x] $x
+} -cleanup {
+ removeFile $f
+} -result {1 {0 1 2 3 4 26 27 13 10 0}}
+# Need to set up ahead of the test
+set f [makeFile readFile24.txt ""]
+removeFile $f
+test iocmd.readFile-2.4 "readFile procedure: behaviour" -body {
+ readFile $f
+} -returnCodes error -result "couldn't open \"$f\": no such file or directory"
+
+# Tests of writeFile
+
+test iocmd.writeFile-1.1 "writeFile procedure: syntax" -body {
+ writeFile
+} -returnCodes error -result {wrong # args: should be "writeFile filename ?mode? data"}
+test iocmd.writeFile-1.2 "writeFile procedure: syntax" -body {
+ writeFile a b c d
+} -returnCodes error -result {wrong # args: should be "writeFile filename ?mode? data"}
+test iocmd.writeFile-1.3 "writeFile procedure: syntax" -body {
+ writeFile gorp gorp2 gorp3
+} -returnCodes error -result {bad mode "gorp2": must be binary or text}
+
+test iocmd.writeFile-2.1 "readFile procedure: behaviour" -setup {
+ set f [makeFile writeFile21.txt ""]
+ removeFile $f
+} -body {
+ list [writeFile $f "File\nContents\n"] [apply {filename {
+ set f [open $filename]
+ set text [read $f]
+ close $f
+ return $text
+ }} $f]
+} -cleanup {
+ removeFile $f
+} -result [list {} "File\nContents\n"]
+test iocmd.writeFile-2.2 "readFile procedure: behaviour" -setup {
+ set f [makeFile writeFile22.txt ""]
+ removeFile $f
+} -body {
+ writeFile $f text "File\nContents\n"
+ apply {filename {
+ set f [open $filename]
+ set text [read $f]
+ close $f
+ return $text
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result "File\nContents\n"
+test iocmd.writeFile-2.3 "readFile procedure: behaviour" -setup {
+ set f [makeFile writeFile23.txt ""]
+ removeFile $f
+} -body {
+ writeFile $f binary $BIN_DATA
+ apply {filename {
+ set f [open $filename rb]
+ set bytes [read $f]
+ close $f
+ binary scan $bytes c* x
+ return $x
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result {0 1 2 3 4 26 27 13 10 0}
+
+# Tests of foreachLine
+
+test iocmd.foreachLine-1.1 "foreachLine procedure: syntax" -returnCodes error -body {
+ foreachLine
+} -result {wrong # args: should be "foreachLine filename varName body"}
+test iocmd.foreachLine-1.2 "foreachLine procedure: syntax" -returnCodes error -body {
+ foreachLine a b c d
+} -result {wrong # args: should be "foreachLine filename varName body"}
+test iocmd.foreachLine-1.3 "foreachLine procedure: syntax" -setup {
+ set f [makeFile foreachLine13.txt ""]
+} -body {
+ apply {filename {
+ array set b {1 1}
+ foreachLine $filename b {}
+ }} $f
+} -cleanup {
+ removeFile $f
+} -returnCodes error -result {can't set "line": variable is array}
+
+test iocmd.foreachLine-2.1 "foreachLine procedure: behaviour" -setup {
+ set f [makeFile foreachLine21.txt "a\nb\nc"]
+} -body {
+ apply {filename {
+ set lines {}
+ foreachLine $filename v {
+ lappend lines $v
+ }
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result {a b c}
+test iocmd.foreachLine-2.2 "foreachLine procedure: behaviour" -setup {
+ set f [makeFile foreachLine22.txt "a\nbb\nc\ndd"]
+} -body {
+ apply {filename {
+ set lines {}
+ foreachLine $filename v {
+ if {[string length $v] == 1} continue
+ lappend lines $v
+ }
+ return $lines
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result {bb dd}
+test iocmd.foreachLine-2.3 "foreachLine procedure: behaviour" -setup {
+ set f [makeFile foreachLine23.txt "a\nbb\nccc\ndd\ne"]
+} -body {
+ apply {filename {
+ set lines {}
+ foreachLine $filename v {
+ if {[string length $v] > 2} break
+ lappend lines $v
+ }
+ return $lines
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result {a bb}
+test iocmd.foreachLine-2.4 "foreachLine procedure: behaviour" -setup {
+ set f [makeFile foreachLine24.txt "a\nbb\nccc\ndd\ne"]
+} -body {
+ apply {filename {
+ set lines {}
+ foreachLine $filename v {
+ if {[string length $v] > 2} {
+ return $v
+ }
+ lappend lines $v
+ }
+ return $lines
+ }} $f
+} -cleanup {
+ removeFile $f
+} -result {ccc}
+test iocmd.foreachLine-2.5 "foreachLine procedure: behaviour" -setup {
+ set f [makeFile foreachLine25.txt "a\nbb\nccc\ndd\ne"]
+} -body {
+ apply {filename {
+ set lines {}
+ foreachLine $filename v {
+ if {[string length $v] > 2} {
+ error "line too long"
+ }
+ lappend lines $v
+ }
+ return $lines
+ }} $f
+} -cleanup {
+ removeFile $f
+} -returnCodes error -result {line too long}
+
# ### ### ### ######### ######### #########
# ### ### ### ######### ######### #########