Go checkEqualJSON()

Jmnote (토론 | 기여)님의 2022년 5월 24일 (화) 21:36 판 (→‎개요)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

Go checkEqualJSON()
Go
Copy
func checkEqualJSON(t *testing.T, want string, got interface{}, extras ...interface{}) {
	if reflect.TypeOf(got).String() == "*errors.errorString" {
		got = fmt.Sprintf("%s", got)
	}
	extraMessage := ""
	for _, extra := range extras {
		if extra != nil {
			extraMessage += fmt.Sprintf("%v", extra)
		}
	}
	temp, err := json.Marshal(got)
	_, file, line, _ := runtime.Caller(1)
	t.Logf("%s:%d: %s\n", filepath.Base(file), line, extraMessage)
	if err != nil {
		t.Fatalf("%s:%d: %s\ncannot marshal to json from got=[%v]", filepath.Base(file), line, extraMessage, got)
	}
	gotJSONString := string(temp)
	if strings.Compare(gotJSONString, want) != 0 {
		t.Fatalf("%s:%d: %s\nwant == `%v`\ngot === `%s`", filepath.Base(file), line, extraMessage, want, gotJSONString)
	}
}

2 같이 보기[ | ]