Skip to content
Tcl Source Code Artifact [81b74a27f6]
Login

Artifact [81b74a27f6]

Artifact 81b74a27f6f37944b910e4272aa10bc8bfca6bbd0b00d4669b1eeada66b4d37a:


# 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} {
    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]}
    puts -nonewline stdout "$prompt"
    return [gets stdin]
}


test winConsole-1.0 {Console blocking gets} -constraints {win interactive xx} -body {
    set response [prompt "Type a line of text and press Return\n"]
    yesno "Did you type \"$response\""
} -result 1

test winConsole-1.1 {Console file channel: non-blocking gets} {win interactive} {
    set oldmode [fconfigure stdin]

    puts stdout "Enter abcdef<return> now: " nonewline
    flush stdout
    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

}  "abcdef"




#cleanup

::tcltest::cleanupTests
return