-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommit_test.go
137 lines (115 loc) · 3.19 KB
/
commit_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package dataset
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"github.com/ipfs/go-datastore"
)
func TestCommitMsgAssign(t *testing.T) {
doug := &User{Id: "doug_id", Email: "doug@example.com"}
expect := &CommitMsg{
Author: doug,
Title: "expect title",
Message: "expect message",
}
got := &CommitMsg{
Author: &User{Id: "maha_id", Email: "maha@example.com"},
Title: "title",
Message: "message",
}
got.Assign(&CommitMsg{
Author: doug,
Title: "expect title",
}, &CommitMsg{
Message: "expect message",
})
if err := CompareCommitMsgs(expect, got); err != nil {
t.Error(err)
}
got.Assign(nil, nil)
if err := CompareCommitMsgs(expect, got); err != nil {
t.Error(err)
}
emptyMsg := &CommitMsg{}
emptyMsg.Assign(expect)
if err := CompareCommitMsgs(expect, emptyMsg); err != nil {
t.Error(err)
}
}
func TestCommitMsgMarshalJSON(t *testing.T) {
cases := []struct {
in *CommitMsg
out []byte
err error
}{
{&CommitMsg{Title: "title"}, []byte(`{"title":"title"}`), nil},
{&CommitMsg{Author: &User{Id: "foo"}}, []byte(`{"author":{"id":"foo"},"title":""}`), nil},
}
for i, c := range cases {
got, err := c.in.MarshalJSON()
if err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if !bytes.Equal(c.out, got) {
t.Errorf("case %d error mismatch. %s != %s", i, string(c.out), string(got))
continue
}
}
strbytes, err := json.Marshal(&CommitMsg{path: datastore.NewKey("/path/to/dataset")})
if err != nil {
t.Errorf("unexpected string marshal error: %s", err.Error())
return
}
if !bytes.Equal(strbytes, []byte("\"/path/to/dataset\"")) {
t.Errorf("marshal strbyte interface byte mismatch: %s != %s", string(strbytes), "\"/path/to/dataset\"")
}
}
func TestCommitMsgUnmarshalJSON(t *testing.T) {
cases := []struct {
data string
result *CommitMsg
err error
}{
{`{}`, &CommitMsg{}, nil},
{`{ "title": "title", "message": "message"}`, &CommitMsg{Title: "title", Message: "message"}, nil},
{`{ "author" : { "id": "id", "email": "email@email.com"} }`, &CommitMsg{Author: &User{Id: "id", Email: "email@email.com"}}, nil},
}
for i, c := range cases {
cm := &CommitMsg{}
if err := json.Unmarshal([]byte(c.data), cm); err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if err := CompareCommitMsgs(cm, c.result); err != nil {
t.Errorf("case %d comparison error: %s", i, err)
continue
}
}
strq := &CommitMsg{}
path := "/path/to/msg"
if err := json.Unmarshal([]byte(`"`+path+`"`), strq); err != nil {
t.Errorf("unmarshal string path error: %s", err.Error())
return
}
if strq.path.String() != path {
t.Errorf("unmarshal didn't set proper path: %s != %s", path, strq.path)
return
}
}
func CompareCommitMsgs(a, b *CommitMsg) error {
if a == nil && b == nil {
return nil
} else if a == nil && b != nil || a != nil && b == nil {
return fmt.Errorf("nil mismatch: %s != %s", a, b)
}
// TODO - compare authors
if a.Title != b.Title {
return fmt.Errorf("Title mismatch: %s != %s", a.Title, b.Title)
}
if a.Message != b.Message {
return fmt.Errorf("Message mismatch: %s != %s", a.Message, b.Message)
}
return nil
}