From cbe036543ca23a8b90832201cc405702b339d90a Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sun, 4 Aug 2024 15:36:28 +0800 Subject: [PATCH] refactor: Remove convery and assert (#75) * refactor: Remove convery and assert Signed-off-by: Xuanwo * format code Signed-off-by: Xuanwo --------- Signed-off-by: Xuanwo --- go.mod | 11 --- go.sum | 29 ------- locale_darwin.go | 50 ++++++------ locale_darwin_test.go | 20 ++--- locale_posix_test.go | 144 +++++++++++++++-------------------- locale_shared_test.go | 168 +++++++++++++++++------------------------ locale_test.go | 93 ++++++----------------- locale_windows_test.go | 22 +++--- 8 files changed, 196 insertions(+), 341 deletions(-) diff --git a/go.mod b/go.mod index b012ae3..8112e66 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,6 @@ module github.com/Xuanwo/go-locale go 1.17 require ( - github.com/smartystreets/goconvey v1.7.2 - github.com/stretchr/testify v1.9.0 golang.org/x/sys v0.22.0 golang.org/x/text v0.16.0 ) - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect - github.com/jtolds/gls v4.20.0+incompatible // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/smartystreets/assertions v1.2.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/go.sum b/go.sum index c4cc264..f2f7b27 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,4 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= -github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= -github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -32,7 +10,6 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -78,15 +55,9 @@ golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/locale_darwin.go b/locale_darwin.go index 97fba70..386c0c8 100644 --- a/locale_darwin.go +++ b/locale_darwin.go @@ -22,8 +22,8 @@ var detectors = []detector{ // - global AppleLanguages // // ref: -// - Apple Developer Guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html -// - Homebrew: https://github.com/Homebrew/brew/pull/7940 +// - Apple Developer Guide: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html +// - Homebrew: https://github.com/Homebrew/brew/pull/7940 func detectViaDefaultsSystem() ([]string, error) { // Read user's apple locale setting. m, err := parseDefaultsSystemAppleLocale("-g") @@ -73,28 +73,30 @@ func parseDefaultsSystemAppleLocale(domain string) ([]string, error) { // Output should be like: // // ( -// en, -// ja, -// fr, -// de, -// es, -// it, -// pt, -// "pt-PT", -// nl, -// sv, -// nb, -// da, -// fi, -// ru, -// pl, -// "zh-Hans", -// "zh-Hant", -// ko, -// ar, -// cs, -// hu, -// tr +// +// en, +// ja, +// fr, +// de, +// es, +// it, +// pt, +// "pt-PT", +// nl, +// sv, +// nb, +// da, +// fi, +// ru, +// pl, +// "zh-Hans", +// "zh-Hant", +// ko, +// ar, +// cs, +// hu, +// tr +// // ) func parseDefaultsSystemAppleLanguages(domain string) ([]string, error) { cmd := exec.Command("defaults", "read", domain, "AppleLanguages") diff --git a/locale_darwin_test.go b/locale_darwin_test.go index 91b7199..6e4b821 100644 --- a/locale_darwin_test.go +++ b/locale_darwin_test.go @@ -2,20 +2,16 @@ package locale import ( "testing" - - . "github.com/smartystreets/goconvey/convey" ) func TestDetectViaUserDefaultsSystem(t *testing.T) { - Convey("detect via user defaults system", t, func() { - langs, err := detectViaDefaultsSystem() + langs, err := detectViaDefaultsSystem() - t.Logf("langs: %v", langs) - Convey("The error should not be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The langs should not be empty", func() { - So(langs, ShouldNotBeEmpty) - }) - }) + t.Logf("langs: %v", langs) + if err != nil { + t.Errorf("Expected nil error, got: %v", err) + } + if len(langs) == 0 { + t.Error("Expected non-empty langs, got empty slice") + } } diff --git a/locale_posix_test.go b/locale_posix_test.go index 5c6e44f..e7d2744 100644 --- a/locale_posix_test.go +++ b/locale_posix_test.go @@ -7,95 +7,71 @@ import ( "os" "path" "testing" - - . "github.com/smartystreets/goconvey/convey" ) -func TestGetLocaleConfPath(t *testing.T) { - Convey("get locale conf path", t, func() { - // Make sure env has clear before current test. - setupEnv() - - Reset(func() { - // Reset all env after every Convey. - setupEnv() - }) - - Convey("When user set XDG_CONFIG_HOME", func() { - tmpDir := setupLocaleConf("locale.conf") - Reset(func() { - _ = os.RemoveAll(tmpDir) - }) - - err := os.Setenv("XDG_CONFIG_HOME", tmpDir) - if err != nil { - t.Error(err) - } - - fp := getLocaleConfPath() - - Convey("The path should be equal", func() { - So(fp, ShouldEqual, path.Join(tmpDir, "locale.conf")) - }) - }) - - Convey("When user set HOME", func() { - tmpDir := setupLocaleConf(".config/locale.conf") - Reset(func() { - _ = os.RemoveAll(tmpDir) - }) - - err := os.Setenv("HOME", tmpDir) - if err != nil { - t.Error(err) - } - - fp := getLocaleConfPath() - - Convey("The path should be equal", func() { - So(fp, ShouldEqual, path.Join(tmpDir, ".config/locale.conf")) - }) - }) - - Convey("When fallback to system level locale.conf", func() { - var localeExist bool - _, err := os.Stat("/etc/locale.conf") - if err == nil { - localeExist = true - } +func TestXDGConfigHome(t *testing.T) { + setupEnv() + tmpDir := setupLocaleConf("locale.conf") + defer os.RemoveAll(tmpDir) + + err := os.Setenv("XDG_CONFIG_HOME", tmpDir) + if err != nil { + t.Fatal(err) + } + + fp := getLocaleConfPath() + expected := path.Join(tmpDir, "locale.conf") + if fp != expected { + t.Errorf("Expected path %s, got %s", expected, fp) + } +} - fp := getLocaleConfPath() +func TestHOME(t *testing.T) { + setupEnv() + tmpDir := setupLocaleConf(".config/locale.conf") + defer os.RemoveAll(tmpDir) + + err := os.Setenv("HOME", tmpDir) + if err != nil { + t.Fatal(err) + } + + fp := getLocaleConfPath() + expected := path.Join(tmpDir, ".config/locale.conf") + if fp != expected { + t.Errorf("Expected path %s, got %s", expected, fp) + } +} - Convey("The path should be equal", func() { - So(fp == "/etc/locale.conf", ShouldEqual, localeExist) - }) - }) - }) +func TestFallbackToSystem(t *testing.T) { + setupEnv() + var localeExist bool + _, err := os.Stat("/etc/locale.conf") + if err == nil { + localeExist = true + } + + fp := getLocaleConfPath() + if (fp == "/etc/locale.conf") != localeExist { + t.Errorf("Expected path to be /etc/locale.conf: %v, got: %s", localeExist, fp) + } } func TestDetectViaLocaleConf(t *testing.T) { - Convey("detect via locale conf", t, func() { - setupEnv() - Reset(func() { - setupEnv() - }) - - tmpDir := setupLocaleConf("locale.conf") - Reset(func() { - _ = os.RemoveAll(tmpDir) - }) - err := os.Setenv("XDG_CONFIG_HOME", tmpDir) - if err != nil { - t.Error(err) - } - - lang, err := detectViaLocaleConf() - - Convey("The error should be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The lang should not be empty", func() { - So(lang, ShouldNotBeEmpty) - }) - }) + setupEnv() + tmpDir := setupLocaleConf("locale.conf") + defer os.RemoveAll(tmpDir) + + err := os.Setenv("XDG_CONFIG_HOME", tmpDir) + if err != nil { + t.Fatal(err) + } + + lang, err := detectViaLocaleConf() + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + if len(lang) == 0 { + t.Error("Expected non-empty lang, got empty string") + } } diff --git a/locale_shared_test.go b/locale_shared_test.go index e78ff09..8ec3c18 100644 --- a/locale_shared_test.go +++ b/locale_shared_test.go @@ -3,130 +3,98 @@ package locale import ( "errors" "os" + "reflect" "testing" - - . "github.com/smartystreets/goconvey/convey" ) func TestDetectViaEnvLanguage(t *testing.T) { - Convey("detect via env language", t, func() { - // Make sure env has clear before current test. - setupEnv() - - Reset(func() { - // Reset all env after every Convey. + tests := []struct { + name string + envValue string + want []string + wantErr error + }{ + {"Valid single value", "en_US", []string{"en_US"}, nil}, + {"Multiple values", "en_US:zh_CN", []string{"en_US", "zh_CN"}, nil}, + {"Empty value", "", nil, ErrNotDetected}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { setupEnv() - }) + defer setupEnv() - Convey("When LANGUAGE has valid value", func() { - err := os.Setenv("LANGUAGE", "en_US") + err := os.Setenv("LANGUAGE", tt.envValue) if err != nil { - t.Error(err) + t.Fatal(err) } - lang, err := detectViaEnvLanguage() + got, err := detectViaEnvLanguage() + t.Logf("langs: %v", got) - t.Logf("langs: %v", lang) - Convey("The error should not be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The lang should not be equal", func() { - So(lang, ShouldResemble, []string{"en_US"}) - }) - }) - - Convey("When LANGUAGE has multiple value", func() { - err := os.Setenv("LANGUAGE", "en_US:zh_CN") - if err != nil { - t.Error(err) + if !errors.Is(err, tt.wantErr) { + t.Errorf("detectViaEnvLanguage() error = %v, wantErr %v", err, tt.wantErr) } - - lang, err := detectViaEnvLanguage() - - t.Logf("langs: %v", lang) - Convey("The error should not be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The lang should not be equal", func() { - So(lang, ShouldResemble, []string{"en_US", "zh_CN"}) - }) - }) - - Convey("When LANGUAGE is empty", func() { - err := os.Setenv("LANGUAGE", "") - if err != nil { - t.Error(err) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("detectViaEnvLanguage() = %v, want %v", got, tt.want) } - - lang, err := detectViaEnvLanguage() - - t.Logf("langs: %v", lang) - Convey("The error should be ErrNotDetected", func() { - So(errors.Is(err, ErrNotDetected), ShouldBeTrue) - }) - Convey("The lang should be empty", func() { - So(lang, ShouldBeEmpty) - }) }) - }) + } } func TestDetectViaEnvLc(t *testing.T) { - Convey("detect via env language", t, func() { - // Make sure env has clear before current test. - setupEnv() - - Reset(func() { - // Reset all env after every Convey. + tests := []struct { + name string + setEnv bool + envKey string + envVal string + want []string + wantErr error + }{ + {"LC_ALL set", true, "LC_ALL", "en_US.UTF-8", []string{"en_US"}, nil}, + {"No LC env set", false, "", "", nil, ErrNotDetected}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { setupEnv() - }) + defer setupEnv() - Convey("When LC_ALL has been set", func() { - err := os.Setenv("LC_ALL", "en_US.UTF-8") - if err != nil { - t.Error(err) + if tt.setEnv { + err := os.Setenv(tt.envKey, tt.envVal) + if err != nil { + t.Fatal(err) + } } - lang, err := detectViaEnvLc() + got, err := detectViaEnvLc() + t.Logf("langs: %v", got) - t.Logf("langs: %v", lang) - Convey("The error should not be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The lang should not be equal", func() { - So(lang, ShouldResemble, []string{"en_US"}) - }) - }) - - Convey("When no LC env has been set", func() { - lang, err := detectViaEnvLc() - - Convey("The error should be ErrNotDetected", func() { - So(errors.Is(err, ErrNotDetected), ShouldBeTrue) - }) - Convey("The lang should be empty", func() { - So(lang, ShouldBeEmpty) - }) + if !errors.Is(err, tt.wantErr) { + t.Errorf("detectViaEnvLc() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("detectViaEnvLc() = %v, want %v", got, tt.want) + } }) - }) + } } func TestParseEnvLc(t *testing.T) { - Convey("parse env lc", t, func() { - Convey("When input en_US.UTF-8", func() { - x := parseEnvLc("en_US.UTF-8") - - Convey("The lang should be en_US", func() { - So(x, ShouldEqual, "en_US") - }) - }) - - Convey("When input C.UTF-8", func() { - x := parseEnvLc("C.UTF-8") - - Convey("The lang should be en_US", func() { - So(x, ShouldEqual, "en_US") - }) + tests := []struct { + name string + input string + want string + }{ + {"en_US.UTF-8", "en_US.UTF-8", "en_US"}, + {"C.UTF-8", "C.UTF-8", "en_US"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := parseEnvLc(tt.input); got != tt.want { + t.Errorf("parseEnvLc() = %v, want %v", got, tt.want) + } }) - }) + } } diff --git a/locale_test.go b/locale_test.go index c5c3d20..3c341c3 100644 --- a/locale_test.go +++ b/locale_test.go @@ -2,10 +2,10 @@ package locale import ( "errors" + "reflect" "sync" "testing" - "github.com/stretchr/testify/assert" "golang.org/x/text/language" ) @@ -36,7 +36,7 @@ func (l *mock) set(s []string, e error) { func TestInternalDetect(t *testing.T) { detectors = []detector{mockLang.get} - teseerr := errors.New("test error") + testErr := errors.New("test error") tests := []struct { name string mockString []string @@ -44,27 +44,9 @@ func TestInternalDetect(t *testing.T) { expectString []string expectError error }{ - { - "normal", - []string{"en_US"}, - nil, - []string{"en_US"}, - nil, - }, - { - "not detected", - []string(nil), - ErrNotDetected, - []string(nil), - ErrNotDetected, - }, - { - "not detected", - []string(nil), - teseerr, - []string(nil), - teseerr, - }, + {"normal", []string{"en_US"}, nil, []string{"en_US"}, nil}, + {"not detected", nil, ErrNotDetected, nil, ErrNotDetected}, + {"other error", nil, testErr, nil, testErr}, } for _, tt := range tests { @@ -72,12 +54,11 @@ func TestInternalDetect(t *testing.T) { mockLang.set(tt.mockString, tt.mockError) lang, err := detect() - if tt.expectError != nil { - assert.True(t, errors.Is(err, tt.expectError)) - assert.Empty(t, lang) - } else { - assert.Nil(t, err) - assert.EqualValues(t, tt.expectString, lang) + if !errors.Is(err, tt.expectError) { + t.Errorf("detect() error = %v, expectError %v", err, tt.expectError) + } + if !reflect.DeepEqual(lang, tt.expectString) { + t.Errorf("detect() = %v, want %v", lang, tt.expectString) } }) } @@ -93,20 +74,8 @@ func TestDetect(t *testing.T) { expectLang language.Tag expectError error }{ - { - "normal", - []string{"en-US"}, - nil, - language.AmericanEnglish, - nil, - }, - { - "not detected", - []string(nil), - ErrNotDetected, - language.Und, - ErrNotDetected, - }, + {"normal", []string{"en-US"}, nil, language.AmericanEnglish, nil}, + {"not detected", nil, ErrNotDetected, language.Und, ErrNotDetected}, } for _, tt := range tests { @@ -114,12 +83,11 @@ func TestDetect(t *testing.T) { mockLang.set(tt.mockString, tt.mockError) lang, err := Detect() - if tt.expectError != nil { - assert.True(t, errors.Is(err, tt.expectError)) - assert.Empty(t, lang) - } else { - assert.Nil(t, err) - assert.Equal(t, tt.expectLang, lang) + if !errors.Is(err, tt.expectError) { + t.Errorf("Detect() error = %v, expectError %v", err, tt.expectError) + } + if lang != tt.expectLang { + t.Errorf("Detect() = %v, want %v", lang, tt.expectLang) } }) } @@ -135,20 +103,8 @@ func TestDetectAll(t *testing.T) { expectLang []language.Tag expectError error }{ - { - "normal", - []string{"en-US"}, - nil, - []language.Tag{language.AmericanEnglish}, - nil, - }, - { - "not detected", - []string(nil), - ErrNotDetected, - []language.Tag(nil), - ErrNotDetected, - }, + {"normal", []string{"en-US"}, nil, []language.Tag{language.AmericanEnglish}, nil}, + {"not detected", nil, ErrNotDetected, nil, ErrNotDetected}, } for _, tt := range tests { @@ -156,12 +112,11 @@ func TestDetectAll(t *testing.T) { mockLang.set(tt.mockString, tt.mockError) lang, err := DetectAll() - if tt.expectError != nil { - assert.True(t, errors.Is(err, tt.expectError)) - assert.Empty(t, lang) - } else { - assert.Nil(t, err) - assert.Equal(t, tt.expectLang, lang) + if !errors.Is(err, tt.expectError) { + t.Errorf("DetectAll() error = %v, expectError %v", err, tt.expectError) + } + if !reflect.DeepEqual(lang, tt.expectLang) { + t.Errorf("DetectAll() = %v, want %v", lang, tt.expectLang) } }) } diff --git a/locale_windows_test.go b/locale_windows_test.go index bf84d50..9c0bd37 100644 --- a/locale_windows_test.go +++ b/locale_windows_test.go @@ -2,20 +2,18 @@ package locale import ( "testing" - - . "github.com/smartystreets/goconvey/convey" ) func Test_detectViaRegistry(t *testing.T) { - Convey("detect via Windows Registry", t, func() { - langs, err := detectViaRegistry() + langs, err := detectViaRegistry() + + t.Logf("langs: %v", langs) + + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } - t.Logf("langs: %v", langs) - Convey("The error should not be nil", func() { - So(err, ShouldBeNil) - }) - Convey("The langs should not be empty", func() { - So(langs, ShouldNotBeEmpty) - }) - }) + if len(langs) == 0 { + t.Error("Expected non-empty langs, got empty slice") + } }