-
Notifications
You must be signed in to change notification settings - Fork 42
/
reader_util_test.go
166 lines (138 loc) · 2.99 KB
/
reader_util_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package fit_test
import (
"bytes"
"compress/gzip"
"fmt"
"go/format"
"io"
"log"
"os"
"strconv"
"github.com/cespare/xxhash"
"github.com/kortschak/utter"
"github.com/tormoder/fit"
)
func fitFingerprint(fit *fit.File) uint64 {
h := xxhash.New()
utter.Fdump(h, fit)
return h.Sum64()
}
func fitUtterDump(fit *fit.File, path string, compressed bool) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
var w io.WriteCloser
if compressed {
w = gzip.NewWriter(f)
} else {
w = f
}
utter.Fdump(w, fit)
if !compressed {
return f.Close()
}
err = w.Close()
if err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// testingDecodeOpts is a convenience type to simplify code generation for
// reader_files_test.go.
type testingDecodeOpts int
const (
tdoNone testingDecodeOpts = iota
tdoAllWithDiscardLogger
tdoStderrLogger
)
func (tdo testingDecodeOpts) opts() []fit.DecodeOption {
return tdoOpts[tdo]
}
var tdoOpts = [...][]fit.DecodeOption{
nil,
{
fit.WithUnknownFields(),
fit.WithUnknownMessages(),
fit.WithLogger(discardLogger()), // For test coverage.
},
{
fit.WithStdLogger(), // For debugging.
},
}
func (tdo testingDecodeOpts) String() string {
return tdoString[tdo]
}
var tdoString = [...]string{
"tdoNone",
"tdoAllWithDiscardLogger",
"tdoStderrLogger",
}
func discardLogger() *log.Logger {
return log.New(io.Discard, "", 0)
}
func regenerateDecodeTestTable() error {
const header = `// Generated by reader_util_test.go.
// Only edit to bootstrap new entries or change existing entries.
package fit_test
var decodeTestFiles = [...]struct {
folder string
name string
source string
wantErr bool
fingerprint uint64
compress bool
dopts testingDecodeOpts
skipEncode bool
encodeNote string
}{
`
g := new(gen)
g.WriteString(header)
for _, dto := range decodeTestFiles {
g.openField()
g.writeStringQField(dto.folder)
g.writeStringQField(dto.name)
g.writeStringQField(dto.source)
g.writeBoolField(dto.wantErr)
g.writeUintField(dto.fingerprint)
g.writeBoolField(dto.compress)
g.writeStringField(dto.dopts.String())
g.writeBoolField(dto.skipEncode)
g.writeStringQField(dto.encodeNote)
g.closeField()
}
g.WriteString("}\n")
preFormat := g.Bytes()
src, err := format.Source(preFormat)
if err != nil {
return fmt.Errorf("format.Source: %w on\n%s", err, preFormat)
}
return os.WriteFile("reader_files_test.go", src, 0o644)
}
type gen struct {
bytes.Buffer
}
func (g *gen) openField() {
g.WriteString("{\n")
}
func (g *gen) closeField() {
g.WriteString("},\n")
}
func (g *gen) writeStringField(s string) {
g.WriteString(s)
g.WriteString(",\n")
}
func (g *gen) writeStringQField(s string) {
g.WriteString(strconv.Quote(s))
g.WriteString(",\n")
}
func (g *gen) writeBoolField(b bool) {
g.WriteString(strconv.FormatBool(b))
g.WriteString(",\n")
}
func (g *gen) writeUintField(u uint64) {
g.WriteString(strconv.FormatUint(u, 10))
g.WriteString(",\n")
}