Skip to content

Add matcher for structs with ignored order in nested fields #71

@sebastianbuechler

Description

@sebastianbuechler

In PR golang/mock#546 we got a matcher that ignores the order of a slice or array. Sometimes the order can't be controlled properly it makes sense to have this matcher. But it is only capable of ignoring the order if the struct is of type slice or array, and not nested.

I would propose to add a matcher that will return true for unordered nested structs as well. Example unit test:

func TestOrderlessMatcher(t *testing.T) {
	testCases := []struct {
		expected interface{}
		value    interface{}
		matches  bool
	}{
		{
			struct{ List []string }{List: []string{"Value1", "Value2"}},
			struct{ List []string }{List: []string{"Value2", "Value1"}},
			true,
		},
	}
	for i, testCase := range testCases {
		t.Run(fmt.Sprintf("Test #%d", i), func(t *testing.T) {
			assert := assert.New(t)
			m := matchers.NewOrderlessMatcher(testCase.expected)
			assert.Equal(testCase.matches, m.Matches(testCase.value))
		})
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions