source [file dirname [info script]]/testing.tcl

set havetty 0
catch {
    set havetty [expr {"tty" in [stdout -commands]}]
}
if {!$havetty || ![stdout isatty]} {
    skiptest "  (aio tty)"
}

test tty-1.1 {tty status} {
    set dict [stdout tty]
    dict exists $dict output
} 1

test tty-1.2 {tty bad param} -body {
    stdout tty bad value
} -returnCodes error -result {bad setting "bad": must be baud, data, echo, handshake, input, output, parity, stop, vmin, or vtime}

test tty-1.3 {tty bad baud} -body {
    stdout tty baud 12345
} -returnCodes error -result {bad value for baud: 12345}

test tty-1.4 {tty bad fd} -body {
    set f [open [file tempfile] w]
    $f tty
} -returnCodes error -match regexp -result {^(Inappropriate ioctl for device|Not a tty)$} -cleanup {
    $f close
}


set n 0
foreach {param value} {
    output raw
    input raw
    handshake rtscts
} {
    test tty-1.[incr n] "tty setting $param" -setup {
        set savetty [stdout tty]
    } -body "stdout tty $param $value; dict get \[stdout tty\] $param" \
        -result $value -cleanup {
        stdout tty $savetty
    }
}

set n 0
foreach param {output input handshake baud stop data vmin vtime} {
    test tty-2.[incr n] "tty bad setting $param" -setup {
        set savetty [stdout tty]
    } -body "stdout tty $param bad" \
    -returnCodes error -result "bad value for $param: bad" -cleanup {
        stdout tty $savetty
    }
}

testreport
