# 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
}

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}

close
wait

set filename /tmp/null.[pid]
set fid [open $filename w]
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
