From d7d436a8099039a3a0b68ebe5e4965aaa02d43f0 Mon Sep 17 00:00:00 2001 From: vediatoni Date: Wed, 8 Jun 2022 14:35:01 +0200 Subject: [PATCH] Added unit test for `CallArgs()` --- codegen/field_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/codegen/field_test.go b/codegen/field_test.go index 13177049e3..7395827404 100644 --- a/codegen/field_test.go +++ b/codegen/field_test.go @@ -10,6 +10,7 @@ import ( "github.com/99designs/gqlgen/codegen/config" "github.com/stretchr/testify/require" + ast2 "github.com/vektah/gqlparser/v2/ast" ) func TestFindField(t *testing.T) { @@ -114,3 +115,69 @@ func TestEqualFieldName(t *testing.T) { }) } } + +func TestField_CallArgs(t *testing.T) { + tt := []struct { + Name string + Field + Expected string + }{ + { + Name: "Field with method that has context, and two args (string, interface)", + Field: Field{ + MethodHasContext: true, + Args: []*FieldArgument{ + { + ArgumentDefinition: &ast2.ArgumentDefinition{ + Name: "test", + }, + TypeReference: &config.TypeReference{ + GO: &types.Interface{}, + }, + }, + { + ArgumentDefinition: &ast2.ArgumentDefinition{ + Name: "test2", + }, + TypeReference: &config.TypeReference{ + GO: types.Typ[types.Int], + }, + }, + }, + }, + Expected: `ctx, ` + ` + func () interface{} { + if fc.Args["test"] == nil { + return nil + } + return fc.Args["test"].(interface{}) + }(), fc.Args["test2"].(int)`, + }, + { + Name: "Resolver field that isn't root object with single int argument", + Field: Field{ + Object: &Object{ + Root: false, + }, + IsResolver: true, + Args: []*FieldArgument{ + { + ArgumentDefinition: &ast2.ArgumentDefinition{ + Name: "test", + }, + TypeReference: &config.TypeReference{ + GO: types.Typ[types.Int], + }, + }, + }, + }, + Expected: `rctx, obj, fc.Args["test"].(int)`, + }, + } + + for _, tc := range tt { + t.Run(tc.Name, func(t *testing.T) { + require.Equal(t, tc.CallArgs(), tc.Expected) + }) + } +}