]> git.earman.xyz Git - sensor-watch.git/blob
9f999443d751ca80bf8408f8943140e430973826
[sensor-watch.git] /
1 #include "unity.h"
2 #include "event_processor.h"
3 #include "mock_display.h"
4 #include <string.h>
5
6 void setUp (void)
7 {
8 }
9
10 void tearDown (void)
11 {
12 }
13 /*
14     Test that a single function was called.
15 */
16 void
17 test_whenTheDeviceIsReset_thenTheStatusLedIsTurnedOff()
18 {
19     // When
20     event_deviceReset();
21
22     // Then
23     TEST_ASSERT_EQUAL(1, display_turnOffStatusLed_fake.call_count);
24     // or use the helper macro...
25     TEST_ASSERT_CALLED(display_turnOffStatusLed);
26 }
27
28 /*
29     Test that a single function is NOT called.
30 */
31 void
32 test_whenThePowerReadingIsLessThan5_thenTheStatusLedIsNotTurnedOn(void)
33 {
34     // When
35     event_powerReadingUpdate(4);
36
37     // Then
38     TEST_ASSERT_EQUAL(0, display_turnOnStatusLed_fake.call_count);
39     // or use the helper macro...
40     TEST_ASSERT_NOT_CALLED(display_turnOffStatusLed);
41 }
42
43 /*
44     Test that a single function was called with the correct arugment.
45 */
46 void
47 test_whenTheVolumeKnobIsMaxed_thenVolumeDisplayIsSetTo11(void)
48 {
49     // When
50     event_volumeKnobMaxed();
51
52     // Then
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);
57 }
58
59 /*
60     Test a sequence of calls.
61 */
62
63 void
64 test_whenTheModeSelectButtonIsPressed_thenTheDisplayModeIsCycled(void)
65 {
66     // When
67     event_modeSelectButtonPressed();
68     event_modeSelectButtonPressed();
69     event_modeSelectButtonPressed();
70
71     // Then
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);
79 }
80
81 /*
82     Mock a return value from a function.
83 */
84 void
85 test_givenTheDisplayHasAnError_whenTheDeviceIsPoweredOn_thenTheDisplayIsPoweredDown(void)
86 {
87     // Given
88     display_isError_fake.return_val = true;
89
90     // When
91     event_devicePoweredOn();
92
93     // Then
94     TEST_ASSERT_EQUAL(1, display_powerDown_fake.call_count);
95     // or use the helper macro...
96     TEST_ASSERT_CALLED(display_powerDown);
97 }
98
99 /*
100         Mock a sequence of calls with return values.
101 */
102
103 /*
104     Mocking a function with a value returned by reference.
105 */
106 void
107 test_givenTheUserHasTypedSleep_whenItIsTimeToCheckTheKeyboard_theDisplayIsPoweredDown(void)
108 {
109     // Given
110     char mockedEntry[] = "sleep";
111     void return_mock_value(char * entry, int length)
112     {
113         if (length > strlen(mockedEntry))
114         {
115             strncpy(entry, mockedEntry, length);
116         }
117     }
118     display_getKeyboardEntry_fake.custom_fake = return_mock_value;
119
120     // When
121     event_keyboardCheckTimerExpired();
122
123     // Then
124     TEST_ASSERT_EQUAL(1, display_powerDown_fake.call_count);
125     // or use the helper macro...
126     TEST_ASSERT_CALLED(display_powerDown);
127 }
128
129 /*
130     Mock a function with a function pointer parameter.
131 */
132 void
133 test_givenNewDataIsAvailable_whenTheDisplayHasUpdated_thenTheEventIsComplete(void)
134 {
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))
138     {
139         //Save the callback function.
140         registeredCallback = callback;
141     }
142     display_updateData_fake.custom_fake = mock_display_updateData;
143
144     // Given
145     event_newDataAvailable(10);
146
147     // When
148     if (registeredCallback != 0)
149     {
150         registeredCallback();
151     }
152
153     // Then
154     TEST_ASSERT_EQUAL(true, eventProcessor_isLastEventComplete());
155 }