Compare commits

..

No commits in common. "0f599e58ced05329d45f28abb86becb386ea5006" and "503f01bdddd2df36d6eb527fefe3d14aba79d70d" have entirely different histories.

1 changed files with 5 additions and 111 deletions

View File

@ -1,7 +1,6 @@
package reqdata package reqdata
import ( import (
"math"
"testing" "testing"
) )
@ -31,9 +30,8 @@ func TestSimpleString(t *testing.T) {
t.FailNow() t.FailNow()
} }
} }
func TestStringSlice(t *testing.T) { func TestStringSlice(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: `["str1", "str2"]`} p := Parameter{Parsed: false, File: false, Value: "some-string"}
err := p.Parse() err := p.Parse()
@ -47,118 +45,14 @@ func TestStringSlice(t *testing.T) {
t.FailNow() t.FailNow()
} }
slice, canCast := p.Value.([]interface{}) cast, canCast := p.Value.(string)
if !canCast { if !canCast {
t.Errorf("expected parameter to be a []interface{}") t.Errorf("expected parameter to be a string")
t.FailNow() t.FailNow()
} }
if len(slice) != 2 { if cast != "some-string" {
t.Errorf("expected 2 values, got %d", len(slice)) t.Errorf("expected parameter to equal 'some-string', got '%s'", cast)
t.FailNow() 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
BoolValue bool
}{
{"true", true},
{"false", false},
}
for i, tcase := range tcases {
t.Run("case "+string(i), func(t *testing.T) {
p := Parameter{Parsed: false, File: false, Value: tcase.Raw}
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.(bool)
if !canCast {
t.Errorf("expected parameter to be a bool")
t.FailNow()
}
if cast != tcase.BoolValue {
t.Errorf("expected a value of %t, got %t", tcase.BoolValue, cast)
t.FailNow()
}
})
}
}
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) {
p := Parameter{Parsed: false, File: false, Value: tcase.Raw}
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.(float64)
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)
t.FailNow()
}
})
}
} }