2014-07-19 01:51:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-09-02 17:55:27 +00:00
|
|
|
package runtime
|
2014-07-19 01:51:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-09-05 23:11:30 +00:00
|
|
|
type EmbeddedTest struct {
|
|
|
|
JSONBase `yaml:",inline" json:",inline"`
|
|
|
|
Object EmbeddedObject `yaml:"object,omitempty" json:"object,omitempty"`
|
|
|
|
EmptyObject EmbeddedObject `yaml:"emptyObject,omitempty" json:"emptyObject,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*EmbeddedTest) IsAnAPIObject() {}
|
|
|
|
|
2014-09-05 21:43:35 +00:00
|
|
|
func TestEmbeddedObject(t *testing.T) {
|
2014-09-05 23:11:30 +00:00
|
|
|
// TODO(dbsmith) fix EmbeddedObject to not use DefaultScheme.
|
|
|
|
s := DefaultScheme
|
|
|
|
s.AddKnownTypes("", &EmbeddedTest{})
|
|
|
|
s.AddKnownTypes("v1beta1", &EmbeddedTest{})
|
2014-07-19 01:51:28 +00:00
|
|
|
|
|
|
|
outer := &EmbeddedTest{
|
|
|
|
JSONBase: JSONBase{ID: "outer"},
|
2014-09-05 21:43:35 +00:00
|
|
|
Object: EmbeddedObject{
|
2014-07-19 01:51:28 +00:00
|
|
|
&EmbeddedTest{
|
|
|
|
JSONBase: JSONBase{ID: "inner"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:11:30 +00:00
|
|
|
wire, err := s.Encode(outer)
|
2014-07-19 01:51:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected encode error '%v'", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Wire format is:\n%v\n", string(wire))
|
|
|
|
|
2014-09-05 23:11:30 +00:00
|
|
|
decoded, err := s.Decode(wire)
|
2014-07-19 01:51:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected decode error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := outer, decoded; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected: %#v but got %#v", e, a)
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:55:27 +00:00
|
|
|
// test JSON decoding, too, since Decode uses yaml unmarshalling.
|
2014-07-19 01:51:28 +00:00
|
|
|
var decodedViaJSON EmbeddedTest
|
|
|
|
err = json.Unmarshal(wire, &decodedViaJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected decode error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things that Decode would have done for us:
|
|
|
|
decodedViaJSON.Kind = ""
|
2014-07-26 00:59:41 +00:00
|
|
|
decodedViaJSON.APIVersion = ""
|
2014-07-19 01:51:28 +00:00
|
|
|
|
|
|
|
if e, a := outer, &decodedViaJSON; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected: %#v but got %#v", e, a)
|
|
|
|
}
|
|
|
|
}
|