forked from helm-unittest/helm-unittest
/
equal_validator.go
62 lines (53 loc) · 1.27 KB
/
equal_validator.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
package validators
import (
"reflect"
"github.com/lrills/helm-unittest/unittest/common"
"github.com/lrills/helm-unittest/unittest/valueutils"
)
// EqualValidator validate whether the value of Path equal to Value
type EqualValidator struct {
Path string
Value interface{}
}
func (a EqualValidator) failInfo(actual interface{}, not bool) []string {
var notAnnotation string
if not {
notAnnotation = " NOT to equal"
}
failFormat := `
Path:%s
Expected` + notAnnotation + `:
%s`
expectedYAML := common.TrustedMarshalYAML(a.Value)
if not {
return splitInfof(failFormat, a.Path, expectedYAML)
}
actualYAML := common.TrustedMarshalYAML(actual)
return splitInfof(
failFormat+`
Actual:
%s
Diff:
%s
`,
a.Path,
expectedYAML,
actualYAML,
diff(expectedYAML, actualYAML),
)
}
// Validate implement Validatable
func (a EqualValidator) Validate(context *ValidateContext) (bool, []string) {
manifest, err := context.getManifest()
if err != nil {
return false, splitInfof(errorFormat, err.Error())
}
actual, err := valueutils.GetValueOfSetPath(manifest, a.Path)
if err != nil {
return false, splitInfof(errorFormat, err.Error())
}
if reflect.DeepEqual(a.Value, actual) == context.Negative {
return false, a.failInfo(actual, context.Negative)
}
return true, []string{}
}