1package command 2 3import ( 4 "fmt" 5 "testing" 6) 7 8type OutputTest struct { 9 XMLName string `json:"-"` 10 TestString string `json:"test_string"` 11 TestInt int `json:"test_int"` 12 TestNil []byte `json:"test_nil"` 13 TestNested OutputTestNested `json:"nested"` 14} 15 16type OutputTestNested struct { 17 NestKey string `json:"nest_key"` 18} 19 20func (o OutputTest) String() string { 21 return fmt.Sprintf("%s %d %s", o.TestString, o.TestInt, o.TestNil) 22} 23 24func TestCommandOutput(t *testing.T) { 25 var formatted []byte 26 result := OutputTest{ 27 TestString: "woooo a string", 28 TestInt: 77, 29 TestNil: nil, 30 TestNested: OutputTestNested{ 31 NestKey: "nest_value", 32 }, 33 } 34 35 json_expected := `{ 36 "test_string": "woooo a string", 37 "test_int": 77, 38 "test_nil": null, 39 "nested": { 40 "nest_key": "nest_value" 41 } 42}` 43 formatted, _ = formatOutput(result, "json") 44 if string(formatted) != json_expected { 45 t.Fatalf("bad json:\n%s\n\nexpected:\n%s", formatted, json_expected) 46 } 47 48 text_expected := "woooo a string 77" 49 formatted, _ = formatOutput(result, "text") 50 if string(formatted) != text_expected { 51 t.Fatalf("bad output:\n\"%s\"\n\nexpected:\n\"%s\"", formatted, text_expected) 52 } 53 54 error_expected := `Invalid output format "boo"` 55 _, err := formatOutput(result, "boo") 56 if err.Error() != error_expected { 57 t.Fatalf("bad output:\n\"%s\"\n\nexpected:\n\"%s\"", err.Error(), error_expected) 58 } 59} 60