mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-06-06 05:25:29 +00:00
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
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
compiler: 'gcc.exe'
|
||||
linker: 'gcc.exe'
|
||||
path: 'c:/mingw/bin/'
|
||||
compile_flags: '-c'
|
||||
link_flags: ''
|
||||
+39
-5
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "Simple.h"
|
||||
|
||||
int Add(int a, int b)
|
||||
{
|
||||
return (a + b);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef _SIMPLE_H
|
||||
#define _SIMPLE_H
|
||||
|
||||
int Add(int a, int b);
|
||||
|
||||
#endif // _SIMPLE_H
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "unity.h"
|
||||
#include "Simple.h"
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user