go-i18n/transform_test.go
Claude 32a55f5d35
test(i18n): add tests for 8 uncovered files, coverage 69.9% -> 86.6%
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>
2026-02-20 02:09:08 +00:00

126 lines
2.6 KiB
Go

package i18n
import (
"testing"
"github.com/stretchr/testify/assert"
)
// --- getCount ---
func TestGetCount_Good(t *testing.T) {
tests := []struct {
name string
data any
want int
}{
{"nil", nil, 0},
{"map_string_any", map[string]any{"Count": 5}, 5},
{"map_string_any_float", map[string]any{"Count": 3.7}, 3},
{"map_string_int", map[string]int{"Count": 42}, 42},
{"no_count_key", map[string]any{"Name": "test"}, 0},
{"wrong_type", "a string", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getCount(tt.data)
assert.Equal(t, tt.want, got)
})
}
}
// --- toInt ---
func TestToInt_Good(t *testing.T) {
tests := []struct {
name string
val any
want int
}{
{"nil", nil, 0},
{"int", int(42), 42},
{"int64", int64(100), 100},
{"int32", int32(200), 200},
{"int16", int16(300), 300},
{"int8", int8(127), 127},
{"uint", uint(10), 10},
{"uint64", uint64(20), 20},
{"uint32", uint32(30), 30},
{"uint16", uint16(40), 40},
{"uint8", uint8(50), 50},
{"float64", float64(3.14), 3},
{"float32", float32(2.71), 2},
{"string", "not a number", 0},
{"bool", true, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toInt(tt.val)
assert.Equal(t, tt.want, got)
})
}
}
// --- toInt64 ---
func TestToInt64_Good(t *testing.T) {
tests := []struct {
name string
val any
want int64
}{
{"nil", nil, 0},
{"int", int(42), 42},
{"int64", int64(100), 100},
{"int32", int32(200), 200},
{"int16", int16(300), 300},
{"int8", int8(127), 127},
{"uint", uint(10), 10},
{"uint64", uint64(20), 20},
{"uint32", uint32(30), 30},
{"uint16", uint16(40), 40},
{"uint8", uint8(50), 50},
{"float64", float64(3.14), 3},
{"float32", float32(2.71), 2},
{"string", "not a number", 0},
{"bool", true, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toInt64(tt.val)
assert.Equal(t, tt.want, got)
})
}
}
// --- toFloat64 ---
func TestToFloat64_Good(t *testing.T) {
tests := []struct {
name string
val any
want float64
}{
{"nil", nil, 0},
{"float64", float64(3.14), 3.14},
{"float32", float32(2.5), 2.5},
{"int", int(42), 42.0},
{"int64", int64(100), 100.0},
{"int32", int32(200), 200.0},
{"int16", int16(300), 300.0},
{"int8", int8(127), 127.0},
{"uint", uint(10), 10.0},
{"uint64", uint64(20), 20.0},
{"uint32", uint32(30), 30.0},
{"uint16", uint16(40), 40.0},
{"uint8", uint8(50), 50.0},
{"string", "not a number", 0},
{"bool", true, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toFloat64(tt.val)
assert.InDelta(t, tt.want, got, 0.01)
})
}
}