Skip to content

Commit

Permalink
fix singleaxis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston committed Sep 30, 2024
1 parent c039b02 commit 9174f33
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions components/gantry/singleaxis/singleaxis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ var fakeFrame = &referenceframe.LinkConfig{
var badFrame = &referenceframe.LinkConfig{}

var (
count = 0
pinValues = []int{1, 1, 0}
count = 0
defaultPinValues = []int{1, 1, 0}
limitPinValues = []int{1, 0, 1, 0, 1, 1, 0}
)

func createFakeMotor() motor.Motor {
Expand All @@ -51,26 +52,19 @@ func createFakeMotor() motor.Motor {
}
}

func createLimitBoard() board.Board {
injectGPIOPin := &inject.GPIOPin{
GetFunc: func(ctx context.Context, extra map[string]interface{}) (bool, error) { return true, nil },
SetFunc: func(ctx context.Context, high bool, extra map[string]interface{}) error { return nil },
}
return &inject.Board{GPIOPinByNameFunc: func(pin string) (board.GPIOPin, error) { return injectGPIOPin, nil }}
}

func createFakeBoard() board.Board {
func createFakeBoard(pinValues []int) board.Board {
pinCount := 0
injectGPIOPin := &inject.GPIOPin{
GetFunc: func(ctx context.Context, extra map[string]interface{}) (bool, error) {
pinVal := false
if pinValues[pinCount] == 1 {
return true, nil
pinVal = true
}
pinCount++
if pinCount == len(pinValues) {
pinCount = 0
}
return false, nil
return pinVal, nil
},
SetFunc: func(ctx context.Context, high bool, extra map[string]interface{}) error { return nil },
}
Expand All @@ -80,7 +74,7 @@ func createFakeBoard() board.Board {
func createFakeDepsForTestNewSingleAxis(t *testing.T) resource.Dependencies {
t.Helper()
deps := make(resource.Dependencies)
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)
deps[motor.Named(motorName)] = createFakeMotor()
return deps
}
Expand Down Expand Up @@ -206,7 +200,7 @@ func TestNewSingleAxis(t *testing.T) {
}
deps = make(resource.Dependencies)
deps[motor.Named(motorName)] = injectMotor
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)

_, err = newSingleAxis(ctx, deps, fakecfg, logger)
test.That(t, err.Error(), test.ShouldContainSubstring, "invalid gantry type")
Expand All @@ -221,7 +215,7 @@ func TestNewSingleAxis(t *testing.T) {

deps = make(resource.Dependencies)
deps[motor.Named(motorName)] = injectMotor
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)
properties, _ := injectMotor.Properties(ctx, nil)
_, err = newSingleAxis(ctx, deps, fakecfg, logger)
expectedErr := motor.NewPropertyUnsupportedError(properties, motorName)
Expand Down Expand Up @@ -276,7 +270,7 @@ func TestHome(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(limitPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -316,7 +310,7 @@ func TestHome(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(limitPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -337,7 +331,7 @@ func TestHome(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(limitPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -354,7 +348,7 @@ func TestHomeLimitSwitch(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(limitPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -451,7 +445,7 @@ func TestHomeLimitSwitch2(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(limitPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -536,7 +530,7 @@ func TestTestLimit(t *testing.T) {
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2"},
motor: createFakeMotor(),
board: createLimitBoard(),
board: createFakeBoard(limitPinValues),
rpm: float64(300),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
Expand All @@ -551,7 +545,7 @@ func TestTestLimitTimeout(t *testing.T) {
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2"},
motor: createFakeMotor(),
board: createLimitBoard(),
board: createFakeBoard(defaultPinValues),
rpm: float64(3000),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
Expand Down Expand Up @@ -579,7 +573,7 @@ func TestLimitHit(t *testing.T) {
ctx := context.Background()
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2", "3"},
board: createLimitBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
}
Expand All @@ -601,7 +595,7 @@ func TestPosition(t *testing.T) {
},
PositionFunc: func(ctx context.Context, extra map[string]interface{}) (float64, error) { return 1, nil },
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
positionLimits: []float64{0, 1},
positionRange: 1.0,
limitHigh: true,
Expand All @@ -622,7 +616,7 @@ func TestPosition(t *testing.T) {
return 1, errors.New("not supported")
},
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
limitSwitchPins: []string{"1", "2"},
positionLimits: []float64{0, 1},
Expand Down Expand Up @@ -650,7 +644,7 @@ func TestMoveToPosition(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
logger: logger,
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
motor: createFakeMotor(),
limitHigh: true,
positionRange: 10,
Expand Down Expand Up @@ -752,7 +746,7 @@ func TestStop(t *testing.T) {

fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -771,7 +765,7 @@ func TestCurrentInputs(t *testing.T) {

fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -788,7 +782,7 @@ func TestCurrentInputs(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -810,7 +804,7 @@ func TestCurrentInputs(t *testing.T) {
return 5, errors.New("nope")
},
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: false,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -846,7 +840,7 @@ func TestGoToInputs(t *testing.T) {
test.That(t, err.Error(), test.ShouldContainSubstring, "is homed")

fakegantry := &singleAxis{
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitSwitchPins: []string{"1", "2"},
limitHigh: true,
motor: createFakeMotor(),
Expand Down

0 comments on commit 9174f33

Please sign in to comment.