initial
This commit is contained in:
52
formscanner_test.go
Normal file
52
formscanner_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package webutil
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormScannerTypes(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
form url.Values
|
||||
dest any
|
||||
want any
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "string", form: url.Values{"f": {"hello"}}, dest: new(string), want: "hello"},
|
||||
{name: "int", form: url.Values{"f": {"42"}}, dest: new(int), want: int(42)},
|
||||
{name: "int8", form: url.Values{"f": {"8"}}, dest: new(int8), want: int8(8)},
|
||||
{name: "int16", form: url.Values{"f": {"16"}}, dest: new(int16), want: int16(16)},
|
||||
{name: "int32", form: url.Values{"f": {"32"}}, dest: new(int32), want: int32(32)},
|
||||
{name: "int64", form: url.Values{"f": {"64"}}, dest: new(int64), want: int64(64)},
|
||||
{name: "uint", form: url.Values{"f": {"1"}}, dest: new(uint), want: uint(1)},
|
||||
{name: "uint8", form: url.Values{"f": {"8"}}, dest: new(uint8), want: uint8(8)},
|
||||
{name: "uint16", form: url.Values{"f": {"16"}}, dest: new(uint16), want: uint16(16)},
|
||||
{name: "uint32", form: url.Values{"f": {"32"}}, dest: new(uint32), want: uint32(32)},
|
||||
{name: "uint64", form: url.Values{"f": {"64"}}, dest: new(uint64), want: uint64(64)},
|
||||
{name: "float32", form: url.Values{"f": {"1.5"}}, dest: new(float32), want: float32(1.5)},
|
||||
{name: "float64", form: url.Values{"f": {"1.5"}}, dest: new(float64), want: float64(1.5)},
|
||||
{name: "bool/present", form: url.Values{"f": {""}}, dest: new(bool), want: true},
|
||||
{name: "bool/absent", form: url.Values{}, dest: new(bool), want: false},
|
||||
{name: "int/invalid", form: url.Values{"f": {"abc"}}, dest: new(int), wantErr: true},
|
||||
{name: "unsupported type", form: url.Values{"f": {"x"}}, dest: new([]string), wantErr: true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := NewFormScanner(tc.form)
|
||||
s.Scan("f", tc.dest)
|
||||
err := s.Error()
|
||||
if tc.wantErr != (err != nil) {
|
||||
t.Fatalf("error = %v, wantErr %v", err, tc.wantErr)
|
||||
}
|
||||
if !tc.wantErr {
|
||||
got := reflect.ValueOf(tc.dest).Elem().Interface()
|
||||
if got != tc.want {
|
||||
t.Fatalf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user