Test nested objects.

This commit is contained in:
scruzin 2019-01-13 12:09:06 +10:30
parent f8b8d06b2e
commit 8cd5627974
1 changed files with 28 additions and 8 deletions

View File

@ -207,15 +207,27 @@ func TestObject(t *testing.T) {
t.Errorf("Decoded wrong boolean value") t.Errorf("Decoded wrong boolean value")
} }
// Construct a more complicated object. // Construct a more complicated object that includes a nested object.
var obj2 Object var obj2 Object
for i, _ := range testStrings { for i, _ := range testStrings {
obj2.Properties = append(obj2.Properties, Property{Type: typeString, String: testStrings[i]}) obj2.Properties = append(obj2.Properties, Property{Type: typeString, String: testStrings[i]})
obj2.Properties = append(obj2.Properties, Property{Type: typeNumber, Number: float64(testNumbers[i])}) obj2.Properties = append(obj2.Properties, Property{Type: typeNumber, Number: float64(testNumbers[i])})
} }
obj2.Properties = append(obj2.Properties, Property{Type: typeBoolean, Number: 0}) obj2.Properties = append(obj2.Properties, Property{Type: TypeObject, Object: obj1})
obj2.Properties = append(obj2.Properties, Property{Type: typeBoolean, Number: 1}) obj2.Properties = append(obj2.Properties, Property{Type: typeBoolean, Number: 1})
// Retrieve nested object
obj, err := obj2.ObjectProperty("", 8)
if err != err {
t.Errorf("Failed to retrieve object")
}
if len(obj.Properties) < 1 {
t.Errorf("Properties missing for nested object")
}
if obj.Properties[0].Type != typeBoolean {
t.Errorf("Wrong property type for nested object")
}
// Encode it. // Encode it.
enc = buf[:] enc = buf[:]
enc, err = Encode(&obj2, enc) enc, err = Encode(&obj2, enc)
@ -234,25 +246,33 @@ func TestObject(t *testing.T) {
// Find some properties that exist. // Find some properties that exist.
s, err := obj2.StringProperty("", 2) s, err := obj2.StringProperty("", 2)
if err != nil { if err != nil {
t.Errorf("Property(2) failed") t.Errorf("StringProperty failed")
} }
if s != "foo" { if s != "foo" {
t.Errorf("Property(2) returned wrong Property") t.Errorf("StringProperty returned wrong String")
} }
n, err := obj2.NumberProperty("", 3) n, err := obj2.NumberProperty("", 3)
if err != nil { if err != nil {
t.Errorf("Property(3) failed") t.Errorf("NumberProperty failed")
} }
if n != 1 { if n != 1 {
t.Errorf("Property(3) returned wrong Property") t.Errorf("NumberProperty returned wrong Number")
}
obj, err = obj2.ObjectProperty("", 8)
if err != nil {
t.Errorf("ObjectProperty failed")
return
}
if obj.Properties[0].Type != typeBoolean {
t.Errorf("ObjectProperty returned object with wrong property")
} }
prop, err := obj2.Property("", 9, typeBoolean) prop, err := obj2.Property("", 9, typeBoolean)
if err != nil { if err != nil {
t.Errorf("Property(9) failed") t.Errorf("Property failed")
return return
} }
if prop.Number != 1 { if prop.Number != 1 {
t.Errorf("Property(9) returned wrong Property") t.Errorf("Property returned wrong Property")
} }
// Try to find one that doesn't exist. // Try to find one that doesn't exist.