Add test for load file command

This commit is contained in:
Terence Boldt 2021-10-30 19:30:05 -04:00
parent 84479fdb01
commit 105a6c318e
2 changed files with 52 additions and 23 deletions

View File

@ -12,11 +12,11 @@ import (
) )
type MockIoData struct { type MockIoData struct {
BytesToRead []byte BytesToRead []byte
BytesWritten []byte BytesWritten []byte
byteRead int NumberBytesRead int
byteWritten int NumberBytesWritten int
ErrorToThrow error ErrorToThrow error
} }
type MockIo struct { type MockIo struct {
@ -28,51 +28,51 @@ func (mockIo MockIo) Init() {
} }
func (mockIo MockIo) WriteByte(data byte) error { func (mockIo MockIo) WriteByte(data byte) error {
mockIo.Data.BytesWritten[mockIo.Data.byteWritten] = data mockIo.Data.BytesWritten[mockIo.Data.NumberBytesWritten] = data
mockIo.Data.byteWritten++ mockIo.Data.NumberBytesWritten++
return mockIo.Data.ErrorToThrow return mockIo.Data.ErrorToThrow
} }
func (mockIo MockIo) WriteString(outString string) error { func (mockIo MockIo) WriteString(outString string) error {
for i, b := range outString { for i, b := range outString {
mockIo.Data.BytesWritten[i+mockIo.Data.byteWritten] = byte(b) mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = byte(b)
} }
mockIo.Data.byteWritten += len(outString) mockIo.Data.NumberBytesWritten += len(outString)
return mockIo.Data.ErrorToThrow return mockIo.Data.ErrorToThrow
} }
func (mockIo MockIo) WriteBlock(buffer []byte) error { func (mockIo MockIo) WriteBlock(buffer []byte) error {
for i, b := range buffer { for i, b := range buffer {
mockIo.Data.BytesWritten[i+mockIo.Data.byteWritten] = b mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = b
} }
mockIo.Data.byteWritten += len(buffer) mockIo.Data.NumberBytesWritten += len(buffer)
return mockIo.Data.ErrorToThrow return mockIo.Data.ErrorToThrow
} }
func (mockIo MockIo) WriteBuffer(buffer []byte) error { func (mockIo MockIo) WriteBuffer(buffer []byte) error {
for i, b := range buffer { for i, b := range buffer {
mockIo.Data.BytesWritten[i+mockIo.Data.byteWritten] = b mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = b
} }
mockIo.Data.byteWritten += len(buffer) mockIo.Data.NumberBytesWritten += len(buffer)
return mockIo.Data.ErrorToThrow return mockIo.Data.ErrorToThrow
} }
func (mockIo MockIo) ReadByte() (byte, error) { func (mockIo MockIo) ReadByte() (byte, error) {
b := mockIo.Data.BytesToRead[mockIo.Data.byteRead] b := mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
mockIo.Data.byteRead++ mockIo.Data.NumberBytesRead++
return b, mockIo.Data.ErrorToThrow return b, mockIo.Data.ErrorToThrow
} }
func (mockIo MockIo) ReadString() (string, error) { func (mockIo MockIo) ReadString() (string, error) {
builder := strings.Builder{} builder := strings.Builder{}
for { for {
if mockIo.Data.byteRead > len(mockIo.Data.BytesToRead) { if mockIo.Data.NumberBytesRead > len(mockIo.Data.BytesToRead) {
return "", errors.New("Read more data than available") return "", errors.New("Read more data than available")
} }
builder.WriteByte(mockIo.Data.BytesToRead[mockIo.Data.byteRead]) builder.WriteByte(mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead])
mockIo.Data.byteRead++ mockIo.Data.NumberBytesRead++
if mockIo.Data.BytesToRead[mockIo.Data.byteRead] == 0 { if mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead] == 0 {
mockIo.Data.byteRead++ mockIo.Data.NumberBytesRead++
break break
} }
} }
@ -80,12 +80,12 @@ func (mockIo MockIo) ReadString() (string, error) {
} }
func (mockIo MockIo) ReadBlock(buffer []byte) error { func (mockIo MockIo) ReadBlock(buffer []byte) error {
if mockIo.Data.byteRead+512 > len(mockIo.Data.BytesToRead) { if mockIo.Data.NumberBytesRead+512 > len(mockIo.Data.BytesToRead) {
return errors.New("Read more data than available") return errors.New("Read more data than available")
} }
for i := 0; i < 512; i++ { for i := 0; i < 512; i++ {
buffer[i] = mockIo.Data.BytesToRead[mockIo.Data.byteRead] buffer[i] = mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
mockIo.Data.byteRead++ mockIo.Data.NumberBytesRead++
} }
return mockIo.Data.ErrorToThrow return mockIo.Data.ErrorToThrow
} }

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"os"
"testing" "testing"
"github.com/tjboldt/Apple2-IO-RPi/RaspberryPi/apple2driver/a2io" "github.com/tjboldt/Apple2-IO-RPi/RaspberryPi/apple2driver/a2io"
@ -24,3 +25,31 @@ func TestLoadFileCommandReturnZeroLengthOnFileNotFound(t *testing.T) {
t.Errorf("MenuCommand() sent = %s; want 00", got) t.Errorf("MenuCommand() sent = %s; want 00", got)
} }
} }
func TestLoadFileCommandReturnsFile(t *testing.T) {
mockIoData := a2io.MockIoData{
BytesToRead: []byte("loadFile.go\x00"),
BytesWritten: make([]byte, 10000),
}
mockIo := a2io.MockIo{Data: &mockIoData}
SetCommunication(&mockIo)
LoadFileCommand()
fileInfo, _ := os.Stat("loadFile.go")
got := mockIoData.BytesWritten
lowSize := fileInfo.Size() & 0xFF
hiSize := (fileInfo.Size() & 0xFF00) >> 8
if got[0] != byte(lowSize) && got[1] != byte(hiSize) {
t.Errorf("MenuCommand() fileSize = %02X%02X; want %02X%02X", got[1], got[0], hiSize, lowSize)
}
if mockIoData.NumberBytesWritten-2 != int(fileInfo.Size()) {
t.Errorf("MenuCommand() byte count = %04X; want %04X", mockIoData.NumberBytesWritten-2, fileInfo.Size())
}
}