package webutil import ( "net/url" "testing" ) func TestScanStruct(t *testing.T) { t.Run("fields scanned by name", func(t *testing.T) { type S struct { Name string Age int Active bool } var s S err := ScanStruct(url.Values{ "Name": {"Alice"}, "Age": {"30"}, "Active": {"on"}, }, &s) if err != nil { t.Fatal(err) } if s.Name != "Alice" || s.Age != 30 || !s.Active { t.Fatalf("unexpected: %+v", s) } }) t.Run("missing form key leaves zero value", func(t *testing.T) { type S struct { Name string Age int } var s S if err := ScanStruct(url.Values{"Name": {"Bob"}}, &s); err != nil { t.Fatal(err) } if s.Name != "Bob" || s.Age != 0 { t.Fatalf("unexpected: %+v", s) } }) t.Run("extra form keys ignored", func(t *testing.T) { type S struct{ Name string } var s S if err := ScanStruct(url.Values{"Name": {"Alice"}, "Extra": {"ignored"}}, &s); err != nil { t.Fatal(err) } }) t.Run("unexported fields skipped", func(t *testing.T) { type S struct { Name string secret string } var s S if err := ScanStruct(url.Values{"Name": {"Alice"}, "secret": {"oops"}}, &s); err != nil { t.Fatal(err) } if s.secret != "" { t.Fatal("unexported field should not be set") } }) t.Run("parse error", func(t *testing.T) { type S struct{ Age int } var s S if err := ScanStruct(url.Values{"Age": {"not-a-number"}}, &s); err == nil { t.Fatal("expected error") } }) t.Run("requires pointer to struct", func(t *testing.T) { if err := ScanStruct(url.Values{}, "not a struct"); err == nil { t.Fatal("expected error") } }) }