Add dedicated test files for compose.go, context.go, debug.go, hooks.go, i18n.go, localise.go, time.go, and transform.go. Uses testify assert/require with table-driven tests and Good/Bad/Ugly naming. Key coverage improvements: - transform.go: toInt/toInt64/toFloat64 18.8% -> 100% - time.go: TimeAgo/FormatAgo 0% -> 100%/87.5% - compose.go: newTemplateData/SetFormality/IsInformal 0% -> 100% - context.go: all functions now 100% - debug.go: package-level SetDebug 0% -> 100% - hooks.go: RegisterLocales 0% -> 100% - i18n.go: T/Raw/N/SetMode/AddHandler/PrependHandler all covered Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package i18n
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSetDebug_Good_PackageLevel(t *testing.T) {
|
|
svc, err := New()
|
|
require.NoError(t, err)
|
|
|
|
// Ensure Init() has run first, then override with our service
|
|
_ = Init()
|
|
SetDefault(svc)
|
|
|
|
SetDebug(true)
|
|
assert.True(t, svc.Debug())
|
|
|
|
SetDebug(false)
|
|
assert.False(t, svc.Debug())
|
|
}
|
|
|
|
func TestSetDebug_Good_ServiceLevel(t *testing.T) {
|
|
svc, err := New()
|
|
require.NoError(t, err)
|
|
|
|
svc.SetDebug(true)
|
|
assert.True(t, svc.Debug())
|
|
|
|
svc.SetDebug(false)
|
|
assert.False(t, svc.Debug())
|
|
}
|
|
|
|
func TestDebugFormat_Good(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
text string
|
|
want string
|
|
}{
|
|
{"simple", "greeting", "Hello", "[greeting] Hello"},
|
|
{"dotted_key", "i18n.label.status", "Status:", "[i18n.label.status] Status:"},
|
|
{"empty_text", "key", "", "[key] "},
|
|
{"empty_key", "", "text", "[] text"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := debugFormat(tt.key, tt.text)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDebugMode_Good_Integration(t *testing.T) {
|
|
svc, err := New()
|
|
require.NoError(t, err)
|
|
SetDefault(svc)
|
|
|
|
svc.SetDebug(true)
|
|
defer svc.SetDebug(false)
|
|
|
|
// T() should wrap output in debug format
|
|
got := svc.T("prompt.yes")
|
|
assert.Equal(t, "[prompt.yes] y", got)
|
|
|
|
// Raw() should also wrap output in debug format
|
|
got = svc.Raw("prompt.yes")
|
|
assert.Equal(t, "[prompt.yes] y", got)
|
|
}
|