-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: argument comparison between two sets of args
Aligns with <https://github.com/graphql/graphql-js/blob/main/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts#L626-L642>. Added tests.
- Loading branch information
Showing
2 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
validator/rules/overlapping_fields_can_be_merged_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package validator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/vektah/gqlparser/v2/ast" | ||
) | ||
|
||
func Test_sameArguments(t *testing.T) { | ||
tests := map[string]struct { | ||
args func() (args1, args2 []*ast.Argument) | ||
result bool | ||
}{ | ||
"both argument lists empty": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return nil, nil | ||
}, | ||
result: true, | ||
}, | ||
"args 1 empty, args 2 not": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return nil, []*ast.Argument{ | ||
{ | ||
Name: "thing", | ||
Value: &ast.Value{Raw: "a thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
} | ||
}, | ||
result: false, | ||
}, | ||
"args 2 empty, args 1 not": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return []*ast.Argument{ | ||
{ | ||
Name: "thing", | ||
Value: &ast.Value{Raw: "a thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
}, nil | ||
}, | ||
result: false, | ||
}, | ||
"args 1 mismatches args 2 names": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return []*ast.Argument{ | ||
{ | ||
Name: "thing1", | ||
Value: &ast.Value{Raw: "1 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
}, | ||
[]*ast.Argument{ | ||
{ | ||
Name: "thing2", | ||
Value: &ast.Value{Raw: "2 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
} | ||
}, | ||
result: false, | ||
}, | ||
"args 1 mismatches args 2 values": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return []*ast.Argument{ | ||
{ | ||
Name: "thing1", | ||
Value: &ast.Value{Raw: "1 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
}, | ||
[]*ast.Argument{ | ||
{ | ||
Name: "thing1", | ||
Value: &ast.Value{Raw: "2 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
} | ||
}, | ||
result: false, | ||
}, | ||
"args 1 matches args 2 names and values": { | ||
args: func() (args1 []*ast.Argument, args2 []*ast.Argument) { | ||
return []*ast.Argument{ | ||
{ | ||
Name: "thing1", | ||
Value: &ast.Value{Raw: "1 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
}, | ||
[]*ast.Argument{ | ||
{ | ||
Name: "thing1", | ||
Value: &ast.Value{Raw: "1 thing"}, | ||
Position: &ast.Position{}, | ||
}, | ||
} | ||
}, | ||
result: true, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
args1, args2 := tc.args() | ||
|
||
resp := sameArguments(args1, args2) | ||
|
||
if resp != tc.result { | ||
t.Fatalf("Expected %t got %t", tc.result, resp) | ||
} | ||
}) | ||
} | ||
} |