You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

105 lines
1.5 KiB

package forms
import (
"reflect"
"testing"
)
type testStruct struct {
A string
B int
C float64
D nested
Anon
PA *string
PD *nested
SA []string
SB []int
SC []float64
}
type nested struct {
A string
B int
C float64
}
type Anon struct {
E string
}
var input = Values{
`A`: {`test`},
`B`: {`10`},
`C`: {`1.25`},
`D.A`: {`test2`},
`D.B`: {`20`},
`D.C`: {`2.50`},
`E`: {`test3`},
`PA`: {`test4`},
`PD.A`: {`test5`},
`PD.B`: {`30`},
`PD.C`: {`3.75`},
`SA`: {`test6`, `slice`},
`SB`: {`3`, `2`},
`SC`: {`4.50`, `1.32`},
}
var expected = testStruct{
A: `test`,
B: 10,
C: 1.25,
D: nested{A: `test2`, B: 20, C: 2.50},
Anon: Anon{E: `test3`},
SA: []string{`test6`, `slice`},
SB: []int{3, 2},
SC: []float64{4.50, 1.32},
}
var expectedPA = `test4`
var expectedPD = nested{`test5`, 30, 3.75}
func TestDecode(t *testing.T) {
data := testStruct{}
_ = Decode(input, &data)
if *data.PA != expectedPA {
t.Errorf(`PA was incorrect. Expected %v but got %v`, expectedPA, *data.PA)
}
if *data.PD != expectedPD {
t.Errorf(`PA was incorrect. Expected %v but got %v`, expectedPD, *data.PD)
}
expected.PA, expected.PD = data.PA, data.PD
if !reflect.DeepEqual(&data, &expected) {
t.Errorf("Data did not match expected output. Expected \n%v\nbut got\n%v", expected, data)
}
}
func TestDecodeError(t *testing.T) {
var data struct {
A struct {
B float64
}
}
err := Decode(Values{`A.B`: {`a`}}, &data)
if err == nil {
t.Error(`Expected error but got nil`)
}
t.Log(err)
}