fix unit tests by comparing json structs

pull/12300/head
Sharif Elgamal 2021-08-17 14:06:52 -07:00
parent 2df63c00af
commit eebbad0978
3 changed files with 48 additions and 27 deletions

View File

@ -18,7 +18,9 @@ package out
import (
"bytes"
"encoding/json"
"os"
"reflect"
"strings"
"testing"
@ -86,7 +88,7 @@ func TestDisplayProblem(t *testing.T) {
}
}
func TestDisplayJSON(t *testing.T) {
func TestDisplayProblemJSON(t *testing.T) {
defer SetJSON(false)
SetJSON(true)
@ -96,7 +98,6 @@ func TestDisplayJSON(t *testing.T) {
}{
{
k: &reason.Kind{
ID: "BUG",
ExitCode: 4,
Advice: "fix me!",
@ -117,10 +118,20 @@ func TestDisplayJSON(t *testing.T) {
return "random-id"
}
JSON = true
Error(*tc.k, "my error")
actual := buf.String()
if actual != tc.expected {
actual := buf.Bytes()
var actualJSON struct{}
var expectedJSON struct{}
err := json.Unmarshal(actual, &actualJSON)
if err != nil {
t.Fatalf("error unmarshalling actual: %v", err)
}
err = json.Unmarshal([]byte(tc.expected), &expectedJSON)
if err != nil {
t.Fatalf("error unmarshalling expected: %v", err)
}
if !reflect.DeepEqual(expectedJSON, actualJSON) {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", tc.expected, actual)
}
})

View File

@ -18,8 +18,10 @@ package register
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
)
@ -39,11 +41,9 @@ func TestPrintStep(t *testing.T) {
}
PrintStep("message")
actual := buf.String()
actual := buf.Bytes()
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
CompareJSON(t, actual, []byte(expected))
}
func TestPrintInfo(t *testing.T) {
@ -59,11 +59,10 @@ func TestPrintInfo(t *testing.T) {
}
PrintInfo("info")
actual := buf.String()
actual := buf.Bytes()
CompareJSON(t, actual, []byte(expected))
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
}
func TestError(t *testing.T) {
@ -79,11 +78,9 @@ func TestError(t *testing.T) {
}
PrintError("error")
actual := buf.String()
actual := buf.Bytes()
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
CompareJSON(t, actual, []byte(expected))
}
func TestErrorExitCode(t *testing.T) {
@ -99,10 +96,9 @@ func TestErrorExitCode(t *testing.T) {
}
PrintErrorExitCode("error", 5, map[string]string{"a": "b"}, map[string]string{"c": "d"})
actual := buf.String()
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
actual := buf.Bytes()
CompareJSON(t, actual, []byte(expected))
}
func TestWarning(t *testing.T) {
@ -118,9 +114,24 @@ func TestWarning(t *testing.T) {
}
PrintWarning("warning")
actual := buf.String()
actual := buf.Bytes()
if actual != expected {
CompareJSON(t, actual, []byte(expected))
}
// CompareJSON compares the structs of actual and expected instead of just the strings
func CompareJSON(t *testing.T, actual []byte, expected []byte) {
var actualJSON struct{}
var expectedJSON struct{}
err := json.Unmarshal(actual, &actualJSON)
if err != nil {
t.Fatalf("error unmarshalling actual: %v", err)
}
err = json.Unmarshal(expected, &expectedJSON)
if err != nil {
t.Fatalf("error unmarshalling expected: %v", err)
}
if !reflect.DeepEqual(expectedJSON, actualJSON) {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
}

View File

@ -42,9 +42,8 @@ func TestSetCurrentStep(t *testing.T) {
}
PrintStep("message")
actual := buf.String()
actual := buf.Bytes()
if actual != expected {
t.Fatalf("expected didn't match actual:\nExpected:\n%v\n\nActual:\n%v", expected, actual)
}
// Unmarshal both strings to JSON and compare the structs
CompareJSON(t, actual, []byte(expected))
}