-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat_test.go
More file actions
104 lines (89 loc) · 2.72 KB
/
format_test.go
File metadata and controls
104 lines (89 loc) · 2.72 KB
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
package feedx_test
import (
"bytes"
"errors"
"io"
"testing"
"github.com/bsm/feedx"
"github.com/bsm/feedx/internal/testdata"
)
func TestDetectFormat(t *testing.T) {
examples := []struct {
Input string
Exp feedx.Format
}{
{Input: "/path/to/file.json", Exp: feedx.JSONFormat},
{Input: "/path/to/file.json.gz", Exp: feedx.JSONFormat},
{Input: "/path/to/file.json.flate", Exp: feedx.JSONFormat},
{Input: "/path/to/file.jsonz", Exp: feedx.JSONFormat},
{Input: "/path/to/file.pb", Exp: feedx.ProtobufFormat},
{Input: "/path/to/file.pb.gz", Exp: feedx.ProtobufFormat},
{Input: "/path/to/file.pb.flate", Exp: feedx.ProtobufFormat},
{Input: "/path/to/file.pbz", Exp: feedx.ProtobufFormat},
{Input: "/path/to/file.cbor", Exp: feedx.CBORFormat},
{Input: "/path/to/file.cbor.gz", Exp: feedx.CBORFormat},
{Input: "/path/to/file.cbor.flate", Exp: feedx.CBORFormat},
{Input: "/path/to/file.cborz", Exp: feedx.CBORFormat},
{Input: "", Exp: (*feedx.NoFormat)(nil)},
{Input: "/path/to/file", Exp: (*feedx.NoFormat)(nil)},
{Input: "/path/to/file.txt", Exp: (*feedx.NoFormat)(nil)},
}
for _, x := range examples {
if got := feedx.DetectFormat(x.Input); got != x.Exp {
t.Errorf("expected %s for %q, but got %s", x.Exp, x.Input, got)
}
}
}
func TestFormat(t *testing.T) {
t.Run("json", func(t *testing.T) {
testFormat(t, feedx.JSONFormat)
})
t.Run("protobuf", func(t *testing.T) {
testFormat(t, feedx.ProtobufFormat)
})
t.Run("cbor", func(t *testing.T) {
testFormat(t, feedx.CBORFormat)
})
}
func testFormat(t *testing.T, f feedx.Format) {
t.Helper()
buf := new(bytes.Buffer)
enc, err := f.NewEncoder(buf)
if err != nil {
t.Fatal("expected no error, got", err)
}
defer func() { _ = enc.Close() }()
if err := enc.Encode(seed()); err != nil {
t.Fatal("expected no error, got", err)
}
if err := enc.Encode(seed()); err != nil {
t.Fatal("expected no error, got", err)
}
if err := enc.Close(); err != nil {
t.Fatal("expected no error, got", err)
}
dec, err := f.NewDecoder(buf)
if err != nil {
t.Fatal("expected no error, got", err)
}
defer func() { _ = dec.Close() }()
v1 := new(testdata.MockMessage)
if err := dec.Decode(v1); err != nil {
t.Fatal("expected no error, got", err)
} else if exp, got := "Joe", v1.Name; exp != got {
t.Errorf("expected %q, got %q", exp, got)
}
v2 := new(testdata.MockMessage)
if err := dec.Decode(v2); err != nil {
t.Fatal("expected no error, got", err)
} else if exp, got := "Joe", v2.Name; exp != got {
t.Errorf("expected %q, got %q", exp, got)
}
v3 := new(testdata.MockMessage)
if err := dec.Decode(v3); !errors.Is(err, io.EOF) {
t.Error("expected EOF, got", err)
}
if err := dec.Close(); err != nil {
t.Fatal("expected no error, got", err)
}
}