Skip to content

Commit

Permalink
Add test for TagSet's UnmarhalText
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 4, 2019
1 parent 268e594 commit e38b216
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (t *TagSet) UnmarshalText(data []byte) error {
var list = bytes.Split(data, []byte(","))
*t = make(map[string]bool, len(list))
for _, key := range list {
(*t)[strings.TrimSpace(string(key))] = true
key := strings.TrimSpace(string(key))
if key == "" {
continue
}
(*t)[key] = true
}
return nil
}
Expand Down
23 changes: 22 additions & 1 deletion lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import (
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
"github.com/stretchr/testify/require"
null "gopkg.in/guregu/null.v3"
)

func TestOptions(t *testing.T) {
Expand Down Expand Up @@ -480,3 +481,23 @@ func TestOptionsEnv(t *testing.T) {
})
}
}

func TestTagSetTextUnmarshal(t *testing.T) {

var testMatrix = map[string]map[string]bool{
"": {},
"test": {"test": true},
"test1,test2": {"test1": true, "test2": true},
" test1 , test2 ": {"test1": true, "test2": true},
" test1 , , test2 ": {"test1": true, "test2": true},
" test1 ,, test2 ,,": {"test1": true, "test2": true},
}

for input, expected := range testMatrix {
var set = new(TagSet)
err := set.UnmarshalText([]byte(input))
require.NoError(t, err)

require.Equal(t, (map[string]bool)(*set), expected)
}
}

0 comments on commit e38b216

Please sign in to comment.