This commit is contained in:
Nise Void 2019-10-02 09:58:50 +02:00
parent 056b574ce1
commit 0efb52d29e
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3

View File

@ -1,7 +1,7 @@
package forms package forms
import ( import (
"fmt" "reflect"
"testing" "testing"
) )
@ -31,30 +31,59 @@ type Anon struct {
E string E string
} }
func TestDecode(t *testing.T) { var input = Values{
data := &testStruct{} `A`: {`test`},
`B`: {`10`},
`C`: {`1.25`},
Decode(Values{ `D.A`: {`test2`},
`A`: {`test`}, `D.B`: {`20`},
`B`: {`10`}, `D.C`: {`2.50`},
`C`: {`1.25`},
`D.A`: {`test2`}, `E`: {`test3`},
`D.B`: {`20`},
`D.C`: {`2.50`},
`E`: {`test3`}, `PA`: {`test4`},
`PA`: {`test4`}, `PD.A`: {`test5`},
`PD.B`: {`30`},
`PD.C`: {`3.75`},
`PD.A`: {`test5`}, `SA`: {`test6`, `slice`},
`PD.B`: {`30`}, `SB`: {`3`, `2`},
`PD.C`: {`3.75`}, `SC`: {`4.50`, `1.32`},
}
`SA`: {`test6`, `slice`},
`SB`: {`3`, `2`}, var expected = testStruct{
`SC`: {`4.50`, `1.32`}, A: `test`,
}, data) B: 10,
C: 1.25,
fmt.Println(`data:`, data, *data.PA, *data.PD)
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 dit not match expected output. Expected \n%v\nbut got\n%v", expected, data)
}
} }