forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
81 lines (67 loc) · 1.2 KB
/
response_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
package http
import (
"testing"
cmds "github.com/ipfs/go-ipfs-cmds"
)
type testResponseType struct {
a int
b int
}
type testDecoder struct {
a *int
b *int
}
func (td *testDecoder) Decode(value interface{}) error {
me := value.(*cmds.MaybeError)
o := me.Value.(*testResponseType)
if td.a != nil {
o.a = *td.a
}
if td.b != nil {
o.b = *td.b
}
return nil
}
func TestNextDecodesIntoNewStruct(t *testing.T) {
a1 := 1
b1 := 2
testCommand := &cmds.Command{
Type: &testResponseType{},
}
decoder := &testDecoder{
a: &a1,
b: &b1,
}
r := &cmds.Request{
Command: testCommand,
}
response := &Response{
req: r,
dec: decoder,
}
v, err := response.Next()
if err != nil {
t.Fatal("error decoding response", err)
}
tv := v.(*testResponseType)
if tv.a != 1 {
t.Errorf("tv.a is %#v, expected 1", tv.a)
}
if tv.b != 2 {
t.Errorf("tv.b is %#v, expected 2", tv.b)
}
a2 := 3
decoder.a = &a2
decoder.b = nil
v2, err := response.Next()
if err != nil {
t.Fatal("error decoding response", err)
}
tv2 := v2.(*testResponseType)
if tv2.a != 3 {
t.Errorf("tv2.a is %#v, expected 3", tv2.a)
}
if tv2.b != 0 {
t.Errorf("tv.b is %#v, expected it to be reset to 0", tv2.b)
}
}