github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 // lp:1558657 129 now := time.Now() 130 for _, test := range []struct { 131 other time.Time 132 expected string 133 }{ 134 { 135 other: now, 136 expected: "just now", 137 }, { 138 other: now.Add(-1 * time.Second), 139 expected: "just now", 140 }, { 141 other: now.Add(-2 * time.Second), 142 expected: "2 seconds ago", 143 }, { 144 other: now.Add(-59 * time.Second), 145 expected: "59 seconds ago", 146 }, { 147 other: now.Add(-60 * time.Second), 148 expected: "1 minute ago", 149 }, { 150 other: now.Add(-61 * time.Second), 151 expected: "1 minute ago", 152 }, { 153 other: now.Add(-2 * time.Minute), 154 expected: "2 minutes ago", 155 }, { 156 other: now.Add(-59 * time.Minute), 157 expected: "59 minutes ago", 158 }, { 159 other: now.Add(-60 * time.Minute), 160 expected: "1 hour ago", 161 }, { 162 other: now.Add(-61 * time.Minute), 163 expected: "1 hour ago", 164 }, { 165 other: now.Add(-2 * time.Hour), 166 expected: "2 hours ago", 167 }, { 168 other: now.Add(-23 * time.Hour), 169 expected: "23 hours ago", 170 }, { 171 other: now.Add(-24 * time.Hour), 172 expected: now.Add(-24 * time.Hour).Format("2006-01-02"), 173 }, { 174 other: now.Add(-96 * time.Hour), 175 expected: now.Add(-96 * time.Hour).Format("2006-01-02"), 176 }, 177 } { 178 obtained := common.UserFriendlyDuration(test.other, now) 179 c.Check(obtained, gc.Equals, test.expected) 180 } 181 }