diff --git a/auto/colour_prompt.rb b/auto/colour_prompt.rb index 71fefba..81003dd 100644 --- a/auto/colour_prompt.rb +++ b/auto/colour_prompt.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + if RUBY_PLATFORM =~/(win|w)32$/ begin require 'Win32API' diff --git a/auto/colour_reporter.rb b/auto/colour_reporter.rb index 42a0526..89e7951 100644 --- a/auto/colour_reporter.rb +++ b/auto/colour_reporter.rb @@ -1,3 +1,8 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== require "#{File.expand_path(File.dirname(__FILE__))}/colour_prompt" diff --git a/auto/generate_module.rb b/auto/generate_module.rb index bd8dbe2..17036dd 100644 --- a/auto/generate_module.rb +++ b/auto/generate_module.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + # This script creates all the files with start code necessary for a new module. # A simple module only requires a source file, header file, and test file. # Triad modules require a source, header, and test file for each triad type (like model, conductor, and hardware). diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 0655a3d..d3bbf1f 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + File.expand_path(File.join(File.dirname(__FILE__),'colour_prompt')) class UnityTestRunnerGenerator @@ -187,10 +193,10 @@ class UnityTestRunnerGenerator output.puts(" setUp();") output.puts(" test();") output.puts(" CMock_Verify();") unless (used_mocks.empty?) - output.puts(" } Catch(e) { TEST_FAIL(\"Unhandled Exception!\"); }") if @options[:cexception] + output.puts(" } Catch(e) { TEST_ASSERT_EQUAL_HEX32_MESSAGE(CEXCEPTION_NONE, e, \"Unhandled Exception!\"); }") if @options[:cexception] output.puts(" }") output.puts(" CMock_Destroy();") unless (used_mocks.empty?) - output.puts(" if (TEST_PROTECT())") + output.puts(" if (TEST_PROTECT() && !TEST_IS_IGNORED)") output.puts(" {") output.puts(" tearDown();") output.puts(" }") diff --git a/auto/test_file_filter.rb b/auto/test_file_filter.rb index 60abc20..3dbc26a 100644 --- a/auto/test_file_filter.rb +++ b/auto/test_file_filter.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + require'yaml' module RakefileHelpers diff --git a/auto/unity_test_summary.rb b/auto/unity_test_summary.rb index 929018e..f8035f4 100644 --- a/auto/unity_test_summary.rb +++ b/auto/unity_test_summary.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + #!/usr/bin/ruby # # unity_test_summary.rb diff --git a/docs/Unity Summary.odt b/docs/Unity Summary.odt index 7d99251..18aa98a 100644 Binary files a/docs/Unity Summary.odt and b/docs/Unity Summary.odt differ diff --git a/docs/Unity Summary.pdf b/docs/Unity Summary.pdf index fcf53d6..9cf758d 100644 Binary files a/docs/Unity Summary.pdf and b/docs/Unity Summary.pdf differ diff --git a/docs/Unity Summary.txt b/docs/Unity Summary.txt index a77c773..bf9c803 100644 --- a/docs/Unity Summary.txt +++ b/docs/Unity Summary.txt @@ -2,6 +2,8 @@ Unity Test API ============== +[Copyright (c) 2007 - Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams] + ------------- Running Tests ------------- diff --git a/examples/helper/UnityHelper.c b/examples/helper/UnityHelper.c index db4d310..b06af3b 100644 --- a/examples/helper/UnityHelper.c +++ b/examples/helper/UnityHelper.c @@ -3,19 +3,6 @@ #include #include -static char message[1024]; - -void AssertFloatArrayWithin(const float tolerance, const float* expected, const float* actual, const unsigned int length, const unsigned short line) -{ - unsigned int i; - - for (i = 0; i < length; i++) - { - sprintf(message, "Array mismatch at index %u, Expected %f, Actual %f, Tolerance %f, Delta %f", i, expected[i], actual[i], tolerance, (expected[i]-actual[i])); - UNITY_TEST_ASSERT_WITHIN_FLOAT(tolerance, expected[i], actual[i], line, message); - } -} - void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual, const unsigned short line) { UNITY_TEST_ASSERT_EQUAL_INT(expected.x, actual.x, line, "Example Struct Failed For Field x"); diff --git a/examples/helper/UnityHelper.h b/examples/helper/UnityHelper.h index 1fb3a0e..538caae 100644 --- a/examples/helper/UnityHelper.h +++ b/examples/helper/UnityHelper.h @@ -3,13 +3,10 @@ #include "Types.h" -void AssertFloatArrayWithin(const float tolerance, const float* expected, const float* actual, const unsigned int length, const unsigned short line); void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual, const unsigned short line); -#define UNITY_TEST_ASSERT_FLOAT_ARRAY_WITHIN(tolerance, expected, actual, length, line, message) AssertFloatArrayWithin(tolerance, expected, actual, length, line); #define UNITY_TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual, line, message) AssertEqualExampleStruct(expected, actual, line); -#define TEST_ASSERT_FLOAT_ARRAY_WITHIN(tolerance, expected, actual, length) UNITY_TEST_ASSERT_FLOAT_ARRAY_WITHIN(tolerance, expected, actual, __LINE__, NULL); #define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual) UNITY_TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual, __LINE__, NULL); #endif // _TESTHELPER_H diff --git a/examples/iar_v4.yml b/examples/iar_v4.yml index b6a222b..2b28bd0 100644 --- a/examples/iar_v4.yml +++ b/examples/iar_v4.yml @@ -31,8 +31,8 @@ compiler: items: - [*tools_root, 'arm\inc\'] - 'src\' + - '../src/' - *unit_tests_path - - 'vendor\unity\src\' defines: prefix: '-D' items: diff --git a/examples/iar_v5.yml b/examples/iar_v5.yml index 8173acd..b7cd3ff 100644 --- a/examples/iar_v5.yml +++ b/examples/iar_v5.yml @@ -30,9 +30,8 @@ compiler: items: - [*tools_root, 'arm\inc\'] - 'src\' + - '../src/' - *unit_tests_path - - 'vendor\unity\src\' - - 'iar\iar_v5\incIAR\' defines: prefix: '-D' items: diff --git a/examples/makefile b/examples/makefile new file mode 100644 index 0000000..3d4ac1f --- /dev/null +++ b/examples/makefile @@ -0,0 +1,40 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +C_COMPILER=gcc +TARGET_BASE1=test1 +TARGET_BASE2=test2 +ifeq ($(OS),Windows_NT) + TARGET_EXTENSION=.exe +else + TARGET_EXTENSION=.out +endif +TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION) +TARGET2 = $(TARGET_BASE2)$(TARGET_EXTENSION) +SRC_FILES1=../src/unity.c src/ProductionCode.c test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c +SRC_FILES2=../src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/no_ruby/TestProductionCode2_Runner.c +INC_DIRS=-Isrc -I../src +SYMBOLS=-DTEST + +ifeq ($(OS),Windows_NT) + CLEANUP = del /F /Q build\* && del /F /Q $(TARGET1) && del /F /Q $(TARGET2) +else + CLEANUP = rm -f build/*.o ; rm -f $(TARGET1) ; rm -f $(TARGET2) +endif + +all: clean default + +default: +# ruby auto/generate_test_runner.rb test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c +# ruby auto/generate_test_runner.rb test/TestProductionCode2.c test/no_ruby/TestProductionCode2_Runner.c + $(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1) + $(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES2) -o $(TARGET2) + $(TARGET1) + $(TARGET2) + +clean: + $(CLEANUP) + diff --git a/examples/readme.txt b/examples/readme.txt new file mode 100644 index 0000000..f870057 --- /dev/null +++ b/examples/readme.txt @@ -0,0 +1,18 @@ +Example Project + +This example project gives an example of some passing, ignored, and failing tests. +It's simple and meant for you to look over and get an idea for what all of this stuff does. + +You can build and test using the makefile if you have gcc installed (you may need to tweak +the locations of some tools in the makefile). Otherwise, the rake version will let you +test with gcc or a couple versions of IAR. You can tweak the yaml files to get those versions +running. + +Ruby is required if you're using the rake version (obviously). This version shows off most of +Unity's advanced features (automatically creating test runners, fancy summaries, etc.) + +The makefile version doesn't require anything outside of your normal build tools, but won't do the +extras for you. So that you can test right away, we've written the test runners for you and +put them in the test\no_ruby subdirectory. If you make changes to the tests or source, you might +need to update these (like when you add or remove tests). Do that for a while and you'll learn +why you really want to start using the Ruby tools. \ No newline at end of file diff --git a/examples/src/ProductionCode2.c b/examples/src/ProductionCode2.c index 543f4e4..0cca0d5 100644 --- a/examples/src/ProductionCode2.c +++ b/examples/src/ProductionCode2.c @@ -5,4 +5,5 @@ char* ThisFunctionHasNotBeenTested(int Poor, char* 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/test/no_ruby/TestProductionCode2_Runner.c b/examples/test/no_ruby/TestProductionCode2_Runner.c new file mode 100644 index 0000000..9862a1d --- /dev/null +++ b/examples/test/no_ruby/TestProductionCode2_Runner.c @@ -0,0 +1,46 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ +#include "unity.h" +#include +#include + +char MessageBuffer[50]; + +extern void setUp(void); +extern void tearDown(void); + +extern void test_IgnoredTest(void); +extern void test_AnotherIgnoredTest(void); +extern void test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented(void); + +static void runTest(UnityTestFunction test) +{ + if (TEST_PROTECT()) + { + setUp(); + test(); + } + if (TEST_PROTECT() && !TEST_IS_IGNORED) + { + tearDown(); + } +} +void resetTest() +{ + tearDown(); + setUp(); +} + + +int main(void) +{ + Unity.TestFile = "test/TestProductionCode2.c"; + UnityBegin(); + + // RUN_TEST calls runTest + RUN_TEST(test_IgnoredTest, 13); + RUN_TEST(test_AnotherIgnoredTest, 18); + RUN_TEST(test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented, 23); + + UnityEnd(); + return 0; +} diff --git a/examples/test/no_ruby/TestProductionCode_Runner.c b/examples/test/no_ruby/TestProductionCode_Runner.c new file mode 100644 index 0000000..e32d35f --- /dev/null +++ b/examples/test/no_ruby/TestProductionCode_Runner.c @@ -0,0 +1,50 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ +#include "unity.h" +#include +#include + +char MessageBuffer[50]; + +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); + +static void runTest(UnityTestFunction test) +{ + if (TEST_PROTECT()) + { + setUp(); + test(); + } + if (TEST_PROTECT() && !TEST_IS_IGNORED) + { + tearDown(); + } +} +void resetTest() +{ + tearDown(); + setUp(); +} + + +int main(void) +{ + Unity.TestFile = "test/TestProductionCode.c"; + UnityBegin(); + + // RUN_TEST calls runTest + 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); + + UnityEnd(); + return 0; +} diff --git a/makefile b/makefile index 47ae2d5..20d4c36 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + C_COMPILER=gcc TARGET_BASE = testunity ifeq ($(OS),Windows_NT) @@ -7,7 +13,7 @@ else endif TARGET = $(TARGET_BASE)$(TARGET_EXTENSION) OUT_FILE=-o $(TARGET) -SRC_FILES=src/unity.c test/testunity.c test/testunity_Runner.c +SRC_FILES=src/unity.c test/testunity.c build/testunity_Runner.c INC_DIRS=-Isrc SYMBOLS=-DTEST @@ -20,7 +26,8 @@ endif all: clean default default: - $(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES) $(OUT_FILE)$(OUT_EXTENSION) + ruby auto/generate_test_runner.rb test/testunity.c build/testunity_Runner.c + $(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES) $(OUT_FILE) $(TARGET) clean: diff --git a/rakefile.rb b/rakefile.rb index cf8f610..5b96525 100644 --- a/rakefile.rb +++ b/rakefile.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + HERE = File.expand_path(File.dirname(__FILE__)) + '/' #require HERE + 'config/environment' diff --git a/rakefile_helper.rb b/rakefile_helper.rb index 1147de5..9b856b6 100644 --- a/rakefile_helper.rb +++ b/rakefile_helper.rb @@ -1,3 +1,9 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + require 'yaml' require 'fileutils' require 'auto/unity_test_summary' diff --git a/src/unity.c b/src/unity.c index a8c8545..c2b182b 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1,3 +1,9 @@ +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ + #include "unity.h" #include #include @@ -459,6 +465,7 @@ void UnityAssertEqualString(const char* expected, if (expected[i] != actual[i]) { Unity.CurrentTestFailed = 1; + break; } } } diff --git a/src/unity.h b/src/unity.h index db00771..5fb411b 100644 --- a/src/unity.h +++ b/src/unity.h @@ -1,3 +1,9 @@ +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ + #ifndef UNITY_FRAMEWORK_H #define UNITY_FRAMEWORK_H @@ -41,6 +47,7 @@ UnityConcludeTest(); #define TEST_LINE_NUM (Unity.CurrentTestLineNumber) +#define TEST_IS_IGNORED (Unity.CurrentTestIgnored) //------------------------------------------------------- // Basic Fail and Ignore diff --git a/src/unity_internals.h b/src/unity_internals.h index 3d533a4..305b244 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -1,3 +1,9 @@ +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ + #ifndef UNITY_INTERNALS_H #define UNITY_INTERNALS_H @@ -70,6 +76,10 @@ typedef UNITY_FLOAT_TYPE _UF; #define UNITY_LINE_TYPE unsigned short #endif +#ifndef UNITY_COUNTER_TYPE +#define UNITY_COUNTER_TYPE unsigned short +#endif + //------------------------------------------------------- // Internal Structs Needed //------------------------------------------------------- @@ -90,11 +100,11 @@ struct _Unity const char* TestFile; const char* CurrentTestName; _UU32 CurrentTestLineNumber; - unsigned char NumberOfTests; - unsigned char TestFailures; - unsigned char TestIgnores; - unsigned char CurrentTestFailed; - unsigned char CurrentTestIgnored; + UNITY_COUNTER_TYPE NumberOfTests; + UNITY_COUNTER_TYPE TestFailures; + UNITY_COUNTER_TYPE TestIgnores; + UNITY_COUNTER_TYPE CurrentTestFailed; + UNITY_COUNTER_TYPE CurrentTestIgnored; jmp_buf AbortFrame; }; diff --git a/test/testunity.c b/test/testunity.c index d340346..bc8d206 100644 --- a/test/testunity.c +++ b/test/testunity.c @@ -1,4 +1,8 @@ -#define UNITY_ENABLE_EXTERNAL_ASSERTIONS +/* ========================================== + Unity Project - A Test Framework for C + Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams + [Released under MIT License. Please refer to license.txt for details] +========================================== */ #include #include "unity.h" @@ -17,12 +21,17 @@ TEST_ASSERT_MESSAGE((1u == failed), "<---- [ This Test Should Have Failed But Did Not ]"); \ EXPECT_ABORT_END +int SetToOneToFailInTearDown; + void setUp(void) { + SetToOneToFailInTearDown = 0; } void tearDown(void) { + if (SetToOneToFailInTearDown == 1) + TEST_FAIL("Failed in tearDown"); } void testTrue(void) @@ -429,6 +438,25 @@ void testEqualHex8s(void) TEST_ASSERT_EQUAL_HEX8(*p0, 0x22); } +void testEqualHex8sNegatives(void) +{ + _UU8 v0, v1; + _UU8 *p0, *p1; + + v0 = 0xDD; + v1 = 0xDD; + p0 = &v0; + p1 = &v1; + + TEST_ASSERT_EQUAL_HEX8(0xDD, 0xDD); + TEST_ASSERT_EQUAL_HEX8(v0, v1); + TEST_ASSERT_EQUAL_HEX8(0xDD, v1); + TEST_ASSERT_EQUAL_HEX8(v0, 0xDD); + TEST_ASSERT_EQUAL_HEX8(*p0, v1); + TEST_ASSERT_EQUAL_HEX8(*p0, *p1); + TEST_ASSERT_EQUAL_HEX8(*p0, 0xDD); +} + void testEqualHex16s(void) { _UU16 v0, v1; @@ -689,7 +717,7 @@ void testUIntsNotWithinDelta(void) int failed; EXPECT_ABORT_BEGIN - TEST_ASSERT_UINT_WITHIN(1, 2147483647, 2147483649); + TEST_ASSERT_UINT_WITHIN(1, 2147483647u, 2147483649u); EXPECT_ABORT_END failed = Unity.CurrentTestFailed; @@ -738,7 +766,7 @@ void testHEX32sNotWithinDelta(void) int failed; EXPECT_ABORT_BEGIN - TEST_ASSERT_HEX32_WITHIN(1, 2147483647, 2147483649); + TEST_ASSERT_HEX32_WITHIN(1, 2147483647u, 2147483649u); EXPECT_ABORT_END failed = Unity.CurrentTestFailed; @@ -1477,3 +1505,8 @@ void testProtection(void) TEST_ASSERT_EQUAL(3, mask); } +void testIgnoredAndThenFailInTearDown(void) +{ + SetToOneToFailInTearDown = 1; + TEST_IGNORE(); +}