Skip to content

Commit

Permalink
routes to Test
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Sep 27, 2023
1 parent 2e584b9 commit d7eebcc
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 13 deletions.
2 changes: 2 additions & 0 deletions doc/finite_state_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ A tabular representation of state transitions follows.
`Idle -> Init` | `Com::h_reqInit()`
`Init -> Ready` | `OpInit::update()`
`Ready -> Knit` | `OpKnit::startKnitting()`
`Init -> Test` | `OpTest::startTest()`
`Ready -> Test` | `OpTest::startTest()`
`Knit -> Test` | `OpTest::startTest()`
`Knit -> Init` | `m_workedOnLine && m_lastLineFlag`
`Test -> Init` | `OpTest::end()`
7 changes: 7 additions & 0 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ void Com::h_reqTest() const {
send_cnfTest(Err_t::Success);
}

/*!
* \brief Handle `quitCmd` (cancel) command.
*/
void Com::h_quitCmd() const {
GlobalController::setState(GlobalOpInit::m_instance);
}

/*!
* \brief Handle unrecognized command.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ComInterface {
virtual void h_cnfLine(const uint8_t *buffer, size_t size) = 0;
virtual void h_reqInfo() const = 0;
virtual void h_reqTest() const = 0;
virtual void h_quitCmd() const = 0;
virtual void h_unrecognized() const = 0;
};

Expand Down Expand Up @@ -129,6 +130,7 @@ class GlobalCom final {
static void h_cnfLine(const uint8_t *buffer, size_t size);
static void h_reqInfo();
static void h_reqTest();
static void h_quitCmd();
static void h_unrecognized();

private:
Expand All @@ -152,6 +154,7 @@ class Com : public ComInterface {
void h_cnfLine(const uint8_t *buffer, size_t size) final;
void h_reqInfo() const final;
void h_reqTest() const final;
void h_quitCmd() const final;
void h_unrecognized() const final;

private:
Expand Down
4 changes: 4 additions & 0 deletions src/ayab/global_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void GlobalCom::h_reqTest() {
m_instance->h_reqTest();
}

void GlobalCom::h_quitCmd() {
m_instance->h_quitCmd();
}

void GlobalCom::h_unrecognized() {
m_instance->h_unrecognized();
}
2 changes: 2 additions & 0 deletions src/ayab/opIdle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ void OpIdle::com(const uint8_t *buffer, size_t size) {
case static_cast<uint8_t>(API_t::reqInit):
GlobalCom::h_reqInit(buffer, size);
break;

default:
GlobalCom::h_unrecognized();
break;
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/ayab/opInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,16 @@ bool OpInit::isReady() {
* \brief Communication callback for state OpInit
*/
void OpInit::com(const uint8_t *buffer, size_t size) {
(void) buffer;
(void) size;
switch (buffer[0]) {
case static_cast<uint8_t>(API_t::reqTest):
GlobalCom::h_reqTest();
break;

default:
GlobalCom::h_unrecognized();
break;
}
(void) size; // unused
}

/*!
Expand Down
10 changes: 8 additions & 2 deletions src/ayab/opKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,26 @@ void OpKnit::begin() {
}

/*!
* \brief Update knitting procedure.
* \brief Update knitting operation.
*/
void OpKnit::update() {
knit();
}

