2020-03-01 20:34:14 +00:00
|
|
|
package reqdata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleString(t *testing.T) {
|
|
|
|
p := Parameter{Parsed: false, File: false, Value: "some-string"}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
cast, canCast := p.Value.(string)
|
|
|
|
if !canCast {
|
|
|
|
t.Errorf("expected parameter to be a string")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cast != "some-string" {
|
|
|
|
t.Errorf("expected parameter to equal 'some-string', got '%s'", cast)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestStringSlice(t *testing.T) {
|
2020-03-02 20:51:06 +00:00
|
|
|
p := Parameter{Parsed: false, File: false, Value: `["str1", "str2"]`}
|
2020-03-01 20:34:14 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:51:06 +00:00
|
|
|
slice, canCast := p.Value.([]interface{})
|
2020-03-01 20:34:14 +00:00
|
|
|
if !canCast {
|
2020-03-02 20:51:06 +00:00
|
|
|
t.Errorf("expected parameter to be a []interface{}")
|
2020-03-01 20:34:14 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:51:06 +00:00
|
|
|
if len(slice) != 2 {
|
|
|
|
t.Errorf("expected 2 values, got %d", len(slice))
|
2020-03-01 20:34:14 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2020-03-02 20:51:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-01 20:34:14 +00:00
|
|
|
}
|