go-i18n/debug_test.go
Claude 97f9c758d1
chore(ax): AX compliance sweep — banned imports, naming, Good/Bad/Ugly tests
- compose.go: remove fmt import, use local stringer interface + core.Sprint
- hooks.go: replace stdlib log with dappco.re/go/core/log
- localise.go: replace os.Getenv with core.Env
- loader.go: replace strings.CutPrefix with core.HasPrefix/TrimPrefix
- reversal/tokeniser.go: replace strings.Fields with local splitFields helper
- validate.go: rename sb → builder (AX naming)
- calibrate.go, classify.go: rename cfg → configuration (AX naming)
- numbers.go: rename local fmt variable → numberFormat
- All test files: add Good/Bad/Ugly triads per AX test naming convention

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:42:40 +01:00

91 lines
2 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)
}
// TestSetDebug_Bad verifies that calling SetDebug on a nil service does not panic.
//
// SetDefault(nil); SetDebug(true) // must not panic
func TestSetDebug_Bad(t *testing.T) {
SetDefault(nil)
SetDebug(true) // must not panic
// Restore a working default for subsequent tests.
svc, err := New()
require.NoError(t, err)
SetDefault(svc)
}
// TestDebugFormat_Ugly verifies that both key and text empty produces "[] ".
//
// debugFormat("", "") // "[] "
func TestDebugFormat_Ugly(t *testing.T) {
got := debugFormat("", "")
assert.Equal(t, "[] ", got)
}