/*!
* \brief Communication callback for knitting procedure.
* \brief Communication callback for knitting operation.
*/
void OpKnit::com(const uint8_t *buffer, size_t size) {
switch (buffer[0]) {
case static_cast<uint8_t>(API_t::cnfLine):
GlobalCom::h_cnfLine(buffer, size);
break;

case static_cast<uint8_t>(API_t::reqTest):
GlobalCom::h_reqTest();
break;

// FIXME needs to be a `Cancel` command in the API that resets state from `OpKnit` to `OpInit`
default:
GlobalCom::h_unrecognized();
break;
Expand Down
3 changes: 3 additions & 0 deletions src/ayab/opReady.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ void OpReady::com(const uint8_t *buffer, size_t size) {
case static_cast<uint8_t>(API_t::reqStart):
GlobalCom::h_reqStart(buffer, size);
break;

case static_cast<uint8_t>(API_t::reqTest):
GlobalCom::h_reqTest();
break;

default:
GlobalCom::h_unrecognized();
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ayab/opTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void OpTest::com(const uint8_t *buffer, size_t size) {
break;

case static_cast<uint8_t>(API_t::quitCmd):
end();
GlobalCom::h_quitCmd();
break;

default:
Expand All @@ -155,7 +155,6 @@ void OpTest::com(const uint8_t *buffer, size_t size) {
void OpTest::end() {
m_autoReadOn = false;
m_autoTestOn = false;
GlobalController::setState(GlobalOpInit::m_instance);
GlobalOpKnit::init();
GlobalEncoders::setUpInterrupt();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ayab/solenoids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void Solenoids::setSolenoids(uint16_t state) {
*/
// GCOVR_EXCL_START
void Solenoids::write(uint16_t newState) {
(void)newState;
//(void)newState;
mcp_0.writeGPIO(lowByte(newState));
mcp_1.writeGPIO(highByte(newState));
}
Expand Down
5 changes: 5 additions & 0 deletions test/mocks/com_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ void Com::h_reqTest() const {
gComMock->h_reqTest();
}

void Com::h_quitCmd() const {
assert(gComMock != nullptr);
gComMock->h_quitCmd();
}

void Com::h_unrecognized() const {
assert(gComMock != nullptr);
gComMock->h_unrecognized();
Expand Down
1 change: 1 addition & 0 deletions test/mocks/com_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ComMock : public ComInterface {
MOCK_METHOD2(h_cnfLine, void(const uint8_t *buffer, size_t size));
MOCK_CONST_METHOD0(h_reqInfo, void());
MOCK_CONST_METHOD0(h_reqTest, void());
MOCK_CONST_METHOD0(h_quitCmd, void());
MOCK_CONST_METHOD0(h_unrecognized, void());
};

Expand Down
8 changes: 8 additions & 0 deletions test/test_OpIdle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <gtest/gtest.h>

#include <com.h>
#include <opIdle.h>

#include <controller_mock.h>
Expand Down Expand Up @@ -82,6 +83,13 @@ TEST_F(OpIdleTest, test_init) {
opIdle->init();
}

TEST_F(OpIdleTest, test_reqTest) {
// no calls expected
// can't enter state `OpTest` from state `OpIdle`
const uint8_t buffer[] = {static_cast<uint8_t>(API_t::reqTest)};
opIdle->com(buffer, 1);
}

TEST_F(OpIdleTest, test_unrecognized) {
// no calls expected
const uint8_t buffer[] = {0xFF};
Expand Down
28 changes: 24 additions & 4 deletions test/test_OpInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <opInit.h>
#include <opReady.h>
#include <opTest.h>

#include <controller_mock.h>
#include <opKnit_mock.h>
Expand All @@ -37,6 +38,7 @@ using ::testing::Return;

extern OpInit *opInit;
extern OpReady *opReady;
extern OpTest *opTest;

extern ControllerMock *controller;
extern OpKnitMock *opKnit;
Expand Down Expand Up @@ -103,10 +105,19 @@ TEST_F(OpInitTest, test_init) {
ASSERT_EQ(opInit->m_lastHall, Direction_t::NoDirection);
}

TEST_F(OpInitTest, test_com) {
TEST_F(OpInitTest, test_reqTest) {
EXPECT_CALL(*controllerMock, setState(opTest));
const uint8_t buffer[] = {static_cast<uint8_t>(API_t::reqTest)};
opInit->com(buffer, 1);

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(controllerMock));
}

TEST_F(OpInitTest, test_com_unrecognized) {
// no calls expected
const uint8_t *buffer = {};
opInit->com(buffer, 0);
const uint8_t buffer[] = {0xFF};
opInit->com(buffer, 1);
}

TEST_F(OpInitTest, test_end) {
Expand All @@ -118,14 +129,20 @@ TEST_F(OpInitTest, test_begin910) {
EXPECT_CALL(*controllerMock, getMachineType());
EXPECT_CALL(*arduinoMock, digitalWrite(LED_PIN_A, LOW));
opInit->begin();

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(controllerMock));
}

TEST_F(OpInitTest, test_updateF) {
// isReady() == false
expect_update(get_position_past_right(Machine_t::Kh910), Direction_t::Left, Direction_t::Left);
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opInit));
EXPECT_CALL(*controllerMock, getState).Times(0);
EXPECT_CALL(*controllerMock, setState(opReady)).Times(0);
opInit->update();

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(controllerMock));
}

TEST_F(OpInitTest, test_updateT) {
Expand All @@ -134,6 +151,9 @@ TEST_F(OpInitTest, test_updateT) {
EXPECT_CALL(*controllerMock, getState).WillOnce(Return(opInit));
EXPECT_CALL(*controllerMock, setState(opReady));
opInit->update();

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(controllerMock));
}

TEST_F(OpInitTest, test_op_init_RLL) {
Expand Down
5 changes: 5 additions & 0 deletions test/test_OpKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gtest/gtest.h>

#include <board.h>
#include <com.h>
#include <opKnit.h>

#include <beeper_mock.h>
Expand Down Expand Up @@ -291,6 +292,10 @@ TEST_F(OpKnitTest, test_com) {
EXPECT_CALL(*comMock, h_cnfLine);
opKnit->com(cnf, 1);

const uint8_t reqTest[] = {static_cast<uint8_t>(API_t::reqTest)};
EXPECT_CALL(*comMock, h_reqTest);
opKnit->com(reqTest, 1);

const uint8_t unrec[] = {0xFF};
EXPECT_CALL(*comMock, h_unrecognized);
opKnit->com(unrec, 1);
Expand Down
10 changes: 8 additions & 2 deletions test/test_OpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,21 @@ TEST_F(OpTestTest, test_autoTestCmd) {

TEST_F(OpTestTest, test_quitCmd) {
const uint8_t buf[] = {static_cast<uint8_t>(API_t::quitCmd)};
EXPECT_CALL(*opKnitMock, init);
EXPECT_CALL(*controllerMock, setState(opInit));
opTest->com(buf, 1);

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock));
ASSERT_TRUE(Mock::VerifyAndClear(controllerMock));
}

TEST_F(OpTestTest, test_end) {
EXPECT_CALL(*opKnitMock, init);
opTest->end();

// test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(opKnitMock));
}

TEST_F(OpTestTest, test_loop_null) {
expect_startTest(0U);
EXPECT_CALL(*arduinoMock, millis).Times(0);
Expand Down

0 comments on commit d7eebcc

Please sign in to comment.