Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed typo in comment from 'no not' to 'do not'. #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion slice/minus.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package slice

// MinusStrings gets a new []string containing all items in s, that
// no not appear in minus.
// do not appear in minus.
func MinusStrings(s, minus []string) []string {

a := []string{}
Expand Down
19 changes: 3 additions & 16 deletions slice/plus.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
package slice

// PlusStrings adds two []string arrays together, returning a new []string.
// PlusStrings adds two []string arrays together, returning a new copied []string.
func PlusStrings(s, plus []string) []string {

var l int

a := []string{}

l = len(s)
for i := 0; i < l; i++ {
a = append(a, s[i])
}

l = len(plus)
for i := 0; i < l; i++ {
a = append(a, plus[i])
}

return a
a := make([]string, 0, len(s)+len(plus))
return append(append(a, s...), plus...)

}
50 changes: 49 additions & 1 deletion slice/plus_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package slice

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPlusStrings(t *testing.T) {
Expand All @@ -20,3 +21,50 @@ func TestPlusStrings(t *testing.T) {
}

}

func TestPlusStrings_ensureCopy(t *testing.T) {

s1 := []string{"one", "two", "three", "four", "five"}
s2 := []string{"six", "seven"}

all := PlusStrings(s1[:2], s2)
s1[0] = "eight"

if assert.Equal(t, 4, len(all)) {
assert.Equal(t, "one", all[0])
assert.Equal(t, "two", all[1])
assert.Equal(t, "six", all[2])
assert.Equal(t, "seven", all[3])
}

if assert.Equal(t, 5, len(s1)) {
assert.Equal(t, "eight", s1[0])
assert.Equal(t, "two", s1[1])
assert.Equal(t, "three", s1[2])
assert.Equal(t, "four", s1[3])
assert.Equal(t, "five", s1[4])
}

}

func TestPlusStrings_ensureCopyNilPlus(t *testing.T) {

s1 := []string{"one", "two", "three", "four", "five"}

all := PlusStrings(s1[:2], nil)
s1[0] = "eight"

if assert.Equal(t, 2, len(all)) {
assert.Equal(t, "one", all[0])
assert.Equal(t, "two", all[1])
}

if assert.Equal(t, 5, len(s1)) {
assert.Equal(t, "eight", s1[0])
assert.Equal(t, "two", s1[1])
assert.Equal(t, "three", s1[2])
assert.Equal(t, "four", s1[3])
assert.Equal(t, "five", s1[4])
}

}