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/iox/read_test.go

174 lines
3.2 KiB
Go

4 years ago
package iox
import (
"bytes"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/fs"
"github.com/zeromicro/go-zero/core/stringx"
4 years ago
)
func TestReadText(t *testing.T) {
tests := []struct {
input string
expect string
}{
{
input: `a`,
expect: `a`,
}, {
input: `a
`,
expect: `a`,
}, {
input: `a
b`,
expect: `a
b`,
}, {
input: `a
b
`,
expect: `a
b`,
},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
2 years ago
tmpFile, err := fs.TempFilenameWithText(test.input)
4 years ago
assert.Nil(t, err)
2 years ago
defer os.Remove(tmpFile)
4 years ago
2 years ago
content, err := ReadText(tmpFile)
4 years ago
assert.Nil(t, err)
assert.Equal(t, test.expect, content)
})
}
}
func TestReadTextError(t *testing.T) {
_, err := ReadText("not-exist")
assert.NotNil(t, err)
}
4 years ago
func TestReadTextLines(t *testing.T) {
text := `1
2
#a
3`
2 years ago
tmpFile, err := fs.TempFilenameWithText(text)
4 years ago
assert.Nil(t, err)
2 years ago
defer os.Remove(tmpFile)
4 years ago
tests := []struct {
options []TextReadOption
expectLines int
}{
{
nil,
6,
}, {
[]TextReadOption{KeepSpace(), OmitWithPrefix("#")},
6,
}, {
[]TextReadOption{WithoutBlank()},
4,
}, {
[]TextReadOption{OmitWithPrefix("#")},
5,
}, {
[]TextReadOption{WithoutBlank(), OmitWithPrefix("#")},
3,
},
}
for _, test := range tests {
t.Run(stringx.Rand(), func(t *testing.T) {
2 years ago
lines, err := ReadTextLines(tmpFile, test.options...)
4 years ago
assert.Nil(t, err)
assert.Equal(t, test.expectLines, len(lines))
})
}
}
func TestReadTextLinesError(t *testing.T) {
_, err := ReadTextLines("not-exist")
assert.NotNil(t, err)
}
4 years ago
func TestDupReadCloser(t *testing.T) {
input := "hello"
reader := io.NopCloser(bytes.NewBufferString(input))
4 years ago
r1, r2 := DupReadCloser(reader)
verify := func(r io.Reader) {
output, err := io.ReadAll(r)
4 years ago
assert.Nil(t, err)
assert.Equal(t, input, string(output))
}
verify(r1)
verify(r2)
}
func TestLimitDupReadCloser(t *testing.T) {
input := "hello world"
limitBytes := int64(4)
reader := io.NopCloser(bytes.NewBufferString(input))
r1, r2 := LimitDupReadCloser(reader, limitBytes)
verify := func(r io.Reader) {
output, err := io.ReadAll(r)
assert.Nil(t, err)
assert.Equal(t, input, string(output))
}
verifyLimit := func(r io.Reader, limit int64) {
output, err := io.ReadAll(r)
if limit < int64(len(input)) {
input = input[:limit]
}
assert.Nil(t, err)
assert.Equal(t, input, string(output))
}
verify(r1)
verifyLimit(r2, limitBytes)
}
4 years ago
func TestReadBytes(t *testing.T) {
reader := io.NopCloser(bytes.NewBufferString("helloworld"))
4 years ago
buf := make([]byte, 5)
err := ReadBytes(reader, buf)
assert.Nil(t, err)
assert.Equal(t, "hello", string(buf))
}
func TestReadBytesNotEnough(t *testing.T) {
reader := io.NopCloser(bytes.NewBufferString("hell"))
4 years ago
buf := make([]byte, 5)
err := ReadBytes(reader, buf)
assert.Equal(t, io.EOF, err)
}
func TestReadBytesChunks(t *testing.T) {
buf := make([]byte, 5)
reader, writer := io.Pipe()
go func() {
for i := 0; i < 10; i++ {
writer.Write([]byte{'a'})
time.Sleep(10 * time.Millisecond)
}
}()
err := ReadBytes(reader, buf)
assert.Nil(t, err)
assert.Equal(t, "aaaaa", string(buf))
}