From 843370a7c28e89b1d0c42206dac2c35217165d46 Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Wed, 17 Jul 2013 22:14:47 +1000 Subject: [PATCH 1/7] Updated example makefile * Make would fail due to missing build directory. Now it doesn't try to delete build/ --- examples/makefile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/makefile b/examples/makefile index 98258e4..2e9f746 100644 --- a/examples/makefile +++ b/examples/makefile @@ -2,8 +2,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] -# ========================================== +# ========================================== +UNITY_ROOT=.. C_COMPILER=gcc TARGET_BASE1=test1 TARGET_BASE2=test2 @@ -14,13 +15,13 @@ else 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 +SRC_FILES1=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c +SRC_FILES2=$(UNITY_ROOT)/src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/no_ruby/TestProductionCode2_Runner.c +INC_DIRS=-Isrc -I$(UNITY_ROOT)/src SYMBOLS=-DTEST ifeq ($(OS),Windows_NT) - CLEANUP = del /F /Q build\* && del /F /Q $(TARGET1) && del /F /Q $(TARGET2) + CLEANUP = del /F /Q $(TARGET1) && del /F /Q $(TARGET2) else CLEANUP = rm -f build/*.o ; rm -f $(TARGET1) ; rm -f $(TARGET2) endif @@ -37,4 +38,4 @@ default: clean: $(CLEANUP) - + From 5932565bae8cf8c884ec8fd49a21a067c3a981cb Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Wed, 17 Jul 2013 22:42:43 +1000 Subject: [PATCH 2/7] Updated example * Moved existing example to its own directory, with the intention of adding more examples * Existing example uses its own .yml file instead of one of the files in targets/. --- examples/{ => example_x}/helper/UnityHelper.c | 20 +- examples/{ => example_x}/helper/UnityHelper.h | 24 +- examples/{ => example_x}/makefile | 82 +-- examples/{ => example_x}/rakefile.rb | 87 +-- examples/{ => example_x}/rakefile_helper.rb | 512 +++++++++--------- examples/{ => example_x}/readme.txt | 34 +- examples/{ => example_x}/src/ProductionCode.c | 48 +- examples/{ => example_x}/src/ProductionCode.h | 6 +- .../{ => example_x}/src/ProductionCode2.c | 18 +- .../{ => example_x}/src/ProductionCode2.h | 4 +- examples/example_x/target_gcc_32.yml | 46 ++ .../{ => example_x}/test/TestProductionCode.c | 124 ++--- .../test/TestProductionCode2.c | 62 +-- .../test/no_ruby/TestProductionCode2_Runner.c | 92 ++-- .../test/no_ruby/TestProductionCode_Runner.c | 100 ++-- 15 files changed, 653 insertions(+), 606 deletions(-) rename examples/{ => example_x}/helper/UnityHelper.c (97%) rename examples/{ => example_x}/helper/UnityHelper.h (97%) rename examples/{ => example_x}/makefile (96%) rename examples/{ => example_x}/rakefile.rb (88%) rename examples/{ => example_x}/rakefile_helper.rb (92%) rename examples/{ => example_x}/readme.txt (98%) rename examples/{ => example_x}/src/ProductionCode.c (97%) rename examples/{ => example_x}/src/ProductionCode.h (96%) rename examples/{ => example_x}/src/ProductionCode2.c (97%) rename examples/{ => example_x}/src/ProductionCode2.h (97%) create mode 100644 examples/example_x/target_gcc_32.yml rename examples/{ => example_x}/test/TestProductionCode.c (97%) rename examples/{ => example_x}/test/TestProductionCode2.c (94%) rename examples/{ => example_x}/test/no_ruby/TestProductionCode2_Runner.c (94%) rename examples/{ => example_x}/test/no_ruby/TestProductionCode_Runner.c (97%) diff --git a/examples/helper/UnityHelper.c b/examples/example_x/helper/UnityHelper.c similarity index 97% rename from examples/helper/UnityHelper.c rename to examples/example_x/helper/UnityHelper.c index b06af3b..9cf42c6 100644 --- a/examples/helper/UnityHelper.c +++ b/examples/example_x/helper/UnityHelper.c @@ -1,10 +1,10 @@ -#include "unity.h" -#include "UnityHelper.h" -#include -#include - -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"); - UNITY_TEST_ASSERT_EQUAL_INT(expected.y, actual.y, line, "Example Struct Failed For Field y"); -} +#include "unity.h" +#include "UnityHelper.h" +#include +#include + +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"); + UNITY_TEST_ASSERT_EQUAL_INT(expected.y, actual.y, line, "Example Struct Failed For Field y"); +} diff --git a/examples/helper/UnityHelper.h b/examples/example_x/helper/UnityHelper.h similarity index 97% rename from examples/helper/UnityHelper.h rename to examples/example_x/helper/UnityHelper.h index 538caae..1516111 100644 --- a/examples/helper/UnityHelper.h +++ b/examples/example_x/helper/UnityHelper.h @@ -1,12 +1,12 @@ -#ifndef _TESTHELPER_H -#define _TESTHELPER_H - -#include "Types.h" - -void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual, const unsigned short line); - -#define UNITY_TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual, line, message) AssertEqualExampleStruct(expected, actual, line); - -#define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual) UNITY_TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual, __LINE__, NULL); - -#endif // _TESTHELPER_H +#ifndef _TESTHELPER_H +#define _TESTHELPER_H + +#include "Types.h" + +void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual, const unsigned short line); + +#define UNITY_TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual, line, message) AssertEqualExampleStruct(expected, actual, line); + +#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/makefile b/examples/example_x/makefile similarity index 96% rename from examples/makefile rename to examples/example_x/makefile index 2e9f746..05e7e69 100644 --- a/examples/makefile +++ b/examples/example_x/makefile @@ -1,41 +1,41 @@ -# ========================================== -# 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] -# ========================================== - -UNITY_ROOT=.. -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=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c -SRC_FILES2=$(UNITY_ROOT)/src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/no_ruby/TestProductionCode2_Runner.c -INC_DIRS=-Isrc -I$(UNITY_ROOT)/src -SYMBOLS=-DTEST - -ifeq ($(OS),Windows_NT) - CLEANUP = 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) - +# ========================================== +# 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] +# ========================================== + +UNITY_ROOT=../.. +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=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c +SRC_FILES2=$(UNITY_ROOT)/src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/no_ruby/TestProductionCode2_Runner.c +INC_DIRS=-Isrc -I$(UNITY_ROOT)/src +SYMBOLS=-DTEST + +ifeq ($(OS),Windows_NT) + CLEANUP = 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/rakefile.rb b/examples/example_x/rakefile.rb similarity index 88% rename from examples/rakefile.rb rename to examples/example_x/rakefile.rb index 9eade7a..2cfadc4 100644 --- a/examples/rakefile.rb +++ b/examples/example_x/rakefile.rb @@ -1,43 +1,44 @@ -HERE = File.expand_path(File.dirname(__FILE__)) + '/' - -require 'rake' -require 'rake/clean' -require 'rake/testtask' -require HERE+'rakefile_helper' - -TEMP_DIRS = [ - File.join(HERE, 'build') -] - -TEMP_DIRS.each do |dir| - directory(dir) - CLOBBER.include(dir) -end - -task :prepare_for_tests => TEMP_DIRS - -include RakefileHelpers - -# Load default configuration, for now -DEFAULT_CONFIG_FILE = 'gcc_32.yml' -configure_toolchain(DEFAULT_CONFIG_FILE) - -task :unit => [:prepare_for_tests] do - run_tests get_unit_test_files -end - -desc "Generate test summary" -task :summary do - report_summary -end - -desc "Build and test Unity" -task :all => [:clean, :unit, :summary] -task :default => [:clobber, :all] -task :ci => [:default] -task :cruise => [:default] - -desc "Load configuration" -task :config, :config_file do |t, args| - configure_toolchain(args[:config_file]) -end +HERE = File.expand_path(File.dirname(__FILE__)) + '/' +UNITY_ROOT = File.expand_path(File.dirname(__FILE__)) + '/../..' + +require 'rake' +require 'rake/clean' +require 'rake/testtask' +require HERE+'rakefile_helper' + +TEMP_DIRS = [ + File.join(HERE, 'build') +] + +TEMP_DIRS.each do |dir| + directory(dir) + CLOBBER.include(dir) +end + +task :prepare_for_tests => TEMP_DIRS + +include RakefileHelpers + +# Load default configuration, for now +DEFAULT_CONFIG_FILE = 'target_gcc_32.yml' +configure_toolchain(DEFAULT_CONFIG_FILE) + +task :unit => [:prepare_for_tests] do + run_tests get_unit_test_files +end + +desc "Generate test summary" +task :summary do + report_summary +end + +desc "Build and test Unity" +task :all => [:clean, :unit, :summary] +task :default => [:clobber, :all] +task :ci => [:default] +task :cruise => [:default] + +desc "Load configuration" +task :config, :config_file do |t, args| + configure_toolchain(args[:config_file]) +end diff --git a/examples/rakefile_helper.rb b/examples/example_x/rakefile_helper.rb similarity index 92% rename from examples/rakefile_helper.rb rename to examples/example_x/rakefile_helper.rb index f78803f..6702145 100644 --- a/examples/rakefile_helper.rb +++ b/examples/example_x/rakefile_helper.rb @@ -1,256 +1,256 @@ -require 'yaml' -require 'fileutils' -require HERE+'../auto/unity_test_summary' -require HERE+'../auto/generate_test_runner' -require HERE+'../auto/colour_reporter' - -module RakefileHelpers - - C_EXTENSION = '.c' - - def load_configuration(config_file) - $cfg_file = "../targets/#{config_file}" - $cfg = YAML.load(File.read($cfg_file)) - end - - def configure_clean - CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil? - end - - def configure_toolchain(config_file=DEFAULT_CONFIG_FILE) - config_file += '.yml' unless config_file =~ /\.yml$/ - load_configuration('../targets/'+config_file) - configure_clean - end - - def get_unit_test_files - path = $cfg['compiler']['unit_tests_path'] + 'Test*' + C_EXTENSION - path.gsub!(/\\/, '/') - FileList.new(path) - end - - def get_local_include_dirs - include_dirs = $cfg['compiler']['includes']['items'].dup - include_dirs.delete_if {|dir| dir.is_a?(Array)} - return include_dirs - end - - def extract_headers(filename) - includes = [] - lines = File.readlines(filename) - lines.each do |line| - m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/) - if not m.nil? - includes << m[1] - end - end - return includes - end - - def find_source_file(header, paths) - paths.each do |dir| - src_file = dir + header.ext(C_EXTENSION) - if (File.exists?(src_file)) - return src_file - end - end - return nil - end - - def tackit(strings) - if strings.is_a?(Array) - result = "\"#{strings.join}\"" - else - result = strings - end - return result - end - - def squash(prefix, items) - result = '' - items.each { |item| result += " #{prefix}#{tackit(item)}" } - return result - end - - def build_compiler_fields - command = tackit($cfg['compiler']['path']) - if $cfg['compiler']['defines']['items'].nil? - defines = '' - else - defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items']) - end - options = squash('', $cfg['compiler']['options']) - includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items']) - includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) - return {:command => command, :defines => defines, :options => options, :includes => includes} - end - - def compile(file, defines=[]) - compiler = build_compiler_fields - cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " + - "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" - obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}" - execute(cmd_str + obj_file) - return obj_file - end - - def build_linker_fields - command = tackit($cfg['linker']['path']) - if $cfg['linker']['options'].nil? - options = '' - else - options = squash('', $cfg['linker']['options']) - end - if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?) - includes = '' - else - includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items']) - end - includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) - return {:command => command, :options => options, :includes => includes} - end - - def link_it(exe_name, obj_list) - linker = build_linker_fields - cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " + - (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join + - $cfg['linker']['bin_files']['prefix'] + ' ' + - $cfg['linker']['bin_files']['destination'] + - exe_name + $cfg['linker']['bin_files']['extension'] - execute(cmd_str) - end - - def build_simulator_fields - return nil if $cfg['simulator'].nil? - if $cfg['simulator']['path'].nil? - command = '' - else - command = (tackit($cfg['simulator']['path']) + ' ') - end - if $cfg['simulator']['pre_support'].nil? - pre_support = '' - else - pre_support = squash('', $cfg['simulator']['pre_support']) - end - if $cfg['simulator']['post_support'].nil? - post_support = '' - else - post_support = squash('', $cfg['simulator']['post_support']) - end - return {:command => command, :pre_support => pre_support, :post_support => post_support} - end - - def execute(command_string, verbose=true, raise_on_fail=true) - report command_string - output = `#{command_string}`.chomp - report(output) if (verbose && !output.nil? && (output.length > 0)) - if (($?.exitstatus != 0) and (raise_on_fail)) - raise "Command failed. (Returned #{$?.exitstatus})" - end - return output - end - - def report_summary - summary = UnityTestSummary.new - summary.set_root_path(HERE) - results_glob = "#{$cfg['compiler']['build_path']}*.test*" - results_glob.gsub!(/\\/, '/') - results = Dir[results_glob] - summary.set_targets(results) - summary.run - fail_out "FAIL: There were failures" if (summary.failures > 0) - end - - def run_tests(test_files) - - report 'Running system tests...' - - # Tack on TEST define for compiling unit tests - load_configuration($cfg_file) - test_defines = ['TEST'] - $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? - $cfg['compiler']['defines']['items'] << 'TEST' - - include_dirs = get_local_include_dirs - - # Build and execute each unit test - test_files.each do |test| - obj_list = [] - - # Detect dependencies and build required required modules - extract_headers(test).each do |header| - # Compile corresponding source file if it exists - src_file = find_source_file(header, include_dirs) - if !src_file.nil? - obj_list << compile(src_file, test_defines) - end - end - - # Build the test runner (generate if configured to do so) - test_base = File.basename(test, C_EXTENSION) - runner_name = test_base + '_Runner.c' - if $cfg['compiler']['runner_path'].nil? - runner_path = $cfg['compiler']['build_path'] + runner_name - test_gen = UnityTestRunnerGenerator.new($cfg_file) - test_gen.run(test, runner_path) - else - runner_path = $cfg['compiler']['runner_path'] + runner_name - end - - obj_list << compile(runner_path, test_defines) - - # Build the test module - obj_list << compile(test, test_defines) - - # Link the test executable - link_it(test_base, obj_list) - - # Execute unit test and generate results file - simulator = build_simulator_fields - executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension'] - if simulator.nil? - cmd_str = executable - else - cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}" - end - output = execute(cmd_str, true, false) - test_results = $cfg['compiler']['build_path'] + test_base - if output.match(/OK$/m).nil? - test_results += '.testfail' - else - test_results += '.testpass' - end - File.open(test_results, 'w') { |f| f.print output } - end - end - - def build_application(main) - - report "Building application..." - - obj_list = [] - load_configuration($cfg_file) - main_path = $cfg['compiler']['source_path'] + main + C_EXTENSION - - # Detect dependencies and build required required modules - include_dirs = get_local_include_dirs - extract_headers(main_path).each do |header| - src_file = find_source_file(header, include_dirs) - if !src_file.nil? - obj_list << compile(src_file) - end - end - - # Build the main source file - main_base = File.basename(main_path, C_EXTENSION) - obj_list << compile(main_path) - - # Create the executable - link_it(main_base, obj_list) - end - - def fail_out(msg) - puts msg - exit(-1) - end -end +require 'yaml' +require 'fileutils' +require UNITY_ROOT+'/auto/unity_test_summary' +require UNITY_ROOT+'/auto/generate_test_runner' +require UNITY_ROOT+'/auto/colour_reporter' + +module RakefileHelpers + + C_EXTENSION = '.c' + + def load_configuration(config_file) + $cfg_file = config_file + $cfg = YAML.load(File.read($cfg_file)) + end + + def configure_clean + CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil? + end + + def configure_toolchain(config_file=DEFAULT_CONFIG_FILE) + config_file += '.yml' unless config_file =~ /\.yml$/ + load_configuration(config_file) + configure_clean + end + + def get_unit_test_files + path = $cfg['compiler']['unit_tests_path'] + 'Test*' + C_EXTENSION + path.gsub!(/\\/, '/') + FileList.new(path) + end + + def get_local_include_dirs + include_dirs = $cfg['compiler']['includes']['items'].dup + include_dirs.delete_if {|dir| dir.is_a?(Array)} + return include_dirs + end + + def extract_headers(filename) + includes = [] + lines = File.readlines(filename) + lines.each do |line| + m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/) + if not m.nil? + includes << m[1] + end + end + return includes + end + + def find_source_file(header, paths) + paths.each do |dir| + src_file = dir + header.ext(C_EXTENSION) + if (File.exists?(src_file)) + return src_file + end + end + return nil + end + + def tackit(strings) + if strings.is_a?(Array) + result = "\"#{strings.join}\"" + else + result = strings + end + return result + end + + def squash(prefix, items) + result = '' + items.each { |item| result += " #{prefix}#{tackit(item)}" } + return result + end + + def build_compiler_fields + command = tackit($cfg['compiler']['path']) + if $cfg['compiler']['defines']['items'].nil? + defines = '' + else + defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items']) + end + options = squash('', $cfg['compiler']['options']) + includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items']) + includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) + return {:command => command, :defines => defines, :options => options, :includes => includes} + end + + def compile(file, defines=[]) + compiler = build_compiler_fields + cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " + + "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" + obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}" + execute(cmd_str + obj_file) + return obj_file + end + + def build_linker_fields + command = tackit($cfg['linker']['path']) + if $cfg['linker']['options'].nil? + options = '' + else + options = squash('', $cfg['linker']['options']) + end + if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?) + includes = '' + else + includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items']) + end + includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) + return {:command => command, :options => options, :includes => includes} + end + + def link_it(exe_name, obj_list) + linker = build_linker_fields + cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " + + (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join + + $cfg['linker']['bin_files']['prefix'] + ' ' + + $cfg['linker']['bin_files']['destination'] + + exe_name + $cfg['linker']['bin_files']['extension'] + execute(cmd_str) + end + + def build_simulator_fields + return nil if $cfg['simulator'].nil? + if $cfg['simulator']['path'].nil? + command = '' + else + command = (tackit($cfg['simulator']['path']) + ' ') + end + if $cfg['simulator']['pre_support'].nil? + pre_support = '' + else + pre_support = squash('', $cfg['simulator']['pre_support']) + end + if $cfg['simulator']['post_support'].nil? + post_support = '' + else + post_support = squash('', $cfg['simulator']['post_support']) + end + return {:command => command, :pre_support => pre_support, :post_support => post_support} + end + + def execute(command_string, verbose=true, raise_on_fail=true) + report command_string + output = `#{command_string}`.chomp + report(output) if (verbose && !output.nil? && (output.length > 0)) + if (($?.exitstatus != 0) and (raise_on_fail)) + raise "Command failed. (Returned #{$?.exitstatus})" + end + return output + end + + def report_summary + summary = UnityTestSummary.new + summary.set_root_path(HERE) + results_glob = "#{$cfg['compiler']['build_path']}*.test*" + results_glob.gsub!(/\\/, '/') + results = Dir[results_glob] + summary.set_targets(results) + summary.run + fail_out "FAIL: There were failures" if (summary.failures > 0) + end + + def run_tests(test_files) + + report 'Running system tests...' + + # Tack on TEST define for compiling unit tests + load_configuration($cfg_file) + test_defines = ['TEST'] + $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? + $cfg['compiler']['defines']['items'] << 'TEST' + + include_dirs = get_local_include_dirs + + # Build and execute each unit test + test_files.each do |test| + obj_list = [] + + # Detect dependencies and build required required modules + extract_headers(test).each do |header| + # Compile corresponding source file if it exists + src_file = find_source_file(header, include_dirs) + if !src_file.nil? + obj_list << compile(src_file, test_defines) + end + end + + # Build the test runner (generate if configured to do so) + test_base = File.basename(test, C_EXTENSION) + runner_name = test_base + '_Runner.c' + if $cfg['compiler']['runner_path'].nil? + runner_path = $cfg['compiler']['build_path'] + runner_name + test_gen = UnityTestRunnerGenerator.new($cfg_file) + test_gen.run(test, runner_path) + else + runner_path = $cfg['compiler']['runner_path'] + runner_name + end + + obj_list << compile(runner_path, test_defines) + + # Build the test module + obj_list << compile(test, test_defines) + + # Link the test executable + link_it(test_base, obj_list) + + # Execute unit test and generate results file + simulator = build_simulator_fields + executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension'] + if simulator.nil? + cmd_str = executable + else + cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}" + end + output = execute(cmd_str, true, false) + test_results = $cfg['compiler']['build_path'] + test_base + if output.match(/OK$/m).nil? + test_results += '.testfail' + else + test_results += '.testpass' + end + File.open(test_results, 'w') { |f| f.print output } + end + end + + def build_application(main) + + report "Building application..." + + obj_list = [] + load_configuration($cfg_file) + main_path = $cfg['compiler']['source_path'] + main + C_EXTENSION + + # Detect dependencies and build required required modules + include_dirs = get_local_include_dirs + extract_headers(main_path).each do |header| + src_file = find_source_file(header, include_dirs) + if !src_file.nil? + obj_list << compile(src_file) + end + end + + # Build the main source file + main_base = File.basename(main_path, C_EXTENSION) + obj_list << compile(main_path) + + # Create the executable + link_it(main_base, obj_list) + end + + def fail_out(msg) + puts msg + exit(-1) + end +end diff --git a/examples/readme.txt b/examples/example_x/readme.txt similarity index 98% rename from examples/readme.txt rename to examples/example_x/readme.txt index f870057..6c7780e 100644 --- a/examples/readme.txt +++ b/examples/example_x/readme.txt @@ -1,18 +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 +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/ProductionCode.c b/examples/example_x/src/ProductionCode.c similarity index 97% rename from examples/src/ProductionCode.c rename to examples/example_x/src/ProductionCode.c index 1ec9433..500b44b 100644 --- a/examples/src/ProductionCode.c +++ b/examples/example_x/src/ProductionCode.c @@ -1,24 +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; -} + +#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/src/ProductionCode.h b/examples/example_x/src/ProductionCode.h similarity index 96% rename from examples/src/ProductionCode.h rename to examples/example_x/src/ProductionCode.h index ca55d80..250ca0d 100644 --- a/examples/src/ProductionCode.h +++ b/examples/example_x/src/ProductionCode.h @@ -1,3 +1,3 @@ - -int FindFunction_WhichIsBroken(int NumberToFind); -int FunctionWhichReturnsLocalVariable(void); + +int FindFunction_WhichIsBroken(int NumberToFind); +int FunctionWhichReturnsLocalVariable(void); diff --git a/examples/src/ProductionCode2.c b/examples/example_x/src/ProductionCode2.c similarity index 97% rename from examples/src/ProductionCode2.c rename to examples/example_x/src/ProductionCode2.c index 0cca0d5..a8c72e1 100644 --- a/examples/src/ProductionCode2.c +++ b/examples/example_x/src/ProductionCode2.c @@ -1,9 +1,9 @@ - -#include "ProductionCode2.h" - -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; -} + +#include "ProductionCode2.h" + +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/src/ProductionCode2.h b/examples/example_x/src/ProductionCode2.h similarity index 97% rename from examples/src/ProductionCode2.h rename to examples/example_x/src/ProductionCode2.h index 3047e68..34ae980 100644 --- a/examples/src/ProductionCode2.h +++ b/examples/example_x/src/ProductionCode2.h @@ -1,2 +1,2 @@ - -char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction); + +char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction); diff --git a/examples/example_x/target_gcc_32.yml b/examples/example_x/target_gcc_32.yml new file mode 100644 index 0000000..f155508 --- /dev/null +++ b/examples/example_x/target_gcc_32.yml @@ -0,0 +1,46 @@ +# Copied from ~Unity/targets/gcc_32.yml +unity_root: &unity_root '../..' +compiler: + path: gcc + source_path: 'src/' + unit_tests_path: &unit_tests_path 'test/' + build_path: &build_path 'build/' + options: + - '-c' + - '-m32' + - '-Wall' + - '-Wno-address' + - '-std=c99' + - '-pedantic' + includes: + prefix: '-I' + items: + - 'src/' + - '../../src/' + - *unit_tests_path + defines: + prefix: '-D' + items: + - UNITY_INCLUDE_DOUBLE + - UNITY_SUPPORT_TEST_CASES + object_files: + prefix: '-o' + extension: '.o' + destination: *build_path +linker: + path: gcc + options: + - -lm + - '-m32' + includes: + prefix: '-I' + object_files: + path: *build_path + extension: '.o' + bin_files: + prefix: '-o' + extension: '.exe' + destination: *build_path +colour: true +:unity: + :plugins: [] diff --git a/examples/test/TestProductionCode.c b/examples/example_x/test/TestProductionCode.c similarity index 97% rename from examples/test/TestProductionCode.c rename to examples/example_x/test/TestProductionCode.c index 027d7cf..28a5581 100644 --- a/examples/test/TestProductionCode.c +++ b/examples/example_x/test/TestProductionCode.c @@ -1,62 +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(1)); - 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()); -} + +#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(1)); + 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/test/TestProductionCode2.c b/examples/example_x/test/TestProductionCode2.c similarity index 94% rename from examples/test/TestProductionCode2.c rename to examples/example_x/test/TestProductionCode2.c index 009a2d3..e2119cc 100644 --- a/examples/test/TestProductionCode2.c +++ b/examples/example_x/test/TestProductionCode2.c @@ -1,31 +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 -} + +#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/test/no_ruby/TestProductionCode2_Runner.c b/examples/example_x/test/no_ruby/TestProductionCode2_Runner.c similarity index 94% rename from examples/test/no_ruby/TestProductionCode2_Runner.c rename to examples/example_x/test/no_ruby/TestProductionCode2_Runner.c index 9862a1d..56515ae 100644 --- a/examples/test/no_ruby/TestProductionCode2_Runner.c +++ b/examples/example_x/test/no_ruby/TestProductionCode2_Runner.c @@ -1,46 +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; -} +/* 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/example_x/test/no_ruby/TestProductionCode_Runner.c similarity index 97% rename from examples/test/no_ruby/TestProductionCode_Runner.c rename to examples/example_x/test/no_ruby/TestProductionCode_Runner.c index e32d35f..64112f3 100644 --- a/examples/test/no_ruby/TestProductionCode_Runner.c +++ b/examples/example_x/test/no_ruby/TestProductionCode_Runner.c @@ -1,50 +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; -} +/* 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; +} From 1b9b197298ced7cd2a5594a8e82991835a1e25b0 Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Wed, 17 Jul 2013 22:48:50 +1000 Subject: [PATCH 3/7] Added another example * Simple makefile example, extracted from existing example --- examples/example_1/makefile | 41 ++++++++++++ examples/example_1/readme.txt | 5 ++ examples/example_1/src/ProductionCode.c | 24 +++++++ examples/example_1/src/ProductionCode.h | 3 + examples/example_1/src/ProductionCode2.c | 9 +++ examples/example_1/src/ProductionCode2.h | 2 + examples/example_1/test/TestProductionCode.c | 62 +++++++++++++++++++ examples/example_1/test/TestProductionCode2.c | 31 ++++++++++ .../test_runners/TestProductionCode2_Runner.c | 46 ++++++++++++++ .../test_runners/TestProductionCode_Runner.c | 50 +++++++++++++++ 10 files changed, 273 insertions(+) create mode 100644 examples/example_1/makefile create mode 100644 examples/example_1/readme.txt create mode 100644 examples/example_1/src/ProductionCode.c create mode 100644 examples/example_1/src/ProductionCode.h create mode 100644 examples/example_1/src/ProductionCode2.c create mode 100644 examples/example_1/src/ProductionCode2.h create mode 100644 examples/example_1/test/TestProductionCode.c create mode 100644 examples/example_1/test/TestProductionCode2.c create mode 100644 examples/example_1/test/test_runners/TestProductionCode2_Runner.c create mode 100644 examples/example_1/test/test_runners/TestProductionCode_Runner.c diff --git a/examples/example_1/makefile b/examples/example_1/makefile new file mode 100644 index 0000000..6c73383 --- /dev/null +++ b/examples/example_1/makefile @@ -0,0 +1,41 @@ +# ========================================== +# 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] +# ========================================== + +UNITY_ROOT=../.. +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=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c +SRC_FILES2=$(UNITY_ROOT)/src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/test_runners/TestProductionCode2_Runner.c +INC_DIRS=-Isrc -I$(UNITY_ROOT)/src +SYMBOLS=-DTEST + +ifeq ($(OS),Windows_NT) + CLEANUP = 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/test_runners/TestProductionCode_Runner.c +# ruby auto/generate_test_runner.rb test/TestProductionCode2.c test/test_runners/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/example_1/readme.txt b/examples/example_1/readme.txt new file mode 100644 index 0000000..dfed815 --- /dev/null +++ b/examples/example_1/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_1/src/ProductionCode.c b/examples/example_1/src/ProductionCode.c new file mode 100644 index 0000000..500b44b --- /dev/null +++ b/examples/example_1/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_1/src/ProductionCode.h b/examples/example_1/src/ProductionCode.h new file mode 100644 index 0000000..250ca0d --- /dev/null +++ b/examples/example_1/src/ProductionCode.h @@ -0,0 +1,3 @@ + +int FindFunction_WhichIsBroken(int NumberToFind); +int FunctionWhichReturnsLocalVariable(void); diff --git a/examples/example_1/src/ProductionCode2.c b/examples/example_1/src/ProductionCode2.c new file mode 100644 index 0000000..a8c72e1 --- /dev/null +++ b/examples/example_1/src/ProductionCode2.c @@ -0,0 +1,9 @@ + +#include "ProductionCode2.h" + +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/example_1/src/ProductionCode2.h b/examples/example_1/src/ProductionCode2.h new file mode 100644 index 0000000..34ae980 --- /dev/null +++ b/examples/example_1/src/ProductionCode2.h @@ -0,0 +1,2 @@ + +char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction); diff --git a/examples/example_1/test/TestProductionCode.c b/examples/example_1/test/TestProductionCode.c new file mode 100644 index 0000000..28a5581 --- /dev/null +++ b/examples/example_1/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(1)); + 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_1/test/TestProductionCode2.c b/examples/example_1/test/TestProductionCode2.c new file mode 100644 index 0000000..e2119cc --- /dev/null +++ b/examples/example_1/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_1/test/test_runners/TestProductionCode2_Runner.c b/examples/example_1/test/test_runners/TestProductionCode2_Runner.c new file mode 100644 index 0000000..56515ae --- /dev/null +++ b/examples/example_1/test/test_runners/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/example_1/test/test_runners/TestProductionCode_Runner.c b/examples/example_1/test/test_runners/TestProductionCode_Runner.c new file mode 100644 index 0000000..64112f3 --- /dev/null +++ b/examples/example_1/test/test_runners/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; +} From efc1df828ff106e49a26d9408c35bc05b8f99b50 Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Wed, 17 Jul 2013 23:23:09 +1000 Subject: [PATCH 4/7] Added example * Added example that uses unity test fixture --- examples/example_2/makefile | 36 +++++++++++ examples/example_2/readme.txt | 5 ++ examples/example_2/src/ProductionCode.c | 24 +++++++ examples/example_2/src/ProductionCode.h | 3 + examples/example_2/src/ProductionCode2.c | 9 +++ examples/example_2/src/ProductionCode2.h | 2 + examples/example_2/test/TestProductionCode.c | 64 +++++++++++++++++++ examples/example_2/test/TestProductionCode2.c | 33 ++++++++++ .../test_runners/TestProductionCode2_Runner.c | 9 +++ .../test_runners/TestProductionCode_Runner.c | 11 ++++ .../example_2/test/test_runners/all_tests.c | 12 ++++ 11 files changed, 208 insertions(+) create mode 100644 examples/example_2/makefile create mode 100644 examples/example_2/readme.txt create mode 100644 examples/example_2/src/ProductionCode.c create mode 100644 examples/example_2/src/ProductionCode.h create mode 100644 examples/example_2/src/ProductionCode2.c create mode 100644 examples/example_2/src/ProductionCode2.h create mode 100644 examples/example_2/test/TestProductionCode.c create mode 100644 examples/example_2/test/TestProductionCode2.c create mode 100644 examples/example_2/test/test_runners/TestProductionCode2_Runner.c create mode 100644 examples/example_2/test/test_runners/TestProductionCode_Runner.c create mode 100644 examples/example_2/test/test_runners/all_tests.c diff --git a/examples/example_2/makefile b/examples/example_2/makefile new file mode 100644 index 0000000..f5b6399 --- /dev/null +++ b/examples/example_2/makefile @@ -0,0 +1,36 @@ +# ========================================== +# 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] +# ========================================== + +UNITY_ROOT=../.. +C_COMPILER=gcc +TARGET_BASE1=all_tests +ifeq ($(OS),Windows_NT) + TARGET_EXTENSION=.exe +else + TARGET_EXTENSION=.out +endif +TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION) +SRC_FILES1=$(UNITY_ROOT)/src/unity.c $(UNITY_ROOT)/extras/fixture/src/unity_fixture.c src/ProductionCode.c src/ProductionCode2.c test/TestProductionCode.c test/TestProductionCode2.c test/test_runners/TestProductionCode_Runner.c test/test_runners/TestProductionCode2_Runner.c test/test_runners/all_tests.c +INC_DIRS=-Isrc -I$(UNITY_ROOT)/src -I$(UNITY_ROOT)/extras/fixture/src +SYMBOLS= + +ifeq ($(OS),Windows_NT) + CLEANUP = del /F /Q $(TARGET1) +else + CLEANUP = rm -f build/*.o ; rm -f $(TARGET1) +endif + +all: clean default + +default: +# ruby auto/generate_test_runner.rb test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c +# ruby auto/generate_test_runner.rb test/TestProductionCode2.c test/test_runners/TestProductionCode2_Runner.c + $(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1) + ./$(TARGET1) + +clean: + $(CLEANUP) + diff --git a/examples/example_2/readme.txt b/examples/example_2/readme.txt new file mode 100644 index 0000000..eeaa477 --- /dev/null +++ b/examples/example_2/readme.txt @@ -0,0 +1,5 @@ +Example 2 +========= + +Same as the first example, but now using Unity's test fixture to group tests +together. \ No newline at end of file diff --git a/examples/example_2/src/ProductionCode.c b/examples/example_2/src/ProductionCode.c new file mode 100644 index 0000000..500b44b --- /dev/null +++ b/examples/example_2/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_2/src/ProductionCode.h b/examples/example_2/src/ProductionCode.h new file mode 100644 index 0000000..250ca0d --- /dev/null +++ b/examples/example_2/src/ProductionCode.h @@ -0,0 +1,3 @@ + +int FindFunction_WhichIsBroken(int NumberToFind); +int FunctionWhichReturnsLocalVariable(void); diff --git a/examples/example_2/src/ProductionCode2.c b/examples/example_2/src/ProductionCode2.c new file mode 100644 index 0000000..a8c72e1 --- /dev/null +++ b/examples/example_2/src/ProductionCode2.c @@ -0,0 +1,9 @@ + +#include "ProductionCode2.h" + +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/example_2/src/ProductionCode2.h b/examples/example_2/src/ProductionCode2.h new file mode 100644 index 0000000..34ae980 --- /dev/null +++ b/examples/example_2/src/ProductionCode2.h @@ -0,0 +1,2 @@ + +char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction); diff --git a/examples/example_2/test/TestProductionCode.c b/examples/example_2/test/TestProductionCode.c new file mode 100644 index 0000000..ff318ab --- /dev/null +++ b/examples/example_2/test/TestProductionCode.c @@ -0,0 +1,64 @@ +#include "ProductionCode.h" +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP(ProductionCode); + +//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; + +TEST_SETUP(ProductionCode) +{ + //This is run before EACH TEST + Counter = 0x5a5a; +} + +TEST_TEAR_DOWN(ProductionCode) +{ +} + +TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode) +{ + //All of these should pass + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999)); + TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1)); +} + +TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken) +{ + // 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)); +} + +TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue) +{ + //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()); +} + +TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain) +{ + //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()); +} + +TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed) +{ + //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_2/test/TestProductionCode2.c b/examples/example_2/test/TestProductionCode2.c new file mode 100644 index 0000000..d9f4efe --- /dev/null +++ b/examples/example_2/test/TestProductionCode2.c @@ -0,0 +1,33 @@ +#include "ProductionCode2.h" +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP(ProductionCode2); + +/* These should be ignored because they are commented out in various ways: +#include "whatever.h" +*/ +//#include "somethingelse.h" + +TEST_SETUP(ProductionCode2) +{ +} + +TEST_TEAR_DOWN(ProductionCode2) +{ +} + +TEST(ProductionCode2, IgnoredTest) +{ + TEST_IGNORE_MESSAGE("This Test Was Ignored On Purpose"); +} + +TEST(ProductionCode2, AnotherIgnoredTest) +{ + TEST_IGNORE_MESSAGE("These Can Be Useful For Leaving Yourself Notes On What You Need To Do Yet"); +} + +TEST(ProductionCode2, ThisFunctionHasNotBeenTested_NeedsToBeImplemented) +{ + TEST_IGNORE(); //Like This +} diff --git a/examples/example_2/test/test_runners/TestProductionCode2_Runner.c b/examples/example_2/test/test_runners/TestProductionCode2_Runner.c new file mode 100644 index 0000000..6fcc3b1 --- /dev/null +++ b/examples/example_2/test/test_runners/TestProductionCode2_Runner.c @@ -0,0 +1,9 @@ +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP_RUNNER(ProductionCode2) +{ + RUN_TEST_CASE(ProductionCode2, IgnoredTest); + RUN_TEST_CASE(ProductionCode2, AnotherIgnoredTest); + RUN_TEST_CASE(ProductionCode2, ThisFunctionHasNotBeenTested_NeedsToBeImplemented); +} \ No newline at end of file diff --git a/examples/example_2/test/test_runners/TestProductionCode_Runner.c b/examples/example_2/test/test_runners/TestProductionCode_Runner.c new file mode 100644 index 0000000..41a416a --- /dev/null +++ b/examples/example_2/test/test_runners/TestProductionCode_Runner.c @@ -0,0 +1,11 @@ +#include "unity.h" +#include "unity_fixture.h" + +TEST_GROUP_RUNNER(ProductionCode) +{ + RUN_TEST_CASE(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode); + RUN_TEST_CASE(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken); + RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue); + RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain); + RUN_TEST_CASE(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed); +} \ No newline at end of file diff --git a/examples/example_2/test/test_runners/all_tests.c b/examples/example_2/test/test_runners/all_tests.c new file mode 100644 index 0000000..29259be --- /dev/null +++ b/examples/example_2/test/test_runners/all_tests.c @@ -0,0 +1,12 @@ +#include "unity_fixture.h" + +static void RunAllTests(void) +{ + RUN_TEST_GROUP(ProductionCode); + RUN_TEST_GROUP(ProductionCode2); +} + +int main(int argc, char * argv[]) +{ + return UnityMain(argc, argv, RunAllTests); +} \ No newline at end of file From d27699d97a446d372bceb83a5599262ce002bd96 Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Wed, 17 Jul 2013 23:27:25 +1000 Subject: [PATCH 5/7] Updated examples * Updated example 2 readme * Broke up huge line in example 2 makefile --- examples/example_2/makefile | 11 ++++++++++- examples/example_2/readme.txt | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/example_2/makefile b/examples/example_2/makefile index f5b6399..994c7a5 100644 --- a/examples/example_2/makefile +++ b/examples/example_2/makefile @@ -13,7 +13,16 @@ else TARGET_EXTENSION=.out endif TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION) -SRC_FILES1=$(UNITY_ROOT)/src/unity.c $(UNITY_ROOT)/extras/fixture/src/unity_fixture.c src/ProductionCode.c src/ProductionCode2.c test/TestProductionCode.c test/TestProductionCode2.c test/test_runners/TestProductionCode_Runner.c test/test_runners/TestProductionCode2_Runner.c test/test_runners/all_tests.c +SRC_FILES1=\ + $(UNITY_ROOT)/src/unity.c \ + $(UNITY_ROOT)/extras/fixture/src/unity_fixture.c \ + src/ProductionCode.c \ + src/ProductionCode2.c \ + test/TestProductionCode.c \ + test/TestProductionCode2.c \ + test/test_runners/TestProductionCode_Runner.c \ + test/test_runners/TestProductionCode2_Runner.c \ + test/test_runners/all_tests.c INC_DIRS=-Isrc -I$(UNITY_ROOT)/src -I$(UNITY_ROOT)/extras/fixture/src SYMBOLS= diff --git a/examples/example_2/readme.txt b/examples/example_2/readme.txt index eeaa477..f0fce65 100644 --- a/examples/example_2/readme.txt +++ b/examples/example_2/readme.txt @@ -2,4 +2,4 @@ Example 2 ========= Same as the first example, but now using Unity's test fixture to group tests -together. \ No newline at end of file +together. Using the test fixture also makes writing test runners much easier. \ No newline at end of file From 8ca802d4dc4213ddbae462e578a7d446ca1a4b39 Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Fri, 19 Jul 2013 21:30:35 +1000 Subject: [PATCH 6/7] Updated examples * Renamed example x to example 3 --- examples/{example_x => example_3}/helper/UnityHelper.c | 0 examples/{example_x => example_3}/helper/UnityHelper.h | 0 examples/{example_x => example_3}/makefile | 0 examples/{example_x => example_3}/rakefile.rb | 0 examples/{example_x => example_3}/rakefile_helper.rb | 0 examples/{example_x => example_3}/readme.txt | 7 ++++--- examples/{example_x => example_3}/src/ProductionCode.c | 0 examples/{example_x => example_3}/src/ProductionCode.h | 0 examples/{example_x => example_3}/src/ProductionCode2.c | 0 examples/{example_x => example_3}/src/ProductionCode2.h | 0 examples/{example_x => example_3}/target_gcc_32.yml | 0 .../{example_x => example_3}/test/TestProductionCode.c | 0 .../{example_x => example_3}/test/TestProductionCode2.c | 0 .../test/no_ruby/TestProductionCode2_Runner.c | 0 .../test/no_ruby/TestProductionCode_Runner.c | 0 15 files changed, 4 insertions(+), 3 deletions(-) rename examples/{example_x => example_3}/helper/UnityHelper.c (100%) rename examples/{example_x => example_3}/helper/UnityHelper.h (100%) rename examples/{example_x => example_3}/makefile (100%) rename examples/{example_x => example_3}/rakefile.rb (100%) rename examples/{example_x => example_3}/rakefile_helper.rb (100%) rename examples/{example_x => example_3}/readme.txt (93%) rename examples/{example_x => example_3}/src/ProductionCode.c (100%) rename examples/{example_x => example_3}/src/ProductionCode.h (100%) rename examples/{example_x => example_3}/src/ProductionCode2.c (100%) rename examples/{example_x => example_3}/src/ProductionCode2.h (100%) rename examples/{example_x => example_3}/target_gcc_32.yml (100%) rename examples/{example_x => example_3}/test/TestProductionCode.c (100%) rename examples/{example_x => example_3}/test/TestProductionCode2.c (100%) rename examples/{example_x => example_3}/test/no_ruby/TestProductionCode2_Runner.c (100%) rename examples/{example_x => example_3}/test/no_ruby/TestProductionCode_Runner.c (100%) diff --git a/examples/example_x/helper/UnityHelper.c b/examples/example_3/helper/UnityHelper.c similarity index 100% rename from examples/example_x/helper/UnityHelper.c rename to examples/example_3/helper/UnityHelper.c diff --git a/examples/example_x/helper/UnityHelper.h b/examples/example_3/helper/UnityHelper.h similarity index 100% rename from examples/example_x/helper/UnityHelper.h rename to examples/example_3/helper/UnityHelper.h diff --git a/examples/example_x/makefile b/examples/example_3/makefile similarity index 100% rename from examples/example_x/makefile rename to examples/example_3/makefile diff --git a/examples/example_x/rakefile.rb b/examples/example_3/rakefile.rb similarity index 100% rename from examples/example_x/rakefile.rb rename to examples/example_3/rakefile.rb diff --git a/examples/example_x/rakefile_helper.rb b/examples/example_3/rakefile_helper.rb similarity index 100% rename from examples/example_x/rakefile_helper.rb rename to examples/example_3/rakefile_helper.rb diff --git a/examples/example_x/readme.txt b/examples/example_3/readme.txt similarity index 93% rename from examples/example_x/readme.txt rename to examples/example_3/readme.txt index 6c7780e..df6fb18 100644 --- a/examples/example_x/readme.txt +++ b/examples/example_3/readme.txt @@ -1,4 +1,5 @@ -Example Project +Example 3 +========= 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. @@ -9,10 +10,10 @@ test with gcc or a couple versions of IAR. You can tweak the yaml files to get 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.) +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 +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/example_x/src/ProductionCode.c b/examples/example_3/src/ProductionCode.c similarity index 100% rename from examples/example_x/src/ProductionCode.c rename to examples/example_3/src/ProductionCode.c diff --git a/examples/example_x/src/ProductionCode.h b/examples/example_3/src/ProductionCode.h similarity index 100% rename from examples/example_x/src/ProductionCode.h rename to examples/example_3/src/ProductionCode.h diff --git a/examples/example_x/src/ProductionCode2.c b/examples/example_3/src/ProductionCode2.c similarity index 100% rename from examples/example_x/src/ProductionCode2.c rename to examples/example_3/src/ProductionCode2.c diff --git a/examples/example_x/src/ProductionCode2.h b/examples/example_3/src/ProductionCode2.h similarity index 100% rename from examples/example_x/src/ProductionCode2.h rename to examples/example_3/src/ProductionCode2.h diff --git a/examples/example_x/target_gcc_32.yml b/examples/example_3/target_gcc_32.yml similarity index 100% rename from examples/example_x/target_gcc_32.yml rename to examples/example_3/target_gcc_32.yml diff --git a/examples/example_x/test/TestProductionCode.c b/examples/example_3/test/TestProductionCode.c similarity index 100% rename from examples/example_x/test/TestProductionCode.c rename to examples/example_3/test/TestProductionCode.c diff --git a/examples/example_x/test/TestProductionCode2.c b/examples/example_3/test/TestProductionCode2.c similarity index 100% rename from examples/example_x/test/TestProductionCode2.c rename to examples/example_3/test/TestProductionCode2.c diff --git a/examples/example_x/test/no_ruby/TestProductionCode2_Runner.c b/examples/example_3/test/no_ruby/TestProductionCode2_Runner.c similarity index 100% rename from examples/example_x/test/no_ruby/TestProductionCode2_Runner.c rename to examples/example_3/test/no_ruby/TestProductionCode2_Runner.c diff --git a/examples/example_x/test/no_ruby/TestProductionCode_Runner.c b/examples/example_3/test/no_ruby/TestProductionCode_Runner.c similarity index 100% rename from examples/example_x/test/no_ruby/TestProductionCode_Runner.c rename to examples/example_3/test/no_ruby/TestProductionCode_Runner.c From e4727092e815f4193553cab57cf1e652478e0ffa Mon Sep 17 00:00:00 2001 From: Warwick Stone Date: Thu, 5 Sep 2013 19:12:26 +1000 Subject: [PATCH 7/7] Added Eclipse error parsers --- extras/eclipse/error_parsers.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 extras/eclipse/error_parsers.txt diff --git a/extras/eclipse/error_parsers.txt b/extras/eclipse/error_parsers.txt new file mode 100644 index 0000000..94e34ff --- /dev/null +++ b/extras/eclipse/error_parsers.txt @@ -0,0 +1,26 @@ +Eclipse error parsers +===================== + +These are a godsend for extracting & quickly navigating to +warnings & error messages from console output. Unforunately +I don't know how to write an Eclipse plugin so you'll have +to add them manually. + +To add a console parser to Eclipse, go to Window --> Preferences +--> C/C++ --> Build --> Settings. Click on the 'Error Parsers' +tab and then click the 'Add...' button. See the table below for +the parser fields to add. + +Eclipse will only parse the console output during a build, so +running your unit tests must be part of your build process. +Either add this to your make/rakefile, or add it as a post- +build step in your Eclipse project settings. + + +Unity unit test error parsers +----------------------------- +Severity Pattern File Line Description +------------------------------------------------------------------------------- +Error (\.+)(.*?):(\d+):(.*?):FAIL: (.*) $2 $3 $5 +Warning (\.+)(.*?):(\d+):(.*?):IGNORE: (.*) $2 $3 $5 +Warning (\.+)(.*?):(\d+):(.*?):IGNORE\s*$ $2 $3 Ignored test