1// Copyright 2015 go-swagger maintainers 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package spec 16 17import ( 18 "encoding/json" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22) 23 24func TestXmlObject_Serialize(t *testing.T) { 25 obj1 := XMLObject{} 26 actual, err := json.Marshal(obj1) 27 if assert.NoError(t, err) { 28 assert.Equal(t, "{}", string(actual)) 29 } 30 31 obj2 := XMLObject{ 32 Name: "the name", 33 Namespace: "the namespace", 34 Prefix: "the prefix", 35 Attribute: true, 36 Wrapped: true, 37 } 38 39 actual, err = json.Marshal(obj2) 40 if assert.NoError(t, err) { 41 var ad map[string]interface{} 42 if assert.NoError(t, json.Unmarshal(actual, &ad)) { 43 assert.Equal(t, obj2.Name, ad["name"]) 44 assert.Equal(t, obj2.Namespace, ad["namespace"]) 45 assert.Equal(t, obj2.Prefix, ad["prefix"]) 46 assert.True(t, ad["attribute"].(bool)) 47 assert.True(t, ad["wrapped"].(bool)) 48 } 49 } 50} 51 52func TestXmlObject_Deserialize(t *testing.T) { 53 expected := XMLObject{} 54 actual := XMLObject{} 55 if assert.NoError(t, json.Unmarshal([]byte("{}"), &actual)) { 56 assert.Equal(t, expected, actual) 57 } 58 59 completed := `{"name":"the name","namespace":"the namespace","prefix":"the prefix","attribute":true,"wrapped":true}` 60 expected = XMLObject{ 61 Name: "the name", 62 Namespace: "the namespace", 63 Prefix: "the prefix", 64 Attribute: true, 65 Wrapped: true, 66 } 67 actual = XMLObject{} 68 if assert.NoError(t, json.Unmarshal([]byte(completed), &actual)) { 69 assert.Equal(t, expected, actual) 70 } 71} 72