-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformat_test.go
76 lines (59 loc) · 2.1 KB
/
format_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
package feedx_test
import (
"bytes"
"io"
"github.com/bsm/feedx"
"github.com/bsm/feedx/internal/testdata"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
)
var _ = Describe("Format", func() {
runSharedTest := func(subject feedx.Format) {
buf := new(bytes.Buffer)
enc, err := subject.NewEncoder(buf)
Expect(err).NotTo(HaveOccurred())
defer enc.Close()
Expect(enc.Encode(seed())).To(Succeed())
Expect(enc.Encode(seed())).To(Succeed())
Expect(enc.Close()).To(Succeed())
dec, err := subject.NewDecoder(buf)
Expect(err).NotTo(HaveOccurred())
defer dec.Close()
v1 := new(testdata.MockMessage)
Expect(dec.Decode(v1)).To(Succeed())
Expect(v1.Name).To(Equal("Joe"))
v2 := new(testdata.MockMessage)
Expect(dec.Decode(v2)).To(Succeed())
Expect(v2.Name).To(Equal("Joe"))
v3 := new(testdata.MockMessage)
Expect(dec.Decode(v3)).To(MatchError(io.EOF))
Expect(dec.Close()).To(Succeed())
}
It("detects the format", func() {
Expect(feedx.DetectFormat("/path/to/file.json")).To(Equal(feedx.JSONFormat))
Expect(feedx.DetectFormat("/path/to/file.json.gz")).To(Equal(feedx.JSONFormat))
Expect(feedx.DetectFormat("/path/to/file.json.flate")).To(Equal(feedx.JSONFormat))
Expect(feedx.DetectFormat("/path/to/file.jsonz")).To(Equal(feedx.JSONFormat))
Expect(feedx.DetectFormat("/path/to/file.pb")).To(Equal(feedx.ProtobufFormat))
Expect(feedx.DetectFormat("/path/to/file.pb.gz")).To(Equal(feedx.ProtobufFormat))
Expect(feedx.DetectFormat("/path/to/file.pb.flate")).To(Equal(feedx.ProtobufFormat))
Expect(feedx.DetectFormat("/path/to/file.pbz")).To(Equal(feedx.ProtobufFormat))
Expect(feedx.DetectFormat("")).To(BeNil())
Expect(feedx.DetectFormat("/path/to/file")).To(BeNil())
Expect(feedx.DetectFormat("/path/to/file.txt")).To(BeNil())
})
Describe("JSONFormat", func() {
var subject = feedx.JSONFormat
var _ feedx.Format = subject
It("encodes/decodes", func() {
runSharedTest(subject)
})
})
Describe("ProtobufFormat", func() {
var subject = feedx.ProtobufFormat
var _ feedx.Format = subject
It("encodes/decodes", func() {
runSharedTest(subject)
})
})
})