-
Notifications
You must be signed in to change notification settings - Fork 15
/
stream_layer_test.go
67 lines (57 loc) · 1.35 KB
/
stream_layer_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
package onion
import (
"bytes"
"context"
"io"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
const validJSON = `
{
"string" : "str",
"number" : 100,
"nested" : {
"bool" : "true"
}
}
`
func TestNewStreamLayer(t *testing.T) {
Convey("Stream layer test", t, func() {
buf := bytes.NewBufferString(validJSON)
l, err := NewStreamLayer(buf, "json", nil)
So(err, ShouldBeNil)
o := New(l)
So(o.GetString("string"), ShouldEqual, "str")
So(o.GetInt("number"), ShouldEqual, 100)
So(o.GetBool("nested.bool"), ShouldBeTrue)
})
}
type dummyDecoder struct {
data map[string]interface{}
err error
}
func (d *dummyDecoder) Decode(context.Context, io.Reader) (map[string]interface{}, error) {
return d.data, d.err
}
func TestRegisterDecoder(t *testing.T) {
Convey("Test dummy decoder", t, func() {
RegisterDecoder(&dummyDecoder{
data: map[string]interface{}{
"hi": 10,
},
}, "dummy")
l, err := NewStreamLayer(bytes.NewReader(nil), "dummy", nil)
So(err, ShouldBeNil)
o := New(l)
So(o.GetInt("hi"), ShouldEqual, 10)
})
Convey("Fail decode", t, func() {
_, err := NewStreamLayer(nil, "hi_i_am_not_a_format", nil)
So(err, ShouldBeError)
buf := bytes.NewBufferString(`{INVALID}`)
_, err = NewStreamLayer(buf, "json", nil)
So(err, ShouldBeError)
_, err = NewStreamLayer(nil, "json", nil)
So(err, ShouldNotBeNil)
})
}