| 1 |
/* ========================================== |
| 2 |
Unity Project - A Test Framework for C |
| 3 |
Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams |
| 4 |
[Released under MIT License. Please refer to license.txt for details] |
| 5 |
========================================== */ |
| 6 |
|
| 7 |
#include <setjmp.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include "unity.h" |
| 10 |
|
| 11 |
/* Support for Meta Test Rig */ |
| 12 |
#define TEST_CASE(...) |
| 13 |
|
| 14 |
/* Include Passthroughs for Linking Tests */ |
| 15 |
void putcharSpy(int c) { (void)putchar(c);} |
| 16 |
void flushSpy(void) {} |
| 17 |
|
| 18 |
#define EXPECT_ABORT_BEGIN \ |
| 19 |
if (TEST_PROTECT()) \ |
| 20 |
{ |
| 21 |
|
| 22 |
#define VERIFY_FAILS_END \ |
| 23 |
} \ |
| 24 |
Unity.CurrentTestFailed = (Unity.CurrentTestFailed != 0) ? 0 : 1; \ |
| 25 |
if (Unity.CurrentTestFailed == 1) { \ |
| 26 |
SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ |
| 27 |
UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \ |
| 28 |
UNITY_OUTPUT_CHAR(':'); \ |
| 29 |
UnityPrint(Unity.CurrentTestName); \ |
| 30 |
UnityPrint(":FAIL: [[[[ Test Should Have Failed But Did Not ]]]]"); \ |
| 31 |
UNITY_OUTPUT_CHAR('\n'); \ |
| 32 |
} |
| 33 |
|
| 34 |
#define VERIFY_IGNORES_END \ |
| 35 |
} \ |
| 36 |
Unity.CurrentTestFailed = (Unity.CurrentTestIgnored != 0) ? 0 : 1; \ |
| 37 |
Unity.CurrentTestIgnored = 0; \ |
| 38 |
if (Unity.CurrentTestFailed == 1) { \ |
| 39 |
SetToOneMeanWeAlreadyCheckedThisGuy = 1; \ |
| 40 |
UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber); \ |
| 41 |
UNITY_OUTPUT_CHAR(':'); \ |
| 42 |
UnityPrint(Unity.CurrentTestName); \ |
| 43 |
UnityPrint(":FAIL: [[[[ Test Should Have Ignored But Did Not ]]]]"); \ |
| 44 |
UNITY_OUTPUT_CHAR('\n'); \ |
| 45 |
} |
| 46 |
|
| 47 |
static int SetToOneToFailInTearDown; |
| 48 |
static int SetToOneMeanWeAlreadyCheckedThisGuy; |
| 49 |
static unsigned NextExpectedStringIndex; |
| 50 |
static unsigned NextExpectedCharIndex; |
| 51 |
|
| 52 |
void suiteSetUp(void) |
| 53 |
{ |
| 54 |
NextExpectedStringIndex = 0; |
| 55 |
NextExpectedCharIndex = 0; |
| 56 |
} |
| 57 |
|
| 58 |
void setUp(void) |
| 59 |
{ |
| 60 |
SetToOneToFailInTearDown = 0; |
| 61 |
SetToOneMeanWeAlreadyCheckedThisGuy = 0; |
| 62 |
} |
| 63 |
|
| 64 |
void tearDown(void) |
| 65 |
{ |
| 66 |
if (SetToOneToFailInTearDown == 1) |
| 67 |
TEST_FAIL_MESSAGE("<= Failed in tearDown"); |
| 68 |
if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0)) |
| 69 |
{ |
| 70 |
UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]"); |
| 71 |
UNITY_OUTPUT_CHAR('\n'); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
TEST_CASE(0) |
| 76 |
TEST_CASE(44) |
| 77 |
TEST_CASE((90)+9) |
| 78 |
void test_TheseShouldAllPass(int Num) |
| 79 |
{ |
| 80 |
TEST_ASSERT_TRUE(Num < 100); |
| 81 |
} |
| 82 |
|
| 83 |
TEST_CASE(3) |
| 84 |
TEST_CASE(77) |
| 85 |
TEST_CASE( (99) + 1 - (1)) |
| 86 |
void test_TheseShouldAllFail(int Num) |
| 87 |
{ |
| 88 |
EXPECT_ABORT_BEGIN |
| 89 |
TEST_ASSERT_TRUE(Num > 100); |
| 90 |
VERIFY_FAILS_END |
| 91 |
} |
| 92 |
|
| 93 |
TEST_CASE(1) |
| 94 |
TEST_CASE(44) |
| 95 |
TEST_CASE(99) |
| 96 |
TEST_CASE(98) |
| 97 |
void test_TheseAreEveryOther(int Num) |
| 98 |
{ |
| 99 |
if (Num & 1) |
| 100 |
{ |
| 101 |
EXPECT_ABORT_BEGIN |
| 102 |
TEST_ASSERT_TRUE(Num > 100); |
| 103 |
VERIFY_FAILS_END |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
TEST_ASSERT_TRUE(Num < 100); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void test_NormalPassesStillWork(void) |
| 112 |
{ |
| 113 |
TEST_ASSERT_TRUE(1); |
| 114 |
} |
| 115 |
|
| 116 |
void test_NormalFailsStillWork(void) |
| 117 |
{ |
| 118 |
EXPECT_ABORT_BEGIN |
| 119 |
TEST_ASSERT_TRUE(0); |
| 120 |
VERIFY_FAILS_END |
| 121 |
} |
| 122 |
|
| 123 |
TEST_CASE(0, "abc") |
| 124 |
TEST_CASE(1, "{") |
| 125 |
TEST_CASE(2, "}") |
| 126 |
TEST_CASE(3, ";") |
| 127 |
TEST_CASE(4, "\"quoted\"") |
| 128 |
void test_StringsArePreserved(unsigned index, const char * str) |
| 129 |
{ |
| 130 |
static const char * const expected[] = |
| 131 |
{ |
| 132 |
"abc", |
| 133 |
"{", |
| 134 |
"}", |
| 135 |
";", |
| 136 |
"\"quoted\"" |
| 137 |
}; |
| 138 |
|
| 139 |
/* Ensure that no test cases are skipped by tracking the next expected index */ |
| 140 |
TEST_ASSERT_EQUAL_UINT32(NextExpectedStringIndex, index); |
| 141 |
TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index); |
| 142 |
TEST_ASSERT_EQUAL_STRING(expected[index], str); |
| 143 |
|
| 144 |
NextExpectedStringIndex++; |
| 145 |
} |
| 146 |
|
| 147 |
TEST_CASE(0, 'x') |
| 148 |
TEST_CASE(1, '{') |
| 149 |
TEST_CASE(2, '}') |
| 150 |
TEST_CASE(3, ';') |
| 151 |
TEST_CASE(4, '\'') |
| 152 |
TEST_CASE(5, '"') |
| 153 |
void test_CharsArePreserved(unsigned index, char c) |
| 154 |
{ |
| 155 |
static const char expected[] = |
| 156 |
{ |
| 157 |
'x', |
| 158 |
'{', |
| 159 |
'}', |
| 160 |
';', |
| 161 |
'\'', |
| 162 |
'"' |
| 163 |
}; |
| 164 |
|
| 165 |
/* Ensure that no test cases are skipped by tracking the next expected index */ |
| 166 |
TEST_ASSERT_EQUAL_UINT32(NextExpectedCharIndex, index); |
| 167 |
TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index); |
| 168 |
TEST_ASSERT_EQUAL(expected[index], c); |
| 169 |
|
| 170 |
NextExpectedCharIndex++; |
| 171 |
} |