| 1 | package cli_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/parsing" |
| 9 | "github.com/tomwright/dasel/v3/parsing/json" |
| 10 | "github.com/tomwright/dasel/v3/parsing/kdl" |
| 11 | "github.com/tomwright/dasel/v3/parsing/toml" |
| 12 | "github.com/tomwright/dasel/v3/parsing/yaml" |
| 13 | ) |
| 14 | |
| 15 | func newStringWithFormat(format parsing.Format, data string) bytesWithFormat { |
| 16 | return bytesWithFormat{ |
| 17 | format: format, |
| 18 | data: append([]byte(data), []byte("\n")...), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | type bytesWithFormat struct { |
| 23 | format parsing.Format |
| 24 | data []byte |
| 25 | } |
| 26 | |
| 27 | type testCases struct { |
| 28 | selector string |
| 29 | in []bytesWithFormat |
| 30 | out []bytesWithFormat |
| 31 | args []string |
| 32 | skip []string |
| 33 | } |
| 34 | |
| 35 | func (tcs testCases) run(t *testing.T) { |
| 36 | for _, i := range tcs.in { |
| 37 | for _, o := range tcs.out { |
| 38 | tcName := fmt.Sprintf("%s to %s", i.format.String(), o.format.String()) |
| 39 | |
| 40 | if slices.Contains(tcs.skip, tcName) { |
| 41 | // Run a test and skip for visibility. |
| 42 | t.Run(tcName, func(t *testing.T) { |
| 43 | t.Skip() |
| 44 | }) |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | args := slices.Clone(tcs.args) |
| 49 | args = append(args, "-i", i.format.String(), "-o", o.format.String()) |
| 50 | if tcs.selector != "" { |
| 51 | args = append(args, tcs.selector) |
| 52 | } |
| 53 | tc := testCase{ |
| 54 | args: args, |
| 55 | in: i.data, |
| 56 | stdout: o.data, |
| 57 | } |
| 58 | t.Run(tcName, runTest(tc)) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestCrossFormatHappyPath(t *testing.T) { |
| 64 | jsonInputData := newStringWithFormat(json.JSON, `{ |
| 65 | "oneTwoThree": 123, |
| 66 | "oneTwoDotThree": 12.3, |
| 67 | "hello": "world", |
| 68 | "boolFalse": false, |
| 69 | "boolTrue": true, |
| 70 | "stringFalse": "false", |
| 71 | "stringTrue": "true", |
| 72 | "sliceOfNumbers": [1, 2, 3, 4, 5], |
| 73 | "mapData": { |
| 74 | "oneTwoThree": 123, |
| 75 | "oneTwoDotThree": 12.3, |
| 76 | "hello": "world", |
| 77 | "boolFalse": false, |
| 78 | "boolTrue": true, |
| 79 | "stringFalse": "false", |
| 80 | "stringTrue": "true", |
| 81 | "sliceOfNumbers": [1, 2, 3, 4, 5], |
| 82 | "mapData": { |
| 83 | "oneTwoThree": 123, |
| 84 | "oneTwoDotThree": 12.3, |
| 85 | "hello": "world", |
| 86 | "boolFalse": false, |
| 87 | "boolTrue": true, |
| 88 | "stringFalse": "false", |
| 89 | "stringTrue": "true", |
| 90 | "sliceOfNumbers": [1, 2, 3, 4, 5] |
| 91 | } |
| 92 | } |
| 93 | }`) |
| 94 | yamlInputData := newStringWithFormat(yaml.YAML, `oneTwoThree: 123 |
| 95 | oneTwoDotThree: 12.3 |
| 96 | hello: world |
| 97 | boolFalse: false |
| 98 | boolTrue: true |
| 99 | stringFalse: "false" |
| 100 | stringTrue: "true" |
| 101 | sliceOfNumbers: |
| 102 | - 1 |
| 103 | - 2 |
| 104 | - 3 |
| 105 | - 4 |
| 106 | - 5 |
| 107 | mapData: |
| 108 | oneTwoThree: 123 |
| 109 | oneTwoDotThree: 12.3 |
| 110 | hello: world |
| 111 | boolFalse: false |
| 112 | boolTrue: true |
| 113 | stringFalse: "false" |
| 114 | stringTrue: "true" |
| 115 | sliceOfNumbers: |
| 116 | - 1 |
| 117 | - 2 |
| 118 | - 3 |
| 119 | - 4 |
| 120 | - 5 |
| 121 | mapData: |
| 122 | oneTwoThree: 123 |
| 123 | oneTwoDotThree: 12.3 |
| 124 | hello: world |
| 125 | boolFalse: false |
| 126 | boolTrue: true |
| 127 | stringFalse: "false" |
| 128 | stringTrue: "true" |
| 129 | sliceOfNumbers: |
| 130 | - 1 |
| 131 | - 2 |
| 132 | - 3 |
| 133 | - 4 |
| 134 | - 5 |
| 135 | `) |
| 136 | |
| 137 | tomlInputData := newStringWithFormat(toml.TOML, ` |
| 138 | oneTwoThree = 123 |
| 139 | oneTwoDotThree = 12.3 |
| 140 | hello = 'world' |
| 141 | boolFalse = false |
| 142 | boolTrue = true |
| 143 | stringFalse = 'false' |
| 144 | stringTrue = 'true' |
| 145 | sliceOfNumbers = [1, 2, 3, 4, 5] |
| 146 | |
| 147 | [mapData] |
| 148 | oneTwoThree = 123 |
| 149 | oneTwoDotThree = 12.3 |
| 150 | hello = 'world' |
| 151 | boolFalse = false |
| 152 | boolTrue = true |
| 153 | stringFalse = 'false' |
| 154 | stringTrue = 'true' |
| 155 | sliceOfNumbers = [1, 2, 3, 4, 5] |
| 156 | |
| 157 | [mapData.mapData] |
| 158 | oneTwoThree = 123 |
| 159 | oneTwoDotThree = 12.3 |
| 160 | hello = 'world' |
| 161 | boolFalse = false |
| 162 | boolTrue = true |
| 163 | stringFalse = 'false' |
| 164 | stringTrue = 'true' |
| 165 | sliceOfNumbers = [1, 2, 3, 4, 5] |
| 166 | `) |
| 167 | |
| 168 | t.Run("select", func(t *testing.T) { |
| 169 | newTestsWithPrefix := func(prefix string) func(*testing.T) { |
| 170 | return func(t *testing.T) { |
| 171 | t.Run("string", testCases{ |
| 172 | selector: prefix + "hello", |
| 173 | in: []bytesWithFormat{ |
| 174 | jsonInputData, |
| 175 | yamlInputData, |
| 176 | tomlInputData, |
| 177 | }, |
| 178 | out: []bytesWithFormat{ |
| 179 | newStringWithFormat(json.JSON, `"world"`), |
| 180 | newStringWithFormat(yaml.YAML, `world`), |
| 181 | newStringWithFormat(toml.TOML, `'world'`), |
| 182 | }, |
| 183 | }.run) |
| 184 | t.Run("int", testCases{ |
| 185 | selector: prefix + "oneTwoThree", |
| 186 | in: []bytesWithFormat{ |
| 187 | jsonInputData, |
| 188 | yamlInputData, |
| 189 | tomlInputData, |
| 190 | }, |
| 191 | out: []bytesWithFormat{ |
| 192 | newStringWithFormat(json.JSON, `123`), |
| 193 | newStringWithFormat(yaml.YAML, `123`), |
| 194 | newStringWithFormat(toml.TOML, `123`), |
| 195 | }, |
| 196 | }.run) |
| 197 | t.Run("float", testCases{ |
| 198 | selector: prefix + "oneTwoDotThree", |
| 199 | in: []bytesWithFormat{ |
| 200 | jsonInputData, |
| 201 | yamlInputData, |
| 202 | tomlInputData, |
| 203 | }, |
| 204 | out: []bytesWithFormat{ |
| 205 | newStringWithFormat(json.JSON, `12.3`), |
| 206 | newStringWithFormat(yaml.YAML, `12.3`), |
| 207 | newStringWithFormat(toml.TOML, `12.3`), |
| 208 | }, |
| 209 | }.run) |
| 210 | t.Run("bool", func(t *testing.T) { |
| 211 | t.Run("true", testCases{ |
| 212 | selector: prefix + "boolTrue", |
| 213 | in: []bytesWithFormat{ |
| 214 | jsonInputData, |
| 215 | yamlInputData, |
| 216 | }, |
| 217 | out: []bytesWithFormat{ |
| 218 | newStringWithFormat(json.JSON, `true`), |
| 219 | newStringWithFormat(yaml.YAML, `true`), |
| 220 | newStringWithFormat(toml.TOML, `true`), |
| 221 | }, |
| 222 | }.run) |
| 223 | t.Run("false", testCases{ |
| 224 | selector: prefix + "boolFalse", |
| 225 | in: []bytesWithFormat{ |
| 226 | jsonInputData, |
| 227 | yamlInputData, |
| 228 | }, |
| 229 | out: []bytesWithFormat{ |
| 230 | newStringWithFormat(json.JSON, `false`), |
| 231 | newStringWithFormat(yaml.YAML, `false`), |
| 232 | newStringWithFormat(toml.TOML, `false`), |
| 233 | }, |
| 234 | }.run) |
| 235 | t.Run("true string", testCases{ |
| 236 | selector: prefix + "stringTrue", |
| 237 | in: []bytesWithFormat{ |
| 238 | jsonInputData, |
| 239 | yamlInputData, |
| 240 | }, |
| 241 | out: []bytesWithFormat{ |
| 242 | newStringWithFormat(json.JSON, `"true"`), |
| 243 | newStringWithFormat(yaml.YAML, `"true"`), |
| 244 | newStringWithFormat(toml.TOML, `'true'`), |
| 245 | }, |
| 246 | }.run) |
| 247 | t.Run("false string", testCases{ |
| 248 | selector: prefix + "stringFalse", |
| 249 | in: []bytesWithFormat{ |
| 250 | jsonInputData, |
| 251 | yamlInputData, |
| 252 | }, |
| 253 | out: []bytesWithFormat{ |
| 254 | newStringWithFormat(json.JSON, `"false"`), |
| 255 | newStringWithFormat(yaml.YAML, `"false"`), |
| 256 | newStringWithFormat(toml.TOML, `'false'`), |
| 257 | }, |
| 258 | }.run) |
| 259 | }) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | t.Run("root", newTestsWithPrefix("")) |
| 264 | t.Run("nested once", newTestsWithPrefix("mapData.")) |
| 265 | t.Run("nested twice", newTestsWithPrefix("mapData.mapData.")) |
| 266 | }) |
| 267 | } |
| 268 | |
| 269 | func TestKDLCrossFormat(t *testing.T) { |
| 270 | kdlInputData := newStringWithFormat(kdl.KDL, `hello "world" |
| 271 | oneTwoThree 123 |
| 272 | oneTwoDotThree 12.3 |
| 273 | boolTrue #true |
| 274 | boolFalse #false`) |
| 275 | |
| 276 | jsonInputData := newStringWithFormat(json.JSON, `{ |
| 277 | "hello": "world", |
| 278 | "oneTwoThree": 123, |
| 279 | "oneTwoDotThree": 12.3, |
| 280 | "boolTrue": true, |
| 281 | "boolFalse": false |
| 282 | }`) |
| 283 | |
| 284 | yamlInputData := newStringWithFormat(yaml.YAML, `hello: world |
| 285 | oneTwoThree: 123 |
| 286 | oneTwoDotThree: 12.3 |
| 287 | boolTrue: true |
| 288 | boolFalse: false`) |
| 289 | |
| 290 | t.Run("select string", testCases{ |
| 291 | selector: "hello", |
| 292 | in: []bytesWithFormat{ |
| 293 | kdlInputData, |
| 294 | jsonInputData, |
| 295 | yamlInputData, |
| 296 | }, |
| 297 | out: []bytesWithFormat{ |
| 298 | newStringWithFormat(json.JSON, `"world"`), |
| 299 | newStringWithFormat(yaml.YAML, `world`), |
| 300 | newStringWithFormat(toml.TOML, `'world'`), |
| 301 | }, |
| 302 | }.run) |
| 303 | |
| 304 | t.Run("select int", testCases{ |
| 305 | selector: "oneTwoThree", |
| 306 | in: []bytesWithFormat{ |
| 307 | kdlInputData, |
| 308 | jsonInputData, |
| 309 | yamlInputData, |
| 310 | }, |
| 311 | out: []bytesWithFormat{ |
| 312 | newStringWithFormat(json.JSON, `123`), |
| 313 | newStringWithFormat(yaml.YAML, `123`), |
| 314 | newStringWithFormat(toml.TOML, `123`), |
| 315 | }, |
| 316 | }.run) |
| 317 | |
| 318 | t.Run("select float", testCases{ |
| 319 | selector: "oneTwoDotThree", |
| 320 | in: []bytesWithFormat{ |
| 321 | kdlInputData, |
| 322 | jsonInputData, |
| 323 | yamlInputData, |
| 324 | }, |
| 325 | out: []bytesWithFormat{ |
| 326 | newStringWithFormat(json.JSON, `12.3`), |
| 327 | newStringWithFormat(yaml.YAML, `12.3`), |
| 328 | newStringWithFormat(toml.TOML, `12.3`), |
| 329 | }, |
| 330 | }.run) |
| 331 | |
| 332 | t.Run("select bool true", testCases{ |
| 333 | selector: "boolTrue", |
| 334 | in: []bytesWithFormat{ |
| 335 | kdlInputData, |
| 336 | jsonInputData, |
| 337 | yamlInputData, |
| 338 | }, |
| 339 | out: []bytesWithFormat{ |
| 340 | newStringWithFormat(json.JSON, `true`), |
| 341 | newStringWithFormat(yaml.YAML, `true`), |
| 342 | newStringWithFormat(toml.TOML, `true`), |
| 343 | }, |
| 344 | }.run) |
| 345 | |
| 346 | t.Run("select bool false", testCases{ |
| 347 | selector: "boolFalse", |
| 348 | in: []bytesWithFormat{ |
| 349 | kdlInputData, |
| 350 | jsonInputData, |
| 351 | yamlInputData, |
| 352 | }, |
| 353 | out: []bytesWithFormat{ |
| 354 | newStringWithFormat(json.JSON, `false`), |
| 355 | newStringWithFormat(yaml.YAML, `false`), |
| 356 | newStringWithFormat(toml.TOML, `false`), |
| 357 | }, |
| 358 | }.run) |
| 359 | |
| 360 | t.Run("KDL to JSON full document", testCases{ |
| 361 | selector: "$root", |
| 362 | in: []bytesWithFormat{ |
| 363 | kdlInputData, |
| 364 | }, |
| 365 | out: []bytesWithFormat{ |
| 366 | newStringWithFormat(json.JSON, `{ |
| 367 | "hello": "world", |
| 368 | "oneTwoThree": 123, |
| 369 | "oneTwoDotThree": 12.3, |
| 370 | "boolTrue": true, |
| 371 | "boolFalse": false |
| 372 | }`), |
| 373 | }, |
| 374 | }.run) |
| 375 | |
| 376 | t.Run("JSON to KDL full document", testCases{ |
| 377 | selector: "$root", |
| 378 | in: []bytesWithFormat{ |
| 379 | jsonInputData, |
| 380 | }, |
| 381 | out: []bytesWithFormat{ |
| 382 | newStringWithFormat(kdl.KDL, `hello "world" |
| 383 | oneTwoThree 123 |
| 384 | oneTwoDotThree 12.3 |
| 385 | boolTrue #true |
| 386 | boolFalse #false`), |
| 387 | }, |
| 388 | }.run) |
| 389 | |
| 390 | t.Run("YAML to KDL full document", testCases{ |
| 391 | selector: "$root", |
| 392 | in: []bytesWithFormat{ |
| 393 | yamlInputData, |
| 394 | }, |
| 395 | out: []bytesWithFormat{ |
| 396 | newStringWithFormat(kdl.KDL, `hello "world" |
| 397 | oneTwoThree 123 |
| 398 | oneTwoDotThree 12.3 |
| 399 | boolTrue #true |
| 400 | boolFalse #false`), |
| 401 | }, |
| 402 | }.run) |
| 403 | } |