Przeglądaj źródła

filled error info with Caller instead of assertEqual

master
Patrick Peng Sun 8 lat temu
rodzic
commit
cd9592f3b0
3 zmienionych plików z 90 dodań i 16 usunięć
  1. +24
    -1
      common_test.go
  2. +12
    -5
      inMsg.go
  3. +54
    -10
      inMsg_test.go

+ 24
- 1
common_test.go Wyświetl plik



import ( import (
"fmt" "fmt"
"runtime"
"testing" "testing"
) )


if len(message) == 0 { if len(message) == 0 {
message = fmt.Sprintf("%v != %v", a, b) message = fmt.Sprintf("%v != %v", a, b)
} }
message = fmt.Sprintf("%v != %v", a, b)
message = fmt.Sprintf("%s : %s", MyCaller(), message)
t.Fatal(message) t.Fatal(message)
} }


func TestDummy(t *testing.T) { func TestDummy(t *testing.T) {
t.Log("Testing common field") 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()
}

+ 12
- 5
inMsg.go Wyświetl plik

//picture //picture
type PicMsg struct { type PicMsg struct {
PicUrl string PicUrl string
MeidaId string
MediaId string
MsgId int64 MsgId int64
} }


//voice //voice
type VoiceMsg struct { type VoiceMsg struct {
MeidaId string
Format string
MsgId int64
MediaId string
Format string
MsgId int64
Recognition string
} }


//video //video


//short video //short video
type ShortVideoMsg struct { type ShortVideoMsg struct {
MeidaId string
MediaId string
ThumbMediaId string ThumbMediaId string
MsgId int64 MsgId int64
} }
//ReadPicMsg extract text message //ReadPicMsg extract text message
func ReadPicMsg(s string) PicMsg { func ReadPicMsg(s string) PicMsg {
var r = PicMsg{} var r = PicMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }


//ReadVoiceMsg extract text message //ReadVoiceMsg extract text message
func ReadVoiceMsg(s string) VoiceMsg { func ReadVoiceMsg(s string) VoiceMsg {
var r = VoiceMsg{} var r = VoiceMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }


//ReadVideoMsg extract text message //ReadVideoMsg extract text message
func ReadVideoMsg(s string) VideoMsg { func ReadVideoMsg(s string) VideoMsg {
var r = VideoMsg{} var r = VideoMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }


//ReadShortVideoMsg extract text message //ReadShortVideoMsg extract text message
func ReadShortVideoMsg(s string) ShortVideoMsg { func ReadShortVideoMsg(s string) ShortVideoMsg {
var r = ShortVideoMsg{} var r = ShortVideoMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }


//ReadLocationMsg extract text message //ReadLocationMsg extract text message
func ReadLocationMsg(s string) LocationMsg { func ReadLocationMsg(s string) LocationMsg {
var r = LocationMsg{} var r = LocationMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }


//ReadLinkMsg extract text message //ReadLinkMsg extract text message
func ReadLinkMsg(s string) LinkMsg { func ReadLinkMsg(s string) LinkMsg {
var r = LinkMsg{} var r = LinkMsg{}
xml.Unmarshal([]byte(s), &r)
return r return r
} }

+ 54
- 10
inMsg_test.go Wyświetl plik

</xml>` </xml>`
h := ReadCommonHeader(msg) h := ReadCommonHeader(msg)
m := ReadTextMsg(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, h.MsgType, "text", "")
AssertEqual(t, m.MsgId, int64(1234567890123456), "") AssertEqual(t, m.MsgId, int64(1234567890123456), "")
} }


/*
func TestingPicMsg(t *testing.T) {
func TestPicMsg(t *testing.T) {
msg := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>
<MediaId><![CDATA[media_id]]></MediaId> <MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId> <MsgId>1234567890123456</MsgId>
</xml>` </xml>`
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 := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>
<MsgType><![CDATA[voice]]></MsgType> <MsgType><![CDATA[voice]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId> <MediaId><![CDATA[media_id]]></MediaId>
<Format><![CDATA[Format]]></Format> <Format><![CDATA[Format]]></Format>
<Recognition><![CDATA[腾讯微信团队]]></Recognition>
<Recognition><![CDATA[some result]]></Recognition>
<MsgId>1234567890123456</MsgId> <MsgId>1234567890123456</MsgId>
</xml>` </xml>`

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 := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId> <MsgId>1234567890123456</MsgId>
</xml>` </xml>`

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 := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId> <MsgId>1234567890123456</MsgId>
</xml>` </xml>`
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 := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>
<Location_X>23.134521</Location_X> <Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y> <Location_Y>113.358803</Location_Y>
<Scale>20</Scale> <Scale>20</Scale>
<Label><![CDATA[位置信息]]></Label>
<Label><![CDATA[Location description]]></Label>
<MsgId>1234567890123456</MsgId> <MsgId>1234567890123456</MsgId>
</xml>` </xml>`

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 := `<xml> msg := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName> <ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>

Ładowanie…
Anuluj
Zapisz