Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Jun 2, 2019
1 parent d43c6cb commit d8857a6
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
21 changes: 21 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package iso6391

import "testing"

func TestCodes(t *testing.T) {
if len(Codes) != 184 {
t.Fatalf("Codes must have 184 entries, but was: %v", len(Codes))
}
}

func TestNames(t *testing.T) {
if len(Names) != 184 {
t.Fatalf("Names must have 184 entries, but was: %v", len(Codes))
}
}

func TestNativeNames(t *testing.T) {
if len(NativeNames) != 184 {
t.Fatalf("NativeNames must have 184 entries, but was: %v", len(Codes))
}
}
22 changes: 22 additions & 0 deletions iso6391.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ func NativeName(code string) string {
return FromCode(code).NativeName
}

// CodeForName returns the language code for given name.
func CodeForName(name string) string {
for code, lang := range Languages {
if lang.Name == name {
return code
}
}

return ""
}

// CodeForNativeName returns the language code for given native name.
func CodeForNativeName(name string) string {
for code, lang := range Languages {
if lang.NativeName == name {
return code
}
}

return ""
}

// ValidCode returns true if the given code is a valid ISO 639-1 language code.
// The code must be passed in lowercase.
func ValidCode(code string) bool {
Expand Down
173 changes: 173 additions & 0 deletions iso6391_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package iso6391

import "testing"

func TestFromCode(t *testing.T) {
lang := FromCode("en")

if lang.Name != "English" || lang.NativeName != "English" {
t.Fatalf("Expected English, but was: %v", lang)
}

lang = FromCode("zh")

if lang.Name != "Chinese" || lang.NativeName != "中文" {
t.Fatalf("Expected Chinese, but was: %v", lang)
}

lang = FromCode("xx")

if lang.Name != "" || lang.NativeName != "" {
t.Fatalf("Expected no valid language, but was: %v", lang)
}
}

func TestFromName(t *testing.T) {
lang := FromName("English")

if lang.Code != "en" || lang.NativeName != "English" {
t.Fatalf("Expected English, but was: %v", lang)
}

lang = FromName("Chinese")

if lang.Code != "zh" || lang.NativeName != "中文" {
t.Fatalf("Expected Chinese, but was: %v", lang)
}

lang = FromName("Neverheardof")

if lang.Code != "" || lang.NativeName != "" {
t.Fatalf("Expected no valid language, but was: %v", lang)
}
}

func TestFromNativeName(t *testing.T) {
lang := FromNativeName("English")

if lang.Code != "en" || lang.Name != "English" {
t.Fatalf("Expected English, but was: %v", lang)
}

lang = FromNativeName("中文")

if lang.Code != "zh" || lang.Name != "Chinese" {
t.Fatalf("Expected Chinese, but was: %v", lang)
}

lang = FromNativeName("Neverheardof")

if lang.Code != "" || lang.Name != "" {
t.Fatalf("Expected no valid language, but was: %v", lang)
}
}

func TestName(t *testing.T) {
name := Name("en")

if name != "English" {
t.Fatalf("Expected English, but was: %v", name)
}

name = Name("xx")

if name != "" {
t.Fatalf("Expected no valid name, but was: %v", name)
}
}

func TestNativeName(t *testing.T) {
name := NativeName("zh")

if name != "中文" {
t.Fatalf("Expected Chinese, but was: %v", name)
}

name = NativeName("xx")

if name != "" {
t.Fatalf("Expected no valid native name, but was: %v", name)
}
}

func TestCodeForName(t *testing.T) {
code := CodeForName("English")

if code != "en" {
t.Fatalf("Expected English, but was: %v", code)
}

code = CodeForName("Chinese")

if code != "zh" {
t.Fatalf("Expected Chinese, but was: %v", code)
}

code = CodeForName("Neverheardof")

if code != "" {
t.Fatalf("Expected no valid code, but was: %v", code)
}
}

func TestCodeForNativeName(t *testing.T) {
code := CodeForNativeName("English")

if code != "en" {
t.Fatalf("Expected English, but was: %v", code)
}

code = CodeForNativeName("中文")

if code != "zh" {
t.Fatalf("Expected Chinese, but was: %v", code)
}

code = CodeForNativeName("Neverheardof")

if code != "" {
t.Fatalf("Expected no valid code, but was: %v", code)
}
}

func TestValidCode(t *testing.T) {
if !ValidCode("en") {
t.Fatal("Code must be valid")
}

if !ValidCode("zh") {
t.Fatal("Code must be valid")
}

if ValidCode("xx") {
t.Fatal("Code must be invalid")
}
}

func TestValidName(t *testing.T) {
if !ValidName("English") {
t.Fatal("Name must be valid")
}

if !ValidName("Chinese") {
t.Fatal("Name must be valid")
}

if ValidName("Neverheardof") {
t.Fatal("Name must be invalid")
}
}

func TestValidNativeName(t *testing.T) {
if !ValidNativeName("English") {
t.Fatal("Native name must be valid")
}

if !ValidNativeName("中文") {
t.Fatal("Native name must be valid")
}

if ValidNativeName("Neverheardof") {
t.Fatal("Native name must be invalid")
}
}

0 comments on commit d8857a6

Please sign in to comment.