github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/formatter/json_test.go (about) 1 package formatter 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 8 "github.com/wata727/tflint/tflint" 9 ) 10 11 func Test_jsonPrint(t *testing.T) { 12 cases := []struct { 13 Name string 14 Issues tflint.Issues 15 Error *tflint.Error 16 Stdout string 17 }{ 18 { 19 Name: "no issues", 20 Issues: tflint.Issues{}, 21 Stdout: `{"issues":[],"errors":[]}`, 22 }, 23 { 24 Name: "error", 25 Error: tflint.NewContextError("Failed to work", errors.New("I don't feel like working")), 26 Stdout: `{"issues":[],"errors":[{"message":"I don't feel like working"}]}`, 27 }, 28 } 29 30 for _, tc := range cases { 31 stdout := &bytes.Buffer{} 32 stderr := &bytes.Buffer{} 33 formatter := &Formatter{Stdout: stdout, Stderr: stderr} 34 35 formatter.jsonPrint(tc.Issues, tc.Error) 36 37 if stdout.String() != tc.Stdout { 38 t.Fatalf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String()) 39 } 40 } 41 }