forms/forms_test.go

90 lines
1.3 KiB
Go
Raw Normal View History

2018-12-16 15:50:35 +01:00
package forms
import (
2019-10-02 09:58:50 +02:00
"reflect"
2018-12-16 15:50:35 +01:00
"testing"
)
type testStruct struct {
A string
B int
C float64
D nested
Anon
PA *string
PD *nested
2019-10-01 16:08:20 +02:00
SA []string
SB []int
SC []float64
2018-12-16 15:50:35 +01:00
}
type nested struct {
A string
B int
C float64
}
type Anon struct {
E string
}
2019-10-02 09:58:50 +02:00
var input = Values{
`A`: {`test`},
`B`: {`10`},
`C`: {`1.25`},
`D.A`: {`test2`},
`D.B`: {`20`},
`D.C`: {`2.50`},
`E`: {`test3`},
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
`PA`: {`test4`},
`PD.A`: {`test5`},
`PD.B`: {`30`},
`PD.C`: {`3.75`},
`SA`: {`test6`, `slice`},
`SB`: {`3`, `2`},
`SC`: {`4.50`, `1.32`},
}
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
var expected = testStruct{
A: `test`,
B: 10,
C: 1.25,
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
D: nested{A: `test2`, B: 20, C: 2.50},
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
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{}
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
Decode(input, &data)
2019-10-01 16:08:20 +02:00
2019-10-02 09:58:50 +02:00
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)
}
2018-12-16 15:50:35 +01:00
2019-10-02 09:58:50 +02:00
expected.PA, expected.PD = data.PA, data.PD
if !reflect.DeepEqual(&data, &expected) {
t.Errorf("Data dit not match expected output. Expected \n%v\nbut got\n%v", expected, data)
}
2018-12-16 15:50:35 +01:00
}