Skip to content

Support ranges over maps #14

Closed
Closed
@kevgo

Description

@kevgo

Thanks for this awesome linter! It has tripled the speed of my unit tests and I use it daily. I noticed an apparent false positive when using maps as test data. Consider this code snippet:

func Upper(text string) string {
	return strings.ToUpper(text)
}

func TestUpper(t *testing.T) {
	t.Parallel()
	tests := map[string]string{
		"one": "ONE",
		"two": "TWO",
	}
	for test := range tests {
		t.Run(test, func(t *testing.T) {
			t.Parallel()
			have := Upper(test)
			want := tests[test]
			assert.Equal(t, want, have)
		})
	}
}

paralleltest produces this linter error which seems a false positive:

Range statement for test TestUpper does not use range value in test Run

Please note that it is also common to destructure the test variable into give, want. It would be phantastic if paralleltest would supported that as well. Here is an example:

func Upper(text string) string {
	return strings.ToUpper(text)
}

func TestUpper(t *testing.T) {
	t.Parallel()
	tests := map[string]string{
		"one": "ONE",
		"two": "TWO",
	}
	for give, want := range tests {
		t.Run(give, func(t *testing.T) {
			t.Parallel()
			have := Upper(give)
			assert.Equal(t, want, have)
		})
	}
}

Activity

kunwardeep

kunwardeep commented on Feb 4, 2022

@kunwardeep
Owner

Hey @kevgo. I am glad this linter has helped you. Would you please be able to raise a PR so that we can put a fix in ?

kunwardeep

kunwardeep commented on Jun 3, 2022

@kunwardeep
Owner

Hey @kevgo would you be able to test this with the latest fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kevgo@kunwardeep

        Issue actions

          Support ranges over maps · Issue #14 · kunwardeep/paralleltest