Files
relspecgo/pkg/writers/template/string_helpers_test.go
T
warkanum 495a21b67b test: expand coverage across readers, writers, cmd, ui, diff and merge
Implements tests/_plans and previously deferred packages; updates plan
README with new coverage numbers.
2026-10-03 21:33:59 +02:00

152 lines
3.9 KiB
Go

package template
import (
"reflect"
"testing"
)
func TestCaseConversions(t *testing.T) {
tests := []struct {
in, camel, pascal, snake, kebab string
}{
{"", "", "", "", ""},
{"user_name", "userName", "UserName", "user_name", "user-name"},
{"http_request", "httpRequest", "HTTPRequest", "http_request", "http-request"},
{"user_id", "userID", "UserID", "user_id", "user-id"},
{"UserName", "username", "UserName", "user_name", "user-name"},
{"HTTPRequest", "httprequest", "HTTPRequest", "http_request", "http-request"},
{"userID", "userid", "UserID", "user_id", "user-id"},
{"name", "name", "Name", "name", "name"},
{"ÜberUser", "überuser", "ÜberUser", "über_user", "über-user"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
if got := ToCamelCase(tt.in); got != tt.camel {
t.Errorf("ToCamelCase = %q, want %q", got, tt.camel)
}
if got := ToPascalCase(tt.in); got != tt.pascal {
t.Errorf("ToPascalCase = %q, want %q", got, tt.pascal)
}
if got := ToSnakeCase(tt.in); got != tt.snake {
t.Errorf("ToSnakeCase = %q, want %q", got, tt.snake)
}
if got := ToKebabCase(tt.in); got != tt.kebab {
t.Errorf("ToKebabCase = %q, want %q", got, tt.kebab)
}
})
}
}
func TestPluralize(t *testing.T) {
tests := []struct{ in, want string }{
{"", ""},
{"user", "users"},
{"person", "people"},
{"Person", "people"},
{"status", "statuses"},
{"cats", "cats"},
{"bus", "buses"},
{"dress", "dresses"},
{"box", "boxes"},
{"quiz", "quizes"},
{"church", "churches"},
{"dish", "dishes"},
{"category", "categories"},
{"day", "days"},
{"leaf", "leaves"},
{"knife", "knives"},
{"hero", "heroes"},
{"video", "videos"},
}
for _, tt := range tests {
if got := Pluralize(tt.in); got != tt.want {
t.Errorf("Pluralize(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestSingularize(t *testing.T) {
tests := []struct{ in, want string }{
{"", ""},
{"users", "user"},
{"people", "person"},
{"Children", "child"},
{"categories", "category"},
{"ies", "ie"},
{"leaves", "leaf"},
{"buses", "bus"},
{"boxes", "box"},
{"churches", "church"},
{"dishes", "dish"},
{"dress", "dress"},
{"user", "user"},
}
for _, tt := range tests {
if got := Singularize(tt.in); got != tt.want {
t.Errorf("Singularize(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestPlainStringWrappers(t *testing.T) {
if ToUpper("aB") != "AB" || ToLower("aB") != "ab" {
t.Error("case")
}
if Title("hello world") != "Hello World" || Title("") != "" {
t.Errorf("Title: %q", Title("hello world"))
}
if Trim(" a \n") != "a" {
t.Error("Trim")
}
if TrimPrefix("foobar", "foo") != "bar" || TrimPrefix("bar", "foo") != "bar" {
t.Error("TrimPrefix")
}
if TrimSuffix("foobar", "bar") != "foo" || TrimSuffix("foo", "bar") != "foo" {
t.Error("TrimSuffix")
}
if Replace("aaa", "a", "b", 2) != "bba" || Replace("aaa", "a", "b", -1) != "bbb" {
t.Error("Replace")
}
if !StringContains("abc", "b") || StringContains("abc", "z") {
t.Error("StringContains")
}
if !HasPrefix("abc", "ab") || HasPrefix("abc", "bc") {
t.Error("HasPrefix")
}
if !HasSuffix("abc", "bc") || HasSuffix("abc", "ab") {
t.Error("HasSuffix")
}
if got := Split("a,b", ","); !reflect.DeepEqual(got, []string{"a", "b"}) {
t.Errorf("Split: %v", got)
}
if Join([]string{"a", "b"}, "-") != "a-b" || Join(nil, "-") != "" {
t.Error("Join")
}
}
func TestCapitalizeAndIsVowel(t *testing.T) {
tests := []struct{ in, want string }{
{"", ""},
{"id", "ID"},
{"Uuid", "UUID"},
{"http", "HTTP"},
{"name", "Name"},
{"élan", "Élan"},
}
for _, tt := range tests {
if got := capitalize(tt.in); got != tt.want {
t.Errorf("capitalize(%q) = %q, want %q", tt.in, got, tt.want)
}
}
for _, c := range []byte("aeiouAEIOU") {
if !isVowel(c) {
t.Errorf("%c should be vowel", c)
}
}
for _, c := range []byte("bcxyz") {
if isVowel(c) {
t.Errorf("%c should not be vowel", c)
}
}
}