# This file tests the tclWinConsole.c file.
#
# 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
# generates output for errors. No output means no errors were found.
#
# Copyright © 1999 Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
if {"::tcltest" ni [namespace children]} {
package require tcltest 2.5
namespace import -force ::tcltest::*
}
::tcltest::ConstraintInitializer twapi { expr {![catch {package require twapi}]} }
# Prompt user for a yes/no response
proc yesno {question {default "Y"}} {
set answer ""
# Make sure we are seen but catch because ui and console
# packages may not be available
catch {twapi::set_foreground_window [twapi::get_console_window]}
while {![string is boolean -strict $answer]} {
puts -nonewline stdout "$question Type Y/N followed by Enter \[$default\] : "
flush stdout
set answer [string trim [gets stdin]]
if {$answer eq ""} {
set answer $default
}
}
return [expr {!! $answer}]
}
proc prompt {prompt} {
# Make sure we are seen but catch because twapi ui and console
# packages may not be available
catch {twapi::set_foreground_window [twapi::get_console_window]}
puts -nonewline stdout "$prompt"
flush stdout
}
# Input tests
test console-gets-1.0 {Console blocking gets} -constraints {win interactive} -body {
set response [prompt "Type \"xyz\" and hit Enter: "]
gets stdin
} -result xyz
test console-gets-1.1 {Console file channel: non-blocking gets} {win interactive} {
set oldmode [fconfigure stdin]
set response [prompt "Type \"abc\" and hit Enter: "]
fileevent stdin readable {
if {[gets stdin line] >= 0} {
set result $line
} elseif {[eof stdin]} {
set result "gets failed"
}
}
fconfigure stdin -blocking 0 -buffering line
set result {}
vwait result
#cleanup the fileevent
fileevent stdin readable {}
fconfigure stdin {*}$oldmode
set result
} abc
# Output tests
test console-puts-1.0 {Console blocking puts stdout} -constraints {win interactive} -body {
puts stdout "123"
yesno "Did you see the string \"123\"?"
} -result 1
test console-puts-1.1 {Console blocking puts stderr} -constraints {win interactive} -body {
puts stderr "456"
yesno "Did you see the string \"456\"?"
} -result 1
# fconfigure tests
## stdin
test console-fconfigure-1.0 {
Console get stdin configuration
} -constraints {win interactive} -body {
lsort [dict keys [fconfigure stdin]]
} -result {-blocking -buffering -buffersize -encoding -eofchar -inputmode -translation}
set testnum 0
foreach {opt result} {
-blocking 1
-buffering line
-buffersize 4096
-encoding utf-16
-inputmode normal
-translation auto
} {
test console-fconfigure-1.[incr testnum] "Console get stdin option $opt" \
-constraints {win interactive} -body {
fconfigure stdin $opt
} -result $result
}
test console-fconfigure-1.[incr testnum] {
Console get stdin option -eofchar
} -constraints {win interactive} -body {
fconfigure stdin -eofchar
} -result \x1a
test console-fconfigure-1.[incr testnum] {
fconfigure -inputmode password
} -constraints {win interactive} -body {
prompt "Type \"password\" and hit Enter. You should NOT see characters echoed: "
fconfigure stdin -inputmode password
gets stdin password
fconfigure stdin -inputmode normal
set password_echoed [yesno "\nWere the characters echoed?"]
prompt "Type \"normal\" and hit Enter. You should see characters echoed: "
gets stdin normal
set normal_echoed [yesno "Were the characters echoed?"]
list $password_echoed $password $normal_echoed $normal
} -result [list 0 password 1 normal]
## stdout/stderr
foreach chan {stdout stderr} major {2 3} {
test console-fconfigure-$major.0 "Console get $chan configuration" -constraints {
win interactive
} -body {
lsort [dict keys [fconfigure $chan]]
} -result {-blocking -buffering -buffersize -encoding -eofchar -translation -winsize}
set testnum 0
foreach {opt result} {
-blocking 1
-buffersize 4096
-encoding utf-16
-translation crlf
} {
test console-fconfigure-$major.[incr testnum] "Console get $chan option $opt" \
-constraints {win interactive} -body {
fconfigure $chan $opt
} -result $result
}
test console-fconfigure-$major.[incr testnum] "Console get $chan option -winsize" -constraints {win interactive} -body {
fconfigure $chan -winsize
} -result {\d+ \d+} -match regexp
test console-fconfigure-$major.[incr testnum] "Console get $chan option -buffering" -constraints {win interactive} -body {
fconfigure $chan -buffering
} -result [expr {$chan eq "stdout" ? "line" : "none"}]
}
#cleanup
::tcltest::cleanupTests
return