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
|
diff --git a/ffjsonnet_test.go b/ffjsonnet_test.go
index 50a76e249aba598ae01f5b74d1c9cf38948a799e..7e6234cff11dea5cf132a971ed96ee8eee1e4116 100644
--- a/ffjsonnet_test.go
+++ b/ffjsonnet_test.go
@@ -9,9 +9,58 @@ "github.com/peterbourgon/ff/v3/fftest"
"go.jolheiser.com/ffjsonnet"
)
-func TestJSONParser(t *testing.T) {
- t.Parallel()
+func TestSchema(t *testing.T) {
+ for _, testcase := range []struct {
+ name string
+ file string
+ schema string
+ want fftest.Vars
+ }{
+ {
+ name: "valid schema",
+ file: "testdata/basic.json",
+ schema: "import \"time\"\n{ s: string, i: int, b: bool, d: time.Duration }",
+ want: fftest.Vars{S: "s", I: 10, B: true, D: 5 * time.Second},
+ },
+ {
+ name: "valid schema (jsonnet)",
+ file: "testdata/basic.jsonnet",
+ schema: "import \"time\"\n{ s: string, i: int, b: bool, d: time.Duration }",
+ want: fftest.Vars{S: "s", I: 10, B: true, D: 5 * time.Second},
+ },
+ {
+ name: "invalid schema constraint",
+ file: "testdata/basic.json",
+ schema: "i: >100",
+ want: fftest.Vars{WantParseErrorString: "i:"},
+ },
+ {
+ name: "invalid schema type",
+ file: "testdata/basic.json",
+ schema: "s: int",
+ want: fftest.Vars{WantParseErrorString: "s:"},
+ },
+ {
+ name: "invalid duration",
+ file: "testdata/bad_duration.json",
+ schema: "import \"time\"\n{ d: time.Duration }",
+ want: fftest.Vars{WantParseErrorString: "d:"},
+ },
+ } {
+ t.Run(testcase.name, func(t *testing.T) {
+ t.Parallel()
+ fs, vars := fftest.Pair()
+ pc := &ffjsonnet.ParseConfig{Schema: testcase.schema}
+ vars.ParseError = ff.Parse(fs, nil,
+ ff.WithConfigFile(testcase.file),
+ ff.WithConfigFileParser(pc.Parse),
+ )
+ fftest.Compare(t, &testcase.want, vars)
+ })
+ }
+}
+func TestJSONParser(t *testing.T) {
for _, testcase := range []struct {
name string
args []string
@@ -48,8 +97,15 @@ args: []string{},
file: "testdata/bad.json",
want: fftest.Vars{WantParseErrorString: "end of file"},
},
+ {
+ name: "invalid duration",
+ args: []string{},
+ file: "testdata/bad_duration.json",
+ want: fftest.Vars{WantParseErrorString: "parse error"},
+ },
} {
t.Run(testcase.name, func(t *testing.T) {
+ t.Parallel()
fs, vars := fftest.Pair()
vars.ParseError = ff.Parse(fs, testcase.args,
ff.WithConfigFile(testcase.file),
|