Improve Read tests for paster
parent
85556339ab
commit
edc451f9c6
|
@ -1,10 +1,12 @@
|
|||
package paste
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/ollien/updown/repository"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
|
@ -53,15 +55,23 @@ func TestRead(t *testing.T) {
|
|||
paster := setupPaster()
|
||||
mockFile := paster.file.(*MockFile)
|
||||
|
||||
buffer := make([]byte, 5)
|
||||
mockFile.On("Read", buffer).Return(len(buffer), nil).Once()
|
||||
pasteContents := "hello"
|
||||
readBuffer := make([]byte, len(pasteContents))
|
||||
mockFile.On("Read", readBuffer).Return(len(readBuffer), io.EOF).Run(func(args mock.Arguments) {
|
||||
buffer := args.Get(0).([]byte)
|
||||
// Dump pasteContents into the buffer
|
||||
for i, char := range pasteContents {
|
||||
buffer[i] = byte(char)
|
||||
}
|
||||
}).Once()
|
||||
// Ensure we seek to the start of the file
|
||||
mockFile.On("Seek", int64(0), 0).Return(int64(0), nil).Once()
|
||||
mockFile.AssertNotCalled(t, "Close")
|
||||
mockFile.AssertNotCalled(t, "Write")
|
||||
|
||||
paster.Read(buffer)
|
||||
paster.Read(readBuffer)
|
||||
mockFile.AssertExpectations(t)
|
||||
assert.Equal(t, pasteContents, string(readBuffer))
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue