瀏覽代碼

filled error info with Caller instead of assertEqual

master
Patrick Peng Sun 8 年之前
父節點
當前提交
cd9592f3b0
共有 3 個文件被更改,包括 90 次插入16 次删除
  1. +24
    -1
      common_test.go
  2. +12
    -5
      inMsg.go
  3. +54
    -10
      inMsg_test.go

+ 24
- 1
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()
}

+ 12
- 5
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
}

+ 54
- 10
inMsg_test.go 查看文件

@@ -33,13 +33,12 @@ func TestTxtMsg(t *testing.T) {
</xml>`
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 := `<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
@@ -49,9 +48,17 @@ func TestingPicMsg(t *testing.T) {
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</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>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
@@ -59,12 +66,22 @@ func TestingVoiceMsg(t *testing.T) {
<MsgType><![CDATA[voice]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId>
<Format><![CDATA[Format]]></Format>
<Recognition><![CDATA[腾讯微信团队]]></Recognition>
<Recognition><![CDATA[some result]]></Recognition>
<MsgId>1234567890123456</MsgId>
</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>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
@@ -74,9 +91,18 @@ func TestingVideoMsg(t *testing.T) {
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId>
</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>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
@@ -86,9 +112,16 @@ func TestingShortVideoMsg(t *testing.T) {
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId>
</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>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
@@ -97,12 +130,23 @@ func TestingLocationMsg(t *testing.T) {
<Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y>
<Scale>20</Scale>
<Label><![CDATA[位置信息]]></Label>
<Label><![CDATA[Location description]]></Label>
<MsgId>1234567890123456</MsgId>
</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>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>

Loading…
取消
儲存