Skip to content

Commit

Permalink
Split of "" should be empty. Length of empty array should be 0
Browse files Browse the repository at this point in the history
  • Loading branch information
bobtfish committed Aug 11, 2015
1 parent 8df1dec commit ff8fb43
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func interpolationFuncSplit() ast.Function {
Callback: func(args []interface{}) (interface{}, error) {
sep := args[0].(string)
s := args[1].(string)
if s == "" {
return NewStringList([]string{}).String(), nil
}
return NewStringList(strings.Split(s, sep)).String(), nil
},
}
Expand Down
7 changes: 6 additions & 1 deletion config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ func TestInterpolateFuncLength(t *testing.T) {
},

// Lists
{
`${length(split(",", ""))}`,
"0",
false,
},
{
`${length(split(",", "a"))}`,
"1",
Expand Down Expand Up @@ -352,7 +357,7 @@ func TestInterpolateFuncSplit(t *testing.T) {

{
`${split(",", "")}`,
NewStringList([]string{""}).String(),
NewStringList([]string{}).String(),
false,
},

Expand Down
9 changes: 4 additions & 5 deletions config/string_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ func (sl StringList) Length() int {

// Returns a slice of strings as represented by this StringList
func (sl StringList) Slice() []string {
parts := strings.Split(string(sl), stringListDelim)

switch len(parts) {
case 0, 1:
if string(sl) == stringListDelim {
return []string{}
case 2:
}
if string(sl) == strings.Join([]string{stringListDelim, stringListDelim}, "") {
return []string{""}
}
parts := strings.Split(string(sl), stringListDelim)

// strip empty elements generated by leading and trailing delimiters
return parts[1 : len(parts)-1]
Expand Down
29 changes: 29 additions & 0 deletions config/string_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"testing"
)

func TestStringListLength(t *testing.T) {
empty := NewStringList([]string{})
l := empty.Length()
if l != 0 {
t.Errorf("L != 0")
}
}

func TestStringListSlice(t *testing.T) {
empty := NewStringList([]string{})
s := empty.Slice()
if len(s) != 0 {
t.Errorf("Empty StringList Slice not zero length")
}
oneelem_empty := NewStringList([]string{""})
s = oneelem_empty.Slice()
if len(s) != 1 {
t.Errorf("Stringlist of empty element not length 1")
}
if s[0] != "" {
t.Errorf("Stringlist of empty element first element not empty")
}
}

0 comments on commit ff8fb43

Please sign in to comment.