github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 "time" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/juju/common" 14 ) 15 16 type ConformSuite struct{} 17 18 var _ = gc.Suite(&ConformSuite{}) 19 20 func (s *ConformSuite) TestConformYAML(c *gc.C) { 21 var goodInterfaceTests = []struct { 22 description string 23 inputInterface interface{} 24 expectedInterface map[string]interface{} 25 expectedError string 26 }{{ 27 description: "An interface requiring no changes.", 28 inputInterface: map[string]interface{}{ 29 "key1": "value1", 30 "key2": "value2", 31 "key3": map[string]interface{}{ 32 "foo1": "val1", 33 "foo2": "val2"}}, 34 expectedInterface: map[string]interface{}{ 35 "key1": "value1", 36 "key2": "value2", 37 "key3": map[string]interface{}{ 38 "foo1": "val1", 39 "foo2": "val2"}}, 40 }, { 41 description: "Substitute a single inner map[i]i.", 42 inputInterface: map[string]interface{}{ 43 "key1": "value1", 44 "key2": "value2", 45 "key3": map[interface{}]interface{}{ 46 "foo1": "val1", 47 "foo2": "val2"}}, 48 expectedInterface: map[string]interface{}{ 49 "key1": "value1", 50 "key2": "value2", 51 "key3": map[string]interface{}{ 52 "foo1": "val1", 53 "foo2": "val2"}}, 54 }, { 55 description: "Substitute nested inner map[i]i.", 56 inputInterface: map[string]interface{}{ 57 "key1a": "val1a", 58 "key2a": "val2a", 59 "key3a": map[interface{}]interface{}{ 60 "key1b": "val1b", 61 "key2b": map[interface{}]interface{}{ 62 "key1c": "val1c"}}}, 63 expectedInterface: map[string]interface{}{ 64 "key1a": "val1a", 65 "key2a": "val2a", 66 "key3a": map[string]interface{}{ 67 "key1b": "val1b", 68 "key2b": map[string]interface{}{ 69 "key1c": "val1c"}}}, 70 }, { 71 description: "Substitute nested map[i]i within []i.", 72 inputInterface: map[string]interface{}{ 73 "key1a": "val1a", 74 "key2a": []interface{}{5, "foo", map[string]interface{}{ 75 "key1b": "val1b", 76 "key2b": map[interface{}]interface{}{ 77 "key1c": "val1c"}}}}, 78 expectedInterface: map[string]interface{}{ 79 "key1a": "val1a", 80 "key2a": []interface{}{5, "foo", map[string]interface{}{ 81 "key1b": "val1b", 82 "key2b": map[string]interface{}{ 83 "key1c": "val1c"}}}}, 84 }, { 85 description: "An inner map[interface{}]interface{} with an int key.", 86 inputInterface: map[string]interface{}{ 87 "key1": "value1", 88 "key2": "value2", 89 "key3": map[interface{}]interface{}{ 90 "foo1": "val1", 91 5: "val2"}}, 92 expectedError: "map keyed with non-string value", 93 }, { 94 description: "An inner []interface{} containing a map[i]i with an int key.", 95 inputInterface: map[string]interface{}{ 96 "key1a": "val1b", 97 "key2a": "val2b", 98 "key3a": []interface{}{"foo1", 5, map[interface{}]interface{}{ 99 "key1b": "val1b", 100 "key2b": map[interface{}]interface{}{ 101 "key1c": "val1c", 102 5: "val2c"}}}}, 103 expectedError: "map keyed with non-string value", 104 }} 105 106 for i, test := range goodInterfaceTests { 107 c.Logf("test %d: %s", i, test.description) 108 input := test.inputInterface 109 cleansedInterfaceMap, err := common.ConformYAML(input) 110 if test.expectedError == "" { 111 if !c.Check(err, jc.ErrorIsNil) { 112 continue 113 } 114 c.Check(cleansedInterfaceMap, gc.DeepEquals, test.expectedInterface) 115 } else { 116 c.Check(err, gc.ErrorMatches, test.expectedError) 117 } 118 } 119 } 120 121 type userFriendlyDurationSuite struct { 122 testing.IsolationSuite 123 } 124 125 var _ = gc.Suite(&userFriendlyDurationSuite{}) 126 127 func (*userFriendlyDurationSuite) TestFormat(c *gc.C) { 128 now := time.Now() 129 for _, test := range []struct { 130 other time.Time 131 expected string 132 }{ 133 { 134 other: now, 135 expected: "just now", 136 }, { 137 other: now.Add(-1 * time.Second), 138 expected: "just now", 139 }, { 140 other: now.Add(-2 * time.Second), 141 expected: "2 seconds ago", 142 }, { 143 other: now.Add(-59 * time.Second), 144 expected: "59 seconds ago", 145 }, { 146 other: now.Add(-60 * time.Second), 147 expected: "1 minute ago", 148 }, { 149 other: now.Add(-61 * time.Second), 150 expected: "1 minute ago", 151 }, { 152 other: now.Add(-2 * time.Minute), 153 expected: "2 minutes ago", 154 }, { 155 other: now.Add(-59 * time.Minute), 156 expected: "59 minutes ago", 157 }, { 158 other: now.Add(-60 * time.Minute), 159 expected: "1 hour ago", 160 }, { 161 other: now.Add(-61 * time.Minute), 162 expected: "1 hour ago", 163 }, { 164 other: now.Add(-2 * time.Hour), 165 expected: "2 hours ago", 166 }, { 167 other: now.Add(-23 * time.Hour), 168 expected: "23 hours ago", 169 }, { 170 other: now.Add(-24 * time.Hour), 171 expected: now.Add(-24 * time.Hour).Format("2006-01-02"), 172 }, { 173 other: now.Add(-96 * time.Hour), 174 expected: now.Add(-96 * time.Hour).Format("2006-01-02"), 175 }, 176 } { 177 obtained := common.UserFriendlyDuration(test.other, now) 178 c.Check(obtained, gc.Equals, test.expected) 179 } 180 }