read_test.go 2.4 KB

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