From e132a5af42db83f2b6173b878fe7921aa32b81fd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 2 Mar 2020 22:19:28 +0100 Subject: [PATCH] test conversion from 1-sized slice to first element (bool vs json boolean primitive) --- internal/reqdata/parameter_test.go | 106 +++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/internal/reqdata/parameter_test.go b/internal/reqdata/parameter_test.go index c4a9ce4..7e35532 100644 --- a/internal/reqdata/parameter_test.go +++ b/internal/reqdata/parameter_test.go @@ -162,3 +162,109 @@ func TestJsonPrimitiveFloat(t *testing.T) { } } + +func TestOneSliceStringToString(t *testing.T) { + p := Parameter{Parsed: false, File: false, Value: []string{"lonely-string"}} + + if err := p.Parse(); 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 != "lonely-string" { + t.Errorf("expected a value of '%s', got '%s'", "lonely-string", cast) + t.FailNow() + } +} + +func TestOneSliceBoolToBool(t *testing.T) { + tcases := []struct { + Raw bool + }{ + {true}, + {false}, + } + + for i, tcase := range tcases { + + t.Run("case "+string(i), func(t *testing.T) { + + p := Parameter{Parsed: false, File: false, Value: []bool{tcase.Raw}} + + if err := p.Parse(); 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.Raw { + t.Errorf("expected a value of '%t', got '%t'", tcase.Raw, cast) + t.FailNow() + } + + }) + } + +} + +func TestOneSliceJsonBoolToBool(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: []string{tcase.Raw}} + + if err := p.Parse(); 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() + } + + }) + } + +}