...

Source file src/github.com/go-openapi/spec/xml_object_test.go

Documentation: github.com/go-openapi/spec

     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  
    15  package spec
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestXmlObject_Serialize(t *testing.T) {
    26  	obj1 := XMLObject{}
    27  	actual, err := json.Marshal(obj1)
    28  	require.NoError(t, err)
    29  	assert.Equal(t, "{}", string(actual))
    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  	require.NoError(t, err)
    41  
    42  	var ad map[string]interface{}
    43  	require.NoError(t, json.Unmarshal(actual, &ad))
    44  	assert.Equal(t, obj2.Name, ad["name"])
    45  	assert.Equal(t, obj2.Namespace, ad["namespace"])
    46  	assert.Equal(t, obj2.Prefix, ad["prefix"])
    47  	assert.True(t, ad["attribute"].(bool))
    48  	assert.True(t, ad["wrapped"].(bool))
    49  }
    50  
    51  func TestXmlObject_Deserialize(t *testing.T) {
    52  	expected := XMLObject{}
    53  	actual := XMLObject{}
    54  	require.NoError(t, json.Unmarshal([]byte("{}"), &actual))
    55  	assert.Equal(t, expected, actual)
    56  
    57  	completed := `{"name":"the name","namespace":"the namespace","prefix":"the prefix","attribute":true,"wrapped":true}`
    58  	expected = XMLObject{
    59  		Name:      "the name",
    60  		Namespace: "the namespace",
    61  		Prefix:    "the prefix",
    62  		Attribute: true,
    63  		Wrapped:   true,
    64  	}
    65  
    66  	actual = XMLObject{}
    67  	require.NoError(t, json.Unmarshal([]byte(completed), &actual))
    68  	assert.Equal(t, expected, actual)
    69  }
    70  

View as plain text