diff --git a/examples/example_4/meson.build b/examples/example_4/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..5e34a3340ba66f25dd65689ccb337d4326e4521d --- /dev/null +++ b/examples/example_4/meson.build @@ -0,0 +1,17 @@ +################################################################################### +# # +# NAME: examples/example_4/meson.build # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +example_dir = include_directories('.', join_paths('.', 'src')) + +subdir('src') +subdir('test') \ No newline at end of file diff --git a/examples/example_4/readme.txt b/examples/example_4/readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..dfed81502293e43d15b55be05f3900912dc10911 --- /dev/null +++ b/examples/example_4/readme.txt @@ -0,0 +1,5 @@ +Example 1 +========= + +Close to the simplest possible example of Unity, using only basic features. +Run make to build & run the example tests. \ No newline at end of file diff --git a/examples/example_4/src/ProductionCode.c b/examples/example_4/src/ProductionCode.c new file mode 100644 index 0000000000000000000000000000000000000000..db128e5bd75f158beaf899bf8b24b228f52e20f9 --- /dev/null +++ b/examples/example_4/src/ProductionCode.c @@ -0,0 +1,24 @@ + +#include "ProductionCode.h" + +int Counter = 0; +int NumbersToFind[9] = { 0, 34, 55, 66, 32, 11, 1, 77, 888 }; /* some obnoxious array to search that is 1-based indexing instead of 0. */ + +/* This function is supposed to search through NumbersToFind and find a particular number. + * If it finds it, the index is returned. Otherwise 0 is returned which sorta makes sense since + * NumbersToFind is indexed from 1. Unfortunately it's broken + * (and should therefore be caught by our tests) */ +int FindFunction_WhichIsBroken(int NumberToFind) +{ + int i = 0; + while (i < 8) /* Notice I should have been in braces */ + i++; + if (NumbersToFind[i] == NumberToFind) /* Yikes! I'm getting run after the loop finishes instead of during it! */ + return i; + return 0; +} + +int FunctionWhichReturnsLocalVariable(void) +{ + return Counter; +} diff --git a/examples/example_4/src/ProductionCode.h b/examples/example_4/src/ProductionCode.h new file mode 100644 index 0000000000000000000000000000000000000000..250ca0dc6fe9c1f30238da929c9e685a972fbf3e --- /dev/null +++ b/examples/example_4/src/ProductionCode.h @@ -0,0 +1,3 @@ + +int FindFunction_WhichIsBroken(int NumberToFind); +int FunctionWhichReturnsLocalVariable(void); diff --git a/examples/example_4/src/ProductionCode2.c b/examples/example_4/src/ProductionCode2.c new file mode 100644 index 0000000000000000000000000000000000000000..98ee7eebc044f8ceb9e083e8003d59dd17f6ff4b --- /dev/null +++ b/examples/example_4/src/ProductionCode2.c @@ -0,0 +1,11 @@ + +#include "ProductionCode2.h" + +char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction) +{ + (void)Poor; + (void)LittleFunction; + /* Since There Are No Tests Yet, This Function Could Be Empty For All We Know. + * Which isn't terribly useful... but at least we put in a TEST_IGNORE so we won't forget */ + return (char*)0; +} diff --git a/examples/example_4/src/ProductionCode2.h b/examples/example_4/src/ProductionCode2.h new file mode 100644 index 0000000000000000000000000000000000000000..34ae980d182f27aedad61bd012f642e6dd160a99 --- /dev/null +++ b/examples/example_4/src/ProductionCode2.h @@ -0,0 +1,2 @@ + +char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction); diff --git a/examples/example_4/src/meson.build b/examples/example_4/src/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..77c391963113db830e1bb99474ffd72e165d5301 --- /dev/null +++ b/examples/example_4/src/meson.build @@ -0,0 +1,23 @@ +################################################################################### +# # +# NAME: examples/example_4/src/meson.build # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +example_src = files('ProductionCode.c', 'ProductionCode2.c') + +example_lib = library(meson.project_name(), + sources: example_src, + include_directories: example_dir) + +example_dep = declare_dependency( + version: meson.project_version(), + link_with: example_lib, + include_directories: example_dir) \ No newline at end of file diff --git a/examples/example_4/test/TestProductionCode.c b/examples/example_4/test/TestProductionCode.c new file mode 100644 index 0000000000000000000000000000000000000000..404c371843b9fc01b987a58b1f586a4c861a0246 --- /dev/null +++ b/examples/example_4/test/TestProductionCode.c @@ -0,0 +1,62 @@ + +#include "ProductionCode.h" +#include "unity.h" + +/* sometimes you may want to get at local data in a module. + * for example: If you plan to pass by reference, this could be useful + * however, it should often be avoided */ +extern int Counter; + +void setUp(void) +{ + /* This is run before EACH TEST */ + Counter = 0x5a5a; +} + +void tearDown(void) +{ +} + +void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void) +{ + /* All of these should pass */ + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(2)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1)); +} + +void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void) +{ + /* You should see this line fail in your test summary */ + TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34)); + + /* Notice the rest of these didn't get a chance to run because the line above failed. + * Unit tests abort each test function on the first sign of trouble. + * Then NEXT test function runs as normal. */ + TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888)); +} + +void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void) +{ + /* This should be true because setUp set this up for us before this test */ + TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); + + /* This should be true because we can still change our answer */ + Counter = 0x1234; + TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); +} + +void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void) +{ + /* This should be true again because setup was rerun before this test (and after we changed it to 0x1234) */ + TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); +} + +void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void) +{ + /* Sometimes you get the test wrong. When that happens, you get a failure too... and a quick look should tell + * you what actually happened...which in this case was a failure to setup the initial condition. */ + TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); +} diff --git a/examples/example_4/test/TestProductionCode2.c b/examples/example_4/test/TestProductionCode2.c new file mode 100644 index 0000000000000000000000000000000000000000..7d940c171cfc35d834e0bcb3b7c0d57ae9bf2e50 --- /dev/null +++ b/examples/example_4/test/TestProductionCode2.c @@ -0,0 +1,31 @@ + +#include "ProductionCode2.h" +#include "unity.h" + +/* These should be ignored because they are commented out in various ways: +#include "whatever.h" +#include "somethingelse.h" +*/ + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_IgnoredTest(void) +{ + TEST_IGNORE_MESSAGE("This Test Was Ignored On Purpose"); +} + +void test_AnotherIgnoredTest(void) +{ + TEST_IGNORE_MESSAGE("These Can Be Useful For Leaving Yourself Notes On What You Need To Do Yet"); +} + +void test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented(void) +{ + TEST_IGNORE(); /* Like This */ +} diff --git a/examples/example_4/test/meson.build b/examples/example_4/test/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..d551df90efbf9f475d8fc101d60ac618d99c740e --- /dev/null +++ b/examples/example_4/test/meson.build @@ -0,0 +1,14 @@ +################################################################################### +# # +# NAME: examples/example_4/test/meson.build # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +subdir('test_runners') \ No newline at end of file diff --git a/examples/example_4/test/test_runners/TestProductionCode2_Runner.c b/examples/example_4/test/test_runners/TestProductionCode2_Runner.c new file mode 100644 index 0000000000000000000000000000000000000000..cf72c219d2c8fe7df1fb0f5ed5d818ade49f9c63 --- /dev/null +++ b/examples/example_4/test/test_runners/TestProductionCode2_Runner.c @@ -0,0 +1,53 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +/*=======Test Runner Used To Run Each Test Below=====*/ +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT()) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +/*=======Automagically Detected Files To Include=====*/ +#include "unity.h" +#include +#include +#include "ProductionCode2.h" + +/*=======External Functions This Runner Calls=====*/ +extern void setUp(void); +extern void tearDown(void); +extern void test_IgnoredTest(void); +extern void test_AnotherIgnoredTest(void); +extern void test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented(void); + + +/*=======Test Reset Option=====*/ +void resetTest(void); +void resetTest(void) +{ + tearDown(); + setUp(); +} + + +/*=======MAIN=====*/ +int main(void) +{ + UnityBegin("test/TestProductionCode2.c"); + RUN_TEST(test_IgnoredTest, 18); + RUN_TEST(test_AnotherIgnoredTest, 23); + RUN_TEST(test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented, 28); + + return (UnityEnd()); +} diff --git a/examples/example_4/test/test_runners/TestProductionCode_Runner.c b/examples/example_4/test/test_runners/TestProductionCode_Runner.c new file mode 100644 index 0000000000000000000000000000000000000000..3b49af748cd3b4176acb2056e0f526233ec50906 --- /dev/null +++ b/examples/example_4/test/test_runners/TestProductionCode_Runner.c @@ -0,0 +1,57 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +/*=======Test Runner Used To Run Each Test Below=====*/ +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT()) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +/*=======Automagically Detected Files To Include=====*/ +#include "unity.h" +#include +#include +#include "ProductionCode.h" + +/*=======External Functions This Runner Calls=====*/ +extern void setUp(void); +extern void tearDown(void); +extern void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void); +extern void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void); +extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void); +extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void); +extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void); + + +/*=======Test Reset Option=====*/ +void resetTest(void); +void resetTest(void) +{ + tearDown(); + setUp(); +} + + +/*=======MAIN=====*/ +int main(void) +{ + UnityBegin("test/TestProductionCode.c"); + RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode, 20); + RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken, 30); + RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue, 41); + RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain, 51); + RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed, 57); + + return (UnityEnd()); +} diff --git a/examples/example_4/test/test_runners/meson.build b/examples/example_4/test/test_runners/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..edf35bb07c7ba3e7f6a60f63a4cf1e5c8cfbaf26 --- /dev/null +++ b/examples/example_4/test/test_runners/meson.build @@ -0,0 +1,24 @@ +################################################################################### +# # +# NAME: examples/example_4/test/test_runners/meson.build # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +test_src_1 = [ + 'TestProductionCode_Runner.c', + join_paths('..' ,'TestProductionCode.c') + ] +test_src_2 = [ + 'TestProductionCode2_Runner.c', + join_paths('..' ,'TestProductionCode2.c') + ] + +test('Test production code one', executable('test-1', test_src_1, dependencies: [ example_dep, unity_dep ])) +test('Test production code two', executable('test-2', test_src_2, dependencies: [ example_dep, unity_dep ])) \ No newline at end of file diff --git a/examples/meson.build b/examples/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..df0e7fe71eb0930692a3f26cecafa86e0f11fd41 --- /dev/null +++ b/examples/meson.build @@ -0,0 +1,14 @@ +################################################################################### +# # +# NAME: examples/meson.build # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +subdir('example_4') \ No newline at end of file diff --git a/meson_options.txt b/meson_options.txt new file mode 100755 index 0000000000000000000000000000000000000000..89f43a0cf8966d0b6aba751499baad593e9314d6 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,17 @@ +################################################################################### +# # +# NAME: meson_options.txt # +# # +# AUTHOR: Mike Karlesky, Mark VanderVoord, Greg Williams. # +# WRITTEN BY: Michael Brockus. # +# # +# License: MIT # +# # +################################################################################### + + + +option('with_examples', + type: 'feature', value : 'disabled', + description: 'Enable Unity for unit testing.' +) \ No newline at end of file