github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/common/format_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/juju/common"
    11  )
    12  
    13  type ConformSuite struct{}
    14  
    15  var _ = gc.Suite(&ConformSuite{})
    16  
    17  func (s *ConformSuite) TestConformYAML(c *gc.C) {
    18  	var goodInterfaceTests = []struct {
    19  		description       string
    20  		inputInterface    interface{}
    21  		expectedInterface map[string]interface{}
    22  		expectedError     string
    23  	}{{
    24  		description: "An interface requiring no changes.",
    25  		inputInterface: map[string]interface{}{
    26  			"key1": "value1",
    27  			"key2": "value2",
    28  			"key3": map[string]interface{}{
    29  				"foo1": "val1",
    30  				"foo2": "val2"}},
    31  		expectedInterface: map[string]interface{}{
    32  			"key1": "value1",
    33  			"key2": "value2",
    34  			"key3": map[string]interface{}{
    35  				"foo1": "val1",
    36  				"foo2": "val2"}},
    37  	}, {
    38  		description: "Substitute a single inner map[i]i.",
    39  		inputInterface: map[string]interface{}{
    40  			"key1": "value1",
    41  			"key2": "value2",
    42  			"key3": map[interface{}]interface{}{
    43  				"foo1": "val1",
    44  				"foo2": "val2"}},
    45  		expectedInterface: map[string]interface{}{
    46  			"key1": "value1",
    47  			"key2": "value2",
    48  			"key3": map[string]interface{}{
    49  				"foo1": "val1",
    50  				"foo2": "val2"}},
    51  	}, {
    52  		description: "Substitute nested inner map[i]i.",
    53  		inputInterface: map[string]interface{}{
    54  			"key1a": "val1a",
    55  			"key2a": "val2a",
    56  			"key3a": map[interface{}]interface{}{
    57  				"key1b": "val1b",
    58  				"key2b": map[interface{}]interface{}{
    59  					"key1c": "val1c"}}},
    60  		expectedInterface: map[string]interface{}{
    61  			"key1a": "val1a",
    62  			"key2a": "val2a",
    63  			"key3a": map[string]interface{}{
    64  				"key1b": "val1b",
    65  				"key2b": map[string]interface{}{
    66  					"key1c": "val1c"}}},
    67  	}, {
    68  		description: "Substitute nested map[i]i within []i.",
    69  		inputInterface: map[string]interface{}{
    70  			"key1a": "val1a",
    71  			"key2a": []interface{}{5, "foo", map[string]interface{}{
    72  				"key1b": "val1b",
    73  				"key2b": map[interface{}]interface{}{
    74  					"key1c": "val1c"}}}},
    75  		expectedInterface: map[string]interface{}{
    76  			"key1a": "val1a",
    77  			"key2a": []interface{}{5, "foo", map[string]interface{}{
    78  				"key1b": "val1b",
    79  				"key2b": map[string]interface{}{
    80  					"key1c": "val1c"}}}},
    81  	}, {
    82  		description: "An inner map[interface{}]interface{} with an int key.",
    83  		inputInterface: map[string]interface{}{
    84  			"key1": "value1",
    85  			"key2": "value2",
    86  			"key3": map[interface{}]interface{}{
    87  				"foo1": "val1",
    88  				5:      "val2"}},
    89  		expectedError: "map keyed with non-string value",
    90  	}, {
    91  		description: "An inner []interface{} containing a map[i]i with an int key.",
    92  		inputInterface: map[string]interface{}{
    93  			"key1a": "val1b",
    94  			"key2a": "val2b",
    95  			"key3a": []interface{}{"foo1", 5, map[interface{}]interface{}{
    96  				"key1b": "val1b",
    97  				"key2b": map[interface{}]interface{}{
    98  					"key1c": "val1c",
    99  					5:       "val2c"}}}},
   100  		expectedError: "map keyed with non-string value",
   101  	}}
   102  
   103  	for i, test := range goodInterfaceTests {
   104  		c.Logf("test %d: %s", i, test.description)
   105  		input := test.inputInterface
   106  		cleansedInterfaceMap, err := common.ConformYAML(input)
   107  		if test.expectedError == "" {
   108  			if !c.Check(err, jc.ErrorIsNil) {
   109  				continue
   110  			}
   111  			c.Check(cleansedInterfaceMap, gc.DeepEquals, test.expectedInterface)
   112  		} else {
   113  			c.Check(err, gc.ErrorMatches, test.expectedError)
   114  		}
   115  	}
   116  }