You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nbyte/util_test.go

56 lines
1.5 KiB
Go

1 year ago
package nbyte_test
import (
"errors"
"git.noahlan.cn/noahlan/ntool/nbyte"
"git.noahlan.cn/noahlan/ntool/ntest/assert"
"git.noahlan.cn/noahlan/ntool/ntime"
"testing"
)
func TestFirstLine(t *testing.T) {
bs := []byte("hi\ninhere")
assert.Eq(t, []byte("hi"), nbyte.FirstLine(bs))
assert.Eq(t, []byte("hi"), nbyte.FirstLine([]byte("hi")))
}
func TestStrOrErr(t *testing.T) {
bs := []byte("hi, inhere")
assert.Eq(t, "hi, inhere", nbyte.SafeString(bs, nil))
assert.Eq(t, "", nbyte.SafeString(bs, errors.New("error")))
str, err := nbyte.StrOrErr(bs, nil)
assert.NoErr(t, err)
assert.Eq(t, "hi, inhere", str)
str, err = nbyte.StrOrErr(bs, errors.New("error"))
assert.Err(t, err)
assert.Eq(t, "", str)
}
func TestMd5(t *testing.T) {
assert.NotEmpty(t, nbyte.Md5("abc"))
assert.NotEmpty(t, nbyte.Md5([]int{12, 34}))
}
func TestAppendAny(t *testing.T) {
assert.Eq(t, []byte("123"), nbyte.AppendAny(nil, 123))
assert.Eq(t, []byte("123"), nbyte.AppendAny([]byte{}, 123))
assert.Eq(t, []byte("123"), nbyte.AppendAny([]byte("1"), 23))
assert.Eq(t, []byte("1<nil>"), nbyte.AppendAny([]byte("1"), nil))
assert.Eq(t, "3600000000000", string(nbyte.AppendAny([]byte{}, ntime.OneHour)))
}
func TestCut(t *testing.T) {
// test for nbyte.Cut()
b, a, ok := nbyte.Cut([]byte("age=123"), '=')
assert.True(t, ok)
assert.Eq(t, []byte("age"), b)
assert.Eq(t, []byte("123"), a)
b, a, ok = nbyte.Cut([]byte("age=123"), 'x')
assert.False(t, ok)
assert.Eq(t, []byte("age=123"), b)
assert.Empty(t, a)
}