Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
added tests and bug fixes for the case changing utilities
Browse files Browse the repository at this point in the history
Signed-off-by: Holly Hunt <holly.hunt1101@hotmail.com>
  • Loading branch information
Wyvinar committed Jun 7, 2024
1 parent b220ac8 commit eb2f349
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 6 deletions.
22 changes: 17 additions & 5 deletions openapi2beans/pkg/utils/caseChanging.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func StringToCamel(inputString string) string {
case CAMEL: camelString = inputString
case PASCAL: camelString = pascalToCamel(inputString)
case SNAKE_VARIANT: camelString = snakeVariantsToCamel(inputString)
default: camelString = inputString
}

return camelString
Expand All @@ -43,6 +44,7 @@ func StringToPascal(inputString string) string {
case CAMEL: pascalString = camelToPascal(inputString)
case PASCAL: pascalString = inputString
case SNAKE_VARIANT: pascalString = snakeVariantsToPascal(inputString)
default: pascalString = inputString
}

return pascalString
Expand All @@ -55,6 +57,7 @@ func StringToSnake(inputString string) string {
switch stringCase {
case CAMEL, PASCAL: snakeString = camelVariantsToSnake(inputString)
case SNAKE_VARIANT: snakeString = snakeVariantsToSnake(inputString)
default: snakeString = inputString
}

return snakeString
Expand All @@ -67,14 +70,19 @@ func StringToScreamingSnake(inputString string) string {
switch stringCase {
case CAMEL, PASCAL: screamingSnakeString = camelVariantsToScreamingSnake(inputString)
case SNAKE_VARIANT: screamingSnakeString = snakeVariantsToScreamingSnake(inputString)
default: screamingSnakeString = inputString
}

return screamingSnakeString
}

// To camel functions
func pascalToCamel(pascalString string) string {
return strings.ToLower(string(pascalString[0])) + pascalString[1:]
var camelString string
if pascalString != "" {
camelString = strings.ToLower(string(pascalString[0])) + pascalString[1:]
}
return camelString
}

func snakeVariantsToCamel(snakeString string) string {
Expand All @@ -95,7 +103,11 @@ func snakeVariantsToCamel(snakeString string) string {

// To pascal functions
func camelToPascal(camelString string) string {
return strings.ToUpper(string(camelString[0])) + camelString[1:]
var pascalString string
if camelString != "" {
pascalString = strings.ToUpper(string(camelString[0])) + camelString[1:]
}
return pascalString
}

func snakeVariantsToPascal(inputString string) string {
Expand All @@ -106,14 +118,14 @@ func snakeVariantsToPascal(inputString string) string {
func camelVariantsToSnake(camelString string) string {
var snakeString string

for _, char := range camelString[1:] {
if unicode.IsUpper(char) {
for i, char := range camelString {
if unicode.IsUpper(char) && i != 0 {
snakeString += "_"
}
snakeString += string(char)
}

return snakeString
return strings.ToLower(snakeString)
}

func snakeVariantsToSnake(inputString string) string {
Expand Down
252 changes: 251 additions & 1 deletion openapi2beans/pkg/utils/caseChanging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,192 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPascalToCamel(t *testing.T) {
pascalString := "PascalString"
camelString := pascalToCamel(pascalString)
assert.Equal(t, "pascalString", camelString)

pascalString = "P"
camelString = pascalToCamel(pascalString)
assert.Equal(t, "p", camelString)

pascalString = ""
camelString = pascalToCamel(pascalString)
assert.Equal(t, "", camelString)

pascalString = "9123"
camelString = pascalToCamel(pascalString)
assert.Equal(t, "9123", camelString)
}

func TestSnakeVariantsToCamel(t *testing.T) {
snakeString := "snake_string"
camelString := snakeVariantsToCamel(snakeString)
assert.Equal(t, "snakeString", camelString)

snakeString = "SNAKE_STRING"
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "snakeString", camelString)

snakeString = "p"
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "p", camelString)

snakeString = ""
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "", camelString)

snakeString = "1234"
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "1234", camelString)
}

func TestCamelToPascal(t *testing.T) {
camelString := "camelString"
pascalString := camelToPascal(camelString)
assert.Equal(t, "CamelString", pascalString)

camelString = "c"
pascalString = camelToPascal(camelString)
assert.Equal(t, "C", pascalString)

camelString = ""
pascalString = camelToPascal(camelString)
assert.Equal(t, "", pascalString)

camelString = "1234"
pascalString = camelToPascal(camelString)
assert.Equal(t, "1234", pascalString)
}

func TestSnakeVariantsToPascal(t *testing.T) {
snakeString := "snake_string"
pascalString := snakeVariantsToPascal(snakeString)
assert.Equal(t, "SnakeString", pascalString)

snakeString = "SNAKE_STRING"
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "SnakeString", pascalString)

snakeString = "p"
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "P", pascalString)

snakeString = ""
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "", pascalString)

snakeString = "1234"
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "1234", pascalString)
}

