mirror of
https://github.com/tjboldt/Apple2-IO-RPi.git
synced 2024-11-24 15:31:14 +00:00
Add test for load file command
This commit is contained in:
parent
84479fdb01
commit
105a6c318e
@ -12,11 +12,11 @@ import (
|
||||
)
|
||||
|
||||
type MockIoData struct {
|
||||
BytesToRead []byte
|
||||
BytesWritten []byte
|
||||
byteRead int
|
||||
byteWritten int
|
||||
ErrorToThrow error
|
||||
BytesToRead []byte
|
||||
BytesWritten []byte
|
||||
NumberBytesRead int
|
||||
NumberBytesWritten int
|
||||
ErrorToThrow error
|
||||
}
|
||||
|
||||
type MockIo struct {
|
||||
@ -28,51 +28,51 @@ func (mockIo MockIo) Init() {
|
||||
}
|
||||
|
||||
func (mockIo MockIo) WriteByte(data byte) error {
|
||||
mockIo.Data.BytesWritten[mockIo.Data.byteWritten] = data
|
||||
mockIo.Data.byteWritten++
|
||||
mockIo.Data.BytesWritten[mockIo.Data.NumberBytesWritten] = data
|
||||
mockIo.Data.NumberBytesWritten++
|
||||
return mockIo.Data.ErrorToThrow
|
||||
}
|
||||
|
||||
func (mockIo MockIo) WriteString(outString string) error {
|
||||
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
|
||||
}
|
||||
|
||||
func (mockIo MockIo) WriteBlock(buffer []byte) error {
|
||||
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
|
||||
}
|
||||
|
||||
func (mockIo MockIo) WriteBuffer(buffer []byte) error {
|
||||
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
|
||||
}
|
||||
|
||||
func (mockIo MockIo) ReadByte() (byte, error) {
|
||||
b := mockIo.Data.BytesToRead[mockIo.Data.byteRead]
|
||||
mockIo.Data.byteRead++
|
||||
b := mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
|
||||
mockIo.Data.NumberBytesRead++
|
||||
return b, mockIo.Data.ErrorToThrow
|
||||
}
|
||||
|
||||
func (mockIo MockIo) ReadString() (string, error) {
|
||||
builder := strings.Builder{}
|
||||
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")
|
||||
}
|
||||
builder.WriteByte(mockIo.Data.BytesToRead[mockIo.Data.byteRead])
|
||||
mockIo.Data.byteRead++
|
||||
if mockIo.Data.BytesToRead[mockIo.Data.byteRead] == 0 {
|
||||
mockIo.Data.byteRead++
|
||||
builder.WriteByte(mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead])
|
||||
mockIo.Data.NumberBytesRead++
|
||||
if mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead] == 0 {
|
||||
mockIo.Data.NumberBytesRead++
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -80,12 +80,12 @@ func (mockIo MockIo) ReadString() (string, 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")
|
||||
}
|
||||
for i := 0; i < 512; i++ {
|
||||
buffer[i] = mockIo.Data.BytesToRead[mockIo.Data.byteRead]
|
||||
mockIo.Data.byteRead++
|
||||
buffer[i] = mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
|
||||
mockIo.Data.NumberBytesRead++
|
||||
}
|
||||
return mockIo.Data.ErrorToThrow
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"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)
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user