开发自己给自己的代码写测试
eg:
split.go
package split_string
import "strings"
//切割字符串
//example:
// abc,b => [ac]
func Split(str string,sep string) []string {
// str:"abc" sep = "b"
var ret []string
index := strings.Index(str, sep)
for index >= 0 {
ret = append(ret, str[:index])
str = str[index+1:]
index = strings.Index(str, sep)
}
ret = append(ret, str)
return ret
}
split_test.go
package split_string
import (
"reflect"
"testing"
)
func TestSplit(t *testing.T) {
ret := Split("babcbef","b")
want := []string{"", "a", "c", "ef"}
if !reflect.DeepEqual(ret, want) {
//测试用例失败了
t.Errorf("want:%v but got:%v", want,ret)
}
}
func Test2Split(t *testing.T) {
ret := Split("a:b:c",":")
want := []string{"a", "b", "c"}
if !reflect.DeepEqual(ret, want) {
//测试用例失败了
t.Errorf("want:%v but got:%v", want,ret)
}
}