Apple2-IO-RPi/RaspberryPi/apple2driver/handlers/loadFile_test.go

63 lines
1.5 KiB
Go
Raw Normal View History

2022-01-11 04:00:58 +00:00
// Copyright Terence J. Boldt (c)2020-2022
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file contains the tests for the handler for
// loading files directly from the Raspberry Pi onto the Apple II
2021-10-30 11:03:18 +00:00
package handlers
import (
2021-10-30 23:30:05 +00:00
"os"
2021-10-30 11:03:18 +00:00
"testing"
"github.com/tjboldt/Apple2-IO-RPi/RaspberryPi/apple2driver/a2io"
)
func TestLoadFileCommandReturnZeroLengthOnFileNotFound(t *testing.T) {
mockIoData := a2io.MockIoData{
BytesToRead: []byte("FILE_DOES_NOT_EXIST\x00"),
BytesWritten: make([]byte, 1000),
}
mockIo := a2io.MockIo{Data: &mockIoData}
SetCommunication(&mockIo)
LoadFileCommand()
got := mockIoData.BytesWritten
if got[0] != 0 && got[1] != 0 {
t.Errorf("MenuCommand() sent = %s; want 00", got)
}
}
2021-10-30 23:30:05 +00:00
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())
}
}