1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

Add syscall mock

This commit is contained in:
Adrian Zankich 2014-11-07 16:56:13 -08:00
parent a0bcef77fe
commit ece9aa16f9
3 changed files with 37 additions and 15 deletions

View File

@ -18,7 +18,12 @@ func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
return nil, err
}
errno := ioctrl(file, address)
_, _, errno := Syscall(
syscall.SYS_IOCTL,
file.Fd(),
I2CSlave,
uintptr(address),
)
if errno != 0 {
return nil, errors.New(fmt.Sprintf("Failed with syscall.Errno %v", errno))
@ -26,13 +31,3 @@ func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
return file, nil
}
var ioctrl = func(file File, address byte) syscall.Errno {
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
file.Fd(),
I2CSlave, uintptr(address),
)
return errno
}

View File

@ -4,7 +4,6 @@ import (
"github.com/hybridgroup/gobot"
"io"
"os"
"syscall"
"testing"
)
@ -24,9 +23,7 @@ func TestNewI2cDevice(t *testing.T) {
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
gobot.Refute(t, err, nil)
ioctrl = func(file File, address byte) syscall.Errno {
return 0
}
SetSyscall(&MockSyscall{})
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
gobot.Assert(t, err, nil)

30
sysfs/syscall.go Normal file
View File

@ -0,0 +1,30 @@
package sysfs
import (
"syscall"
)
type SystemCaller interface {
Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
}
type NativeSyscall struct{}
type MockSyscall struct{}
var sys SystemCaller = &NativeSyscall{}
func SetSyscall(s SystemCaller) {
sys = s
}
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
return sys.Syscall(trap, a1, a2, a3)
}
func (sys *NativeSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
return syscall.Syscall(trap, a1, a2, a3)
}
func (sys *MockSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
return 0, 0, 0
}