-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.go
85 lines (69 loc) · 1.47 KB
/
test_utils.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
82
83
84
85
package iopi
import "fmt"
// Used as a spy when testing
type Call struct {
Fn string
Arg []byte
}
type FakeFile struct {
Buf []byte
CallHistory []Call
NextRead []byte
}
func NewFakeFile() *FakeFile {
return &FakeFile{
Buf: make([]byte, 2),
NextRead: nil,
}
}
func (c Call) String() string {
return fmt.Sprintf("%s: 0x%08b", c.Fn, c.Arg)
}
// Records a call to the file API
func (f *FakeFile) recordCall(fn string, arg []byte) {
call := Call{fn, arg}
f.CallHistory = append(f.CallHistory, call)
}
// Determine if a call with the specified signature has been made to this File
// instance.
func (f *FakeFile) HasCall(fn string, arg []byte) bool {
for _, call := range f.CallHistory {
if fmt.Sprintf("%s", call) == fmt.Sprintf("%s", Call{fn, arg}) {
return true
}
}
return false
}
func (f *FakeFile) Read(b []byte) (int, error) {
f.recordCall("Read", b)
// Allow for faking outputs
if f.NextRead != nil {
n := copy(b, f.NextRead)
f.NextRead = nil
return n, nil
}
n := copy(b, f.Buf)
return n, nil
}
func (f *FakeFile) Write(b []byte) (int, error) {
f.recordCall("Write", b)
//fmt.Printf("write befor: %b\n", b)
n := copy(f.Buf, b)
//fmt.Printf("write after: %b\n", f.Buf)
return n, nil
}
func (f *FakeFile) Close() error {
f.recordCall("Close", nil)
return nil
}
func (f *FakeFile) Fd() uintptr {
return 0
}
func (f *FakeFile) Name() string {
return "fake"
}
func (f *FakeFile) Reset() {
for i := range f.Buf {
f.Buf[i] = 0
}
}