Skip to content

Instantly share code, notes, and snippets.

@platisd
Last active July 4, 2017 08:11
Show Gist options
  • Save platisd/7bad3e8718fe0ba130e8facf2ab1910c to your computer and use it in GitHub Desktop.
Save platisd/7bad3e8718fe0ba130e8facf2ab1910c to your computer and use it in GitHub Desktop.
Calling ultrasonics in sequence example
#include <Smartcar.h>
SR04 front, back;
unsigned int d1 = 0;
unsigned int d2 = 0;
void setup() {
front.attach(3, 4); //trigger pin, echo pin
back.attach(6,7);
}
void loop() {
d1 = front.getDistance();
d2 = back.getDistance();
}
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "arduino-mock/Arduino.h"
#include "arduino-mock/Serial.h"
#include "Smartcar.h" // The Smartcar library mocks
#include "../../src/MultipleSR04.ino" // Our production code
using ::testing::InSequence;
using ::testing::Return;
using ::testing::_;
class SR04Fixture : public ::testing::Test
{
public:
ArduinoMock* arduinoMock; // Necessary for delay()
SR04Mock* SR04_mock;
// Run this before the tests
virtual void SetUp()
{
arduinoMock = arduinoMockInstance();
SR04_mock = SR04MockInstance();
}
// Run this after the tests
virtual void TearDown()
{
releaseArduinoMock();
releaseSR04Mock();
}
};
TEST_F(SR04Fixture, initsAreCalled) {
InSequence seq;
// Everything below this has to happen in the specific sequence
EXPECT_CALL(*SR04_mock, attach(3, 4));
EXPECT_CALL(*SR04_mock, attach(6, 7));
setup();
}
TEST_F(SR04Fixture, expectGetDistanceCall) {
InSequence seq;
EXPECT_CALL(*SR04_mock, getDistance())
.WillOnce(Return(15));
EXPECT_CALL(*SR04_mock, getDistance())
.WillOnce(Return(25));
loop();
// After we have run loop, we can test whether d1 and d2 got the values
ASSERT_EQ(d1, 15);
ASSERT_EQ(d2, 25);
}
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
@platisd
Copy link
Author

platisd commented Jul 4, 2017

Should try instead:

EXPECT_CALL(*SR04_mock, getDistance())
    .Times(2)
    .WillOnce(Return(15))
    .WillOnce(Return(25));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment