OLE Developer Guide  Latest Git
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Unit Tests

Overview

In order to maintain software quality and speed development the code is unit tested. Even though the project is written in C, we use Mock Objects to intercept function calls and set expectations.

Initial Setup

The unit tests use the gmock and gtest frameworks. To install GMock, run ./install-gmock.sh in the top level project directory. This will download & install gmock / gtest within the local project directory.

Warning
gmock / gtest should not be installed system-wide, see https://code.google.com/p/googletest/wiki/FAQ .

Mocks

Two sets of Mocks are provided:

Harmony Mocks

Any Harmony modules that we use have their API calls proxied through to a mock object. This enables us to stub out the Harmony API for testing. The Harmony mocks consist of a set of headers (found in harmony/include) and the mock harmony objects (in harmony/mocks).

The following is a simple example of how to use the Harmony mocks.

timer.c

1 // Code to test
2 #include "peripheral/tmr/plib_tmr.h"
3 
4 void StartTimer(uint16_t counter) {
5  PLIB_TMR_Counter16BitClear(TMR_ID_2);
6  PLIB_TMR_Period16BitSet(TMR_ID_2, counter);
7  PLIB_TMR_Start(TMR_ID_2);
8 }

TimerTest.cpp

1 #include <gtest/gtest.h>
2 #include "plib_tmr_mock.h"
3 
4 class TimerTest : public testing::Test {
5  public:
6  void SetUp() {
7  PLIB_TMR_SetMock(&m_mock);
8  }
9 
10  void TearDown() {
11  PLIB_TMR_SetMock(NULL);
12  }
13  MockPeripheralTimer m_mock;
14 };
15 
16 TEST(TimerTest, testZeroCounter) {
17  EXPECT_CALL(mock, Counter16BitClear(TMR_ID_2));
18  EXPECT_CALL(mock, Period16BitSet(TMR_ID_2, 10));
19  EXPECT_CALL(mock, Start(TMR_ID_2));
20  StartTimer(10);
21 }

Module Mocks

When one Ja Rule module depends on another, it's often useful to be able to mock out the dependency. Ja Rule module mocks can be found in the mocks/ directory. They work in the same way as the Harmony mocks; calls to the module's functions are proxied to a Mock object.

There is also a set of Matchers that can be used to check that binary data passed to functions matches what is expected.