forked from cyrus-and/gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
81 lines (68 loc) · 1.45 KB
/
io_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package gdb
import (
"fmt"
"testing"
)
func TestIO(t *testing.T) {
input := "package foo"
expectedIn := fmt.Sprintf("%s\r\n", input) // not sure why \r\n...
expectedOut := "package foo\r\n"
// start gdb
gdb, err := New(nil)
if err != nil {
t.Fatal(err)
}
// start processing the output
done := make(chan bool)
go func() {
var n int
buf := make([]byte, 1024)
// read the first line
n, err = gdb.Read(buf)
if err != nil {
t.Fatal(err)
}
if string(buf[:n]) != expectedIn {
fmt.Printf("'%s'\n", buf[:n])
fmt.Printf("'%s'\n", []byte(expectedIn))
t.Fatal("should read back the input")
}
// read the second line
n, err = gdb.Read(buf)
if err != nil {
t.Fatal(err)
}
if string(buf[:n]) != expectedOut {
fmt.Printf("'%s'\n", buf[:n])
fmt.Printf("'%s'\n", []byte(expectedIn))
t.Fatal("should read the proper output")
}
// try another read
n, err = gdb.Read(buf)
if err == nil {
t.Fatal("read should fail")
}
done <- true
}()
// load a program
if _, err = gdb.Send("file-exec-file", "gofmt"); err != nil {
t.Fatal(err)
}
// provide some input
if _, err := gdb.Write([]byte(input)); err != nil {
t.Fatal(err)
}
if _, err := gdb.Write([]byte("\n\x04")); err != nil {
t.Fatal(err)
}
// run the program
if _, err = gdb.Send("exec-run"); err != nil {
t.Fatal(err)
}
// exit gdb
if err := gdb.Exit(); err != nil {
t.Fatal(err)
}
// wait for the output processing
_ = <-done
}