-
Notifications
You must be signed in to change notification settings - Fork 13
/
mock_test.go
68 lines (56 loc) · 1.18 KB
/
mock_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
package gozwave
import (
"encoding/hex"
"io"
"log"
"testing"
)
// generate with impl 'm *mockSerial' io.ReadWriteCloser
type mockSerial struct {
sendToRead chan string
getFromWrite chan []byte
writeLog [][]byte
errorLog []error
}
func newMockSerial() *mockSerial {
return &mockSerial{
sendToRead: make(chan string),
getFromWrite: make(chan []byte),
}
}
func (m *mockSerial) Read(p []byte) (n int, err error) {
data := <-m.sendToRead
bytes, err := hex.DecodeString(data)
if err != nil {
m.errorLog = append(m.errorLog, err)
return 0, err
}
n = copy(p, bytes)
if testing.Verbose() {
log.Printf("MOCK read %x", p[:n])
}
return n, nil
}
func (m *mockSerial) Write(p []byte) (n int, err error) {
if testing.Verbose() {
log.Printf("MOCK write %x\n", p)
}
m.writeLog = append(m.writeLog, p)
m.getFromWrite <- p
return len(p), nil
}
func (m *mockSerial) Close() error {
log.Printf("CLOSE")
return nil
}
type mockPortOpener struct {
mockSerial *mockSerial
}
func (mpo *mockPortOpener) Open() (io.ReadWriteCloser, error) {
return mpo.mockSerial, nil
}
func newMockPortOpener() *mockPortOpener {
return &mockPortOpener{
mockSerial: newMockSerial(),
}
}