-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathformat_test.go
84 lines (74 loc) · 1.71 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
77
78
79
80
81
82
83
84
package bebop
import (
"os"
"path/filepath"
"testing"
)
var testFormatFiles = []string{
"all_consts",
"array_of_strings",
"basic_arrays",
"basic_types",
"documentation",
"enums",
"enums_doc",
"foo",
"jazz",
"lab",
"map_types",
"msgpack_comparison",
"request",
"server",
"union",
}
const fileExt = ".bop"
func TestTokenizeFormat(t *testing.T) {
t.Parallel()
for _, filename := range testFormatFiles {
filename := filename
t.Run(filename, func(t *testing.T) {
t.Parallel()
formatFile(t, "base", filename)
})
}
}
var testIncompatibleFiles = []string{
"quoted_string",
}
func TestTokenizeFormatIncompatible(t *testing.T) {
t.Parallel()
for _, filename := range testIncompatibleFiles {
filename := filename
t.Run(filename, func(t *testing.T) {
t.Parallel()
formatFile(t, "incompatible", filename)
})
}
}
func formatFile(t *testing.T, subdir, filename string) {
t.Helper()
const formattedExt = "_formatted.bop"
f, err := os.Open(filepath.Join("testdata", subdir, filename+fileExt))
if err != nil {
t.Fatalf("failed to open test file %s: %v", filename+fileExt, err)
}
if _, _, err := ReadFile(f); err != nil {
f.Close()
t.Fatalf("can not format unparsable files")
}
f.Close()
f, err = os.Open(filepath.Join("testdata", subdir, filename+fileExt))
if err != nil {
t.Fatalf("failed to open test file (for format) %s: %v", filename+fileExt, err)
}
tr := newTokenReader(f)
out, err := os.Create(filepath.Join("testdata", "formatted", filename+formattedExt))
if err != nil {
t.Fatalf("failed to open out file %s: %v", filename+formattedExt, err)
}
defer out.Close()
err = format(tr, out)
if err != nil {
t.Fatalf("failed to format %s: %v", filename+formattedExt, err)
}
}