tests: extend Diff() helper to accept new options

This commit is contained in:
Vincent Bernat
2022-08-31 14:29:52 +02:00
parent c3d2fc64f8
commit a9bef1c3fc
2 changed files with 33 additions and 11 deletions

View File

@@ -41,15 +41,41 @@ var prettyC = pretty.Config{
}, },
} }
// DiffOption changes the behavior of the Diff function.
type DiffOption struct {
kind int
// When this is a formatter
t reflect.Type
fn interface{}
}
// Diff return a diff of two objects. If no diff, an empty string is // Diff return a diff of two objects. If no diff, an empty string is
// returned. // returned.
func Diff(a, b interface{}) string { func Diff(a, b interface{}, options ...DiffOption) string {
prettyC = prettyC
for _, option := range options {
switch option.kind {
case DiffUnexported.kind:
prettyC.IncludeUnexported = true
case DiffZero.kind:
prettyC.SkipZeroFields = false
case DiffFormatter(nil, nil).kind:
prettyC.Formatter[option.t] = option.fn
}
}
return prettyC.Compare(a, b) return prettyC.Compare(a, b)
} }
// RegisterDiffFormatter add an additional formatter for pretty diff. var (
func RegisterDiffFormatter(t reflect.Type, fn interface{}) { // DiffUnexported will display diff of unexported fields too.
prettyC.Formatter[t] = fn DiffUnexported = DiffOption{kind: 1}
// DiffZero will include zero-field in diff
DiffZero = DiffOption{kind: 2}
)
// DiffFormatter adds a new formatter
func DiffFormatter(t reflect.Type, fn interface{}) DiffOption {
return DiffOption{kind: 3, t: t, fn: fn}
} }
// HTTPEndpointCases describes case for TestHTTPEndpoints // HTTPEndpointCases describes case for TestHTTPEndpoints
@@ -169,7 +195,7 @@ type ConfigurationDecodeCases []struct {
} }
// TestConfigurationDecode helps decoding configuration. It also test decoding from YAML. // TestConfigurationDecode helps decoding configuration. It also test decoding from YAML.
func TestConfigurationDecode(t *testing.T, cases ConfigurationDecodeCases) { func TestConfigurationDecode(t *testing.T, cases ConfigurationDecodeCases, options ...DiffOption) {
t.Helper() t.Helper()
for _, tc := range cases { for _, tc := range cases {
for _, fromYAML := range []bool{false, true} { for _, fromYAML := range []bool{false, true} {
@@ -208,7 +234,7 @@ func TestConfigurationDecode(t *testing.T, cases ConfigurationDecodeCases) {
t.Errorf("Decode() did not error") t.Errorf("Decode() did not error")
} }
if diff := Diff(got, tc.Expected); diff != "" && err == nil { if diff := Diff(got, tc.Expected, options...); diff != "" && err == nil {
t.Fatalf("Decode() (-got, +want):\n%s", diff) t.Fatalf("Decode() (-got, +want):\n%s", diff)
} }
}) })

View File

@@ -83,7 +83,7 @@ func TestNetworkNamesUnmarshalHook(t *testing.T) {
Configuration: func() interface{} { return gin.H{"192.0.2.1/255.0.255.0": "customer"} }, Configuration: func() interface{} { return gin.H{"192.0.2.1/255.0.255.0": "customer"} },
Error: true, Error: true,
}, },
}) }, helpers.DiffFormatter(reflect.TypeOf(helpers.SubnetMap[NetworkAttributes]{}), fmt.Sprint))
} }
func TestDefaultConfiguration(t *testing.T) { func TestDefaultConfiguration(t *testing.T) {
@@ -93,7 +93,3 @@ func TestDefaultConfiguration(t *testing.T) {
t.Fatalf("validate.Struct() error:\n%+v", err) t.Fatalf("validate.Struct() error:\n%+v", err)
} }
} }
func init() {
helpers.RegisterDiffFormatter(reflect.TypeOf(helpers.SubnetMap[NetworkAttributes]{}), fmt.Sprint)
}