Skip to content

Commit

Permalink
test: simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandeaual committed Feb 4, 2024
1 parent 5454254 commit 136b65b
Showing 1 changed file with 13 additions and 44 deletions.
57 changes: 13 additions & 44 deletions locale_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,16 @@
package locale

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func resetEnv(t *testing.T) {
t.Log("Resetting LANGUAGE, LC_ALL, LC_MESSAGES and LANG")

for _, env := range [...]string{"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"} {
err := os.Setenv(env, "")
assert.Equal(t, nil, err)
}
}

func TestMultipleLocales(t *testing.T) {
resetEnv(t)

err := os.Setenv("LANGUAGE", "en_US:fr:ja")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_ALL", "")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_MESSAGES", "")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LANG", "en_US.UTF-8")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LANGUAGE", "en_US:fr:ja")
t.Setenv("LC_ALL", "")
t.Setenv("LC_MESSAGES", "")
t.Setenv("LANG", "en_US.UTF-8")

locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -48,8 +32,7 @@ func TestMultipleLocales(t *testing.T) {
assert.Equal(t, "US", region)

// If "C" is set, we should ignore LANGUAGE
err = os.Setenv("LC_ALL", "C")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LC_ALL", "C")

var nilStringSlice []string

Expand All @@ -71,16 +54,10 @@ func TestMultipleLocales(t *testing.T) {
}

func TestSingleLocale(t *testing.T) {
resetEnv(t)

err := os.Setenv("LANGUAGE", "ja_JP")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_ALL", "en_US")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_MESSAGES", "ko_KR")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LANG", "fr_FR")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LANGUAGE", "ja_JP")
t.Setenv("LC_ALL", "en_US")
t.Setenv("LC_MESSAGES", "ko_KR")
t.Setenv("LANG", "fr_FR")

locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -98,8 +75,7 @@ func TestSingleLocale(t *testing.T) {
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "JP", region)

err = os.Setenv("LANGUAGE", "")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LANGUAGE", "")

locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -113,8 +89,7 @@ func TestSingleLocale(t *testing.T) {
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "US", region)

err = os.Setenv("LC_ALL", "")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LC_ALL", "")

locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -128,8 +103,7 @@ func TestSingleLocale(t *testing.T) {
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "KR", region)

err = os.Setenv("LC_MESSAGES", "")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LC_MESSAGES", "")

locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -145,10 +119,7 @@ func TestSingleLocale(t *testing.T) {
}

func TestLocaleNoRegion(t *testing.T) {
resetEnv(t)

err := os.Setenv("LANG", "fr")
assert.Equal(t, nil, err, "err should be nil")
t.Setenv("LANG", "fr")

locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
Expand All @@ -168,8 +139,6 @@ func TestLocaleNoRegion(t *testing.T) {
}

func TestNoLocale(t *testing.T) {
resetEnv(t)

var nilStringSlice []string

locales, err := GetLocales()
Expand Down

0 comments on commit 136b65b

Please sign in to comment.