Apple2-IO-RPi/RaspberryPi/apple2driver/a2io/mockio.go

113 lines
3.3 KiB
Go
Raw Normal View History

2022-01-11 04:00:58 +00:00
// Copyright Terence J. Boldt (c)2021-2022
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file is used for mocking the GPIO code so that unit tests can be run
// on any machine and not just a Raspberry Pi
2021-11-03 09:33:09 +00:00
2021-10-30 11:03:18 +00:00
package a2io
import (
"errors"
"strings"
)
2021-11-03 09:33:09 +00:00
// MockIoData is used to verify tests were successful
2021-10-30 11:03:18 +00:00
type MockIoData struct {
2021-10-30 23:30:05 +00:00
BytesToRead []byte
BytesWritten []byte
NumberBytesRead int
NumberBytesWritten int
ErrorToThrow error
2021-10-30 11:03:18 +00:00
}
2021-11-03 09:33:09 +00:00
// MockIo implements A2Io to allow unit tests to run without needing GPIO functioning
2021-10-30 11:03:18 +00:00
type MockIo struct {
Data *MockIoData
}
2021-11-03 09:33:09 +00:00
// Init is only here to complete A2Io interface
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) Init() {
}
2021-11-03 09:33:09 +00:00
// WriteByte mocks writing a byte to the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) WriteByte(data byte) error {
2021-10-30 23:30:05 +00:00
mockIo.Data.BytesWritten[mockIo.Data.NumberBytesWritten] = data
mockIo.Data.NumberBytesWritten++
2021-10-30 11:03:18 +00:00
return mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// WriteString mocks writing a string to the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) WriteString(outString string) error {
for i, b := range outString {
2021-10-30 23:30:05 +00:00
mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = byte(b)
2021-10-30 11:03:18 +00:00
}
2021-10-30 23:30:05 +00:00
mockIo.Data.NumberBytesWritten += len(outString)
2021-10-30 11:03:18 +00:00
return mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// WriteBlock mocks writing a block to the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) WriteBlock(buffer []byte) error {
for i, b := range buffer {
2021-10-30 23:30:05 +00:00
mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = b
2021-10-30 11:03:18 +00:00
}
2021-10-30 23:30:05 +00:00
mockIo.Data.NumberBytesWritten += len(buffer)
2021-10-30 11:03:18 +00:00
return mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// WriteBuffer mocks writing a buffer to the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) WriteBuffer(buffer []byte) error {
for i, b := range buffer {
2021-10-30 23:30:05 +00:00
mockIo.Data.BytesWritten[i+mockIo.Data.NumberBytesWritten] = b
2021-10-30 11:03:18 +00:00
}
2021-10-30 23:30:05 +00:00
mockIo.Data.NumberBytesWritten += len(buffer)
2021-10-30 11:03:18 +00:00
return mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// ReadByte mocks reading a byte from the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) ReadByte() (byte, error) {
2021-10-30 23:30:05 +00:00
b := mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
mockIo.Data.NumberBytesRead++
2021-10-30 11:03:18 +00:00
return b, mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// ReadString mocks reading a null terminated string from the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) ReadString() (string, error) {
builder := strings.Builder{}
for {
2021-10-30 23:30:05 +00:00
if mockIo.Data.NumberBytesRead > len(mockIo.Data.BytesToRead) {
2021-11-03 09:33:09 +00:00
return "", errors.New("read more data than available")
2021-10-30 11:03:18 +00:00
}
2021-10-30 23:30:05 +00:00
builder.WriteByte(mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead])
mockIo.Data.NumberBytesRead++
if mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead] == 0 {
mockIo.Data.NumberBytesRead++
2021-10-30 11:03:18 +00:00
break
}
}
return builder.String(), mockIo.Data.ErrorToThrow
}
2021-11-03 09:33:09 +00:00
// ReadBlock mocks reading a 512 byte block from the Apple II
2021-10-30 11:03:18 +00:00
func (mockIo MockIo) ReadBlock(buffer []byte) error {
2021-10-30 23:30:05 +00:00
if mockIo.Data.NumberBytesRead+512 > len(mockIo.Data.BytesToRead) {
2021-11-03 09:33:09 +00:00
return errors.New("read more data than available")
2021-10-30 11:03:18 +00:00
}
for i := 0; i < 512; i++ {
2021-10-30 23:30:05 +00:00
buffer[i] = mockIo.Data.BytesToRead[mockIo.Data.NumberBytesRead]
mockIo.Data.NumberBytesRead++
2021-10-30 11:03:18 +00:00
}
return mockIo.Data.ErrorToThrow
}
// SendCharacter is a pass-through to vt100 implementation
func (mockIo MockIo) SendCharacter(character byte) {
sendCharacter(mockIo, character)
}
// ReadCharacter is a pass-through to vt100 implementation
func (mockIo MockIo) ReadCharacter() (string, error) {
return readCharacter(mockIo)
}