1
0

read_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package iox
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/core/fs"
  10. "github.com/zeromicro/go-zero/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 TestReadTextLines(t *testing.T) {
  49. text := `1
  50. 2
  51. #a
  52. 3`
  53. tmpFile, err := fs.TempFilenameWithText(text)
  54. assert.Nil(t, err)
  55. defer os.Remove(tmpFile)
  56. tests := []struct {
  57. options []TextReadOption
  58. expectLines int
  59. }{
  60. {
  61. nil,
  62. 6,
  63. }, {
  64. []TextReadOption{KeepSpace(), OmitWithPrefix("#")},
  65. 6,
  66. }, {
  67. []TextReadOption{WithoutBlank()},
  68. 4,
  69. }, {
  70. []TextReadOption{OmitWithPrefix("#")},
  71. 5,
  72. }, {
  73. []TextReadOption{WithoutBlank(), OmitWithPrefix("#")},
  74. 3,
  75. },
  76. }
  77. for _, test := range tests {
  78. t.Run(stringx.Rand(), func(t *testing.T) {
  79. lines, err := ReadTextLines(tmpFile, test.options...)
  80. assert.Nil(t, err)
  81. assert.Equal(t, test.expectLines, len(lines))
  82. })
  83. }
  84. }
  85. func TestDupReadCloser(t *testing.T) {
  86. input := "hello"
  87. reader := io.NopCloser(bytes.NewBufferString(input))
  88. r1, r2 := DupReadCloser(reader)
  89. verify := func(r io.Reader) {
  90. output, err := io.ReadAll(r)
  91. assert.Nil(t, err)
  92. assert.Equal(t, input, string(output))
  93. }
  94. verify(r1)
  95. verify(r2)
  96. }
  97. func TestReadBytes(t *testing.T) {
  98. reader := io.NopCloser(bytes.NewBufferString("helloworld"))
  99. buf := make([]byte, 5)
  100. err := ReadBytes(reader, buf)
  101. assert.Nil(t, err)
  102. assert.Equal(t, "hello", string(buf))
  103. }
  104. func TestReadBytesNotEnough(t *testing.T) {
  105. reader := io.NopCloser(bytes.NewBufferString("hell"))
  106. buf := make([]byte, 5)
  107. err := ReadBytes(reader, buf)
  108. assert.Equal(t, io.EOF, err)
  109. }
  110. func TestReadBytesChunks(t *testing.T) {
  111. buf := make([]byte, 5)
  112. reader, writer := io.Pipe()
  113. go func() {
  114. for i := 0; i < 10; i++ {
  115. writer.Write([]byte{'a'})
  116. time.Sleep(10 * time.Millisecond)
  117. }
  118. }()
  119. err := ReadBytes(reader, buf)
  120. assert.Nil(t, err)
  121. assert.Equal(t, "aaaaa", string(buf))
  122. }