Home

ffjsonnet @849918e000d4d3a79af963936b19f50e2a7f598a - refs - log -
-
https://git.jolheiser.com/ffjsonnet.git
jsonnet parser for peterbourgon/ff
ffjsonnet / ffjsonnet_test.go
- raw -
  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
package ffjsonnet_test

import (
	"testing"
	"time"

	"github.com/peterbourgon/ff/v3"
	"github.com/peterbourgon/ff/v3/fftest"
	"go.jolheiser.com/ffjsonnet"
)

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
		file string
		want fftest.Vars
	}{
		{
			name: "empty input",
			args: []string{},
			file: "testdata/empty.json",
			want: fftest.Vars{WantParseErrorString: "end of file"},
		},
		{
			name: "basic KV pairs",
			args: []string{},
			file: "testdata/basic.json",
			want: fftest.Vars{S: "s", I: 10, B: true, D: 5 * time.Second},
		},
		{
			name: "basic KV pairs (jsonnet)",
			args: []string{},
			file: "testdata/basic.jsonnet",
			want: fftest.Vars{S: "s", I: 10, B: true, D: 5 * time.Second},
		},
		{
			name: "value arrays",
			args: []string{},
			file: "testdata/value_arrays.json",
			want: fftest.Vars{S: "bb", I: 12, B: true, D: 5 * time.Second, X: []string{"a", "B", "👍"}},
		},
		{
			name: "bad JSON file",
			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),
				ff.WithConfigFileParser(ffjsonnet.Parser),
			)
			fftest.Compare(t, &testcase.want, vars)
		})
	}
}