OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / johngb / langreg / language_test.go
1 package langreg
2
3 import (
4         "testing"
5
6         . "github.com/smartystreets/goconvey/convey"
7 )
8
9 type langTestInfo struct {
10         code            string
11         expectedEn      string
12         expectedNat     string
13         errExpected     bool
14         testDescription string
15 }
16
17 var langTests = []langTestInfo{
18         {"en", "English", "English", false, "code with ascii character set"},
19         {"EN", "", "", true, "code with uppercase ascii characters"},
20         {"iu", "Inuktitut", "ᐃᓄᒃᑎᑐᑦ", false, "code with non-ascii Unicode character set"},
21         {"  en ", "", "", true, "code with leading and trailng space"},
22         {"\nen ", "", "", true, "code with leading and trailing whitespace"},
23         {"zzz", "", "", true, "code that is too long"},
24         {"z", "", "", true, "code that is too short"},
25         {"zz", "", "", true, "code that is invalid"},
26         {"*z", "", "", true, "code with invalid characters"},
27         {"73", "", "", true, "code with numbers"},
28 }
29
30 func TestLanguageCodeInfo(t *testing.T) {
31         for _, tt := range langTests {
32                 Convey("Given a "+tt.testDescription, t, func() {
33                         actualEn, actualNat, actualErr := LangCodeInfo(tt.code)
34                         So(actualEn, ShouldEqual, tt.expectedEn)
35                         So(actualNat, ShouldEqual, tt.expectedNat)
36                         if !tt.errExpected {
37                                 So(actualErr, ShouldBeNil)
38                         } else {
39                                 So(actualErr, ShouldNotBeNil)
40                         }
41                 })
42         }
43 }
44
45 func TestIsLanguageCode(t *testing.T) {
46         for _, tt := range langTests {
47                 Convey("Given a "+tt.testDescription, t, func() {
48                         actualIsValid := IsValidLanguageCode(tt.code)
49                         So(!actualIsValid, ShouldEqual, tt.errExpected)
50                 })
51         }
52 }
53
54 func TestLangEnglishName(t *testing.T) {
55         for _, tt := range langTests {
56                 Convey("Given a "+tt.testDescription, t, func() {
57                         actualEnName, actualErr := LangEnglishName(tt.code)
58                         So(actualEnName, ShouldEqual, tt.expectedEn)
59                         if !tt.errExpected {
60                                 So(actualErr, ShouldBeNil)
61                         } else {
62                                 So(actualErr, ShouldNotBeNil)
63                         }
64                 })
65         }
66 }
67
68 func TestLangNativeName(t *testing.T) {
69         for _, tt := range langTests {
70                 Convey("Given a "+tt.testDescription, t, func() {
71                         actualNatName, actualErr := LangNativeName(tt.code)
72                         So(actualNatName, ShouldEqual, tt.expectedNat)
73                         if !tt.errExpected {
74                                 So(actualErr, ShouldBeNil)
75                         } else {
76                                 So(actualErr, ShouldNotBeNil)
77                         }
78                 })
79         }
80 }
81
82 func BenchmarkLangCodeInfo(b *testing.B) {
83         for n := 0; n < b.N; n++ {
84                 _, _, err := LangCodeInfo("zu")
85                 if err != nil {
86                         b.Error(err.Error())
87                 }
88         }
89 }
90
91 func BenchmarkIsValidLanguageCode(b *testing.B) {
92         for n := 0; n < b.N; n++ {
93                 isValid := IsValidLanguageCode("zu")
94                 if !isValid {
95                         b.Error("invalid language code")
96                 }
97         }
98 }
99
100 func BenchmarkLangEnglishName(b *testing.B) {
101         for n := 0; n < b.N; n++ {
102                 _, err := LangEnglishName("zu")
103                 if err != nil {
104                         b.Error(err.Error())
105                 }
106         }
107 }
108
109 func BenchmarkLangNativeName(b *testing.B) {
110         for n := 0; n < b.N; n++ {
111                 _, err := LangEnglishName("zu")
112                 if err != nil {
113                         b.Error(err.Error())
114                 }
115         }
116 }