From cd9592f3b0431f288a5d221aaef6790268415673 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Tue, 25 Apr 2017 17:49:59 +1000 Subject: [PATCH] filled error info with Caller instead of assertEqual --- common_test.go | 25 +++++++++++++++++++- inMsg.go | 17 ++++++++++---- inMsg_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/common_test.go b/common_test.go index 89a68e3..fe32537 100644 --- a/common_test.go +++ b/common_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "runtime" "testing" ) @@ -20,10 +21,32 @@ func AssertEqual(t *testing.T, a interface{}, b interface{}, message string) { if len(message) == 0 { message = fmt.Sprintf("%v != %v", a, b) } - message = fmt.Sprintf("%v != %v", a, b) + message = fmt.Sprintf("%s : %s", MyCaller(), message) t.Fatal(message) } func TestDummy(t *testing.T) { t.Log("Testing common field") } + +// MyCaller returns the caller of the function that called it :) +func MyCaller() string { + + // we get the callers as uintptrs - but we just need 1 + fpcs := make([]uintptr, 1) + + // skip 3 levels to get to the caller of whoever called Caller() + n := runtime.Callers(3, fpcs) + if n == 0 { + return "n/a" // proper error her would be better + } + + // get the info of the actual function that's in the pointer + fun := runtime.FuncForPC(fpcs[0] - 1) + if fun == nil { + return "n/a" + } + + // return its name + return fun.Name() +} diff --git a/inMsg.go b/inMsg.go index fd1be8f..c4c9a0f 100644 --- a/inMsg.go +++ b/inMsg.go @@ -22,15 +22,16 @@ type TextMsg struct { //picture type PicMsg struct { PicUrl string - MeidaId string + MediaId string MsgId int64 } //voice type VoiceMsg struct { - MeidaId string - Format string - MsgId int64 + MediaId string + Format string + MsgId int64 + Recognition string } //video @@ -42,7 +43,7 @@ type VideoMsg struct { //short video type ShortVideoMsg struct { - MeidaId string + MediaId string ThumbMediaId string MsgId int64 } @@ -80,35 +81,41 @@ func ReadTextMsg(s string) TextMsg { //ReadPicMsg extract text message func ReadPicMsg(s string) PicMsg { var r = PicMsg{} + xml.Unmarshal([]byte(s), &r) return r } //ReadVoiceMsg extract text message func ReadVoiceMsg(s string) VoiceMsg { var r = VoiceMsg{} + xml.Unmarshal([]byte(s), &r) return r } //ReadVideoMsg extract text message func ReadVideoMsg(s string) VideoMsg { var r = VideoMsg{} + xml.Unmarshal([]byte(s), &r) return r } //ReadShortVideoMsg extract text message func ReadShortVideoMsg(s string) ShortVideoMsg { var r = ShortVideoMsg{} + xml.Unmarshal([]byte(s), &r) return r } //ReadLocationMsg extract text message func ReadLocationMsg(s string) LocationMsg { var r = LocationMsg{} + xml.Unmarshal([]byte(s), &r) return r } //ReadLinkMsg extract text message func ReadLinkMsg(s string) LinkMsg { var r = LinkMsg{} + xml.Unmarshal([]byte(s), &r) return r } diff --git a/inMsg_test.go b/inMsg_test.go index 57145a8..0c92dd1 100644 --- a/inMsg_test.go +++ b/inMsg_test.go @@ -33,13 +33,12 @@ func TestTxtMsg(t *testing.T) { ` h := ReadCommonHeader(msg) m := ReadTextMsg(msg) - AssertEqual(t, m.Content, "this is a test", "Content is not right") + AssertEqual(t, m.Content, "this is a test", "Content failed") AssertEqual(t, h.MsgType, "text", "") AssertEqual(t, m.MsgId, int64(1234567890123456), "") } -/* -func TestingPicMsg(t *testing.T) { +func TestPicMsg(t *testing.T) { msg := ` @@ -49,9 +48,17 @@ func TestingPicMsg(t *testing.T) { 1234567890123456 ` + h := ReadCommonHeader(msg) + AssertEqual(t, h.MsgType, "image", "") + + m := ReadPicMsg(msg) + AssertEqual(t, m.MediaId, "media_id", "Media id failed") + AssertEqual(t, m.PicUrl, "this is a url", "PicUrl failed") + AssertEqual(t, m.MsgId, int64(1234567890123456), "MsgId not match") + } -func TestingVoiceMsg(t *testing.T) { +func TestVoiceMsg(t *testing.T) { msg := ` @@ -59,12 +66,22 @@ func TestingVoiceMsg(t *testing.T) { - + 1234567890123456 ` + + h := ReadCommonHeader(msg) + AssertEqual(t, h.MsgType, "voice", "Message type should be voice") + + m := ReadVoiceMsg(msg) + AssertEqual(t, m.MediaId, "media_id", "Media id failed") + AssertEqual(t, m.Format, "Format", "Format failed") + AssertEqual(t, m.Recognition, "some result", "Recognition failed") + AssertEqual(t, m.MsgId, int64(1234567890123456), "MsgId not match") + } -func TestingVideoMsg(t *testing.T) { +func TestVideoMsg(t *testing.T) { msg := ` @@ -74,9 +91,18 @@ func TestingVideoMsg(t *testing.T) { 1234567890123456 ` + + h := ReadCommonHeader(msg) + AssertEqual(t, h.MsgType, "video", "Message type should be video") + + m := ReadVideoMsg(msg) + AssertEqual(t, m.MediaId, "media_id", "Media id failed") + AssertEqual(t, m.ThumbMediaId, "thumb_media_id", "Format failed") + AssertEqual(t, m.MsgId, int64(1234567890123456), "MsgId not match") + } -func TestingShortVideoMsg(t *testing.T) { +func TestShortVideoMsg(t *testing.T) { msg := ` @@ -86,9 +112,16 @@ func TestingShortVideoMsg(t *testing.T) { 1234567890123456 ` + h := ReadCommonHeader(msg) + AssertEqual(t, h.MsgType, "shortvideo", "Message type should be video") + + m := ReadShortVideoMsg(msg) + AssertEqual(t, m.MediaId, "media_id", "Media id failed") + AssertEqual(t, m.ThumbMediaId, "thumb_media_id", "Format failed") + AssertEqual(t, m.MsgId, int64(1234567890123456), "MsgId not match") } -func TestingLocationMsg(t *testing.T) { +func TestLocationMsg(t *testing.T) { msg := ` @@ -97,12 +130,23 @@ func TestingLocationMsg(t *testing.T) { 23.134521 113.358803 20 - + 1234567890123456 ` + + h := ReadCommonHeader(msg) + AssertEqual(t, h.MsgType, "location", "Message type should be video") + + m := ReadLocationMsg(msg) + AssertEqual(t, m.Location_X, (float64(23.134521)), "Location_X failed") + AssertEqual(t, m.Location_Y, (float64(113.358803)), "Location_Y failed") + AssertEqual(t, m.Scale, int(22), "Format failed") + AssertEqual(t, m.Label, "Location description", "Location Label failed") + AssertEqual(t, m.MsgId, int64(1234567890123456), "MsgId not match") } -func TestingLinkMsg(t *testing.T) { +/* +func TestLinkMsg(t *testing.T) { msg := `