func TestCamelVariantsToSnake(t *testing.T) {
camelString := "camelString"
snakeString := camelVariantsToSnake(camelString)
assert.Equal(t, "camel_string", snakeString)

camelString = "PascalString"
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "pascal_string", snakeString)

camelString = "c"
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "c", snakeString)

camelString = ""
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "", snakeString)

camelString = "1234"
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "1234", snakeString)
}

func TestSnakeVariantsToSnake(t *testing.T) {
snakeString := "snake_string"
resultingSnake := snakeVariantsToSnake(snakeString)
assert.Equal(t, "snake_string", resultingSnake)

snakeString = "sNaKe_StRiNg"
resultingSnake = snakeVariantsToSnake(snakeString)
assert.Equal(t, "snake_string", resultingSnake)

snakeString = "SNAKE_STRING"
resultingSnake = snakeVariantsToSnake(snakeString)
assert.Equal(t, "snake_string", resultingSnake)

snakeString = "p"
resultingSnake = snakeVariantsToSnake(snakeString)
assert.Equal(t, "p", resultingSnake)

snakeString = ""
resultingSnake = snakeVariantsToSnake(snakeString)
assert.Equal(t, "", resultingSnake)

snakeString = "1234"
resultingSnake = snakeVariantsToSnake(snakeString)
assert.Equal(t, "1234", resultingSnake)
}

func TestCamelVariantsToScreamingSnake(t *testing.T) {
camelString := "camelString"
snakeString := camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "CAMEL_STRING", snakeString)

camelString = "PascalString"
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "PASCAL_STRING", snakeString)

camelString = "c"
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "C", snakeString)

camelString = ""
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "", snakeString)

camelString = "1234"
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "1234", snakeString)
}

func TestSnakeVariantsToScreamingSnake(t *testing.T) {
snakeString := "snake_string"
resultingSnake := snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "SNAKE_STRING", resultingSnake)

snakeString = "sNaKe_StRiNg"
resultingSnake = snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "SNAKE_STRING", resultingSnake)

snakeString = "SNAKE_STRING"
resultingSnake = snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "SNAKE_STRING", resultingSnake)

snakeString = "p"
resultingSnake = snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "P", resultingSnake)

snakeString = ""
resultingSnake = snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "", resultingSnake)

snakeString = "1234"
resultingSnake = snakeVariantsToScreamingSnake(snakeString)
assert.Equal(t, "1234", resultingSnake)
}


