Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

#5 string helpers #12

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix Contains helper & added tests for Contains
octoper committed Apr 11, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8e09b06399bdbd7e6a4edc33847f56ef1d3ce266
7 changes: 5 additions & 2 deletions str/str.go
Original file line number Diff line number Diff line change
@@ -82,8 +82,11 @@ func Between(subject string, from string, to string) string {
}

func Contains(haystack string, needle string) bool {
// TODO create tests
return strings.Index(haystack, needle) == -1
if len(needle) == 0 {
return false
}

return strings.Contains(haystack, needle)
}

// func ContainsFromSlice(haystack string, needle []string) bool {
67 changes: 38 additions & 29 deletions test/str_test.go
Original file line number Diff line number Diff line change
@@ -55,15 +55,15 @@ func Test_AfterLast(t *testing.T) {
require.Equal(t, "", str.After("", ""))
require.Equal(t, "", str.After("", "han"))
require.Equal(t, "hannah", str.After("hannah", ""))
require.Equal(t,"tte", str.AfterLast("yvette", "yve"))
require.Equal(t,"e", str.AfterLast("yvette", "t"))
require.Equal(t,"e", str.AfterLast("ééé yvette", "t"))
require.Equal(t,"", str.AfterLast("yvette", "tte"))
require.Equal(t,"yvette", str.AfterLast("yvette", "xxxx"))
require.Equal(t,"te", str.AfterLast("yv0et0te", "0"))
require.Equal(t,"te", str.AfterLast("yv0et0te", "0"))
require.Equal(t,"te", str.AfterLast("yv2et2te", "2"))
require.Equal(t,"foo", str.AfterLast("----foo", "---"))
require.Equal(t, "tte", str.AfterLast("yvette", "yve"))
require.Equal(t, "e", str.AfterLast("yvette", "t"))
require.Equal(t, "e", str.AfterLast("ééé yvette", "t"))
require.Equal(t, "", str.AfterLast("yvette", "tte"))
require.Equal(t, "yvette", str.AfterLast("yvette", "xxxx"))
require.Equal(t, "te", str.AfterLast("yv0et0te", "0"))
require.Equal(t, "te", str.AfterLast("yv0et0te", "0"))
require.Equal(t, "te", str.AfterLast("yv2et2te", "2"))
require.Equal(t, "foo", str.AfterLast("----foo", "---"))
}

func Test_Before(t *testing.T) {
@@ -77,27 +77,36 @@ func Test_Before(t *testing.T) {
}

func Test_BeforeLast(t *testing.T) {
require.Equal(t,"yve", str.BeforeLast("yvette", "tte"))
require.Equal(t,"yvet", str.BeforeLast("yvette", "t"))
require.Equal(t,"ééé ", str.BeforeLast("ééé yvette", "yve"))
require.Equal(t,"", str.BeforeLast("yvette", "yve"))
require.Equal(t,"yvette", str.BeforeLast("yvette", "xxxx"))
require.Equal(t,"yvette", str.BeforeLast("yvette", ""))
require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0"))
require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0"))
require.Equal(t,"yv2et", str.BeforeLast("yv2et2te", "2"))
require.Equal(t, "yve", str.BeforeLast("yvette", "tte"))
require.Equal(t, "yvet", str.BeforeLast("yvette", "t"))
require.Equal(t, "ééé ", str.BeforeLast("ééé yvette", "yve"))
require.Equal(t, "", str.BeforeLast("yvette", "yve"))
require.Equal(t, "yvette", str.BeforeLast("yvette", "xxxx"))
require.Equal(t, "yvette", str.BeforeLast("yvette", ""))
require.Equal(t, "yv0et", str.BeforeLast("yv0et0te", "0"))
require.Equal(t, "yv0et", str.BeforeLast("yv0et0te", "0"))
require.Equal(t, "yv2et", str.BeforeLast("yv2et2te", "2"))
}

func Test_Between(t *testing.T) {
require.Equal(t,"abc", str.Between("abc", "", "c"))
require.Equal(t,"abc", str.Between("abc", "a", ""))
require.Equal(t,"abc", str.Between("abc", "", ""))
require.Equal(t,"b", str.Between("abc", "a", "c"))
require.Equal(t,"b", str.Between("dddabc", "a", "c"))
require.Equal(t,"b", str.Between("abcddd", "a", "c"))
require.Equal(t,"b", str.Between("dddabcddd", "a", "c"))
require.Equal(t,"nn", str.Between("hannah", "ha", "ah"))
require.Equal(t,"a]ab[b", str.Between("[a]ab[b]", "[", "]"))
require.Equal(t,"foo", str.Between("foofoobar", "foo", "bar"))
require.Equal(t,"bar", str.Between("foobarbar", "foo", "bar"))
require.Equal(t, "abc", str.Between("abc", "", "c"))
require.Equal(t, "abc", str.Between("abc", "a", ""))
require.Equal(t, "abc", str.Between("abc", "", ""))
require.Equal(t, "b", str.Between("abc", "a", "c"))
require.Equal(t, "b", str.Between("dddabc", "a", "c"))
require.Equal(t, "b", str.Between("abcddd", "a", "c"))
require.Equal(t, "b", str.Between("dddabcddd", "a", "c"))
require.Equal(t, "nn", str.Between("hannah", "ha", "ah"))
require.Equal(t, "a]ab[b", str.Between("[a]ab[b]", "[", "]"))
require.Equal(t, "foo", str.Between("foofoobar", "foo", "bar"))
require.Equal(t, "bar", str.Between("foobarbar", "foo", "bar"))
}

func Test_Contains(t *testing.T) {
require.True(t, str.Contains("taylor", "ylo"))
require.True(t, str.Contains("taylor", "taylor"))

require.False(t, str.Contains("taylor", "xxx"))
require.False(t, str.Contains("taylor", ""))
require.False(t, str.Contains("", ""))
}