forked from q191201771/naza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
将 lal/pkg/util 的代码拷贝至本仓库中. 修改代码中import lal的路径为本仓库路径
- Loading branch information
1 parent
849f0a8
commit bee8b4c
Showing
20 changed files
with
1,199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage.txt | ||
coverage.html | ||
profile.out |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) | ||
} |
Oops, something went wrong.