Skip to content

Commit

Permalink
将 lal/pkg/util 的代码拷贝至本仓库中. 修改代码中import lal的路径为本仓库路径
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Aug 27, 2019
1 parent 849f0a8 commit bee8b4c
Show file tree
Hide file tree
Showing 20 changed files with 1,199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage.txt
coverage.html
profile.out
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/nezha.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

229 changes: 229 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Package assert 提供了单元测试时的断言功能
//
// 代码参考了 https://github.com/stretchr/testify
//
package assert

import (
"bytes"
"reflect"
)

type TestingT interface {
Errorf(format string, args ...interface{})
}

func Equal(t TestingT, expected interface{}, actual interface{}, msg ...string) {
if !equal(expected, actual) {
t.Errorf("%s expected=%+v, actual=%+v", msg, expected, actual)
}
return
}

func isNil(actual interface{}) bool {
if actual == nil {
return true
}
v := reflect.ValueOf(actual)
k := v.Kind()
if k == reflect.Chan || k == reflect.Map || k == reflect.Ptr || k == reflect.Interface || k == reflect.Slice {
return v.IsNil()
}
return false
}

func equal(expected, actual interface{}) bool {
if expected == nil {
return isNil(actual)
}

exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}

act, ok := actual.([]byte)
if !ok {
return false
}
//if exp == nil || act == nil {
// return exp == nil && act == nil
//}
return bytes.Equal(exp, act)
}
26 changes: 26 additions & 0 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package assert

import "testing"

func TestEqual(t *testing.T) {
Equal(t, nil, nil)
Equal(t, nil, nil, "fxxk.")
Equal(t, 1, 1)
Equal(t, "aaa", "aaa")
var ch chan struct{}
Equal(t, nil, ch)
var m map[string]string
Equal(t, nil, m)
var p *int
Equal(t, nil, p)
var i interface{}
Equal(t, nil, i)
var b []byte
Equal(t, nil, b)

Equal(t, true, isNil(nil))
Equal(t, false, isNil("aaa"))
Equal(t, false, equal([]byte{}, "aaa"))
Equal(t, true, equal([]byte{}, []byte{}))
Equal(t, true, equal([]byte{0, 1, 2}, []byte{0, 1, 2}))
}
Loading

0 comments on commit bee8b4c

Please sign in to comment.