-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroundtrip_test.go
More file actions
57 lines (48 loc) · 1.21 KB
/
roundtrip_test.go
File metadata and controls
57 lines (48 loc) · 1.21 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
package gcode_test
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"github.com/lestrrat-go/gcode"
"github.com/stretchr/testify/require"
)
// streamCopy reads every line from src through gcode.Reader, writes
// each through gcode.Writer, and returns the formatted output.
func streamCopy(t *testing.T, src io.Reader) []byte {
t.Helper()
var buf bytes.Buffer
r := gcode.NewReader(src)
w := gcode.NewWriter(&buf)
var line gcode.Line
for {
err := r.Read(&line)
if err == io.EOF {
break
}
require.NoError(t, err)
require.NoError(t, w.Write(line))
}
require.NoError(t, w.Flush())
return buf.Bytes()
}
func TestRoundTripCorpus(t *testing.T) {
t.Parallel()
matches, err := filepath.Glob("testdata/*.gcode")
require.NoError(t, err)
require.NotEmpty(t, matches)
for _, path := range matches {
path := path
t.Run(filepath.Base(path), func(t *testing.T) {
t.Parallel()
source, err := os.ReadFile(path)
require.NoError(t, err)
// First pass canonicalises; second pass must be stable.
pass1 := streamCopy(t, bytes.NewReader(source))
pass2 := streamCopy(t, bytes.NewReader(pass1))
require.Equal(t, string(pass1), string(pass2),
"round-trip not stable for %s", path)
})
}
}