2025-03-14 15:50:30 +00:00
|
|
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/token"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2025-03-26 14:06:44 +00:00
|
|
|
func buildHandler(ret *[]string) Handler {
|
|
|
|
return Handler{
|
|
|
|
OnMsgid: func(fset *token.FileSet, pos token.Pos, msgid string) {
|
|
|
|
*ret = append(*ret, msgid)
|
|
|
|
},
|
|
|
|
OnUnexpectedInvoke: func(fset *token.FileSet, pos token.Pos, funcname string, argc int) {},
|
|
|
|
LocaleTrFunctions: InitLocaleTrFunctions(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-14 15:50:30 +00:00
|
|
|
func HandleGoFileWrapped(t *testing.T, fname, src string) []string {
|
|
|
|
var ret []string
|
2025-03-26 14:06:44 +00:00
|
|
|
handler := buildHandler(&ret)
|
|
|
|
require.NoError(t, handler.HandleGoFile(fname, src))
|
2025-03-14 15:50:30 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleTemplateFileWrapped(t *testing.T, fname, src string) []string {
|
|
|
|
var ret []string
|
2025-03-26 14:06:44 +00:00
|
|
|
handler := buildHandler(&ret)
|
|
|
|
require.NoError(t, handler.HandleTemplateFile(fname, src))
|
2025-03-14 15:50:30 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUsagesParser(t *testing.T) {
|
|
|
|
t.Run("go, simple", func(t *testing.T) {
|
2025-03-28 22:22:21 +00:00
|
|
|
assert.Equal(t,
|
2025-03-14 15:50:30 +00:00
|
|
|
[]string{"what.an.example"},
|
|
|
|
HandleGoFileWrapped(t, "<g1>", "package main\nfunc Render(ctx *context.Context) string { return ctx.Tr(\"what.an.example\"); }\n"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("template, simple", func(t *testing.T) {
|
2025-03-28 22:22:21 +00:00
|
|
|
assert.Equal(t,
|
2025-03-14 15:50:30 +00:00
|
|
|
[]string{"what.an.example"},
|
|
|
|
HandleTemplateFileWrapped(t, "<t1>", "{{ ctx.Locale.Tr \"what.an.example\" }}\n"))
|
|
|
|
})
|
|
|
|
}
|