Skip to content

Commit

Permalink
Add syscall mock
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 8, 2014
1 parent a0bcef7 commit ece9aa1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
17 changes: 6 additions & 11 deletions sysfs/i2c_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@ 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))
}

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
}
5 changes: 1 addition & 4 deletions sysfs/i2c_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/hybridgroup/gobot"
"io"
"os"
"syscall"
"testing"
)

Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions sysfs/syscall.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ece9aa1

Please sign in to comment.