Skip to content

Commit

Permalink
km_adapter: split cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanperret committed Oct 14, 2024
1 parent 38586c8 commit e53149f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 43 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ add_executable(${PROJECT_NAME}_e2e

${PROJECT_SOURCE_DIR}/mocks/knitting_machine.cpp

${PROJECT_SOURCE_DIR}/mocks/knitting_machine_adapter.cpp
${PROJECT_SOURCE_DIR}/mocks/io_expanders_mock.cpp

${SOURCE_DIRECTORY}/encoders.cpp
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/io_expanders_mock.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!`
/*!
* \file io_expanders_mock.cpp
*
* This file is part of AYAB.
Expand Down
8 changes: 4 additions & 4 deletions test/mocks/io_expanders_mock.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!`
/*!
* \file io_expanders_mock.h
*
* This file is part of AYAB.
Expand All @@ -23,8 +23,8 @@
#ifndef IO_EXPANDERS_MOCK_H_
#define IO_EXPANDERS_MOCK_H_

#include <array>
#include <Wire.h>
#include <array>

/*! \brief Simulate I2C I/O expanders
*
Expand All @@ -39,11 +39,11 @@
* gpioState(). The result is the state of the 16 digital outputs,
* in the order that they are normally connected to the knitting
* machine's solenoids, i.e. starting from the leftmost solenoid.
*
*
* Note that a KH270 machine only has 12 solenoids, but since this
* class simulates the I/O expanders, that always have 16 outputs,
* that fact is not relevant here.
*
*
* Creating an instance of this class takes over WireMock, no expectations
* should be setup on it from outside until that instance is destroyed
* and releaseWireMock is called.
Expand Down
65 changes: 65 additions & 0 deletions test/mocks/knitting_machine_adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*!
* \file knitting_machine_adapter.cpp
*
* This file is part of AYAB.
*
* AYAB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AYAB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* http://ayab-knitting.com
*/
#include "knitting_machine_adapter.h"

#include <gmock/gmock.h>

#include "knitting_machine.h"

#include "board.h"

using namespace ::testing;

KnittingMachineAdapter::KnittingMachineAdapter(KnittingMachine &km,
ArduinoMock &arduinoMock,
Flags flags)
: m_km(km), m_flags(flags) {
EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_A))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([&] { return m_km.getEncoderOutput1() ? HIGH : LOW; }));

EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_B))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([&] { return m_km.getEncoderOutput2() ? HIGH : LOW; }));

EXPECT_CALL(arduinoMock, analogRead(EOL_PIN_L))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[&] { return m_km.getLeftPositionSensorVoltage() * 1023 / 5; }));

EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_C))
.Times(AnyNumber())
.WillRepeatedly(Invoke([&] { return m_km.getBeltPhase() ? HIGH : LOW; }));

EXPECT_CALL(arduinoMock, analogRead(EOL_PIN_R))
.Times(AnyNumber())
.WillRepeatedly(Invoke([&] {
if (m_flags & DigitalRightSensor) {
// On the KH-910 EOL_PIN_R is plugged into the K digital signal
return m_km.getRightPositionSensorKSignal() * 1023 / 5;
} else {
return m_km.getRightPositionSensorVoltage() * 1023 / 5;
}
}));
}
60 changes: 22 additions & 38 deletions test/mocks/knitting_machine_adapter.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
/*!
* \file knitting_machine_adapter.h
*
* This file is part of AYAB.
*
* AYAB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AYAB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AYAB. If not, see <http://www.gnu.org/licenses/>.
*
* Original Work Copyright 2013 Christian Obersteiner, Andreas Müller
* http://ayab-knitting.com
*/
#ifndef KNITTING_MACHINE_ADAPTER_H_
#define KNITTING_MACHINE_ADAPTER_H_

#include <gmock/gmock.h>

#include <Arduino.h>

#include "knitting_machine.h"

#include "board.h"

using namespace ::testing;

/**
* This class connects a KnittingMachine (which does not know anything about
* testing/mocking, or the AYAB code) to ArduinoMock, so that calls to
Expand All @@ -30,38 +45,7 @@ class KnittingMachineAdapter {
};

KnittingMachineAdapter(KnittingMachine &km, ArduinoMock &arduinoMock,
Flags flags = Default)
: m_km(km), m_flags(flags) {
EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_A))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([&] { return m_km.getEncoderOutput1() ? HIGH : LOW; }));
EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_B))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([&] { return m_km.getEncoderOutput2() ? HIGH : LOW; }));

EXPECT_CALL(arduinoMock, analogRead(EOL_PIN_L))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[&] { return m_km.getLeftPositionSensorVoltage() * 1023 / 5; }));

EXPECT_CALL(arduinoMock, digitalRead(ENC_PIN_C))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([&] { return m_km.getBeltPhase() ? HIGH : LOW; }));

EXPECT_CALL(arduinoMock, analogRead(EOL_PIN_R))
.Times(AnyNumber())
.WillRepeatedly(Invoke([&] {
if (m_flags & DigitalRightSensor) {
// On the KH-910 EOL_PIN_R is plugged into the K digital signal
return m_km.getRightPositionSensorKSignal() * 1023 / 5;
} else {
return m_km.getRightPositionSensorVoltage() * 1023 / 5;
}
}));
}
Flags flags = Default);

private:
KnittingMachine &m_km;
Expand Down

0 comments on commit e53149f

Please sign in to comment.