1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
package dbus
import (
"bytes"
"reflect"
"testing"
)
type TestStruct struct {
TestInt int
TestStr string
}
func Test_VariantOfStruct(t *testing.T) {
tester := TestStruct{TestInt: 123, TestStr: "foobar"}
testerDecoded := []interface{}{123, "foobar"}
variant := MakeVariant(testerDecoded)
input := []interface{}{variant}
var output TestStruct
if err := Store(input, &output); err != nil {
t.Fatal(err)
}
if tester != output {
t.Fatalf("%v != %v\n", tester, output)
}
}
func Test_VariantOfSlicePtr(t *testing.T) {
value := []TestStruct{{1, "1"}}
dest := []*TestStruct{}
param := &Message{
Type: TypeMethodCall,
Flags: FlagNoAutoStart,
Headers: map[HeaderField]Variant{
FieldPath: MakeVariant(ObjectPath("/example")),
FieldDestination: MakeVariant(""),
FieldMember: MakeVariant("call"),
},
Body: []interface{}{value},
}
param.Headers[FieldSignature] = MakeVariant(SignatureOf(param.Body...))
buf := new(bytes.Buffer)
err := param.EncodeTo(buf, nativeEndian)
if err != nil {
t.Fatal(err)
}
msg, err := DecodeMessage(buf)
if err != nil {
t.Fatal(err)
}
if err := Store(msg.Body, &dest); err != nil || reflect.DeepEqual(value, dest) {
t.Log(err)
t.FailNow()
}
}
|