From 145024d5464186470fbc5d4e6e7badddcaf8481e Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Wed, 22 Apr 2020 17:36:08 +0545 Subject: [PATCH] Test cases for error added --- helper.go | 5 +- stringy.go | 4 +- stringy_test.go | 140 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 141 insertions(+), 8 deletions(-) diff --git a/helper.go b/helper.go index 991b255..53311ae 100644 --- a/helper.go +++ b/helper.go @@ -13,10 +13,7 @@ func caseHelper(input string, isCamel bool, rule ...string) []string { input = re.ReplaceAllString(input, ReplaceCapital) } input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ") - if len(rule) <= 1 { - rule = []string{".", " ", "_", " ", "-", " "} - } - if len(rule) > 1 && len(rule)%2 != 0 { + if len(rule) > 0 && len(rule)%2 != 0 { panic(errors.New(OddError)) } rule = append(rule, ".", " ", "_", " ", "-", " ") diff --git a/stringy.go b/stringy.go index 313c48e..da70928 100644 --- a/stringy.go +++ b/stringy.go @@ -53,7 +53,7 @@ func New(val string) StringManipulation { // 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 func (i *input) Between(start, end string) StringManipulation { - if start == "" && end == "" || i.Input == "" { + if (start == "" && end == "") || i.Input == "" { return i } @@ -179,7 +179,7 @@ func (i *input) Last(length int) string { func (i *input) LcFirst() string { input := getInput(*i) for i, v := range input { - return string(unicode.ToUpper(v)) + input[i+1:] + return string(unicode.ToLower(v)) + input[i+1:] } return input } diff --git a/stringy_test.go b/stringy_test.go index 62d5ac3..11a5d09 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -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) { str := New("on") 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) { str := New("Camel case this_complicated__string%%") 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) { contains := New("hello mam how are you??") 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) { str := New("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) { - str := New("this is an all lower") - against := "This is an all lower" + str := New("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 { 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) { cleanString := New("special@#remove%%%%") 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) { replaceLast := New("Hello My name is Roshan and his name 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) { str := New("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) { fcn := New("4111 1111 1111 1111") 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) { lcn := New("4111 1111 1111 1348") against := "1348"