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.
go-zero/core/filex/lookup_test.go

69 lines
1.3 KiB
Go

4 years ago
package filex
import (
"os"
"testing"
"zero/core/fs"
"github.com/stretchr/testify/assert"
)
func TestSplitLineChunks(t *testing.T) {
const text = `first line
second line
third line
fourth line
fifth line
sixth line
seventh line
`
fp, err := fs.TempFileWithText(text)
assert.Nil(t, err)
defer func() {
fp.Close()
os.Remove(fp.Name())
}()
offsets, err := SplitLineChunks(fp.Name(), 3)
assert.Nil(t, err)
body := make([]byte, 512)
for _, offset := range offsets {
reader := NewRangeReader(fp, offset.Start, offset.Stop)
n, err := reader.Read(body)
assert.Nil(t, err)
assert.Equal(t, uint8('\n'), body[n-1])
}
}
func TestSplitLineChunksNoFile(t *testing.T) {
_, err := SplitLineChunks("nosuchfile", 2)
assert.NotNil(t, err)
}
func TestSplitLineChunksFull(t *testing.T) {
const text = `first line
second line
third line
fourth line
fifth line
sixth line
`
fp, err := fs.TempFileWithText(text)
assert.Nil(t, err)
defer func() {
fp.Close()
os.Remove(fp.Name())
}()
offsets, err := SplitLineChunks(fp.Name(), 1)
assert.Nil(t, err)
body := make([]byte, 512)
for _, offset := range offsets {
reader := NewRangeReader(fp, offset.Start, offset.Stop)
n, err := reader.Read(body)
assert.Nil(t, err)
assert.Equal(t, []byte(text), body[:n])
}
}