-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
80 lines (73 loc) · 1.63 KB
/
main_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
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"bytes"
"reflect"
"testing"
"time"
)
var (
anEntry *entry = &entry{
Note: "This is a note",
CreatedAt: time.Now(),
Tags: []string{"test", "bla"},
}
)
func TestWriteRead(t *testing.T) {
var buf bytes.Buffer
write(anEntry, &buf, t)
r := bufio.NewReader(&buf)
er, err := NewReader(r)
if err != nil {
t.Fatal(err)
}
got, err := er.Read()
if err != nil {
t.Fatal(err)
}
assertEqual(anEntry, got, t)
}
func TestWriteTwiceReadTwice(t *testing.T) {
var buf bytes.Buffer
write(anEntry, &buf, t)
write(anEntry, &buf, t)
r := bufio.NewReader(&buf)
er, err := NewReader(r)
if err != nil {
t.Fatal(err)
}
got, err := er.Read()
if err != nil {
t.Fatal(err)
}
assertEqual(anEntry, got, t)
got2, err := er.Read()
if err != nil {
t.Fatal(err)
}
assertEqual(anEntry, got2, t)
}
func write(entry *entry, buf *bytes.Buffer, t *testing.T) {
w := bufio.NewWriter(buf)
err := entry.Write(w)
if err != nil {
t.Fatal(err)
}
w.Flush()
}
func assertEqual(want, got *entry, t *testing.T) {
if want.Note != got.Note {
t.Fatalf("\n\texpected note:\n\t\t%#v\n\tgot:\n\t\t%#v", want.Note, got.Note)
}
// Using DeepEqual does not work for Time instances
// since the gob roundtrip loses the monotonic time private
// part of the Time instance.
origTime := want.CreatedAt.Format(time.RFC3339Nano)
gotTime := got.CreatedAt.Format(time.RFC3339Nano)
if origTime != gotTime {
t.Fatalf("\n\texpected created at:\n\t\t%#v\n\tgot:\n\t\t%#v", origTime, gotTime)
}
if !reflect.DeepEqual(want.Tags, got.Tags) {
t.Fatalf("\n\texpected tags:\n\t\t%v\n\tgot:\n\t\t%v", want.Tags, got.Tags)
}
}