Compare commits

...

3 Commits

Author SHA1 Message Date
Adrien Marquès feec6e96d0
test bool slice vs. json bool slice
continuous-integration/drone/push Build is passing Details
2020-03-02 22:42:59 +01:00
Adrien Marquès c2a88f0d2d
test string slice vs. json string slice 2020-03-02 22:42:45 +01:00
Adrien Marquès e1cdd1c2a3
fix parsing non-string slice values
- only string were parsed using wrapped json
 - now we also keep primitive types
2020-03-02 22:42:17 +01:00
2 changed files with 134 additions and 2 deletions

View File

@ -79,7 +79,7 @@ func parseParameter(data interface{}) (interface{}, error) {
// ignore non-string
if element.Kind() != reflect.String {
result[i] = nil
result[i] = element.Interface()
continue
}

View File

@ -32,7 +32,7 @@ func TestSimpleString(t *testing.T) {
}
}
func TestStringSlice(t *testing.T) {
func TestJsonStringSlice(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: `["str1", "str2"]`}
err := p.Parse()
@ -76,6 +76,50 @@ func TestStringSlice(t *testing.T) {
}
func TestStringSlice(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: []string{"str1", "str2"}}
err := p.Parse()
if err != nil {
t.Errorf("unexpected error: <%s>", err)
t.FailNow()
}
if !p.Parsed {
t.Errorf("expected parameter to be parsed")
t.FailNow()
}
slice, canCast := p.Value.([]interface{})
if !canCast {
t.Errorf("expected parameter to be a []interface{}")
t.FailNow()
}
if len(slice) != 2 {
t.Errorf("expected 2 values, got %d", len(slice))
t.FailNow()
}
results := []string{"str1", "str2"}
for i, res := range results {
cast, canCast := slice[i].(string)
if !canCast {
t.Errorf("expected parameter %d to be a []string", i)
continue
}
if cast != res {
t.Errorf("expected first value to be '%s', got '%s'", res, cast)
continue
}
}
}
func TestJsonPrimitiveBool(t *testing.T) {
tcases := []struct {
Raw string
@ -162,3 +206,91 @@ func TestJsonPrimitiveFloat(t *testing.T) {
}
}
func TestJsonBoolSlice(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: []string{"true", "false"}}
err := p.Parse()
if err != nil {
t.Errorf("unexpected error: <%s>", err)
t.FailNow()
}
if !p.Parsed {
t.Errorf("expected parameter to be parsed")
t.FailNow()
}
slice, canCast := p.Value.([]interface{})
if !canCast {
t.Errorf("expected parameter to be a []interface{}")
t.FailNow()
}
if len(slice) != 2 {
t.Errorf("expected 2 values, got %d", len(slice))
t.FailNow()
}
results := []bool{true, false}
for i, res := range results {
cast, canCast := slice[i].(bool)
if !canCast {
t.Errorf("expected parameter %d to be a []bool", i)
continue
}
if cast != res {
t.Errorf("expected first value to be '%t', got '%t'", res, cast)
continue
}
}
}
func TestBoolSlice(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: []bool{true, false}}
err := p.Parse()
if err != nil {
t.Errorf("unexpected error: <%s>", err)
t.FailNow()
}
if !p.Parsed {
t.Errorf("expected parameter to be parsed")
t.FailNow()
}
slice, canCast := p.Value.([]interface{})
if !canCast {
t.Errorf("expected parameter to be a []interface{}")
t.FailNow()
}
if len(slice) != 2 {
t.Errorf("expected 2 values, got %d", len(slice))
t.FailNow()
}
results := []bool{true, false}
for i, res := range results {
cast, canCast := slice[i].(bool)
if !canCast {
t.Errorf("expected parameter %d to be a bool, got %v", i, slice[i])
continue
}
if cast != res {
t.Errorf("expected first value to be '%t', got '%t'", res, cast)
continue
}
}
}