| 299 | } |
| 300 | |
| 301 | func Test_exportFormat_Write(t *testing.T) { |
| 302 | type args struct { |
| 303 | data interface{} |
| 304 | } |
| 305 | tests := []struct { |
| 306 | name string |
| 307 | exporter jsonExporter |
| 308 | args args |
| 309 | wantW string |
| 310 | wantErr bool |
| 311 | istty bool |
| 312 | }{ |
| 313 | { |
| 314 | name: "regular JSON output", |
| 315 | exporter: jsonExporter{}, |
| 316 | args: args{ |
| 317 | data: map[string]string{"name": "hubot"}, |
| 318 | }, |
| 319 | wantW: "{\"name\":\"hubot\"}\n", |
| 320 | wantErr: false, |
| 321 | istty: false, |
| 322 | }, |
| 323 | { |
| 324 | name: "call ExportData", |
| 325 | exporter: jsonExporter{fields: []string{"field1", "field2"}}, |
| 326 | args: args{ |
| 327 | data: &exportableItem{"item1"}, |
| 328 | }, |
| 329 | wantW: "{\"field1\":\"item1:field1\",\"field2\":\"item1:field2\"}\n", |
| 330 | wantErr: false, |
| 331 | istty: false, |
| 332 | }, |
| 333 | { |
| 334 | name: "recursively call ExportData", |
| 335 | exporter: jsonExporter{fields: []string{"f1", "f2"}}, |
| 336 | args: args{ |
| 337 | data: map[string]interface{}{ |
| 338 | "s1": []exportableItem{{"i1"}, {"i2"}}, |
| 339 | "s2": []exportableItem{{"i3"}}, |
| 340 | }, |
| 341 | }, |
| 342 | wantW: "{\"s1\":[{\"f1\":\"i1:f1\",\"f2\":\"i1:f2\"},{\"f1\":\"i2:f1\",\"f2\":\"i2:f2\"}],\"s2\":[{\"f1\":\"i3:f1\",\"f2\":\"i3:f2\"}]}\n", |
| 343 | wantErr: false, |
| 344 | istty: false, |
| 345 | }, |
| 346 | { |
| 347 | name: "with jq filter", |
| 348 | exporter: jsonExporter{filter: ".name"}, |
| 349 | args: args{ |
| 350 | data: map[string]string{"name": "hubot"}, |
| 351 | }, |
| 352 | wantW: "hubot\n", |
| 353 | wantErr: false, |
| 354 | istty: false, |
| 355 | }, |
| 356 | { |
| 357 | name: "with jq filter pretty printing", |
| 358 | exporter: jsonExporter{filter: "."}, |