|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/onsi/gomega" |
| 8 | + "github.com/onsi/gomega/format" |
| 9 | + "github.com/onsi/gomega/gstruct" |
| 10 | + "github.com/onsi/gomega/types" |
| 11 | +) |
| 12 | + |
| 13 | +type idMatcher struct { |
| 14 | + expected interface{} |
| 15 | +} |
| 16 | + |
| 17 | +var ( |
| 18 | + _ types.GomegaMatcher = &idMatcher{} |
| 19 | + _ format.GomegaStringer = &idMatcher{} |
| 20 | +) |
| 21 | + |
| 22 | +func (m *idMatcher) Match(actual interface{}) (bool, error) { |
| 23 | + v := reflect.ValueOf(m.expected) |
| 24 | + id := v.FieldByName("ID").String() |
| 25 | + |
| 26 | + matcher := &gstruct.FieldsMatcher{ |
| 27 | + Fields: gstruct.Fields{ |
| 28 | + "ID": gomega.Equal(id), |
| 29 | + }, |
| 30 | + IgnoreExtras: true, |
| 31 | + } |
| 32 | + |
| 33 | + return matcher.Match(actual) |
| 34 | +} |
| 35 | + |
| 36 | +func (m *idMatcher) FailureMessage(actual interface{}) string { |
| 37 | + return fmt.Sprintf("Expected:\n%s\nto have the same ID as:\n%s", format.Object(actual, 1), format.Object(m.expected, 1)) |
| 38 | +} |
| 39 | + |
| 40 | +func (m *idMatcher) NegatedFailureMessage(actual interface{}) string { |
| 41 | + return fmt.Sprintf("Expected:\n%s\nnot to have the same ID as:\n%s", format.Object(actual, 1), format.Object(m.expected, 1)) |
| 42 | +} |
| 43 | + |
| 44 | +func (m *idMatcher) GomegaString() string { |
| 45 | + return fmt.Sprintf("ID match for:\n%s", format.Object(m.expected, 1)) |
| 46 | +} |
| 47 | + |
| 48 | +func IDOf(expected interface{}) types.GomegaMatcher { |
| 49 | + return &idMatcher{expected: expected} |
| 50 | +} |
| 51 | + |
| 52 | +func ConsistOfIDs[T any](expected []T) types.GomegaMatcher { |
| 53 | + matchers := make([]types.GomegaMatcher, len(expected)) |
| 54 | + for i := range expected { |
| 55 | + matchers[i] = IDOf(expected[i]) |
| 56 | + } |
| 57 | + |
| 58 | + return gomega.ConsistOf(matchers) |
| 59 | +} |
0 commit comments