Improve Read tests for paster

master
Nick Krichevsky 2019-03-16 18:08:30 -04:00
parent 85556339ab
commit edc451f9c6
1 changed files with 13 additions and 3 deletions

View File

@ -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) {