From 761792c3349718810cf80c752970122331b88709 Mon Sep 17 00:00:00 2001 From: mkarlesky Date: Sun, 1 Jun 2008 23:37:05 +0000 Subject: [PATCH] Added very basic system test capability (no mocks yet; just unit tests): semi-generic compiler setups, compilation, linking, and running of test file. git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@11 bf332499-1b4d-0410-844d-d2d48d5cc64c --- mingw.yml | 5 ++ rakefile.rb | 44 ++++++++++++++-- rakefile_helper.rb | 98 +++++++++++++++++++++++++++++++++++ test/system/config.yml | 0 test/system/source/Simple.c | 6 +++ test/system/source/Simple.h | 6 +++ test/system/test/SimpleTest.c | 49 ++++++++++++++++++ test/system/thing_test.rb | 29 ----------- 8 files changed, 203 insertions(+), 34 deletions(-) create mode 100644 mingw.yml create mode 100644 rakefile_helper.rb delete mode 100644 test/system/config.yml create mode 100644 test/system/source/Simple.c create mode 100644 test/system/source/Simple.h create mode 100644 test/system/test/SimpleTest.c delete mode 100644 test/system/thing_test.rb diff --git a/mingw.yml b/mingw.yml new file mode 100644 index 0000000..cd60107 --- /dev/null +++ b/mingw.yml @@ -0,0 +1,5 @@ +compiler: 'gcc.exe' +linker: 'gcc.exe' +path: 'c:/mingw/bin/' +compile_flags: '-c' +link_flags: '' diff --git a/rakefile.rb b/rakefile.rb index 42ac875..d1eef6b 100644 --- a/rakefile.rb +++ b/rakefile.rb @@ -3,8 +3,17 @@ $here = File.dirname(__FILE__) require 'rake' require 'rake/clean' require 'rake/testtask' +require 'rakefile_helper' -CLEAN.include('test/system/build/' + '*.*') +include RakefileConstants +include RakefileHelpers + +SYSTEST_TEST_FILES = FileList.new(SYSTEST_TEST_DIR + '*Test' + C_EXTENSION) + +COMPILER_CONFIGS = FileList.new('*.yml') + +CLEAN.include(SYSTEST_BUILD_DIR + '*.*') +CLEAN.include(SYSTEST_MOCKS_DIR + '*.*') task :default => [ :clobber, 'tests:all' ] @@ -18,11 +27,36 @@ namespace :tests do t.verbose = true end - Rake::TestTask.new('system') do |t| - t.pattern = 'test//system/*_test.rb' - t.verbose = true + desc "Run system tests" + task :system do + COMPILER_CONFIGS.each do |cfg_file| + run_systests(yaml_read(cfg_file), SYSTEST_TEST_FILES) + end + end + +end + + + +def run_systests(config, test_files) + + test_files.each do |test| + obj_list = [] + test_base = File.basename(test, C_EXTENSION) + headers = extract_headers(test) + + headers.each do |header| + compile(config, find_source_file(header)) + obj_list << header.ext(OBJ_EXTENSION) + end + + compile(config, test) + obj_list << test_base.ext(OBJ_EXTENSION) + + link(config, test_base, obj_list) + + execute(SYSTEST_BUILD_DIR + test_base + EXE_EXTENSION) end end - diff --git a/rakefile_helper.rb b/rakefile_helper.rb new file mode 100644 index 0000000..a9e7d1d --- /dev/null +++ b/rakefile_helper.rb @@ -0,0 +1,98 @@ + +def Kernel.is_windows? + processor, platform, *rest = RUBY_PLATFORM.split("-") + platform == 'mswin32' +end + +module RakefileConstants + + C_EXTENSION = '.c' + OBJ_EXTENSION = '.o' + + if (Kernel.is_windows?) + EXE_EXTENSION = '.exe' + else + EXE_EXTENSION = '.out' + end + + UNITY_DIR = 'vendor/unity/src/' + + SYSTEST_BASE = 'test/system/' + SYSTEST_SOURCE_DIR = SYSTEST_BASE + 'source/' + SYSTEST_TEST_DIR = SYSTEST_BASE + 'test/' + SYSTEST_MOCKS_DIR = SYSTEST_BASE + 'mocks/' + SYSTEST_BUILD_DIR = SYSTEST_BASE + 'build/' + + SYSTEST_INCLUDE_DIRS = [SYSTEST_SOURCE_DIR, SYSTEST_TEST_DIR, SYSTEST_MOCKS_DIR, UNITY_DIR] + +end + +module RakefileHelpers + + require 'fileutils' + + def extract_headers(filename) + includes = [] + lines = File.readlines(filename) + lines.each do |line| + m = line.match /#include \"(.*)\"/ + if not m.nil? + includes << m[1] + end + end + return includes + end + + def find_source_file(header) + src_file = '' + SYSTEST_INCLUDE_DIRS.each do |dir| + src_file = dir + header.ext(C_EXTENSION) + if (File.exists?(src_file)) + return src_file + end + end + return '' + end + + def compile(config, file) + cmd_str = + "#{config["path"]}#{config["compiler"]} #{config["compile_flags"]} " + + "-B#{config["path"]} " + + (SYSTEST_INCLUDE_DIRS.map{|dir|"-I#{dir} "}).join + + "#{file} " + + "-o #{SYSTEST_BUILD_DIR}#{File.basename(file, C_EXTENSION)}#{OBJ_EXTENSION}" + execute(cmd_str) + end + + def link(config, exe_name, obj_list) + cmd_str = + "#{config["path"]}#{config["linker"]} " + + "-B#{config["path"]} " + + (obj_list.map{|obj|"#{SYSTEST_BUILD_DIR}#{obj} "}).join + + "-o #{SYSTEST_BUILD_DIR}#{exe_name}#{EXE_EXTENSION}" + execute(cmd_str) + end + + def yaml_read(filename) + return YAML.load(File.read(filename)) + end + + def report(message) + puts message + $stdout.flush + $stderr.flush + end + + def execute(command_string, verbose=true) + report command_string + output = `#{command_string}` + report(output) if verbose + report '' + if $?.exitstatus != 0 + raise "Command failed. (Returned #{$?.exitstatus})" + end + return output + end + +end + diff --git a/test/system/config.yml b/test/system/config.yml deleted file mode 100644 index e69de29..0000000 diff --git a/test/system/source/Simple.c b/test/system/source/Simple.c new file mode 100644 index 0000000..574a20c --- /dev/null +++ b/test/system/source/Simple.c @@ -0,0 +1,6 @@ +#include "Simple.h" + +int Add(int a, int b) +{ + return (a + b); +} diff --git a/test/system/source/Simple.h b/test/system/source/Simple.h new file mode 100644 index 0000000..a5290e1 --- /dev/null +++ b/test/system/source/Simple.h @@ -0,0 +1,6 @@ +#ifndef _SIMPLE_H +#define _SIMPLE_H + +int Add(int a, int b); + +#endif // _SIMPLE_H diff --git a/test/system/test/SimpleTest.c b/test/system/test/SimpleTest.c new file mode 100644 index 0000000..a0f80ac --- /dev/null +++ b/test/system/test/SimpleTest.c @@ -0,0 +1,49 @@ +#include "unity.h" +#include "Simple.h" + +#include +#include + +jmp_buf AbortFrame; + + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_Simple_Add_ShouldSumValues(void) +{ + TEST_ASSERT_EQUAL( 5, Add( 2, 3)); + TEST_ASSERT_EQUAL( 5, Add( 1, 4)); + TEST_ASSERT_EQUAL( 5, Add( -2, 7)); + TEST_ASSERT_EQUAL(100, Add( 25, 75)); + TEST_ASSERT_EQUAL(-10, Add(-90, 80)); +} + + +static void runTest(UnityTestFunction test) +{ + if (TEST_PROTECT()) + { + test(); + } +} + + +int main(void) +{ + Unity.TestFile = "SimpleTest.c"; + UnityBegin(); + + // RUN_TEST calls runTest + RUN_TEST(test_Simple_Add_ShouldSumValues); + + UnityEnd(); + + return 0; +} + diff --git a/test/system/thing_test.rb b/test/system/thing_test.rb deleted file mode 100644 index cfa07b6..0000000 --- a/test/system/thing_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" - -class Thing - def initialize foo, bar - @foo = foo - @bar = bar - end - - def snafu - return @foo.val + @bar.val - end -end - -class ThingTest < Test::Unit::TestCase - def setup - create_mocks :foo, :bar - @thing = Thing.new(@foo, @bar) - end - - def teardown - end - - should "perform a stupid simple test" do - @foo.expect.val.returns(1) - @bar.expect.val.returns(2) - - assert_equal(3, @thing.snafu) - end -end