github.com/shuguocloud/go-zero@v1.3.0/core/iox/read_test.go (about)

     1  package iox
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/shuguocloud/go-zero/core/fs"
    13  	"github.com/shuguocloud/go-zero/core/stringx"
    14  )
    15  
    16  func TestReadText(t *testing.T) {
    17  	tests := []struct {
    18  		input  string
    19  		expect string
    20  	}{
    21  		{
    22  			input:  `a`,
    23  			expect: `a`,
    24  		}, {
    25  			input: `a
    26  `,
    27  			expect: `a`,
    28  		}, {
    29  			input: `a
    30  b`,
    31  			expect: `a
    32  b`,
    33  		}, {
    34  			input: `a
    35  b
    36  `,
    37  			expect: `a
    38  b`,
    39  		},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		t.Run(test.input, func(t *testing.T) {
    44  			tmpfile, err := fs.TempFilenameWithText(test.input)
    45  			assert.Nil(t, err)
    46  			defer os.Remove(tmpfile)
    47  
    48  			content, err := ReadText(tmpfile)
    49  			assert.Nil(t, err)
    50  			assert.Equal(t, test.expect, content)
    51  		})
    52  	}
    53  }
    54  
    55  func TestReadTextLines(t *testing.T) {
    56  	text := `1
    57  
    58      2
    59  
    60      #a
    61      3`
    62  
    63  	tmpfile, err := fs.TempFilenameWithText(text)
    64  	assert.Nil(t, err)
    65  	defer os.Remove(tmpfile)
    66  
    67  	tests := []struct {
    68  		options     []TextReadOption
    69  		expectLines int
    70  	}{
    71  		{
    72  			nil,
    73  			6,
    74  		}, {
    75  			[]TextReadOption{KeepSpace(), OmitWithPrefix("#")},
    76  			6,
    77  		}, {
    78  			[]TextReadOption{WithoutBlank()},
    79  			4,
    80  		}, {
    81  			[]TextReadOption{OmitWithPrefix("#")},
    82  			5,
    83  		}, {
    84  			[]TextReadOption{WithoutBlank(), OmitWithPrefix("#")},
    85  			3,
    86  		},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		t.Run(stringx.Rand(), func(t *testing.T) {
    91  			lines, err := ReadTextLines(tmpfile, test.options...)
    92  			assert.Nil(t, err)
    93  			assert.Equal(t, test.expectLines, len(lines))
    94  		})
    95  	}
    96  }
    97  
    98  func TestDupReadCloser(t *testing.T) {
    99  	input := "hello"
   100  	reader := ioutil.NopCloser(bytes.NewBufferString(input))
   101  	r1, r2 := DupReadCloser(reader)
   102  	verify := func(r io.Reader) {
   103  		output, err := ioutil.ReadAll(r)
   104  		assert.Nil(t, err)
   105  		assert.Equal(t, input, string(output))
   106  	}
   107  
   108  	verify(r1)
   109  	verify(r2)
   110  }
   111  
   112  func TestReadBytes(t *testing.T) {
   113  	reader := ioutil.NopCloser(bytes.NewBufferString("helloworld"))
   114  	buf := make([]byte, 5)
   115  	err := ReadBytes(reader, buf)
   116  	assert.Nil(t, err)
   117  	assert.Equal(t, "hello", string(buf))
   118  }
   119  
   120  func TestReadBytesNotEnough(t *testing.T) {
   121  	reader := ioutil.NopCloser(bytes.NewBufferString("hell"))
   122  	buf := make([]byte, 5)
   123  	err := ReadBytes(reader, buf)
   124  	assert.Equal(t, io.EOF, err)
   125  }
   126  
   127  func TestReadBytesChunks(t *testing.T) {
   128  	buf := make([]byte, 5)
   129  	reader, writer := io.Pipe()
   130  
   131  	go func() {
   132  		for i := 0; i < 10; i++ {
   133  			writer.Write([]byte{'a'})
   134  			time.Sleep(10 * time.Millisecond)
   135  		}
   136  	}()
   137  
   138  	err := ReadBytes(reader, buf)
   139  	assert.Nil(t, err)
   140  	assert.Equal(t, "aaaaa", string(buf))
   141  }