Home

ffcue @main - refs - log -
-
https://git.jolheiser.com/ffcue.git
ffcue / ffcue_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
package ffcue_test

import (
	"os"
	"testing"
	"time"

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

func TestCUEParser(t *testing.T) {
	t.Parallel()

	for _, testcase := range []struct {
		name string
		args []string
		file string
		want fftest.Vars
	}{
		{
			name: "empty input",
			args: []string{},
			file: "testdata/empty.cue",
			want: fftest.Vars{WantParseErrorString: "parse error"},
		},
		{
			name: "basic KV pairs",
			args: []string{},
			file: "testdata/basic.cue",
			want: fftest.Vars{S: "s", I: 10, B: true, D: 5 * time.Second},
		},
		{
			name: "bad cue file",
			args: []string{},
			file: "testdata/bad.cue",
			want: fftest.Vars{WantParseErrorString: "4 errors"},
		},
	} {
		constraints, err := os.ReadFile("testdata/constraints.cue")
		if err != nil {
			panic(err)
		}
		t.Run(testcase.name, func(t *testing.T) {
			cfg := &ffcue.ParseConfig{
				Constraints: string(constraints),
			}
			fs, vars := fftest.Pair()
			vars.ParseError = ff.Parse(fs, testcase.args,
				ff.WithConfigFile(testcase.file),
				ff.WithConfigFileParser(cfg.Parse),
			)
			fftest.Compare(t, &testcase.want, vars)
		})
	}
	for _, testcase := range []struct {
		name string
		args []string
		file string
		want fftest.Vars
	}{
		{
			name: "value arrays",
			args: []string{},
			file: "testdata/value_arrays.cue",
			want: fftest.Vars{S: "bb", I: 12, B: true, D: 5 * time.Second, X: []string{"a", "B", "👍"}},
		},
	} {
		constraints, err := os.ReadFile("testdata/array_constraints.cue")
		if err != nil {
			panic(err)
		}
		t.Run(testcase.name, func(t *testing.T) {
			cfg := &ffcue.ParseConfig{
				Constraints: string(constraints),
			}
			fs, vars := fftest.Pair()
			vars.ParseError = ff.Parse(fs, testcase.args,
				ff.WithConfigFile(testcase.file),
				ff.WithConfigFileParser(cfg.Parse),
			)
			fftest.Compare(t, &testcase.want, vars)
		})
	}
}