2 #include "event_processor.h"
3 #include "mock_display.h"
14 Test that a single function was called.
17 test_whenTheDeviceIsReset_thenTheStatusLedIsTurnedOff()
23 TEST_ASSERT_EQUAL(1, display_turnOffStatusLed_fake.call_count);
24 // or use the helper macro...
25 TEST_ASSERT_CALLED(display_turnOffStatusLed);
29 Test that a single function is NOT called.
32 test_whenThePowerReadingIsLessThan5_thenTheStatusLedIsNotTurnedOn(void)
35 event_powerReadingUpdate(4);
38 TEST_ASSERT_EQUAL(0, display_turnOnStatusLed_fake.call_count);
39 // or use the helper macro...
40 TEST_ASSERT_NOT_CALLED(display_turnOffStatusLed);
44 Test that a single function was called with the correct arugment.
47 test_whenTheVolumeKnobIsMaxed_thenVolumeDisplayIsSetTo11(void)
50 event_volumeKnobMaxed();
53 TEST_ASSERT_EQUAL(1, display_setVolume_fake.call_count);
54 // or use the helper macro...
55 TEST_ASSERT_CALLED(display_setVolume);
56 TEST_ASSERT_EQUAL(11, display_setVolume_fake.arg0_val);
60 Test a sequence of calls.
64 test_whenTheModeSelectButtonIsPressed_thenTheDisplayModeIsCycled(void)
67 event_modeSelectButtonPressed();
68 event_modeSelectButtonPressed();
69 event_modeSelectButtonPressed();
72 TEST_ASSERT_EQUAL_PTR((void *)display_setModeToMinimum, fff.call_history[0]);
73 TEST_ASSERT_EQUAL_PTR((void *)display_setModeToMaximum, fff.call_history[1]);
74 TEST_ASSERT_EQUAL_PTR((void *)display_setModeToAverage, fff.call_history[2]);
75 // or use the helper macros...
76 TEST_ASSERT_CALLED_IN_ORDER(0, display_setModeToMinimum);
77 TEST_ASSERT_CALLED_IN_ORDER(1, display_setModeToMaximum);
78 TEST_ASSERT_CALLED_IN_ORDER(2, display_setModeToAverage);
82 Mock a return value from a function.
85 test_givenTheDisplayHasAnError_whenTheDeviceIsPoweredOn_thenTheDisplayIsPoweredDown(void)
88 display_isError_fake.return_val = true;
91 event_devicePoweredOn();
94 TEST_ASSERT_EQUAL(1, display_powerDown_fake.call_count);
95 // or use the helper macro...
96 TEST_ASSERT_CALLED(display_powerDown);
100 Mock a sequence of calls with return values.
104 Mocking a function with a value returned by reference.
107 test_givenTheUserHasTypedSleep_whenItIsTimeToCheckTheKeyboard_theDisplayIsPoweredDown(void)
110 char mockedEntry[] = "sleep";
111 void return_mock_value(char * entry, int length)
113 if (length > strlen(mockedEntry))
115 strncpy(entry, mockedEntry, length);
118 display_getKeyboardEntry_fake.custom_fake = return_mock_value;
121 event_keyboardCheckTimerExpired();
124 TEST_ASSERT_EQUAL(1, display_powerDown_fake.call_count);
125 // or use the helper macro...
126 TEST_ASSERT_CALLED(display_powerDown);
130 Mock a function with a function pointer parameter.
133 test_givenNewDataIsAvailable_whenTheDisplayHasUpdated_thenTheEventIsComplete(void)
135 // A mock function for capturing the callback handler function pointer.
136 void(*registeredCallback)(void) = 0;
137 void mock_display_updateData(int data, void(*callback)(void))
139 //Save the callback function.
140 registeredCallback = callback;
142 display_updateData_fake.custom_fake = mock_display_updateData;
145 event_newDataAvailable(10);
148 if (registeredCallback != 0)
150 registeredCallback();
154 TEST_ASSERT_EQUAL(true, eventProcessor_isLastEventComplete());