Skip to content

Commit

Permalink
add unit test; fix sample;
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqiang Ren committed Jan 23, 2021
1 parent 42249ea commit fc5f60a
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 257 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/sinngae/goerrcode

go 1.14

require github.com/sinngae/goerrcode/sample v0.0.0-20210122152423-a73f65ffe9b1 // indirect
5 changes: 5 additions & 0 deletions internal/jsonable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package internal

type Jsonable interface {
JsonStr() string
}
1 change: 0 additions & 1 deletion internal/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package internal


type Messager interface {
Message() string
}
6 changes: 3 additions & 3 deletions internal/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type Stacker interface {
Stack() int
Stack() *MyStack
}

type MyStack struct {
Sum [16]byte
Sum [16]byte
Content []byte
}

Expand All @@ -29,4 +29,4 @@ func stack(skip int) []byte {
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
}
return buf.Bytes()
}
}
72 changes: 4 additions & 68 deletions internal/stack_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,14 @@
package internal

import (
"bytes"
"fmt"
"io/ioutil"
"runtime"
"testing"
)
func Stack(skip int) []byte {
buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
for i := skip; ; i++ { // Skip the expected number of frames
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := ioutil.ReadFile(file)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
//fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
fmt.Fprintf(buf, "\t%v: %s, %v\n", pc, lines, line)
}
return buf.Bytes()
}


/*
buf 10*1024
buf: goroutine 6 [running]:
github.com/sinngae/goerrcode/internal.TestGetStack(0xc000126120)
/Users/ziqiangren/gitwork/goerrcode/internal/stack_test.go:41 +0x79
testing.tRunner(0xc000126120, 0x114b2e8)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1039 +0xdc
created by testing.(*T).Run
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1090 +0x372
goroutine 1 [chan receive]:
testing.(*T).Run(0xc000126120, 0x114313a, 0xc, 0x114b2e8, 0x1075ee6)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1091 +0x399
testing.runTests.func1(0xc000126000)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1334 +0x78
testing.tRunner(0xc000126000, 0xc00011de10)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1039 +0xdc
testing.runTests(0xc00000c080, 0x1239260, 0x1, 0x1, 0x0)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1332 +0x2a7
testing.(*M).Run(0xc000114000, 0x0)
/usr/local/Cellar/go/1.14.7/libexec/src/testing/testing.go:1249 +0x1b7
main.main()
_testmain.go:44 +0x135

buf 100
buf: goroutine 6 [running]:
github.com/sinngae/goerrcode/internal.TestGetStack(0xc000126120)
/Users/ziqi
*/
func TestGetStack(t *testing.T) {
testList := []string{"test", "test1"}
for _, str := range testList {
myFunc(str)
}
func Test_stack(t *testing.T) {
fmt.Println(stack(0))
}

func myFunc(test string) {
buf := make([]byte, 2000)
runtime.Stack(buf, true)
fmt.Printf("buf %s: %s\n", test, buf)
func TestStack(t *testing.T) {
fmt.Println(Stack())
}
16 changes: 16 additions & 0 deletions jsonable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package goerrcode

import (
"fmt"

"github.com/sinngae/goerrcode/internal"
)

func JsonStr(err error) string {
cause, ok := err.(internal.Jsonable)
if !ok {
return fmt.Sprintf("\"%s\"", err.Error())
}

return cause.JsonStr()
}
9 changes: 7 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ type withMessage struct {
}

func (msg *withMessage) Error() string {
return msg.JsonStr()
}

func (msg *withMessage) JsonStr() string {
if msg.cause == nil {
return fmt.Sprintf("message:%s", msg.msg)
return fmt.Sprintf("{\"message\":\"%s\"}", msg.msg)
}
return fmt.Sprintf("message[%s] err[%s]", msg.msg, msg.cause.Error())

return fmt.Sprintf("{\"message\":\"%s\", \"err\":%s}", msg.msg, JsonStr(msg.cause))
}

func (msg *withMessage) Message() string {
Expand Down
62 changes: 5 additions & 57 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,12 @@ import (
"testing"
)

var errWithMsg = WithMessage(fmt.Errorf("this is cause"), "this is msg")

func TestWithMessage(t *testing.T) {
type args struct {
err error
msg string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "case 0",
args: args{
err: nil,
msg: "",
},
wantErr: false,
},
{
name: "case 0",
args: args{
err: fmt.Errorf("err test"),
msg: "msg test",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := WithMessage(tt.args.err, tt.args.msg); (err != nil) != tt.wantErr {
t.Errorf("WithMessage() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
fmt.Println(errWithMsg)
}

func TestMessage(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want string
}{
{
name: "case 0",
args: args{
err: WithMessage(fmt.Errorf("err test"), "msg test"),
},
want: "msg: msg test, err: err test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Message(tt.args.err); got != tt.want {
t.Errorf("Message() = %v, want %v", got, tt.want)
}
})
}
}
fmt.Println(Message(errWithMsg))
}
9 changes: 7 additions & 2 deletions retcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ type withRetCode struct {
}

func (rc *withRetCode) Error() string {
return rc.JsonStr()
}

func (rc *withRetCode) JsonStr() string {
if rc.cause == nil {
return fmt.Sprintf("retcode:%d", rc.retCode)
return fmt.Sprintf("{\"retcode\":%d}", rc.retCode)
}
return fmt.Sprintf("retcode[%d] err[%s]", rc.retCode, rc.cause.Error())

return fmt.Sprintf("{\"retcode\":%d, \"err\":%s}", rc.retCode, JsonStr(rc.cause))
}

func (rc *withRetCode) RetCode() int {
Expand Down
10 changes: 7 additions & 3 deletions retcode_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ type withRetCodeMessage struct {
}

func (rm *withRetCodeMessage) Error() string {
return rm.JsonStr()
}

func (rm *withRetCodeMessage) JsonStr() string {
if rm.cause == nil {
return fmt.Sprintf("retcode:%d, message:%s", rm.retCode, rm.msg)
return fmt.Sprintf("{\"retcode\":%d, \"message\":\"%s\"}", rm.retCode, rm.msg)
}
return fmt.Sprintf("retcode:%d, message:%s, err:%s", rm.retCode, rm.msg, rm.cause.Error())
return fmt.Sprintf("{\"retcode\":%d, \"message\":\"%s\", \"err\":%s}", rm.retCode, rm.msg, JsonStr(rm.cause))
}

func (rm *withRetCodeMessage) Cause() error {
Expand Down Expand Up @@ -77,7 +81,7 @@ func RetCodeMsg(err error) *withRetCodeMessage {
for err != nil {
code, ok := err.(*withRetCodeMessage)
if ok {
return code
return code.RetCodeMsg()
}

err = Cause(err)
Expand Down
29 changes: 29 additions & 0 deletions retcode_message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goerrcode

import (
"fmt"
"testing"
)

var (
errCause = fmt.Errorf("this is cause")
rcMsg = NewRetCodeMsg(404, "this is msg")
errWithRcMsg = WithRetCodeMessage(errCause, rcMsg)
errWithRcMsgf = WithRetCodeMessagef(errCause, 404, "this is msg fmt, %s", "this is msg")
)

func TestWithRetCodeMessage(t *testing.T) {
fmt.Println(errWithRcMsg)
}

func TestWithRetCodeMessagef(t *testing.T) {
fmt.Println(errWithRcMsgf)
}

func TestRetCodeMsg(t *testing.T) {
fmt.Println(RetCodeMsg(errWithRcMsg))
}

func TestRcMsg(t *testing.T) {
fmt.Println(rcMsg)
}
Loading

0 comments on commit fc5f60a

Please sign in to comment.