2020-03-01 20:34:14 +00:00
|
|
|
package reqdata
|
|
|
|
|
|
|
|
import (
|
2020-03-02 21:01:16 +00:00
|
|
|
"math"
|
2020-03-01 20:34:14 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleString(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter("some-string")
|
2020-03-01 20:34:14 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
cast, canCast := p.(string)
|
2020-03-01 20:34:14 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 20:56:05 +00:00
|
|
|
|
2020-03-02 21:49:17 +00:00
|
|
|
func TestSimpleFloat(t *testing.T) {
|
|
|
|
tcases := []float64{12.3456789, -12.3456789, 0.0000001, -0.0000001}
|
|
|
|
|
|
|
|
for i, tcase := range tcases {
|
|
|
|
t.Run("case "+string(i), func(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter(tcase)
|
2020-03-02 21:49:17 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
cast, canCast := p.(float64)
|
2020-03-02 21:49:17 +00:00
|
|
|
if !canCast {
|
|
|
|
t.Errorf("expected parameter to be a float64")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if math.Abs(cast-tcase) > 0.00000001 {
|
|
|
|
t.Errorf("expected parameter to equal '%f', got '%f'", tcase, cast)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleBool(t *testing.T) {
|
|
|
|
tcases := []bool{true, false}
|
|
|
|
|
|
|
|
for i, tcase := range tcases {
|
|
|
|
t.Run("case "+string(i), func(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter(tcase)
|
2020-03-02 21:49:17 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
cast, canCast := p.(bool)
|
2020-03-02 21:49:17 +00:00
|
|
|
if !canCast {
|
|
|
|
t.Errorf("expected parameter to be a bool")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cast != tcase {
|
|
|
|
t.Errorf("expected parameter to equal '%t', got '%t'", tcase, cast)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:42:45 +00:00
|
|
|
func TestJsonStringSlice(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter(`["str1", "str2"]`)
|
2020-03-01 20:34:14 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
slice, canCast := p.([]interface{})
|
2020-03-02 21:42:45 +00:00
|
|
|
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 TestStringSlice(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter([]string{"str1", "str2"})
|
2020-03-02 21:42:45 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
slice, canCast := p.([]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
|
|
|
}
|
2020-03-02 20:56:05 +00:00
|
|
|
|
|
|
|
func TestJsonPrimitiveBool(t *testing.T) {
|
|
|
|
tcases := []struct {
|
|
|
|
Raw string
|
|
|
|
BoolValue bool
|
|
|
|
}{
|
|
|
|
{"true", true},
|
|
|
|
{"false", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tcase := range tcases {
|
|
|
|
t.Run("case "+string(i), func(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter(tcase.Raw)
|
2020-03-02 20:56:05 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
cast, canCast := p.(bool)
|
2020-03-02 20:56:05 +00:00
|
|
|
if !canCast {
|
|
|
|
t.Errorf("expected parameter to be a bool")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cast != tcase.BoolValue {
|
2020-03-02 21:00:39 +00:00
|
|
|
t.Errorf("expected a value of %t, got %t", tcase.BoolValue, cast)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-03-02 21:01:16 +00:00
|
|
|
|
|
|
|
func TestJsonPrimitiveFloat(t *testing.T) {
|
|
|
|
tcases := []struct {
|
|
|
|
Raw string
|
|
|
|
FloatValue float64
|
|
|
|
}{
|
|
|
|
{"1", 1},
|
|
|
|
{"-1", -1},
|
|
|
|
|
|
|
|
{"0.001", 0.001},
|
|
|
|
{"-0.001", -0.001},
|
|
|
|
|
|
|
|
{"1.9992", 1.9992},
|
|
|
|
{"-1.9992", -1.9992},
|
|
|
|
|
|
|
|
{"19992", 19992},
|
|
|
|
{"-19992", -19992},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tcase := range tcases {
|
|
|
|
t.Run("case "+string(i), func(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter(tcase.Raw)
|
2020-03-02 21:01:16 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
cast, canCast := p.(float64)
|
2020-03-02 21:01:16 +00:00
|
|
|
if !canCast {
|
|
|
|
t.Errorf("expected parameter to be a float64")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if math.Abs(cast-tcase.FloatValue) > 0.00001 {
|
|
|
|
t.Errorf("expected a value of %f, got %f", tcase.FloatValue, cast)
|
2020-03-02 20:56:05 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-03-02 21:42:59 +00:00
|
|
|
|
|
|
|
func TestJsonBoolSlice(t *testing.T) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter([]string{"true", "false"})
|
2020-03-02 21:42:59 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
slice, canCast := p.([]interface{})
|
2020-03-02 21:42:59 +00:00
|
|
|
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) {
|
2020-03-21 13:45:39 +00:00
|
|
|
p := parseParameter([]bool{true, false})
|
2020-03-02 21:42:59 +00:00
|
|
|
|
2020-03-21 13:45:39 +00:00
|
|
|
slice, canCast := p.([]interface{})
|
2020-03-02 21:42:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|