-
Notifications
You must be signed in to change notification settings - Fork 83
/
io.go
83 lines (69 loc) · 1.8 KB
/
io.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
// Copyright 2018 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package sharedtest
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
)
// MockWriteCloser is a mock io.WriteCloser for testing.
type MockWriteCloser struct {
b bytes.Buffer
closed bool
t *testing.T
}
func (mwc *MockWriteCloser) Write(p []byte) (n int, err error) {
assert.False(mwc.t, mwc.closed)
return mwc.b.Write(p)
}
// Close closes the WriteCloser.
func (mwc *MockWriteCloser) Close() error {
mwc.closed = true
return nil
}
// IsClosed indicates whether the WriteCloser has been closed.
func (mwc *MockWriteCloser) IsClosed() bool {
return mwc.closed
}
// Bytes returns the bytes written to the WriteCloser.
func (mwc *MockWriteCloser) Bytes() []byte {
return mwc.b.Bytes()
}
// NewMockWriteCloser creates a new MockWriteCloser.
func NewMockWriteCloser(t *testing.T) *MockWriteCloser {
return &MockWriteCloser{
b: bytes.Buffer{},
closed: false,
t: t,
}
}
// MockReadCloser is a mock io.ReadCloser for testing.
type MockReadCloser struct {
rc io.ReadCloser
closed bool
t *testing.T
}
func (mrc *MockReadCloser) Read(p []byte) (n int, err error) {
assert.False(mrc.t, mrc.closed)
return mrc.rc.Read(p)
}
// Close closes the ReadCloser.
func (mrc *MockReadCloser) Close() error {
mrc.closed = true
return nil
}
// NewMockReadCloser creates a new MockWriteCloser.
func NewMockReadCloser(t *testing.T, data []byte) *MockReadCloser {
return &MockReadCloser{
rc: ioutil.NopCloser(bytes.NewReader(data)),
closed: false,
t: t,
}
}
// IsClosed returns whether the ReadCloser has been closed.
func (mrc *MockReadCloser) IsClosed() bool {
return mrc.closed
}