github.com/openshift-online/ocm-sdk-go@v0.1.473/data/digger_test.go (about) 1 /* 2 Copyright (c) 2021 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package data 18 19 import ( 20 "context" 21 "time" 22 23 . "github.com/onsi/ginkgo/v2/dsl/core" // nolint 24 . "github.com/onsi/ginkgo/v2/dsl/table" // nolint 25 . "github.com/onsi/gomega" // nolint 26 ) 27 28 var _ = Describe("Digger", func() { 29 var digger *Digger 30 31 BeforeEach(func() { 32 var err error 33 34 // Create a context: 35 ctx := context.Background() 36 37 // Create the digger: 38 digger, err = NewDigger(). 39 Build(ctx) 40 Expect(err).ToNot(HaveOccurred()) 41 }) 42 43 DescribeTable( 44 "Dig", 45 func(object interface{}, path string, expected interface{}) { 46 actual := digger.Dig(object, path) 47 if expected == nil { 48 Expect(actual).To(BeNil()) 49 } else { 50 Expect(actual).To(Equal(expected)) 51 } 52 }, 53 Entry( 54 "Empty path on nil", 55 nil, 56 "", 57 nil, 58 ), 59 Entry( 60 "Path with one segment on nil", 61 nil, 62 "a", 63 nil, 64 ), 65 Entry( 66 "Path with two segments on nil", 67 nil, 68 "a.b", 69 nil, 70 ), 71 Entry( 72 "Empty path on pointer", 73 &subject{}, 74 "", 75 &subject{}, 76 ), 77 Entry( 78 "Path to method returning value", 79 &subject{}, 80 "a", 81 "a_value", 82 ), 83 Entry( 84 "Path to method returning value and true", 85 &subject{}, 86 "b", 87 "b_value", 88 ), 89 Entry( 90 "Path to method returning value and false", 91 &subject{}, 92 "c", 93 nil, 94 ), 95 Entry( 96 "Path with one underscore", 97 &subject{}, 98 "my_d", 99 "my_d_value", 100 ), 101 Entry( 102 "Path with two segments", 103 &subject{}, 104 "s.a", 105 "s/a_value", 106 ), 107 Entry( 108 "Path with three segments", 109 &subject{}, 110 "s.s.a", 111 "s/s/a_value", 112 ), 113 Entry( 114 "Integer method", 115 &subject{}, 116 "e", 117 123, 118 ), 119 Entry( 120 "Float method", 121 &subject{}, 122 "f", 123 1.23, 124 ), 125 Entry( 126 "Time method", 127 &subject{}, 128 "g", 129 time.Unix(1, 23), 130 ), 131 Entry( 132 "Duration method", 133 &subject{}, 134 "h", 135 time.Duration(123), 136 ), 137 Entry( 138 "Prefers `Get...` and returns nil for false", 139 &subject{}, 140 "i", 141 nil, 142 ), 143 Entry( 144 "Doesn't remove `Get...` prefix if not followed by word", 145 &subject{}, 146 "getaway", 147 "getaway_value", 148 ), 149 Entry( 150 "Value receiver", 151 subject{}, 152 "j", 153 "j_value", 154 ), 155 Entry( 156 "String field", 157 subject{ 158 K: "k_value", 159 }, 160 "k", 161 "k_value", 162 ), 163 Entry( 164 "Integer field", 165 subject{ 166 L: 123, 167 }, 168 "l", 169 123, 170 ), 171 Entry( 172 "Float field", 173 subject{ 174 M: 1.23, 175 }, 176 "m", 177 1.23, 178 ), 179 Entry( 180 "Time field", 181 subject{ 182 O: time.Unix(1, 23), 183 }, 184 "o", 185 time.Unix(1, 23), 186 ), 187 Entry( 188 "Duration field", 189 subject{ 190 P: time.Duration(123), 191 }, 192 "p", 193 time.Duration(123), 194 ), 195 Entry( 196 "Nil field", 197 subject{ 198 Q: nil, 199 }, 200 "q", 201 nil, 202 ), 203 Entry( 204 "Field that doesn't exist", 205 subject{}, 206 "does_not_exist", 207 nil, 208 ), 209 Entry( 210 "Map field", 211 map[string]interface{}{ 212 "id": "123", 213 }, 214 "id", 215 "123", 216 ), 217 Entry( 218 "Nested map field", 219 map[string]interface{}{ 220 "object": map[string]interface{}{ 221 "id": "123", 222 }, 223 }, 224 "object.id", 225 "123", 226 ), 227 ) 228 }) 229 230 // subject is used as a subject of the digger tests. 231 type subject struct { 232 prefix string 233 K string 234 L int 235 M float64 236 O time.Time 237 P time.Duration 238 Q *int 239 } 240 241 func (s *subject) S() *subject { 242 return &subject{ 243 prefix: s.prefix + "s/", 244 } 245 } 246 247 func (s *subject) A() string { 248 return s.prefix + "a_value" 249 } 250 251 func (s *subject) GetB() (value string, ok bool) { 252 value = s.prefix + "b_value" 253 ok = true 254 return 255 } 256 257 func (s *subject) GetC() (value string, ok bool) { 258 value = s.prefix + "c_value" 259 ok = false 260 return 261 } 262 263 func (s *subject) MyD() string { 264 return s.prefix + "my_d_value" 265 } 266 267 func (s *subject) E() int { 268 return 123 269 } 270 271 func (s *subject) F() float64 { 272 return 1.23 273 } 274 275 func (s *subject) G() time.Time { 276 return time.Unix(1, 23) 277 } 278 279 func (s *subject) H() time.Duration { 280 return time.Duration(123) 281 } 282 283 func (s *subject) I() string { 284 return "" 285 } 286 287 func (s *subject) GetI() (value string, ok bool) { 288 value = "" 289 ok = false 290 return 291 } 292 293 func (s *subject) Getaway() string { 294 return "getaway_value" 295 } 296 297 func (s subject) J() string { 298 return "j_value" 299 }