forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
51 lines (43 loc) · 926 Bytes
/
writer_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
package astilectron
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
// mockedWriter represents a mocked writer
type mockedWriter struct {
c bool
fn func()
w []string
wg *sync.WaitGroup
}
// Close implements the io.Closer interface
func (w *mockedWriter) Close() error {
w.c = true
return nil
}
// Write implements io.Writer interface
func (w *mockedWriter) Write(p []byte) (int, error) {
w.w = append(w.w, string(p))
if w.fn != nil {
w.fn()
}
if w.wg != nil {
w.wg.Done()
}
return len(p), nil
}
// TestWriter tests the writer
func TestWriter(t *testing.T) {
// Init
var mw = &mockedWriter{}
var w = newWriter(mw)
// Test write
err := w.write(Event{Name: "test", TargetID: "target_id"})
assert.NoError(t, err)
assert.Equal(t, []string{"{\"name\":\"test\",\"targetID\":\"target_id\"}\n"}, mw.w)
// Test close
err = w.close()
assert.NoError(t, err)
assert.True(t, mw.c)
}