Skip to content

Commit

Permalink
add: more test
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jun 4, 2024
1 parent 5684eef commit ede210b
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 8 deletions.
6 changes: 3 additions & 3 deletions infra/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ func Kebab(str string) string {
// name_id_com -> nameIdCom
// name_idcom -> nameIdcom
func SmallCamelCase(fieldName string) string {
return LowTitle(CamelCase(fieldName))
return LowTitle(PascalCase(fieldName))
}

// CamelCase returns the CamelCased name.
// PascalCase returns the CamelCased name.
// If there is an interior underscore followed by a lower case letter,
// drop the underscore and convert the letter to upper case.
// There is a remote possibility of this rewrite causing a name collision,
// but it's so remote we're prepared to pretend it's nonexistent - since the
// C++ generator lowercases names, it's extremely unlikely to have two fields
// with different capitalizations.
// In short, _my_field_name_2 becomes XMyFieldName_2.
func CamelCase(s string) string {
func PascalCase(s string) string {
return JoinCase(s, '_')
}

Expand Down
147 changes: 142 additions & 5 deletions infra/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,150 @@ import (
)

func Test_SnakeCase(t *testing.T) {
if got, want := SnakeCase("PabB2BicNotify"), "pab_b2bic_notify"; got != want {
t.Errorf("got %v, want %v", got, want)
tests := []struct {
name string
args string
want string
}{
{
name: "normal",
args: "PabBBicNotify",
want: "pab_b_bic_notify",
},
{
name: "empty",
args: "",
want: "",
},
{
name: "start with number",
args: "2PabBicNotify",
want: "x_2pab_bic_notify",
},
{
name: "contain number",
args: "PabB2BicNotify",
want: "pab_b2bic_notify",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SnakeCase(tt.args); got != tt.want {
t.Errorf("SnakeCase() = %v, want %v", got, tt.want)
}
})
}
}

func Test_Kebab(t *testing.T) {
tests := []struct {
name string
args string
want string
}{
{
name: "normal",
args: "PabBBicNotify",
want: "pab-b-bic-notify",
},
{
name: "empty",
args: "",
want: "",
},
{
name: "start with number",
args: "2PabBicNotify",
want: "x-2pab-bic-notify",
},
{
name: "contain number",
args: "PabB2BicNotify",
want: "pab-b2bic-notify",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Kebab(tt.args); got != tt.want {
t.Errorf("Kebab() = %v, want %v", got, tt.want)
}
})
}
}

func Test_CamelCase(t *testing.T) {
if got, want := CamelCase("pab_b2bic_notify"), "PabB2BicNotify"; got != want {
t.Errorf("got %v, want %v", got, want)
func Test_SmallCamelCase(t *testing.T) {
tests := []struct {
name string
args string
want string
}{
{
name: "normal",
args: "pab_b_bic_notify",
want: "pabBBicNotify",
},
{
name: "empty",
args: "",
want: "",
},
{
name: "start with number",
args: "x_2pab_b2bic_notify",
want: "x_2PabB2BicNotify",
},
{
name: "contain number",
args: "pab_b2bic_notify",
want: "pabB2BicNotify",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SmallCamelCase(tt.args); got != tt.want {
t.Errorf("SmallCamelCase() = %v, want %v", got, tt.want)
}
})
}
}

func Test_PascalCase(t *testing.T) {
tests := []struct {
name string
args string
want string
}{
{
name: "normal",
args: "pab_b_bic_notify",
want: "PabBBicNotify",
},
{
name: "empty",
args: "",
want: "",
},
{
name: "start with number",
args: "x_2pab_b2bic_notify",
want: "X_2PabB2BicNotify",
},
{
name: "start with delimiter",
args: "_2pab_b2bic_notify",
want: "X2PabB2BicNotify",
},
{
name: "contain number",
args: "pab_b2bic_notify",
want: "PabB2BicNotify",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := PascalCase(tt.args); got != tt.want {
t.Errorf("PascalCase() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ede210b

Please sign in to comment.