| 1 |
|
| 2 |
#include "ProductionCode.h" |
| 3 |
#include "unity.h" |
| 4 |
|
| 5 |
//sometimes you may want to get at local data in a module. |
| 6 |
//for example: If you plan to pass by reference, this could be useful |
| 7 |
//however, it should often be avoided |
| 8 |
extern int Counter; |
| 9 |
|
| 10 |
void setUp(void) |
| 11 |
{ |
| 12 |
//This is run before EACH TEST |
| 13 |
Counter = 0x5a5a; |
| 14 |
} |
| 15 |
|
| 16 |
void tearDown(void) |
| 17 |
{ |
| 18 |
} |
| 19 |
|
| 20 |
void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void) |
| 21 |
{ |
| 22 |
//All of these should pass |
| 23 |
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78)); |
| 24 |
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1)); |
| 25 |
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33)); |
| 26 |
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999)); |
| 27 |
TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1)); |
| 28 |
} |
| 29 |
|
| 30 |
void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void) |
| 31 |
{ |
| 32 |
// You should see this line fail in your test summary |
| 33 |
TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34)); |
| 34 |
|
| 35 |
// Notice the rest of these didn't get a chance to run because the line above failed. |
| 36 |
// Unit tests abort each test function on the first sign of trouble. |
| 37 |
// Then NEXT test function runs as normal. |
| 38 |
TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888)); |
| 39 |
} |
| 40 |
|
| 41 |
void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void) |
| 42 |
{ |
| 43 |
//This should be true because setUp set this up for us before this test |
| 44 |
TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); |
| 45 |
|
| 46 |
//This should be true because we can still change our answer |
| 47 |
Counter = 0x1234; |
| 48 |
TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); |
| 49 |
} |
| 50 |
|
| 51 |
void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void) |
| 52 |
{ |
| 53 |
//This should be true again because setup was rerun before this test (and after we changed it to 0x1234) |
| 54 |
TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); |
| 55 |
} |
| 56 |
|
| 57 |
void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void) |
| 58 |
{ |
| 59 |
//Sometimes you get the test wrong. When that happens, you get a failure too... and a quick look should tell |
| 60 |
// you what actually happened...which in this case was a failure to setup the initial condition. |
| 61 |
TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); |
| 62 |
} |