123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package iox
- import (
- "bytes"
- "io"
- "io/ioutil"
- "os"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/core/fs"
- "github.com/tal-tech/go-zero/core/stringx"
- )
- 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) {
- tmpfile, err := fs.TempFilenameWithText(test.input)
- assert.Nil(t, err)
- defer os.Remove(tmpfile)
- content, err := ReadText(tmpfile)
- assert.Nil(t, err)
- assert.Equal(t, test.expect, content)
- })
- }
- }
- func TestReadTextLines(t *testing.T) {
- text := `1
- 2
- #a
- 3`
- tmpfile, err := fs.TempFilenameWithText(text)
- assert.Nil(t, err)
- defer os.Remove(tmpfile)
- 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) {
- lines, err := ReadTextLines(tmpfile, test.options...)
- assert.Nil(t, err)
- assert.Equal(t, test.expectLines, len(lines))
- })
- }
- }
- func TestDupReadCloser(t *testing.T) {
- input := "hello"
- reader := ioutil.NopCloser(bytes.NewBufferString(input))
- r1, r2 := DupReadCloser(reader)
- verify := func(r io.Reader) {
- output, err := ioutil.ReadAll(r)
- assert.Nil(t, err)
- assert.Equal(t, input, string(output))
- }
- verify(r1)
- verify(r2)
- }
- func TestReadBytes(t *testing.T) {
- reader := ioutil.NopCloser(bytes.NewBufferString("helloworld"))
- buf := make([]byte, 5)
- err := ReadBytes(reader, buf)
- assert.Nil(t, err)
- assert.Equal(t, "hello", string(buf))
- }
- func TestReadBytesNotEnough(t *testing.T) {
- reader := ioutil.NopCloser(bytes.NewBufferString("hell"))
- 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))
- }
|