pkg/expect: ExpectFunc, LineCount

ExpectFunc to make expect more extensible. LineCount to be
able to check 'no output' command.
This commit is contained in:
Gyu-Ho Lee
2016-04-06 15:43:44 -07:00
parent 62990fb5fa
commit 679e5e379b
2 changed files with 57 additions and 3 deletions

View File

@ -18,6 +18,24 @@ package expect
import "testing"
func TestExpectFunc(t *testing.T) {
ep, err := NewExpect("/bin/echo", "hello world")
if err != nil {
t.Fatal(err)
}
wstr := "hello world\r\n"
l, eerr := ep.ExpectFunc(func(a string) bool { return len(a) > 10 })
if eerr != nil {
t.Fatal(eerr)
}
if l != wstr {
t.Fatalf(`got "%v", expected "%v"`, l, wstr)
}
if cerr := ep.Close(); cerr != nil {
t.Fatal(cerr)
}
}
func TestEcho(t *testing.T) {
ep, err := NewExpect("/bin/echo", "hello world")
if err != nil {
@ -39,6 +57,27 @@ func TestEcho(t *testing.T) {
}
}
func TestLineCount(t *testing.T) {
ep, err := NewExpect("/usr/bin/printf", "1\n2\n3")
if err != nil {
t.Fatal(err)
}
wstr := "3"
l, eerr := ep.Expect(wstr)
if eerr != nil {
t.Fatal(eerr)
}
if l != wstr {
t.Fatalf(`got "%v", expected "%v"`, l, wstr)
}
if ep.LineCount() != 3 {
t.Fatalf("got %d, expected 3", ep.LineCount())
}
if cerr := ep.Close(); cerr != nil {
t.Fatal(cerr)
}
}
func TestSend(t *testing.T) {
ep, err := NewExpect("/usr/bin/tr", "a", "b")
if err != nil {