-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimewriter_test.go
More file actions
65 lines (51 loc) · 1.13 KB
/
timewriter_test.go
File metadata and controls
65 lines (51 loc) · 1.13 KB
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
package timewriter
import (
"errors"
"fmt"
"regexp"
"testing"
)
type MockWriter struct {
buf []byte
}
func (m *MockWriter) Write(p []byte) (n int, err error) {
m.buf = append(m.buf, p...)
return len(p), nil
}
func (m *MockWriter) Check(expect []byte) error {
r := regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)? [+-]{1}\d{4} UTC: `)
s := r.Split(string(m.buf), -1)
if len(s) != 3 {
return errors.New("Could not find write")
}
if c := s[1]; c != string(expect) {
return fmt.Errorf("Found wrong write: %+q", c)
}
return nil
}
func testBytes(t *testing.T, buf []byte) {
m := MockWriter{make([]byte, 0)}
l, err := New(&m)
if err != nil {
t.Fatalf("%s", err)
}
n, err := l.Write([]byte(buf))
if n != len(buf) {
t.Fatalf("Write did not return the length of the passed buffer: %d/%d", n, len(buf))
}
if err != nil {
t.Fatalf("Write failed: %s", err)
}
if err = m.Check(buf); err != nil {
t.Fatalf("Check failed: %s", err)
}
}
func TestR(t *testing.T) {
testBytes(t, []byte("\r"))
}
func TestN(t *testing.T) {
testBytes(t, []byte("\n"))
}
func TestRN(t *testing.T) {
testBytes(t, []byte("\r\n"))
}