Update GetLanguageID to return a found boolean per code review

This commit is contained in:
Luke Francl 2021-04-22 16:55:42 -07:00
parent eb043e80a8
commit cabfdaffc0
2 changed files with 16 additions and 12 deletions

View File

@ -491,11 +491,13 @@ func GetLanguageExtensions(language string) []string {
}
// GetLanguageID returns the ID for the language. IDs are assigned by GitHub.
// The input must be the canonical language name. Aliases are not supported.
//
// NOTE: The zero value (0) is a valid language ID, so this API has no way to
// distinguish an invalid language name and a match on the language with ID 0.
func GetLanguageID(language string) int {
return data.IDByLanguage[language]
// NOTE: The zero value (0) is a valid language ID, so this API mimics the Go
// map API. Use the second return value to check if the language was found.
func GetLanguageID(language string) (int, bool) {
id, ok := data.IDByLanguage[language]
return id, ok
}
// Type represent language's type. Either data, programming, markup, prose, or unknown.

View File

@ -550,18 +550,20 @@ func (s *EnryTestSuite) TestGetLanguageID() {
name string
language string
expectedID int
found bool
}{
{name: "TestGetLanguageID_1", language: "1C Enterprise", expectedID: 0},
{name: "TestGetLanguageID_2", language: "BestLanguageEver", expectedID: 0}, // Cannot distinguish from 1C Enterprise
{name: "TestGetLanguageID_3", language: "C++", expectedID: 43},
{name: "TestGetLanguageID_5", language: "Objective-C", expectedID: 257},
{name: "TestGetLanguageID_6", language: "golang", expectedID: 0}, // Aliases are not supported
{name: "TestGetLanguageID_7", language: "Go", expectedID: 132},
{name: "TestGetLanguageID_8", language: "Makefile", expectedID: 220},
{name: "TestGetLanguageID_1", language: "1C Enterprise", expectedID: 0, found: true},
{name: "TestGetLanguageID_2", language: "BestLanguageEver", expectedID: 0, found: false},
{name: "TestGetLanguageID_3", language: "C++", expectedID: 43, found: true},
{name: "TestGetLanguageID_5", language: "Objective-C", expectedID: 257, found: true},
{name: "TestGetLanguageID_6", language: "golang", expectedID: 0, found: false}, // Aliases are not supported
{name: "TestGetLanguageID_7", language: "Go", expectedID: 132, found: true},
{name: "TestGetLanguageID_8", language: "Makefile", expectedID: 220, found: true},
}
for _, test := range tests {
id := GetLanguageID(test.language)
id, found := GetLanguageID(test.language)
assert.Equal(s.T(), test.expectedID, id, fmt.Sprintf("%v: id = %v, expected: %v", test.name, id, test.expectedID))
assert.Equal(s.T(), test.found, found, fmt.Sprintf("%v: found = %t, expected: %t", test.name, found, test.found))
}
}