1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
package coding
import (
"errors"
"io"
"testing"
"github.com/dromara/dongle/internal/mock"
"github.com/stretchr/testify/assert"
)
func TestEncoder_FromString(t *testing.T) {
t.Run("from string", func(t *testing.T) {
encoder := NewEncoder().FromString("hello world")
assert.Equal(t, []byte("hello world"), encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from empty string", func(t *testing.T) {
encoder := NewEncoder().FromString("")
assert.Equal(t, []byte{}, encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from unicode string", func(t *testing.T) {
encoder := NewEncoder().FromString("你好世界")
assert.Equal(t, []byte("你好世界"), encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from large string", func(t *testing.T) {
largeString := "Hello, World! " + string(make([]byte, 1000))
encoder := NewEncoder().FromString(largeString)
assert.Equal(t, []byte(largeString), encoder.src)
assert.Equal(t, encoder, encoder)
})
}
func TestEncoder_FromBytes(t *testing.T) {
t.Run("from bytes", func(t *testing.T) {
data := []byte{0x00, 0x01, 0x02, 0x03}
encoder := NewEncoder().FromBytes(data)
assert.Equal(t, data, encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from empty bytes", func(t *testing.T) {
encoder := NewEncoder().FromBytes([]byte{})
assert.Equal(t, []byte{}, encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from nil bytes", func(t *testing.T) {
encoder := NewEncoder().FromBytes(nil)
assert.Nil(t, encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from large bytes", func(t *testing.T) {
largeData := make([]byte, 1000)
for i := range largeData {
largeData[i] = byte(i % 256)
}
encoder := NewEncoder().FromBytes(largeData)
assert.Equal(t, largeData, encoder.src)
assert.Equal(t, encoder, encoder)
})
t.Run("from binary data", func(t *testing.T) {
binaryData := []byte{0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0xFC}
encoder := NewEncoder().FromBytes(binaryData)
assert.Equal(t, binaryData, encoder.src)
assert.Equal(t, encoder, encoder)
})
}
func TestEncoder_FromFile(t *testing.T) {
t.Run("from file", func(t *testing.T) {
file := mock.NewFile([]byte("hello world"), "test.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
assert.Equal(t, file, encoder.reader)
assert.Equal(t, encoder, encoder)
})
t.Run("from empty file", func(t *testing.T) {
file := mock.NewFile([]byte{}, "empty.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
assert.Equal(t, file, encoder.reader)
assert.Equal(t, encoder, encoder)
})
t.Run("from error file", func(t *testing.T) {
errorFile := mock.NewErrorFile(errors.New("read error"))
encoder := NewEncoder().FromFile(errorFile)
assert.Equal(t, errorFile, encoder.reader)
assert.Equal(t, encoder, encoder)
})
t.Run("from large file", func(t *testing.T) {
largeData := make([]byte, 1000)
for i := range largeData {
largeData[i] = byte(i % 256)
}
file := mock.NewFile(largeData, "large.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
assert.Equal(t, file, encoder.reader)
assert.Equal(t, encoder, encoder)
})
}
func TestEncoder_ToString(t *testing.T) {
t.Run("to string with data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte("hello world")
result := encoder.ToString()
assert.Equal(t, "hello world", result)
})
t.Run("to string with empty data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte{}
result := encoder.ToString()
assert.Equal(t, "", result)
})
t.Run("to string with nil data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = nil
result := encoder.ToString()
assert.Equal(t, "", result)
})
t.Run("to string with unicode data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte("你好世界")
result := encoder.ToString()
assert.Equal(t, "你好世界", result)
})
t.Run("to string with binary data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte{0x00, 0x01, 0x02, 0x03}
result := encoder.ToString()
assert.Equal(t, string([]byte{0x00, 0x01, 0x02, 0x03}), result)
})
}
func TestEncoder_ToBytes(t *testing.T) {
t.Run("to bytes with data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte("hello world")
result := encoder.ToBytes()
assert.Equal(t, []byte("hello world"), result)
})
t.Run("to bytes with empty data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte{}
result := encoder.ToBytes()
assert.Equal(t, []byte(""), result)
})
t.Run("to bytes with nil data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = nil
result := encoder.ToBytes()
assert.Equal(t, []byte(""), result)
})
t.Run("to bytes with unicode data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte("你好世界")
result := encoder.ToBytes()
assert.Equal(t, []byte("你好世界"), result)
})
t.Run("to bytes with binary data", func(t *testing.T) {
encoder := NewEncoder()
encoder.dst = []byte{0x00, 0x01, 0x02, 0x03}
result := encoder.ToBytes()
assert.Equal(t, []byte{0x00, 0x01, 0x02, 0x03}, result)
})
t.Run("to bytes with large data", func(t *testing.T) {
largeData := make([]byte, 1000)
for i := range largeData {
largeData[i] = byte(i % 256)
}
encoder := NewEncoder()
encoder.dst = largeData
result := encoder.ToBytes()
assert.Equal(t, largeData, result)
})
}
func TestEncoder_stream(t *testing.T) {
t.Run("success with data", func(t *testing.T) {
data := []byte("hello stream")
file := mock.NewFile(data, "data.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
out, err := encoder.stream(func(w io.Writer) io.WriteCloser {
return mock.NewWriteCloser(w)
})
assert.NoError(t, err)
assert.Equal(t, data, out)
})
t.Run("empty reader returns empty", func(t *testing.T) {
file := mock.NewFile([]byte{}, "empty.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
out, err := encoder.stream(func(w io.Writer) io.WriteCloser {
return mock.NewWriteCloser(w)
})
assert.NoError(t, err)
assert.Equal(t, []byte{}, out)
})
t.Run("copy error returned", func(t *testing.T) {
copyErr := errors.New("copy error")
// As reader: Read returns error immediately
errRWC := mock.NewErrorReadWriteCloser(copyErr)
encoder := NewEncoder()
encoder.reader = errRWC
out, err := encoder.stream(func(w io.Writer) io.WriteCloser {
// writer also returns error, but io.CopyBuffer will fail at Read first
return errRWC
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "copy error")
assert.Equal(t, []byte{}, out)
})
t.Run("close error after successful copy", func(t *testing.T) {
file := mock.NewFile([]byte("payload"), "p.txt")
defer file.Close()
encoder := NewEncoder().FromFile(file)
out, err := encoder.stream(func(w io.Writer) io.WriteCloser {
// Write succeeds, but Close returns error
return mock.NewCloseErrorWriteCloser(w, errors.New("close error"))
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "close error")
assert.Equal(t, []byte{}, out)
})
}
func TestEncoder_Error(t *testing.T) {
t.Run("stream with pipe error", func(t *testing.T) {
errorWriter := mock.NewErrorReadWriteCloser(errors.New("copy error"))
encoder := NewEncoder()
encoder.reader = errorWriter
result, err := encoder.stream(func(w io.Writer) io.WriteCloser {
return errorWriter
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "copy error")
assert.Equal(t, []byte{}, result)
})
t.Run("stream with close error", func(t *testing.T) {
closeErrorWriter := mock.NewErrorReadWriteCloser(errors.New("close error"))
encoder := NewEncoder()
encoder.reader = closeErrorWriter
result, err := encoder.stream(func(w io.Writer) io.WriteCloser {
return closeErrorWriter
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "close error")
assert.Equal(t, []byte{}, result)
})
}
|