diff --git a/lang.go b/lang.go index 23fe68d..62d6c64 100644 --- a/lang.go +++ b/lang.go @@ -47,6 +47,10 @@ func SimpleTypeName(t reflect.Type) string { // This doesn't include "u" as a vowel since words like "user" should be "a user" // and not "an user". func TypeNameStartsWithVowel(t string) bool { + if t == "" { + return false + } + t = strings.TrimLeft(t, "*") switch strings.ToLower(t[:1]) { diff --git a/lang_test.go b/lang_test.go index 4467109..7b73552 100644 --- a/lang_test.go +++ b/lang_test.go @@ -96,3 +96,91 @@ func TestSimpleTypeName(t *testing.T) { require.Equal(t, test.expected, name) } } + +// Tests that TypeNameStartsWithVowel returns the expected result. +func TestTypeNameStartsWithVowel(t *testing.T) { + tests := []struct { + name string + expected bool + }{ + { + name: "", + expected: false, + }, + { + name: "a", + expected: true, + }, + { + name: "e", + expected: true, + }, + { + name: "i", + expected: true, + }, + { + name: "o", + expected: true, + }, + { + name: "u", + expected: false, + }, + { + name: "int", + expected: true, + }, + { + name: "*int", + expected: true, + }, + { + name: "string", + expected: false, + }, + } + + for _, test := range tests { + actual := schema.TypeNameStartsWithVowel(test.name) + require.Equal(t, test.expected, actual) + } +} + +// Tests that TypeNameStartsWithVowel returns the expected result. +func TestTypeNameWithArticle(t *testing.T) { + tests := []struct { + name string + expected string + }{ + { + name: "float", + expected: "a float", + }, + { + name: "int", + expected: "an int", + }, + { + name: "*string", + expected: "a *string", + }, + { + name: "*int", + expected: "an *int", + }, + { + name: "null", + expected: "null", + }, + { + name: "User", + expected: "a User", + }, + } + + for _, test := range tests { + actual := schema.TypeNameWithArticle(test.name) + require.Equal(t, test.expected, actual) + } +}