Test cases for error added

This commit is contained in:
Roshan Ranabhat 2020-04-22 17:36:08 +05:45
parent 0d0f42b9b5
commit 145024d546
3 changed files with 141 additions and 8 deletions

View File

@ -13,10 +13,7 @@ func caseHelper(input string, isCamel bool, rule ...string) []string {
input = re.ReplaceAllString(input, ReplaceCapital) input = re.ReplaceAllString(input, ReplaceCapital)
} }
input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ") input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ")
if len(rule) <= 1 { if len(rule) > 0 && len(rule)%2 != 0 {
rule = []string{".", " ", "_", " ", "-", " "}
}
if len(rule) > 1 && len(rule)%2 != 0 {
panic(errors.New(OddError)) panic(errors.New(OddError))
} }
rule = append(rule, ".", " ", "_", " ", "-", " ") rule = append(rule, ".", " ", "_", " ", "-", " ")

View File

@ -53,7 +53,7 @@ func New(val string) StringManipulation {
// chain to upper which with make result all upercase or ToLower which // chain to upper which with make result all upercase or ToLower which
// will make result all lower case or Get which will return result as it is // will make result all lower case or Get which will return result as it is
func (i *input) Between(start, end string) StringManipulation { func (i *input) Between(start, end string) StringManipulation {
if start == "" && end == "" || i.Input == "" { if (start == "" && end == "") || i.Input == "" {
return i return i
} }
@ -179,7 +179,7 @@ func (i *input) Last(length int) string {
func (i *input) LcFirst() string { func (i *input) LcFirst() string {
input := getInput(*i) input := getInput(*i)
for i, v := range input { for i, v := range input {
return string(unicode.ToUpper(v)) + input[i+1:] return string(unicode.ToLower(v)) + input[i+1:]
} }
return input return input
} }

View File

@ -13,6 +13,22 @@ func TestInput_Between(t *testing.T) {
} }
} }
func TestInput_EmptyBetween(t *testing.T) {
sm := New("This is example.")
val := sm.Between("", "").ToUpper()
if val != "THIS IS EXAMPLE." {
t.Errorf("Expected: %s but got: %s", "THIS IS EXAMPLE.", val)
}
}
func TestInput_EmptyNoMatchBetween(t *testing.T) {
sm := New("This is example.")
val := sm.Between("hello", "test").ToUpper()
if val != "THIS IS EXAMPLE." {
t.Errorf("Expected: %s but got: %s", "THIS IS EXAMPLE.", val)
}
}
func TestInput_Boolean(t *testing.T) { func TestInput_Boolean(t *testing.T) {
str := New("on") str := New("on")
val := str.Boolean() val := str.Boolean()
@ -21,6 +37,27 @@ func TestInput_Boolean(t *testing.T) {
} }
} }
func TestInput_BooleanOff(t *testing.T) {
str := New("off")
val := str.Boolean()
if val {
t.Errorf("Expected: to be false but got: %v", val)
}
}
func TestInput_BooleanError(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Error expected")
}
}()
str := New("invalid")
val := str.Boolean()
if val {
t.Errorf("Expected: to be false but got: %v", val)
}
}
func TestInput_CamelCase(t *testing.T) { func TestInput_CamelCase(t *testing.T) {
str := New("Camel case this_complicated__string%%") str := New("Camel case this_complicated__string%%")
val := str.CamelCase("%", "") val := str.CamelCase("%", "")
@ -29,6 +66,28 @@ func TestInput_CamelCase(t *testing.T) {
} }
} }
func TestInput_CamelCaseNoRule(t *testing.T) {
str := New("Camel case this_complicated__string%%")
val := str.CamelCase()
if val != "CamelCaseThisComplicatedString%%" {
t.Errorf("Expected: to be %s but got: %s", "CamelCaseThisComplicatedString", val)
}
}
func TestInput_CamelCaseOddRuleError(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Error expected")
}
}()
str := New("Camel case this_complicated__string%%")
val := str.CamelCase("%")
if val != "CamelCaseThisComplicatedString%%" {
t.Errorf("Expected: to be %s but got: %s", "CamelCaseThisComplicatedString", val)
}
}
func TestInput_ContainsAll(t *testing.T) { func TestInput_ContainsAll(t *testing.T) {
contains := New("hello mam how are you??") contains := New("hello mam how are you??")
if val := contains.ContainsAll("mam", "?"); !val { if val := contains.ContainsAll("mam", "?"); !val {
@ -47,6 +106,14 @@ func TestInput_Delimited(t *testing.T) {
} }
} }
func TestInput_DelimitedNoDelimeter(t *testing.T) {
str := New("Delimited case this_complicated__string@@")
against := "delimited.case.this.complicated.string@@"
if val := str.Delimited("").ToLower(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_KebabCase(t *testing.T) { func TestInput_KebabCase(t *testing.T) {
str := New("Kebab case this-complicated___string@@") str := New("Kebab case this-complicated___string@@")
against := "Kebab-case-this-complicated-string" against := "Kebab-case-this-complicated-string"
@ -56,8 +123,16 @@ func TestInput_KebabCase(t *testing.T) {
} }
func TestInput_LcFirst(t *testing.T) { func TestInput_LcFirst(t *testing.T) {
str := New("this is an all lower") str := New("This is an all lower")
against := "This is an all lower" against := "this is an all lower"
if val := str.LcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_LcFirstEmpty(t *testing.T) {
str := New("")
against := ""
if val := str.LcFirst(); val != against { if val := str.LcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val) t.Errorf("Expected: to be %s but got: %s", against, val)
} }
@ -87,6 +162,23 @@ func TestInput_Pad(t *testing.T) {
} }
} }
func TestInput_PadInvalidLength(t *testing.T) {
pad := New("Roshan")
if result := pad.Pad(6, "0", "both"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(6, "0", "left"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(6, "0", "right"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(13, "0", "middle"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
}
func TestInput_RemoveSpecialCharacter(t *testing.T) { func TestInput_RemoveSpecialCharacter(t *testing.T) {
cleanString := New("special@#remove%%%%") cleanString := New("special@#remove%%%%")
against := "specialremove" against := "specialremove"
@ -103,6 +195,14 @@ func TestInput_ReplaceFirst(t *testing.T) {
} }
} }
func TestInput_ReplaceFirstEmptyInput(t *testing.T) {
replaceFirst := New("")
against := ""
if result := replaceFirst.ReplaceFirst("name", "nombre"); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
}
func TestInput_ReplaceLast(t *testing.T) { func TestInput_ReplaceLast(t *testing.T) {
replaceLast := New("Hello My name is Roshan and his name is Alis.") replaceLast := New("Hello My name is Roshan and his name is Alis.")
against := "Hello My name is Roshan and his nombre is Alis." against := "Hello My name is Roshan and his nombre is Alis."
@ -151,6 +251,14 @@ func TestInput_Tease(t *testing.T) {
} }
} }
func TestInput_TeaseEmpty(t *testing.T) {
str := New("This is just simple paragraph on lorem ipsum.")
against := "This is just simple paragraph on lorem ipsum."
if val := str.Tease(200, "..."); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_UcFirst(t *testing.T) { func TestInput_UcFirst(t *testing.T) {
str := New("this is test") str := New("this is test")
against := "This is test" against := "This is test"
@ -159,6 +267,14 @@ func TestInput_UcFirst(t *testing.T) {
} }
} }
func TestInput_EmptyUcFirst(t *testing.T) {
str := New("")
against := ""
if val := str.UcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_First(t *testing.T) { func TestInput_First(t *testing.T) {
fcn := New("4111 1111 1111 1111") fcn := New("4111 1111 1111 1111")
against := "4111" against := "4111"
@ -167,6 +283,26 @@ func TestInput_First(t *testing.T) {
} }
} }
func TestInput_FirstError(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Error expected but got none")
}
}()
fcn := New("4111 1111 1111 1111")
fcn.First(100)
}
func TestInput_LastError(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Error expected but got none")
}
}()
fcn := New("4111 1111 1111 1111")
fcn.Last(100)
}
func TestInput_Last(t *testing.T) { func TestInput_Last(t *testing.T) {
lcn := New("4111 1111 1111 1348") lcn := New("4111 1111 1111 1348")
against := "1348" against := "1348"