/
object.go
41 lines (33 loc) · 1.12 KB
/
object.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
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package testutils
import (
"fmt"
"strings"
"github.com/go-test/deep"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
// DeepEqual compares two objects and shows diff on failure.
func DeepEqual(expected interface{}) types.GomegaMatcher {
return &DeepEqualMatcher{
Expected: expected,
}
}
type DeepEqualMatcher struct {
Expected interface{}
}
func (matcher *DeepEqualMatcher) Match(actual interface{}) (success bool, err error) {
return len(deep.Equal(actual, matcher.Expected)) == 0, nil
}
func (matcher *DeepEqualMatcher) FailureMessage(actual interface{}) (message string) {
diff := deep.Equal(actual, matcher.Expected)
if len(diff) > 0 {
return fmt.Sprintf("unexpected diff in deep equal: \n %s\n", strings.Join(diff, "\n "))
}
return format.Message(actual, "to equal", matcher.Expected)
}
func (matcher *DeepEqualMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to equal", matcher.Expected)
}