github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/environs/simplestreams/json_test.go (about)

     1  package simplestreams_test
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	jc "github.com/juju/testing/checkers"
     7  	gc "gopkg.in/check.v1"
     8  
     9  	"github.com/juju/juju/environs/simplestreams"
    10  )
    11  
    12  type jsonSuite struct{}
    13  
    14  func (s *jsonSuite) TestItemCollectionMarshalling(c *gc.C) {
    15  	// Ensure that unmarshalling a simplestreams.ItemCollection
    16  	// directly (not through ParseCloudMetadata) doesn't
    17  	// cause any surprises.
    18  	var m simplestreams.ItemCollection
    19  	m.Items = make(map[string]interface{})
    20  	err := json.Unmarshal([]byte(`{
    21          "items": {
    22              "a": "b",
    23              "c": 123 
    24          }
    25      }`), &m)
    26  	c.Assert(err, jc.ErrorIsNil)
    27  	c.Assert(m.Items, gc.DeepEquals, map[string]interface{}{
    28  		"a": "b",
    29  		"c": float64(123),
    30  	})
    31  	// Ensure marshalling works as expected, too.
    32  	b, err := json.Marshal(&m)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(string(b), gc.Equals, `{"items":{"a":"b","c":123}}`)
    35  }