testRunnerGeneratorWithMocks.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* This Test File Is Used To Verify Many Combinations Of Using the Generate Test Runner Script */
  2. #include <stdio.h>
  3. #include "unity.h"
  4. #include "Defs.h"
  5. #include "mockMock.h"
  6. #ifdef USE_CEXCEPTION
  7. #include "CException.h"
  8. #endif
  9. /* Notes about prefixes:
  10. test - normal default prefix. these are "always run" tests for this procedure
  11. spec - normal default prefix. required to run default setup/teardown calls.
  12. should - normal default prefix.
  13. qwiktest - custom prefix for when tests skip all setup/teardown calls.
  14. custtest - custom prefix for when tests use custom setup/teardown calls.
  15. paratest - custom prefix for when we want to verify parameterized tests.
  16. extest - custom prefix only used during cexception
  17. suitetest- custom prefix for when we want to use custom suite setup/teardown
  18. */
  19. /* Support for Meta Test Rig */
  20. #define TEST_CASE(a)
  21. void putcharSpy(int c) { (void)putchar(c);} // include passthrough for linking tests
  22. /* Global Variables Used During These Tests */
  23. int CounterSetup = 0;
  24. int CounterTeardown = 0;
  25. int CounterSuiteSetup = 0;
  26. void setUp(void)
  27. {
  28. CounterSetup = 1;
  29. }
  30. void tearDown(void)
  31. {
  32. CounterTeardown = 1;
  33. }
  34. void custom_setup(void)
  35. {
  36. CounterSetup = 2;
  37. }
  38. void custom_teardown(void)
  39. {
  40. CounterTeardown = 2;
  41. }
  42. /*
  43. void test_OldSchoolCommentsShouldBeIgnored(void)
  44. {
  45. TEST_ASSERT_FAIL("Old-School Comments Should Be Ignored");
  46. }
  47. */
  48. void test_ThisTestAlwaysPasses(void)
  49. {
  50. TEST_PASS();
  51. }
  52. void test_ThisTestAlwaysFails(void)
  53. {
  54. TEST_FAIL_MESSAGE("This Test Should Fail");
  55. }
  56. void test_ThisTestAlwaysIgnored(void)
  57. {
  58. TEST_IGNORE_MESSAGE("This Test Should Be Ignored");
  59. }
  60. void qwiktest_ThisTestPassesWhenNoSetupRan(void)
  61. {
  62. TEST_ASSERT_EQUAL_MESSAGE(0, CounterSetup, "Setup Was Unexpectedly Run");
  63. }
  64. void qwiktest_ThisTestPassesWhenNoTeardownRan(void)
  65. {
  66. TEST_ASSERT_EQUAL_MESSAGE(0, CounterTeardown, "Teardown Was Unexpectedly Run");
  67. }
  68. void spec_ThisTestPassesWhenNormalSuiteSetupAndTeardownRan(void)
  69. {
  70. TEST_ASSERT_EQUAL_MESSAGE(0, CounterSuiteSetup, "Suite Setup Was Unexpectedly Run");
  71. }
  72. void spec_ThisTestPassesWhenNormalSetupRan(void)
  73. {
  74. TEST_ASSERT_EQUAL_MESSAGE(1, CounterSetup, "Normal Setup Wasn't Run");
  75. }
  76. void spec_ThisTestPassesWhenNormalTeardownRan(void)
  77. {
  78. TEST_ASSERT_EQUAL_MESSAGE(1, CounterTeardown, "Normal Teardown Wasn't Run");
  79. }
  80. void custtest_ThisTestPassesWhenCustomSetupRan(void)
  81. {
  82. TEST_ASSERT_EQUAL_MESSAGE(2, CounterSetup, "Custom Setup Wasn't Run");
  83. }
  84. void custtest_ThisTestPassesWhenCustomTeardownRan(void)
  85. {
  86. TEST_ASSERT_EQUAL_MESSAGE(2, CounterTeardown, "Custom Teardown Wasn't Run");
  87. }
  88. //void test_NewStyleCommentsShouldBeIgnored(void)
  89. //{
  90. // TEST_ASSERT_FAIL("New Style Comments Should Be Ignored");
  91. //}
  92. void test_NotBeConfusedByLongComplicatedStrings(void)
  93. {
  94. const char* crazyString = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8081\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36\r\nPostman-Token: 768c7149-c3fb-f704-71a2-63918d9195b2\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\n\r\n";
  95. TEST_ASSERT_EQUAL_STRING_MESSAGE(crazyString, crazyString, "These Strings Are The Same");
  96. }
  97. void test_NotDisappearJustBecauseTheTestBeforeAndAfterHaveCrazyStrings(void)
  98. {
  99. TEST_ASSERT_TRUE_MESSAGE(1, "1 Should be True");
  100. }
  101. void test_StillNotBeConfusedByLongComplicatedStrings(void)
  102. {
  103. const char* crazyString = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8081\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36\r\nPostman-Token: 768c7149-c3fb-f704-71a2-63918d9195b2\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\n\r\n";
  104. TEST_ASSERT_EQUAL_STRING_MESSAGE(crazyString, crazyString, "These Strings Are Still The Same");
  105. }
  106. void should_RunTestsStartingWithShouldByDefault(void)
  107. {
  108. TEST_ASSERT_TRUE_MESSAGE(1, "1 Should be True");
  109. }
  110. TEST_CASE(25)
  111. TEST_CASE(125)
  112. TEST_CASE(5)
  113. void paratest_ShouldHandleParameterizedTests(int Num)
  114. {
  115. TEST_ASSERT_EQUAL_MESSAGE(0, (Num % 5), "All The Values Are Divisible By 5");
  116. }
  117. TEST_CASE(7)
  118. void paratest_ShouldHandleParameterizedTests2(int Num)
  119. {
  120. TEST_ASSERT_EQUAL_MESSAGE(7, Num, "The Only Call To This Passes");
  121. }
  122. void paratest_ShouldHandleNonParameterizedTestsWhenParameterizationValid(void)
  123. {
  124. TEST_PASS();
  125. }
  126. TEST_CASE(17)
  127. void paratest_ShouldHandleParameterizedTestsThatFail(int Num)
  128. {
  129. TEST_ASSERT_EQUAL_MESSAGE(3, Num, "This call should fail");
  130. }
  131. #ifdef USE_CEXCEPTION
  132. void extest_ShouldHandleCExceptionInTest(void)
  133. {
  134. TEST_ASSERT_EQUAL_MESSAGE(1, CEXCEPTION_BEING_USED, "Should be pulling in CException");
  135. }
  136. #endif
  137. #ifdef USE_ANOTHER_MAIN
  138. int custom_main(void);
  139. int main(void)
  140. {
  141. return custom_main();
  142. }
  143. #endif
  144. void suitetest_ThisTestPassesWhenCustomSuiteSetupAndTeardownRan(void)
  145. {
  146. TEST_ASSERT_EQUAL_MESSAGE(1, CounterSuiteSetup, "Suite Setup Should Have Run");
  147. }
  148. void test_ShouldCallMockInitAndVerifyFunctionsForEachTest(void)
  149. {
  150. int passesOrIgnores = (int)(Unity.NumberOfTests - Unity.TestFailures);
  151. TEST_ASSERT_EQUAL_MESSAGE(Unity.NumberOfTests, mockMock_Init_Counter, "Mock Init Should Be Called Once Per Test Started");
  152. TEST_ASSERT_EQUAL_MESSAGE(passesOrIgnores, mockMock_Verify_Counter, "Mock Verify Should Be Called Once Per Test Passed");
  153. TEST_ASSERT_EQUAL_MESSAGE(Unity.NumberOfTests - 1, mockMock_Destroy_Counter, "Mock Destroy Should Be Called Once Per Test Completed");
  154. TEST_ASSERT_EQUAL_MESSAGE(0, CMockMemFreeFinalCounter, "Mock MemFreeFinal Should Not Be Called Until End");
  155. }