* updated for better compatibility (now works with 1.8.6, 1.8.7, 1.9.0, 1.9.1)

* fixed bugs and cleaned up strict global ordering support (and added system test to prove it) 

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@130 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2009-06-19 02:05:46 +00:00
parent bdfa059832
commit 3e1651c937
19 changed files with 2450 additions and 2202 deletions
+8 -1
View File
@@ -10,6 +10,13 @@ require "cmock_plugin_manager"
require "cmock_generator_utils"
require "cmock_unityhelper_parser"
#gem install test-unit -v 1.2.3
ruby_version = RUBY_VERSION.split('.')
if (ruby_version[1].to_i == 9) and (ruby_version[2].to_i > 1)
require 'gems'
gem 'test-unit'
require 'test/unit'
end
class CMock
@@ -18,7 +25,7 @@ class CMock
end
def setup_mocks(files)
files.each do |src|
[files].flatten.each do |src|
generate_mock src
end
end
+4 -4
View File
@@ -1,7 +1,6 @@
class CMockFileWriter
require 'ftools'
attr_reader :config
def initialize(config)
@@ -21,8 +20,9 @@ class CMockFileWriter
private ###################################
def update_file(dest, src)
File.delete(dest) if (File.exist?(dest))
File.copy(src, dest)
File.delete(src)
require 'fileutils'
FileUtils.rm(dest) if (File.exist?(dest))
FileUtils.cp(src, dest)
FileUtils.rm(src)
end
end
+1 -1
View File
@@ -223,7 +223,7 @@ module CMockFunctionPrototype
# dive down into nested parentheses to pull out deepest node info
def get_deepest_name_and_args_node
node = name_and_args
while (node.methods.include?('name_and_args'))
while (node.respond_to?(:name_and_args))
node = node.name_and_args
end
return node
File diff suppressed because it is too large Load Diff
+18 -11
View File
@@ -7,13 +7,14 @@ class CMockGenerator
attr_reader :config, :file_writer, :tab, :module_name, :mock_name, :utils, :plugins
def initialize(config, module_name, file_writer, utils, plugins=[])
@config = config
@file_writer = file_writer
@tab = @config.tab
@module_name = module_name
@mock_name = @config.mock_prefix + @module_name
@utils = utils
@plugins = plugins
@utils = utils
@plugins = plugins
@config = config
@tab = @config.tab
@mock_name = @config.mock_prefix + @module_name
@ordered = @config.enforce_strict_ordering
end
def create_mock(parsed_stuff)
@@ -97,21 +98,23 @@ class CMockGenerator
file << "#{@tab}unsigned char placeHolder;\n"
end
file << "#{@tab}unsigned char allocFailure;\n"
file << functions.collect{|function| @plugins.run(:instance_structure, function)}.flatten
file << functions.collect{|function| @plugins.run(:instance_structure, function)}.join
file << "} Mock;\n\n"
end
def create_extern_declarations(file)
file << "extern jmp_buf AbortFrame;\n"
file << "extern int GlobalExpectOrder;\n"
file << "extern int GlobalVerifyOrder;\n"
if (@ordered)
file << "extern int GlobalExpectCount;\n"
file << "extern int GlobalVerifyOrder;\n"
end
file << "\n"
end
def create_mock_verify_function(file, functions)
file << "void #{@mock_name}_Verify(void)\n{\n"
file << "#{@tab}TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n"
file << functions.collect {|function| @plugins.run(:mock_verify, function)}.flatten
file << functions.collect {|function| @plugins.run(:mock_verify, function)}.join
file << "}\n\n"
end
@@ -123,8 +126,12 @@ class CMockGenerator
def create_mock_destroy_function(file, functions)
file << "void #{@mock_name}_Destroy(void)\n{\n"
file << functions.collect {|function| @plugins.run(:mock_destroy, function) }.flatten
file << functions.collect {|function| @plugins.run(:mock_destroy, function) }.join
file << "#{@tab}memset(&Mock, 0, sizeof(Mock));\n"
if (@ordered)
file << "#{@tab}GlobalExpectCount = 0;\n"
file << "#{@tab}GlobalVerifyOrder = 0;\n"
end
file << "}\n\n"
end
@@ -148,7 +155,7 @@ class CMockGenerator
# Return expected value, if necessary
if (function[:return_type] != "void")
file << @utils.code_handle_return_value(function, "#{@tab}")
file << @utils.code_handle_return_value(function, "#{@tab}").join
end
# Close out the function
+3 -3
View File
@@ -16,7 +16,7 @@ class CMockGeneratorPluginCException
def include_files
include = @config.cexception_include
include = "Exception.h" if (include.nil?)
return "#include \"#{include}\"\n"
return ["#include \"#{include}\"\n"]
end
def instance_structure(function)
@@ -32,9 +32,9 @@ class CMockGeneratorPluginCException
def mock_function_declarations(function)
if (function[:args_string] == "void")
return "void #{function[:name]}_ExpectAndThrow(#{@config.cexception_throw_type} toThrow);\n"
return ["void #{function[:name]}_ExpectAndThrow(#{@config.cexception_throw_type} toThrow);\n"]
else
return "void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, #{@config.cexception_throw_type} toThrow);\n"
return ["void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, #{@config.cexception_throw_type} toThrow);\n"]
end
end
+16 -8
View File
@@ -24,9 +24,9 @@ class CMockGeneratorPluginExpect
end
if (@ordered)
lines << [ "#{@tab}#{function[:return_type]} *#{function[:name]}_CallOrder;\n",
"#{@tab}#{function[:return_type]} *#{function[:name]}_CallOrder_Head;\n",
"#{@tab}#{function[:return_type]} *#{function[:name]}_CallOrder_Tail;\n" ]
lines << [ "#{@tab}int *#{function[:name]}_CallOrder;\n",
"#{@tab}int *#{function[:name]}_CallOrder_Head;\n",
"#{@tab}int *#{function[:name]}_CallOrder_Tail;\n" ]
end
function[:args].each do |arg|
@@ -40,15 +40,15 @@ class CMockGeneratorPluginExpect
def mock_function_declarations(function)
if (function[:args_string] == "void")
if (function[:return_type] == 'void')
return "void #{function[:name]}_Expect(void);\n"
return ["void #{function[:name]}_Expect(void);\n"]
else
return "void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"
return ["void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"]
end
else
if (function[:return_type] == 'void')
return "void #{function[:name]}_Expect(#{function[:args_string]});\n"
return ["void #{function[:name]}_Expect(#{function[:args_string]});\n"]
else
return "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"
return ["void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"]
end
end
end
@@ -115,7 +115,7 @@ class CMockGeneratorPluginExpect
end
def mock_verify(function)
return "#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"
return ["#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"]
end
def mock_destroy(function)
@@ -128,6 +128,14 @@ class CMockGeneratorPluginExpect
"#{@tab}#{@tab}Mock.#{function[:name]}_Return_Tail=NULL;\n",
"#{@tab}}\n" ]
end
if (@ordered)
lines << [ "#{@tab}if (Mock.#{function[:name]}_CallOrder_Head)\n",
"#{@tab}{\n",
"#{@tab}#{@tab}free(Mock.#{function[:name]}_CallOrder_Head);\n",
"#{@tab}#{@tab}Mock.#{function[:name]}_CallOrder_Head=NULL;\n",
"#{@tab}#{@tab}Mock.#{function[:name]}_CallOrder_Tail=NULL;\n",
"#{@tab}}\n" ]
end
function[:args].each do |arg|
lines << [ "#{@tab}if (Mock.#{function[:name]}_Expected_#{arg[:name]}_Head)\n",
"#{@tab}{\n",
+3 -3
View File
@@ -14,14 +14,14 @@ class CMockGeneratorPluginIgnore
end
def instance_structure(function)
return "#{@tab}#{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"
return ["#{@tab}#{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"]
end
def mock_function_declarations(function)
if (function[:return_type] == "void")
return "void #{function[:name]}_Ignore(void);\n"
return ["void #{function[:name]}_Ignore(void);\n"]
else
return "void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]});\n"
return ["void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]});\n"]
end
end
+2 -2
View File
@@ -61,7 +61,7 @@ class CMockGeneratorUtils
lines << [ "#{@tab}++GlobalExpectCount;\n",
code_insert_item_into_expect_array("int", "Mock.#{func_name}_CallOrder_Head", "GlobalExpectCount"),
"#{@tab}Mock.#{func_name}_CallOrder = Mock.#{func_name}_CallOrder_Head;\n",
"#{@tab}Mock.#{func_name}_CallOrder += Mock.#{func_name}_CallOrder;\n" ]
"#{@tab}Mock.#{func_name}_CallOrder += Mock.#{func_name}_CallCount;\n" ]
end
lines.flatten
end
@@ -85,7 +85,7 @@ class CMockGeneratorUtils
unity_msg = (unity_func =~ /_MESSAGE/) ? ", #{msg}" : ''
case(unity_func)
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE"
full_expected = (expected.strip[0] == 42) ? expected.slice(1..-1) : "&(#{expected})"
full_expected = (expected =~ /^\*/) ? expected.slice(1..-1) : "&(#{expected})"
return "#{indent}#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg});\n"
when /_ARRAY/
return [ "#{indent}if (*p_expected == NULL)\n",
+4 -1
View File
@@ -39,7 +39,10 @@ class CMockHeaderParser
# void must be void for cmock _ExpectAndReturn calls to process properly.
# to a certain extent, this action assumes we're chewing on pre-processed header files
void_types = source.scan(/typedef\s+(\(\s*)?void(\s*\))?\s+([\w\d]+)\s*;/)
void_types.each {|type| source.gsub!(/#{type}/, 'void')} if void_types.size > 0
if void_types
void_types = void_types.flatten.uniq - ['(',')',nil]
void_types.each {|type| source.gsub!(/#{type}/, 'void')} if void_types.size > 0
end
source.gsub!(/\s*\\\s*/m, ' ') # smush multiline statements into single line
source.gsub!(/\/\*.*?\*\//m, '') # remove block comments (do it first to avoid trouble with embedded line comments)
+2 -2
View File
@@ -17,9 +17,9 @@ class CMockPluginManager
def run(method, args=nil)
if args.nil?
return @plugins.collect{ |plugin| plugin.send(method) if plugin.respond_to?(method) }.flatten
return @plugins.collect{ |plugin| plugin.send(method) if plugin.respond_to?(method) }.flatten.join
else
return @plugins.collect{ |plugin| plugin.send(method, args) if plugin.respond_to?(method) }.flatten
return @plugins.collect{ |plugin| plugin.send(method, args) if plugin.respond_to?(method) }.flatten.join
end
end
end
+3 -3
View File
@@ -102,7 +102,7 @@ class SystemTestGenerator
includes << [MOCK_PREFIX + namix + MOCKABLE_H]
includes << [name + H_EXTENSION]
write_source_file(GENERATED_PATH + TEST_PREFIX + name + C_EXTENSION, includes) do |out|
write_source_file(GENERATED_PATH + TEST_PREFIX + name + C_EXTENSION, includes.flatten) do |out|
out.puts(tests[:common])
out.puts('')
@@ -120,7 +120,7 @@ class SystemTestGenerator
header_file = name + H_EXTENSION
includes = (namix + TYPES_H) if not yaml_hash[:systest][:types].nil?
includes = yaml_hash[:systest][:types].nil? ? [] : [(namix + TYPES_H)]
write_header_file(GENERATED_PATH + header_file, name.upcase, includes) do |out|
out.puts(source[:header])
@@ -131,7 +131,7 @@ class SystemTestGenerator
includes << (namix + MOCKABLE_H) if not yaml_hash[:systest][:mockable].nil?
includes << header_file
write_source_file(GENERATED_PATH + name + C_EXTENSION, includes) do |out|
write_source_file(GENERATED_PATH + name + C_EXTENSION, includes.flatten) do |out|
out.puts(source[:code])
end
end
@@ -0,0 +1,122 @@
---
:cmock:
:enforce_strict_ordering: 1
:plugins:
- # none
:systest:
:types: |
#define UINT32 unsigned int
typedef signed int custom_type;
:mockable: |
UINT32 foo(custom_type a);
UINT32 bar(custom_type b);
:source:
:header: |
UINT32 function_a(int a, int b);
void function_b(void);
:code: |
UINT32 function_a(int a, int b)
{
return foo((custom_type)a) + bar((custom_type)b);
}
void function_b(void) { }
void function_c(void)
{
foo((custom_type)1);
foo((custom_type)2);
bar((custom_type)3);
bar((custom_type)4);
foo((custom_type)5);
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
:units:
- :pass: TRUE
:should: 'successfully exercise two simple ExpectAndReturn mock calls'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)1, 10);
bar_ExpectAndReturn((custom_type)2, 20);
TEST_ASSERT_EQUAL(30, function_a(1, 2));
}
- :pass: FALSE
:should: 'fail because bar() is not called but is expected'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)1, 10);
TEST_ASSERT_EQUAL(30, function_a(1, 2));
}
- :pass: FALSE
:should: 'fail because bar() is called but is not expected'
:code: |
test()
{
bar_ExpectAndReturn((custom_type)1, 10);
function_b();
}
- :pass: FALSE
:should: 'fail because bar and foo called in reverse order'
:code: |
test()
{
bar_ExpectAndReturn((custom_type)2, 20);
foo_ExpectAndReturn((custom_type)1, 10);
TEST_ASSERT_EQUAL(30, function_a(1, 2));
}
- :pass: TRUE
:should: 'fail because bar and foo called out of order'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)1, 10);
foo_ExpectAndReturn((custom_type)2, 10);
bar_ExpectAndReturn((custom_type)3, 20);
bar_ExpectAndReturn((custom_type)4, 10);
foo_ExpectAndReturn((custom_type)5, 10);
function_c();
}
- :pass: FALSE
:should: 'fail because bar and foo called out of order at end'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)1, 10);
foo_ExpectAndReturn((custom_type)2, 10);
bar_ExpectAndReturn((custom_type)3, 20);
foo_ExpectAndReturn((custom_type)5, 10);
bar_ExpectAndReturn((custom_type)4, 10);
function_c();
}
- :pass: FALSE
:should: 'fail because bar and foo called out of order at start'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)2, 10);
foo_ExpectAndReturn((custom_type)1, 10);
bar_ExpectAndReturn((custom_type)3, 20);
bar_ExpectAndReturn((custom_type)4, 10);
foo_ExpectAndReturn((custom_type)5, 10);
function_c();
}
...
+57 -13
View File
@@ -39,9 +39,17 @@ class CMockGeneratorTest < Test::Unit::TestCase
create_mocks :config, :file_writer, :utils, :plugins
@module_name = "PoutPoutFish"
#no strict handling
@config.expect.tab.returns(" ")
@config.expect.mock_prefix.returns("Mock")
@config.expect.enforce_strict_ordering.returns(false)
@cmock_generator = CMockGenerator.new(@config, @module_name, @file_writer, @utils, @plugins)
#strict handling
@config.expect.tab.returns(" ")
@config.expect.mock_prefix.returns("Mock")
@config.expect.enforce_strict_ordering.returns(true)
@cmock_generator_strict = CMockGenerator.new(@config, @module_name, @file_writer, @utils, @plugins)
end
def teardown
@@ -141,6 +149,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
"{\n",
" unsigned char placeHolder;\n",
" unsigned char allocFailure;\n",
"",
"} Mock;\n\n"
]
@@ -157,9 +166,9 @@ class CMockGeneratorTest < Test::Unit::TestCase
expected = [ "static struct MockPoutPoutFishInstance\n",
"{\n",
" unsigned char allocFailure;\n",
" Uno_First(int Candy, int)",
" Dos_First(int Candy, int)",
" Uno_Second(bool Smarty, char)",
" Uno_First(int Candy, int)" +
" Dos_First(int Candy, int)" +
" Uno_Second(bool Smarty, char)" +
" Dos_Second(bool Smarty, char)",
"} Mock;\n\n"
]
@@ -174,8 +183,6 @@ class CMockGeneratorTest < Test::Unit::TestCase
should "create extern declarations for source file" do
output = []
expected = [ "extern jmp_buf AbortFrame;\n",
"extern int GlobalExpectOrder;\n",
"extern int GlobalVerifyOrder;\n",
"\n" ]
@cmock_generator.create_extern_declarations(output)
@@ -183,11 +190,24 @@ class CMockGeneratorTest < Test::Unit::TestCase
assert_equal(expected, output.flatten)
end
should "create extern declarations for source file when using strict ordering" do
output = []
expected = [ "extern jmp_buf AbortFrame;\n",
"extern int GlobalExpectCount;\n",
"extern int GlobalVerifyOrder;\n",
"\n" ]
@cmock_generator_strict.create_extern_declarations(output)
assert_equal(expected, output.flatten)
end
should "create mock verify functions in source file when no functions specified" do
functions = []
output = []
expected = [ "void MockPoutPoutFish_Verify(void)\n{\n",
" TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n",
"",
"}\n\n"
]
@@ -203,9 +223,9 @@ class CMockGeneratorTest < Test::Unit::TestCase
output = []
expected = [ "void MockPoutPoutFish_Verify(void)\n{\n",
" TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n",
" Uno_First",
" Dos_First",
" Uno_Second",
" Uno_First" +
" Dos_First" +
" Uno_Second" +
" Dos_Second",
"}\n\n"
]
@@ -233,6 +253,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
functions = []
output = []
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
"",
" memset(&Mock, 0, sizeof(Mock));\n",
"}\n\n"
]
@@ -248,9 +269,9 @@ class CMockGeneratorTest < Test::Unit::TestCase
]
output = []
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
" Uno_First(int Candy, int)",
" Dos_First(int Candy, int)",
" Uno_Second(bool Smarty, char)",
" Uno_First(int Candy, int)" +
" Dos_First(int Candy, int)" +
" Uno_Second(bool Smarty, char)" +
" Dos_Second(bool Smarty, char)",
" memset(&Mock, 0, sizeof(Mock));\n",
"}\n\n"
@@ -263,6 +284,29 @@ class CMockGeneratorTest < Test::Unit::TestCase
assert_equal(expected, output.flatten)
end
should "create mock destroy functions in source file when extra functions specified with strict ordering" do
functions = [ { :name => "First", :args => "int Candy", :return_type => "int" },
{ :name => "Second", :args => "bool Smarty", :return_type => "char" }
]
output = []
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
" Uno_First(int Candy, int)" +
" Dos_First(int Candy, int)" +
" Uno_Second(bool Smarty, char)" +
" Dos_Second(bool Smarty, char)",
" memset(&Mock, 0, sizeof(Mock));\n",
" GlobalExpectCount = 0;\n",
" GlobalVerifyOrder = 0;\n",
"}\n\n"
]
@plugins.expect.run(:mock_destroy, functions[0]).returns([" Uno_First(int Candy, int)"," Dos_First(int Candy, int)"])
@plugins.expect.run(:mock_destroy, functions[1]).returns([" Uno_Second(bool Smarty, char)"," Dos_Second(bool Smarty, char)"])
@cmock_generator_strict.create_mock_destroy_function(output, functions)
assert_equal(expected, output.flatten)
end
should "create mock implementation functions in source file" do
function = { :modifier => "static",
:return_type => "bool",
@@ -285,7 +329,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
]
@plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.bool"," PreSupaFunctionDos.bool"])
@plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches, const char* named)"," MockSupaFunctionDos(uint32 sandwiches, const char* named)"])
@utils.expect.code_handle_return_value(function," ").returns(" UtilsSupaFunction.bool")
@utils.expect.code_handle_return_value(function," ").returns([" UtilsSupaFunction.bool"])
@cmock_generator.create_mock_implementation(output, function)
@@ -313,7 +357,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
]
@plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.int"," PreSupaFunctionDos.int"])
@plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches)"," MockSupaFunctionDos(uint32 sandwiches)"])
@utils.expect.code_handle_return_value(function," ").returns(" UtilsSupaFunction.int")
@utils.expect.code_handle_return_value(function," ").returns([" UtilsSupaFunction.int"])
@cmock_generator.create_mock_implementation(output, function)
@@ -19,14 +19,14 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
end
should "include the cexception library" do
expected = "#include \"Exception.h\"\n"
expected = ["#include \"Exception.h\"\n"]
@config.expect.cexception_include.returns(nil)
returned = @cmock_generator_plugin_cexception.include_files
assert_equal(expected, returned)
end
should "include the cexception library with a custom path if specified" do
expected = "#include \"../cexception/lib/Exception.h\"\n"
expected = ["#include \"../cexception/lib/Exception.h\"\n"]
@config.expect.cexception_include.returns("../cexception/lib/Exception.h")
returned = @cmock_generator_plugin_cexception.include_files
assert_equal(expected, returned)
@@ -52,7 +52,7 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
function = { :name => "Spruce", :args_string => "void", :return_type => "void" }
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = "void Spruce_ExpectAndThrow(EXCEPTION_TYPE toThrow);\n"
expected = ["void Spruce_ExpectAndThrow(EXCEPTION_TYPE toThrow);\n"]
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -61,7 +61,7 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
function = { :name => "Spruce", :args_string => "const char* Petunia, uint32_t Lily", :return_type => "void" }
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = "void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_TYPE toThrow);\n"
expected = ["void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_TYPE toThrow);\n"]
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -4,12 +4,22 @@ require 'cmock_generator_plugin_expect'
class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
def setup
create_mocks :config, :utils
#no strict ordering
@config.expect.tab.returns(" ")
@config.expect.when_ptr_star.returns(:compare_data)
@config.expect.enforce_strict_ordering.returns(false)
@config.stubs!(:respond_to?).returns(true)
@utils.expect.helpers.returns({})
@cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils)
#strict ordering
@config.expect.tab.returns(" ")
@config.expect.when_ptr_star.returns(:compare_data)
@config.expect.enforce_strict_ordering.returns(true)
@config.stubs!(:respond_to?).returns(true)
@utils.expect.helpers.returns({})
@cmock_generator_plugin_expect_strict = CMockGeneratorPluginExpect.new(@config, @utils)
end
def teardown
@@ -89,9 +99,24 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'void func(void)' and global ordering" do
function = {:name => "Oak", :args => [], :return_type => "void"}
count_type = "uint32"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n",
" int *#{function[:name]}_CallOrder;\n",
" int *#{function[:name]}_CallOrder_Head;\n",
" int *#{function[:name]}_CallOrder_Tail;\n"
]
returned = @cmock_generator_plugin_expect_strict.instance_structure(function)
assert_equal(expected, returned)
end
should "add mock function declaration for functions of style 'void func(void)'" do
function = {:name => "Maple", :args_string => "void", :return_type => "void"}
expected = "void #{function[:name]}_Expect(#{function[:args_string]});\n"
expected = ["void #{function[:name]}_Expect(#{function[:args_string]});\n"]
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -99,7 +124,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function declaration for functions of style 'int func(void)'" do
function = {:name => "Spruce", :args_string => "void", :return_string => "int #{CMOCK_RETURN_PARAM_NAME}"}
expected = "void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"
expected = ["void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"]
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -107,7 +132,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function declaration for functions of style 'const char* func(int tofu)'" do
function = {:name => "Pine", :args_string => "int tofu", :return_string => "const char* #{CMOCK_RETURN_PARAM_NAME}"}
expected = "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"
expected = ["void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"]
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -167,6 +192,26 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
assert_equal(expected, returned)
end
should "add mock function implementation for functions of style 'void func(void)' and strict ordering" do
function = {:name => "Apple", :args => [], :return_type => "void"}
expected = [" Mock.Apple_CallCount++;\n",
" if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Apple Called More Times Than Expected\");\n",
" }\n",
" {\n",
" int* p_expected = Mock.Apple_CallOrder;\n",
" ++GlobalVerifyOrder;\n",
" if (Mock.Apple_CallOrder != Mock.Apple_CallOrder_Tail)\n",
" Mock.Apple_CallOrder++;\n",
" BLEH",
" }\n"
]
@utils.expect.expect_helper('int', '*p_expected', 'GlobalVerifyOrder', "\"Function 'Apple' Called Out Of Order.\""," ").returns(" BLEH")
returned = @cmock_generator_plugin_expect_strict.mock_implementation(function)
assert_equal(expected, returned)
end
should "add mock interfaces for functions of style 'void func(void)'" do
@utils.expect.code_add_base_expectation("Pear").returns("mock_retval_0")
function = {:name => "Pear", :args => [], :args_string => "void", :return_type => "void"}
@@ -236,7 +281,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock verify lines" do
function = {:name => "Banana" }
expected = " TEST_ASSERT_EQUAL_MESSAGE(Mock.Banana_CallsExpected, Mock.Banana_CallCount, \"Function 'Banana' called unexpected number of times.\");\n"
expected = [" TEST_ASSERT_EQUAL_MESSAGE(Mock.Banana_CallsExpected, Mock.Banana_CallCount, \"Function 'Banana' called unexpected number of times.\");\n"]
returned = @cmock_generator_plugin_expect.mock_verify(function)
assert_equal(expected, returned)
end
@@ -279,4 +324,16 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
returned = @cmock_generator_plugin_expect.mock_destroy(function)
assert_equal(expected, returned)
end
should "add mock destroy for functions with strict ordering" do
function = {:name => "Peach", :args => [], :return_type => "void" }
expected = [ " if (Mock.Peach_CallOrder_Head)\n",
" {\n",
" free(Mock.Peach_CallOrder_Head);\n",
" Mock.Peach_CallOrder_Head=NULL;\n",
" Mock.Peach_CallOrder_Tail=NULL;\n",
" }\n" ]
returned = @cmock_generator_plugin_expect_strict.mock_destroy(function)
assert_equal(expected, returned)
end
end
@@ -26,21 +26,21 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
function = {:name => "Grass", :args => [], :return_type => "void"}
@config.expect.ignore_bool_type.returns("BOOL")
expected = " BOOL Grass_IgnoreBool;\n"
expected = [" BOOL Grass_IgnoreBool;\n"]
returned = @cmock_generator_plugin_ignore.instance_structure(function)
assert_equal(expected, returned)
end
should "handle function declarations for functions without return values" do
function = {:name => "Mold", :args_string => "void", :return_type => "void"}
expected = "void Mold_Ignore(void);\n"
expected = ["void Mold_Ignore(void);\n"]
returned = @cmock_generator_plugin_ignore.mock_function_declarations(function)
assert_equal(expected, returned)
end
should "handle function declarations for functions that returns something" do
function = {:name => "Fungus", :args_string => "void", :return_type => "const char*", :return_string => "const char* #{CMOCK_RETURN_PARAM_NAME}"}
expected = "void Fungus_IgnoreAndReturn(const char* #{CMOCK_RETURN_PARAM_NAME});\n"
expected = ["void Fungus_IgnoreAndReturn(const char* #{CMOCK_RETURN_PARAM_NAME});\n"]
returned = @cmock_generator_plugin_ignore.mock_function_declarations(function)
assert_equal(expected, returned)
end
+3 -3
View File
@@ -24,10 +24,10 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
@config.expect.tab.returns(" ")
@config.expect.when_ptr_star.returns(:compare_ptr)
@config.expect.enforce_strict_ordering.returns(false)
@cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A, :B})
@cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A=>1, :B=>2})
assert_equal(@config, @cmock_generator_utils.config)
assert_equal(" ", @cmock_generator_utils.tab)
assert_equal({:A, :B},@cmock_generator_utils.helpers)
assert_equal({:A=>1, :B=>2},@cmock_generator_utils.helpers)
end
should "set up an empty call list if no arguments passed" do
@@ -168,7 +168,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.Nectarine_CallOrder_Tail = &Mock.Nectarine_CallOrder_Head[sz+1];\n",
" }\n",
" Mock.Nectarine_CallOrder = Mock.Nectarine_CallOrder_Head;\n",
" Mock.Nectarine_CallOrder += Mock.Nectarine_CallOrder;\n" ]
" Mock.Nectarine_CallOrder += Mock.Nectarine_CallCount;\n" ]
@cmock_generator_utils.ordered = true
returned = @cmock_generator_utils.code_add_base_expectation("Nectarine")
assert_equal(expected, returned)
+8 -8
View File
@@ -57,12 +57,12 @@ class CMockPluginManagerTest < Test::Unit::TestCase
@cmock_plugins = CMockPluginManager.new(@config, @utils)
@cmock_plugins.plugins = [@pluginA, @pluginB]
@pluginA.stubs!(:test_method).returns("This Is An Awesome Test")
@pluginB.stubs!(:test_method).returns(["And This is Part 2","Of An Awesome Test"])
@pluginA.stubs!(:test_method).returns(["This Is An Awesome Test-"])
@pluginB.stubs!(:test_method).returns(["And This is Part 2-","Of An Awesome Test"])
expected = ["This Is An Awesome Test","And This is Part 2","Of An Awesome Test"]
expected = "This Is An Awesome Test-And This is Part 2-Of An Awesome Test"
output = @cmock_plugins.run(:test_method)
assert_equal(expected, output.flatten)
assert_equal(expected, output)
end
should "run a desired method and arg list over each plugin requested and return the results" do
@@ -72,11 +72,11 @@ class CMockPluginManagerTest < Test::Unit::TestCase
@cmock_plugins = CMockPluginManager.new(@config, @utils)
@cmock_plugins.plugins = [@pluginA, @pluginB]
@pluginA.stubs!(:test_method).returns("This Is An Awesome Test")
@pluginB.stubs!(:test_method).returns(["And This is Part 2","Of An Awesome Test"])
@pluginA.stubs!(:test_method).returns(["This Is An Awesome Test-"])
@pluginB.stubs!(:test_method).returns(["And This is Part 2-","Of An Awesome Test"])
expected = ["This Is An Awesome Test","And This is Part 2","Of An Awesome Test"]
expected = "This Is An Awesome Test-And This is Part 2-Of An Awesome Test"
output = @cmock_plugins.run(:test_method, "chickenpotpie")
assert_equal(expected, output.flatten)
assert_equal(expected, output)
end
end