1
0

read_test.go 3.2 KB

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