# -*- tcl -*- Commands covered:  cat (UNIX), expect
#
# 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.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    # do this in a way that is backward compatible for Tcl 8.3
    namespace import ::tcltest::test ::tcltest::cleanupTests
}
package require Expect

catch {unset x}

log_user 0

exp_spawn cat -u
exp_stty -echo < $spawn_out(slave,name)

test expect-1.1 {exact pattern} {
    expect "*"
    exp_send "a\r"
    
    set timeout 10
    set x 0
    expect -ex a {set x 1}
    set x
} {1}

test expect-1.2 {exact pattern buffering} {
    expect "*"
    exp_send "hiahi\r"
    
    set timeout 10
    set x 0
    expect -ex hi
    expect -ex hi {set x 1}
    set x
} {1}

# if only this test fails, then configure has guessed incorrectly and
# stty accesses the control terminal from stdout.  The quick fix is
# to edit expect_cf.h and define STTY_READS_STDOUT to 1.  (It should
# be commented out.)  If you figure out a way to fix the configure test,
# let me know.  Else, let me know a way to test for your particular
# machine and os version so it can be hardwired.
test expect-1.3 {exact pattern failure} {
    expect "*"
    exp_send "hiahi\r"
    
    set timeout 10
    set x 0
    expect -ex hi {set x 1}
    expect -ex hi {set x 2}
    expect -ex hi {set x 3}
    set x
} {2}

test expect-1.4 {glob pattern} {
    expect "*"
    exp_send "a\r"
    
    set timeout 10
    set x 0
    expect "a" {set x 1}
    set x
} {1}

test expect-1.5 {glob pattern buffering} {
    expect "*"
    exp_send "a\r"
    
    set timeout 10
    set x 0
    expect "*" {set x 1}
    set x
} {1}

test expect-1.6 {glob buffer} {
    expect "*"
    exp_send "philosophic\r"
    
    set timeout 10
    set x 0
    expect "hi"
    set x [string match *phi $expect_out(buffer)]
} {1}

test expect-1.7 {glob string} {
    expect "*"
    exp_send "philosophic\r"
    
    set timeout 10
    set x 0
    expect "hi"
    set expect_out(0,string)
} {hi}

test expect-1.10 {re pattern, gate keeping} {
    expect "*"
    exp_stty raw < $spawn_out(slave,name)
    exp_send "\rats-sw-8>show clock"

    #exp_internal 1
    set timeout 10
    set x 0
    expect -re {(.*)show clock$} {set x 1}
    #exp_internal 0
    exp_stty -raw < $spawn_out(slave,name)
    set x
} {1}

test expect-1.11 {re pattern, gate keeping} {
    exp_stty raw < $spawn_out(slave,name)
    expect "*"
    exp_send "enable"
    
    #exp_internal 1
    set timeout 10
    set x 0
    expect -re {(.*)enable$} {set x 1}
    exp_stty -raw < $spawn_out(slave,name)
    #exp_internal 0
    set x
} {1}

test expect-1.12 {re pattern, gate keeping} {
    exp_stty raw < $spawn_out(slave,name)
    expect "*"
    exp_send "sh clock"
    
    #exp_internal 1
    set timeout 10
    set x 0
    expect -re {(.*)sh clock$} {set x 1}
    exp_stty -raw < $spawn_out(slave,name)
    #exp_internal 0
    set x
} {1}

test expect-1.13 {re pattern, gate keeping} {
    exp_stty raw < $spawn_out(slave,name)
    expect "*"
    exp_send "Password: "
    
    #exp_internal 1
    set timeout 10
    set x 0
    expect -re {^.*Password: ?$} {set x 1}
    exp_stty -raw < $spawn_out(slave,name)
    #exp_internal 0
    set x
} {1}


close
wait

set filename /tmp/null.[pid]
set fid [open $filename w]
fconfigure $fid -encoding iso8859-1
puts $fid "a\u0000b"
close $fid

test expect-1.8 {default remove null behavior} {
    spawn cat $filename
    expect a\u0000b
    set rc [regexp $expect_out(buffer) "ab"]
    wait
    set rc
} {0}

test expect-1.8b {default remove null behavior} {
    spawn cat $filename
    expect ab
    set rc [regexp $expect_out(buffer) "ab"]
    wait
    set rc
} {1}

test expect-1.9 {match null inline} {
    spawn cat $filename
    remove_nulls 0
    expect a\u0000b
    set rc [regexp $expect_out(buffer) "a\u0000b"]
    close
    wait
    set rc
} {1}

file delete -force $filename

cleanupTests
return
