-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliance_test.go
77 lines (69 loc) · 1.66 KB
/
compliance_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
// Copyright (C) 2022 Michael J. Fromberger. All Rights Reserved.
package tomledit_test
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/creachadair/tomledit"
)
type testCase struct {
name string
input string
}
func mustLoadTests(t *testing.T, dir string) []*testCase {
t.Helper()
var cases []*testCase
if err := filepath.Walk(dir, func(path string, fi fs.FileInfo, err error) error {
if err != nil {
return err
} else if filepath.Ext(fi.Name()) != ".toml" {
return nil // skip
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
stem := filepath.Base(filepath.Dir(path))
name := strings.TrimSuffix(filepath.Base(path), ".toml")
cases = append(cases, &testCase{
name: stem + "/" + name,
input: string(data),
})
return nil
}); err != nil {
t.Fatalf("Loading tests failed: %v", err)
}
return cases
}
func TestCompliance(t *testing.T) {
if testing.Short() {
t.Skip("Skipped compliance tests because -test.short is set")
}
t.Run("Valid", func(t *testing.T) {
cases := mustLoadTests(t, "testdata/valid")
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
r := strings.NewReader(test.input)
if _, err := tomledit.Parse(r); err != nil {
t.Errorf("Parse failed: %v", err)
}
})
}
})
t.Run("Invalid", func(t *testing.T) {
cases := mustLoadTests(t, "testdata/invalid")
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
r := strings.NewReader(test.input)
doc, err := tomledit.Parse(r)
if err == nil {
t.Errorf("Parse succeeded with %v", doc)
} else {
t.Logf("Parse correctly failed: %v", err)
}
})
}
})
}