Skip to content

Commit

Permalink
helper: Compare Rule types with custom comparer
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed May 24, 2020
1 parent 1505d63 commit bed0055
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions helper/testing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helper

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -30,7 +31,7 @@ func AssertIssues(t *testing.T, expected Issues, actual Issues) {
opts := []cmp.Option{
// Byte field will be ignored because it's not important in tests such as positions
cmpopts.IgnoreFields(hcl.Pos{}, "Byte"),
cmpopts.IgnoreFields(Issue{}, "Rule"),
ruleComparer(),
}
if !cmp.Equal(expected, actual, opts...) {
t.Fatalf("Expected issues are not matched:\n %s\n", cmp.Diff(expected, actual, opts...))
Expand All @@ -41,9 +42,17 @@ func AssertIssues(t *testing.T, expected Issues, actual Issues) {
func AssertIssuesWithoutRange(t *testing.T, expected Issues, actual Issues) {
opts := []cmp.Option{
cmpopts.IgnoreFields(Issue{}, "Range"),
cmpopts.IgnoreFields(Issue{}, "Rule"),
ruleComparer(),
}
if !cmp.Equal(expected, actual, opts...) {
t.Fatalf("Expected issues are not matched:\n %s\n", cmp.Diff(expected, actual, opts...))
}
}

// ruleComparer returns a Comparer func that checks that two rule interfaces
// have the same underlying type. It does not compare struct fields.
func ruleComparer() cmp.Option {
return cmp.Comparer(func(x, y Rule) bool {
return reflect.TypeOf(x) == reflect.TypeOf(y)
})
}

0 comments on commit bed0055

Please sign in to comment.