-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
97 lines (60 loc) · 2.26 KB
/
config_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
85
86
87
88
89
90
91
92
93
94
95
96
97
package firespace
import (
"fmt"
"testing"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"github.com/stretchr/testify/require"
)
func Test_ConfigFileCueValidion(t *testing.T) {
goConfigFile := ConfigFile{}
validateGoStruct(t, &goConfigFile, "#ConfigFile")
}
func Test_GlobalSettingsCueValidion(t *testing.T) {
goGlobalSettings := GlobalSettings{}
validateGoStruct(t, &goGlobalSettings, "#GlobalSettings")
}
func Test_SpaceSettingsCueValidion(t *testing.T) {
goSpaceSettings := SpaceSettings{}
validateGoStruct(t, &goSpaceSettings, "#SpaceSettings")
}
func Test_ProgramSettingsCueValidion(t *testing.T) {
goProgramSettings := ProgramSettings{}
validateGoStruct(t, &goProgramSettings, "#ProgramSettings")
}
func Test_AdditionalSpaceSettingsCueValidion(t *testing.T) {
goAdditionalSpaceSettings := AdditionalSpaceSettings{}
validateGoStruct(t, &goAdditionalSpaceSettings, "#AdditionalSpaceSettings")
}
func validateGoStruct(t *testing.T, goInterface interface{}, cuePath string) {
require.Empty(t, goInterface)
cCtx := cuecontext.New()
ccVal, err := loadCueConfigValue(cCtx)
require.NoError(t, err)
require.NotEmpty(t, ccVal)
cueLookupValue := ccVal.LookupPath(cue.ParsePath(cuePath))
require.NotEmpty(t, cueLookupValue)
t.Logf("cueLookupValue:\n%#v\n", cueLookupValue)
cueGoInterface := cCtx.EncodeType(goInterface)
openCueGoInterface := cueGoInterface
op, orEntries := cueGoInterface.Expr()
// if op is | and first entry is nil use the sencond entry
if op.String() == "|" {
if orEntries[0].Null() == nil {
openCueGoInterface = orEntries[1]
}
}
// close the struct
closedCueGoInterface := closeStructs(cCtx, openCueGoInterface)
t.Logf("closedCueGoInterface:\n%#v\n", closedCueGoInterface)
// cross unify to check for not allow or missing entries
cueGoUnify := cueLookupValue.Unify(closedCueGoInterface)
require.NoErrorf(t, cueGoUnify.Err(), "cue.unify(go): %s", errors.Details(cueGoUnify.Err(), nil))
goCueUnify := closedCueGoInterface.Unify(cueLookupValue)
require.NoErrorf(t, goCueUnify.Err(), "go.unify(cue): %s", errors.Details(cueGoUnify.Err(), nil))
}
func closeStructs(cctx *cue.Context, v cue.Value) cue.Value {
closedV := cctx.CompileString(fmt.Sprintf("close({%#v})", v))
return closedV
}