func TestStringToCamel(t *testing.T) {
// with camel
inputCamel := "camelCaseString"
camelString := StringToCamel(inputCamel)
assert.Equal(t, inputCamel, camelString)

// with pascal
pascalString := "PascalCaseString"
camelString := StringToCamel(pascalString)
camelString = StringToCamel(pascalString)
assert.Equal(t, "pascalCaseString", camelString)

// with snake
Expand All @@ -23,4 +203,74 @@ func TestStringToCamel(t *testing.T) {
snakeString = "SNAKE_CASE_STRING"
camelString = StringToCamel(snakeString)
assert.Equal(t, "snakeCaseString", camelString)

randomString := "random string with no case >:D"
camelString = StringToCamel(randomString)
assert.Equal(t, randomString, camelString)
}

func TestStringToPascal(t *testing.T) {
camelString := "thisIsCamel"
pascalString := StringToPascal(camelString)
assert.Equal(t, "ThisIsCamel", pascalString)

inputPascal := "ThisIsPascal"
pascalString = StringToPascal(inputPascal)
assert.Equal(t, inputPascal, pascalString)

snakeString := "this_is_snake"
pascalString = StringToPascal(snakeString)
assert.Equal(t, "ThisIsSnake", pascalString)

snakeString = "THIS_IS_SCREAMING_SNAKE"
pascalString = StringToPascal(snakeString)
assert.Equal(t, "ThisIsScreamingSnake", pascalString)

randomString := "random string with no case >:D"
pascalString = StringToPascal(randomString)
assert.Equal(t, randomString, pascalString)
}

func TestStringToSnake(t *testing.T) {
camelString := "thisIsCamel"
snakeString := StringToSnake(camelString)
assert.Equal(t, "this_is_camel", snakeString)

pascalString := "ThisIsPascal"
snakeString = StringToSnake(pascalString)
assert.Equal(t, "this_is_pascal", snakeString)

inputSnake := "this_is_snake"
snakeString = StringToSnake(inputSnake)
assert.Equal(t, inputSnake, snakeString)

snakeString = "THIS_IS_SCREAMING_SNAKE"
snakeString = StringToSnake(snakeString)
assert.Equal(t, "this_is_screaming_snake", snakeString)

randomString := "random string with no case >:D"
snakeString = StringToSnake(randomString)
assert.Equal(t, randomString, snakeString)
}

func TestStringToScreamingSnake(t *testing.T) {
camelString := "thisIsCamel"
screamingSnakeString := StringToScreamingSnake(camelString)
assert.Equal(t, "THIS_IS_CAMEL", screamingSnakeString)

pascalString := "ThisIsPascal"
screamingSnakeString = StringToScreamingSnake(pascalString)
assert.Equal(t, "THIS_IS_PASCAL", screamingSnakeString)

snakeString := "this_is_snake"
screamingSnakeString = StringToScreamingSnake(snakeString)
assert.Equal(t, "THIS_IS_SNAKE", screamingSnakeString)

inputScreamingSnake := "THIS_IS_SCREAMING_SNAKE"
screamingSnakeString = StringToScreamingSnake(inputScreamingSnake)
assert.Equal(t, inputScreamingSnake, screamingSnakeString)

randomString := "random string with no case >:D"
screamingSnakeString = StringToScreamingSnake(randomString)
assert.Equal(t, randomString, screamingSnakeString)
}
18 changes: 18 additions & 0 deletions openapi2beans/pkg/utils/caseChecking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,22 @@ func TestIsPascalCase(t *testing.T) {
pascalString = "101PascalCase"
IsPascal = IsPascalCase(pascalString)
assert.True(t, IsPascal)
}

func TestGetCase(t *testing.T) {
testString := "thisIsCamel"
stringCase := GetCase(testString)
assert.Equal(t, CAMEL, stringCase)

testString = "ThisIsPascal"
stringCase = GetCase(testString)
assert.Equal(t, PASCAL, stringCase)

testString = "this_is_snake"
stringCase = GetCase(testString)
assert.Equal(t, SNAKE_VARIANT, stringCase)

testString = "THIS_IS_SCREAMING_SNAKE"
stringCase = GetCase(testString)
assert.Equal(t, SNAKE_VARIANT, stringCase)
}

0 comments on commit eb2f349

Please sign in to comment.