Skip to content

Commit

Permalink
constants:新增测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Feb 12, 2025
1 parent dd8fbf7 commit def9f3d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion constants/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RegisterCodeTable(language LanguageCode, i18nMsg map[ICodeType]string) {
// # 此方法适用于【动态】注入单个错误码的场景
func RegisterCodeSingle(language LanguageCode, code ICodeType, msg string) {
ctm.mux.Lock()
if _, ok := ctm.maps[language][code]; !ok {
if _, ok := ctm.maps[language]; !ok {
ctm.maps[language] = make(map[ICodeType]string)
}
ctm.maps[language][code] = msg
Expand Down
28 changes: 28 additions & 0 deletions constants/code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package constants

import (
"testing"

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

func TestRegisterCodeTable(t *testing.T) {
const (
c1000000 = CodeType(1000000)
c1000001 = CodeType(1000001)
)
RegisterCodeTable(LanguageEnglish, map[ICodeType]string{c1000000: "1000000", c1000001: "1000001"})
assert.Equal(t, "1000000", c1000000.String())
assert.Equal(t, "1000001", c1000001.String())
}

func TestRegisterCodeSingle(t *testing.T) {
const (
c1000000 = CodeType(1000000)
c1000001 = CodeType(1000001)
)
RegisterCodeSingle(LanguageEnglish, c1000000, "1000000")
RegisterCodeSingle(LanguageEnglish, c1000001, "1000001")
assert.Equal(t, "1000000", c1000000.String())
assert.Equal(t, "1000001", c1000001.String())
}
19 changes: 19 additions & 0 deletions constants/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package constants

import (
"testing"

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

func TestString(t *testing.T) {
for lang, msgMap := range initErrorCodeMsgMap {
RegisterCodeTable(lang, msgMap)
}
assert.Equal(t, "SUCCESS", ErrNone.String(LanguageEnglish.String()))
assert.Equal(t, "成功", ErrNone.String(LanguageChinesePRC.String()))
}

func TestInt(t *testing.T) {
assert.Equal(t, 0, ErrNone.Int())
}
23 changes: 23 additions & 0 deletions constants/i18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package constants

import (
"testing"

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

func TestI18NString(t *testing.T) {
assert.Equal(t, "en", LanguageEnglish.String())
}

func TestI18NToLowerCase(t *testing.T) {
assert.Equal(t, "en", LanguageEnglish.ToLowerCase())
}

func TestI18NToUpperCase(t *testing.T) {
assert.Equal(t, "EN", LanguageEnglish.ToUpperCase())
}

func TestI18NExist(t *testing.T) {
assert.Equal(t, true, LanguageEnglish.Exist())
}
2 changes: 1 addition & 1 deletion utils/version_deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestPrintVersion(t *testing.T) {
Version: constants.GoSailVersion,
Branch: "main",
Revision: "cf6e7f1",
BuildDate: Datetime().FormatDate(time.Now(), YYYY_MM_DD_HH_MM_SS_EN),
BuildDate: FormatDate(time.Now(), YYYY_MM_DD_HH_MM_SS_EN),
GoVersion: runtime.Version(),
}

Expand Down

0 comments on commit def9f3d

Please sign in to comment.