diff --git a/docs/CMock Summary.odt b/docs/CMock Summary.odt index 6a48f1c..970c7f6 100644 Binary files a/docs/CMock Summary.odt and b/docs/CMock Summary.odt differ diff --git a/docs/CMock Summary.pdf b/docs/CMock Summary.pdf index 988966c..d4ad97a 100644 Binary files a/docs/CMock Summary.pdf and b/docs/CMock Summary.pdf differ diff --git a/docs/CMock Summary.txt b/docs/CMock Summary.txt deleted file mode 100644 index 0e85a4d..0000000 --- a/docs/CMock Summary.txt +++ /dev/null @@ -1,102 +0,0 @@ - -Running CMock -============= - -CMock is a Ruby script and class. You can therefore use it directly from the command line, -or include it in your own scripts or rakefiles. - -Mocking from the Command Line ------------------------------ - -After unpacking CMock, you will find CMock.rb in the 'lib' directory. This is the file -that you want to run. It takes a list of header files to be mocked, as well as an optional -yaml file for a more detailed configuration (see config options below). - -For example, this will create three mocks using the configuration specified in MyConfig.yml: - - ruby cmock.rb -oMyConfig.yml super.h duper.h awesome.h - -And this will create two mocks using the default configuration: - - ruby cmock.rb ../mocking/stuff/is/fun.h ../try/it/yourself.h - -Mocking From Scripts or Rake ----------------------------- - -CMock can be used directly from your own scripts or from a rakefile. Start by including -cmock.rb, then create an instance of CMock. When you create your instance, you may -initialize it in one of three ways. - -You may specify nothing, allowing it to run with default settings: - - cmock = CMock.new - -You may specify a YAML file containing the configuration options you desire: - - cmock = CMock.new('../MyConfig.yml') - -You may specify the options explicitly: - - cmock = CMock.new('plugins' => ['cexception','ignore'],'mock_path' => 'my/mocks/') - -Config Options: ---------------- - -The following configuration options can be specified in the yaml file or directly when instantiating. - -Option Purpose ----------------------------------------------------------------- -plugins An array of which plugins to enable. 'expect' is always active. - 'cexception' and 'ignore' are also currently available. - -mock_path The directory where you would like the mock files generated to - be placed. - -includes An array of additional include files which should be added to the - mocks. Useful for global types and definitions used in your project. - -tab What does tab mean in your project? By default: a pair of spaces. - -expect_call_count_type Used internally by CMock... but maybe you don't like int's for - some reason? - -ignore_bool_type Used internally by Ignore plugin... but maybe you have a better - bool type? - -cexception_include Tell cexception plugin where to find Exception.h... only need to - define if it's not in your build path already. - -cexception_throw_type Tell cexception what type you are “throwing” around in your - application. It assumes an int - - -CMock Generated Mock Module Summary -=================================== - -In addition to the mocks themselves, CMock will generate the following functions for use in -your tests. The expect functions are always generated. While the ignore and cexception -functions are only generated if those plugins are enabled: - -Original Function => Generated Expect Functions -------------------------------------------------- -void func(void) => void func_Expect(void) -void func(params) => void func_Expect(expected_params) -retval func(void) => void func_ExpectAndReturn(retval_to_return) -retval func(params) => void func_ExpectAndReturn(expected_params, retval_to_return) - - -Original Function => Generated CException Functions ------------------------------------------------------ -void func(void) => void func_ExpectAndThrow(value_to_throw) -void func(params) => void func_ExpectAndThrow(expected_params, value_to_throw) -retval func(void) => void func_ExpectAndThrow(value_to_throw) -retval func(params) => void func_ExpectAndThrow(expected_params, value_to_throw) - - -Original Function => Generated Ignore Functions -------------------------------------------------- -void func(void) => void func_Ignore(void) -void func(params) => void func_Ignore(void) -retval func(void) => void func_IgnoreAndReturn(retval_to_return) -retval func(params) => void func_IgnoreAndReturn(retval_to_return) - diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index ef42930..f2de309 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -25,6 +25,7 @@ class CMockConfig when Hash then options = CMockDefaultOptions.clone.merge(options) else raise "If you specify arguments, it should be a filename or a hash of options" end + options[:plugins] ||= [] @options = options @options.each_key { |key| eval("def #{key}() return @options[:#{key}] end") } end diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 5bd9ebe..4ae4bda 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -158,7 +158,6 @@ class CMockGenerator file << "#{function[:attributes]} " if (!function[:attributes].nil? && function[:attributes].length > 0) file << "#{function_mod_and_rettype} #{function[:name]}(#{args_string})\n" file << "{\n" - file << @plugins.run(:mock_implementation_prefix, function) file << @plugins.run(:mock_implementation, function) # Return expected value, if necessary diff --git a/lib/cmock_generator_plugin_array.rb b/lib/cmock_generator_plugin_array.rb index 38a1223..17486ff 100644 --- a/lib/cmock_generator_plugin_array.rb +++ b/lib/cmock_generator_plugin_array.rb @@ -1,6 +1,7 @@ class CMockGeneratorPluginArray + attr_reader :priority attr_accessor :config, :utils, :unity_helper, :ordered def initialize(config, utils) @@ -9,6 +10,7 @@ class CMockGeneratorPluginArray @ordered = @config.enforce_strict_ordering @utils = utils @unity_helper = @utils.helpers[:unity_helper] + @priority = 8 end def instance_structure(function) @@ -37,10 +39,6 @@ class CMockGeneratorPluginArray end end - def mock_implementation(function) - nil - end - def mock_interfaces(function) return nil unless function[:contains_ptr?] diff --git a/lib/cmock_generator_plugin_callback.rb b/lib/cmock_generator_plugin_callback.rb new file mode 100644 index 0000000..cd1ab94 --- /dev/null +++ b/lib/cmock_generator_plugin_callback.rb @@ -0,0 +1,79 @@ +class CMockGeneratorPluginCallback + + attr_reader :priority + attr_reader :config, :utils + + def initialize(config, utils) + @config = config + @utils = utils + @priority = 3 + end + + def instance_structure(function) + INSTANCE_STRUCTURE_SNIPPET % [function[:name]] + end + + def mock_function_declarations(function) + if (function[:args_string] == "void") + MOCK_DECLARATION_SNIPPET % [function[:name], function[:return_type], ''] + else + MOCK_DECLARATION_SNIPPET % [function[:name], function[:return_type], function[:args_string] + ', '] + end + end + + def mock_implementation(function) + call_string = function[:args].empty? ? '' : function[:args].map{|m| m[:name]}.join(', ') + ', ' + if (function[:return_type] == 'void') + return MOCK_IMPLEMENTATION_NORET_SNIPPET % [function[:name], call_string] + else + return MOCK_IMPLEMENTATION_RETVAL_SNIPPET % [function[:name], call_string] + end + end + + def mock_interfaces(function) + MOCK_INTERFACE_SNIPPET % [function[:name]] + end + + def mock_destroy(function) + MOCK_DESTROY_SNIPPET % function[:name] + end + + private ############ + + INSTANCE_STRUCTURE_SNIPPET = %q[ CMOCK_%1$s_CALLBACK %1$s_CallbackFunctionPointer; +] + + MOCK_DECLARATION_SNIPPET = %q[ +typedef %2$s (* CMOCK_%1$s_CALLBACK)(%3$sint NumCalls); +void %1$s_StubWithCallback(CMOCK_%1$s_CALLBACK Callback); +] + + MOCK_IMPLEMENTATION_NORET_SNIPPET = %q[ + if (Mock.%1$s_CallbackFunctionPointer != NULL) + { + Mock.%1$s_CallsExpected++; + Mock.%1$s_CallbackFunctionPointer(%2$sMock.%1$s_CallCount++); + return; + } +] + + MOCK_IMPLEMENTATION_RETVAL_SNIPPET = %q[ + if (Mock.%1$s_CallbackFunctionPointer != NULL) + { + Mock.%1$s_CallsExpected++; + return Mock.%1$s_CallbackFunctionPointer(%2$sMock.%1$s_CallCount++); + } +] + + MOCK_INTERFACE_SNIPPET = %q[ +void %1$s_StubWithCallback(CMOCK_%1$s_CALLBACK Callback) +{ + Mock.%1$s_CallbackFunctionPointer = Callback; +} +] + + MOCK_DESTROY_SNIPPET = %q[ + Mock.%1$s_CallbackFunctionPointer = NULL; +] + +end diff --git a/lib/cmock_generator_plugin_cexception.rb b/lib/cmock_generator_plugin_cexception.rb index 823d2b8..d9299de 100644 --- a/lib/cmock_generator_plugin_cexception.rb +++ b/lib/cmock_generator_plugin_cexception.rb @@ -1,37 +1,38 @@ - class CMockGeneratorPluginCexception + attr_reader :priority attr_reader :config, :utils - + def initialize(config, utils) @config = config @utils = utils + @priority = 7 raise "'cexception_include' needs to be defined in config" unless @config.respond_to?(:cexception_include) end - + def include_files include = @config.cexception_include include = "Exception.h" if (include.nil?) return "#include \"#{include}\"\n" end - + def instance_structure(function) INSTANCE_STRUCTURE_SNIPPET % function[:name] end - + def mock_function_declarations(function) if (function[:args_string] == "void") - return "void #{function[:name]}_ExpectAndThrow(EXCEPTION_T toThrow);\n" - else - return "void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, EXCEPTION_T toThrow);\n" + return "void #{function[:name]}_ExpectAndThrow(EXCEPTION_T toThrow);\n" + else + return "void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, EXCEPTION_T toThrow);\n" end end - + def mock_implementation(function) MOCK_IMPLEMENTATION_SNIPPET % function[:name] end - + def mock_interfaces(function) arg_insert = (function[:args_string] == "void") ? "" : "#{function[:args_string]}, " call_string = function[:args].map{|m| m[:name]}.join(', ') @@ -43,13 +44,13 @@ class CMockGeneratorPluginCexception (function[:args_string] != "void") ? " ExpectParameters_#{function[:name]}(#{call_string});\n" : nil, "}\n\n" ].join end - + def mock_destroy(function) MOCK_DESTROY_SNIPPET % function[:name] end - + private ############ - + INSTANCE_STRUCTURE_SNIPPET = %q[ int *%1$s_ThrowOnCallCount; int *%1$s_ThrowOnCallCount_Head; @@ -63,7 +64,7 @@ class CMockGeneratorPluginCexception if ((Mock.%1$s_ThrowOnCallCount != Mock.%1$s_ThrowOnCallCount_Tail) && (Mock.%1$s_ThrowValue != Mock.%1$s_ThrowValue_Tail)) { - if (*Mock.%1$s_ThrowOnCallCount && + if (*Mock.%1$s_ThrowOnCallCount && (Mock.%1$s_CallCount == *Mock.%1$s_ThrowOnCallCount)) { EXCEPTION_T toThrow = *Mock.%1$s_ThrowValue; diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 8b4ce99..7eb629a 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -1,6 +1,7 @@ class CMockGeneratorPluginExpect + attr_reader :priority attr_accessor :config, :utils, :unity_helper, :ordered def initialize(config, utils) @@ -9,6 +10,7 @@ class CMockGeneratorPluginExpect @ordered = @config.enforce_strict_ordering @utils = utils @unity_helper = @utils.helpers[:unity_helper] + @priority = 5 end def instance_structure(function) diff --git a/lib/cmock_generator_plugin_ignore.rb b/lib/cmock_generator_plugin_ignore.rb index 64493d3..d17cd49 100644 --- a/lib/cmock_generator_plugin_ignore.rb +++ b/lib/cmock_generator_plugin_ignore.rb @@ -1,11 +1,13 @@ class CMockGeneratorPluginIgnore + attr_reader :priority attr_reader :config, :utils def initialize(config, utils) @config = config @utils = utils + @priority = 2 end def instance_structure(function) @@ -20,7 +22,7 @@ class CMockGeneratorPluginIgnore end end - def mock_implementation_prefix(function) + def mock_implementation(function) lines = " if (Mock.#{function[:name]}_IgnoreBool)\n {" if (function[:return_type] == "void") lines << "\n return;\n" diff --git a/lib/cmock_plugin_manager.rb b/lib/cmock_plugin_manager.rb index bde23e0..f82f55e 100644 --- a/lib/cmock_plugin_manager.rb +++ b/lib/cmock_plugin_manager.rb @@ -18,6 +18,7 @@ class CMockPluginManager raise "Unable to load plugin '#{plugin_name}'" end end + @plugins.sort! {|a,b| a.priority <=> b.priority } end def run(method, args=nil) diff --git a/test/system/test_interactions/stubs_with_callbacks.yml b/test/system/test_interactions/stubs_with_callbacks.yml new file mode 100644 index 0000000..1e0c6d4 --- /dev/null +++ b/test/system/test_interactions/stubs_with_callbacks.yml @@ -0,0 +1,207 @@ +--- +:cmock: + :plugins: + - :callback + +:systest: + :types: | + #define UINT32 unsigned int + + typedef signed int custom_type; + + :mockable: | + UINT32 foo(custom_type* a); + UINT32 bar(custom_type* b); + int baz(void); + void fuz(int* args, int num); + + :source: + :header: | + void function_a(int a, int b); + UINT32 function_b(void); + int function_c(void); + + :code: | + void function_a(int a, int b) + { + int args[6] = {0, 1, 2, 3, 5, 5}; + args[0] = a; + fuz(args, b); + } + + UINT32 function_b(void) + { + UINT32 sum = 0; + custom_type a = 0; + custom_type b = 0; + sum = foo(&a) + bar(&b); + return sum + a + b; + } + + int function_c(void) + { + return (baz() + baz() + baz()); + } + + :tests: + :common: | + void setUp(void) {} + void tearDown(void) {} + + UINT32 FooAndBarHelper(custom_type* data, int num) + { + num++; + *data = (custom_type)(num * 2); + return (*data * 2); + } + + int BazCallbackPointless(int num) + { + return num; + } + + int BazCallbackComplainsIfCalledMoreThanTwice(int num) + { + TEST_ASSERT_MESSAGE(num < 2, "Do Not Call Baz More Than Twice"); + return num; + } + + void FuzVerifier(int* args, int num_args, int num_calls) + { + int i; + TEST_ASSERT_MESSAGE(num_args < 5, "No More Than 5 Args Allowed"); + for (i = 0; i < num_args; i++) + { + TEST_ASSERT_EQUAL(num_calls + i, args[i]); + } + } + + :units: + - :pass: TRUE + :should: 'successfully exercise two simple ExpectAndReturn mock calls the normal way' + :code: | + test() + { + custom_type exp = 0; + foo_ExpectAndReturn(&exp, 10); + bar_ExpectAndReturn(&exp, 20); + TEST_ASSERT_EQUAL(30, function_b()); + } + + - :pass: FALSE + :should: 'successfully exercise two simple ExpectAndReturn mock calls and catch failure the normal way' + :code: | + test() + { + custom_type exp = 1; + foo_ExpectAndReturn(&exp, 10); + bar_ExpectAndReturn(&exp, 20); + TEST_ASSERT_EQUAL(30, function_b()); + } + + - :pass: TRUE + :should: 'successfully exercise using some basic callbacks' + :code: | + test() + { + foo_StubWithCallback((CMOCK_foo_CALLBACK)FooAndBarHelper); + bar_StubWithCallback((CMOCK_bar_CALLBACK)FooAndBarHelper); + TEST_ASSERT_EQUAL(12, function_b()); + } + + - :pass: FALSE + :should: 'successfully exercise using some basic callbacks and notice failures' + :code: | + test() + { + foo_StubWithCallback((CMOCK_foo_CALLBACK)FooAndBarHelper); + bar_StubWithCallback((CMOCK_bar_CALLBACK)FooAndBarHelper); + TEST_ASSERT_EQUAL(10, function_b()); + } + + - :pass: TRUE + :should: 'successfully exercise a callback with no arguments' + :code: | + test() + { + baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); + TEST_ASSERT_EQUAL(3, function_c()); + } + + - :pass: FALSE + :should: 'successfully throw a failure from within a callback function' + :code: | + test() + { + baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackComplainsIfCalledMoreThanTwice); + function_c(); + } + + - :pass: TRUE + :should: 'be usable for things like dynamically sized memory checking for passing conditions' + :code: | + test() + { + fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); + function_a(0, 4); + } + + - :pass: FALSE + :should: 'be usable for things like dynamically sized memory checking for failing conditions' + :code: | + test() + { + fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); + function_a(0, 5); + } + + - :pass: FALSE + :should: 'be usable for things like dynamically sized memory checking for failing conditions 2' + :code: | + test() + { + fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); + function_a(1, 4); + } + + - :pass: TRUE + :should: 'run them interlaced' + :code: | + test() + { + custom_type exp = 0; + foo_ExpectAndReturn(&exp, 10); + foo_ExpectAndReturn(&exp, 15); + bar_ExpectAndReturn(&exp, 20); + bar_ExpectAndReturn(&exp, 40); + fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); + baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); + + TEST_ASSERT_EQUAL(30, function_b()); + TEST_ASSERT_EQUAL(55, function_b()); + function_a(0, 4); + TEST_ASSERT_EQUAL(3, function_c()); + } + + - :pass: TRUE + :should: 'run them back to back' + :code: | + test() + { + custom_type exp = 0; + foo_ExpectAndReturn(&exp, 10); + bar_ExpectAndReturn(&exp, 20); + TEST_ASSERT_EQUAL(30, function_b()); + + foo_ExpectAndReturn(&exp, 15); + bar_ExpectAndReturn(&exp, 40); + TEST_ASSERT_EQUAL(55, function_b()); + + fuz_StubWithCallback((CMOCK_fuz_CALLBACK)FuzVerifier); + function_a(0, 4); + + baz_StubWithCallback((CMOCK_baz_CALLBACK)BazCallbackPointless); + TEST_ASSERT_EQUAL(3, function_c()); + } + +... diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 0f55132..88f03d0 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -25,10 +25,6 @@ class MockedPluginHelper return " #{@return_this}_#{name}(#{args}, #{rettype})" end - def mock_implementation_prefix(name, rettype) - return " Pre#{name}#{@return_this}.#{rettype}" - end - def mock_implementation(name, args) return " Mock#{name}#{@return_this}(#{args.join(", ")})" end @@ -326,14 +322,11 @@ class CMockGeneratorTest < Test::Unit::TestCase expected = [ "__inline ", "static bool SupaFunction(uint32 sandwiches, const char* named)\n", "{\n", - " PreSupaFunctionUno.bool", - " PreSupaFunctionDos.bool", " MockSupaFunctionUno(uint32 sandwiches, const char* named)", " MockSupaFunctionDos(uint32 sandwiches, const char* named)", " UtilsSupaFunction.bool", "}\n\n" ] - @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"]) @@ -354,14 +347,11 @@ class CMockGeneratorTest < Test::Unit::TestCase output = [] expected = [ "int SupaFunction(uint32 sandwiches, corn ...)\n", "{\n", - " PreSupaFunctionUno.int", - " PreSupaFunctionDos.int", " MockSupaFunctionUno(uint32 sandwiches)", " MockSupaFunctionDos(uint32 sandwiches)", " UtilsSupaFunction.int", "}\n\n" ] - @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"]) diff --git a/test/unit/cmock_generator_plugin_array_test.rb b/test/unit/cmock_generator_plugin_array_test.rb index 5d29ae6..f57f4cd 100644 --- a/test/unit/cmock_generator_plugin_array_test.rb +++ b/test/unit/cmock_generator_plugin_array_test.rb @@ -20,6 +20,7 @@ class CMockGeneratorPluginArrayTest < Test::Unit::TestCase assert_equal(@config, @cmock_generator_plugin_array.config) assert_equal(@utils, @cmock_generator_plugin_array.utils) assert_equal(nil, @cmock_generator_plugin_array.unity_helper) + assert_equal(8, @cmock_generator_plugin_array.priority) end should "not include any additional include files" do @@ -78,21 +79,9 @@ class CMockGeneratorPluginArrayTest < Test::Unit::TestCase returned = @cmock_generator_plugin_array.mock_function_declarations(function) assert_equal(expected, returned) end - - should "not require anything for implementation prefix" do - assert(!@cmock_generator_plugin_array.respond_to?(:mock_implementation_prefix)) - end - should "not have a mock function implementation for functions of style 'int* func(void)'" do - function = {:name => "Apple", :args => [], :return_type => "int*", :contains_ptr? => false} - returned = @cmock_generator_plugin_array.mock_implementation(function) - assert_nil(returned) - end - - should "not have a mock function implementation for functions containing pointers either (handled in expect)" do - function = {:name => "Apple", :args => [{ :type => 'int*', :name => 'sausage', :ptr? => true}], :return_type => "int*", :contains_ptr? => true} - returned = @cmock_generator_plugin_array.mock_implementation(function) - assert_nil(returned) + should "not have a mock function implementation" do + assert(!@cmock_generator_plugin_array.respond_to?(:mock_implementation)) end should "not have a mock interfaces for functions of style 'int* func(void)'" do diff --git a/test/unit/cmock_generator_plugin_callback_test.rb b/test/unit/cmock_generator_plugin_callback_test.rb new file mode 100644 index 0000000..a56faf9 --- /dev/null +++ b/test/unit/cmock_generator_plugin_callback_test.rb @@ -0,0 +1,144 @@ +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require 'cmock_generator_plugin_callback' + +class CMockGeneratorPluginCallbackTest < Test::Unit::TestCase + def setup + create_mocks :config, :utils + + @cmock_generator_plugin_callback = CMockGeneratorPluginCallback.new(@config, @utils) + end + + def teardown + end + + should "have set up internal accessors correctly on init" do + assert_equal(@config, @cmock_generator_plugin_callback.config) + assert_equal(@utils, @cmock_generator_plugin_callback.utils) + assert_equal(3, @cmock_generator_plugin_callback.priority) + end + + should "not include any additional include files" do + assert(!@cmock_generator_plugin_callback.respond_to?(:include_files)) + end + + should "add to control structure" do + function = {:name => "Oak", :args => [:type => "int*", :name => "blah", :ptr? => true], :return_type => "int*"} + expected = " CMOCK_Oak_CALLBACK Oak_CallbackFunctionPointer;\n" + returned = @cmock_generator_plugin_callback.instance_structure(function) + assert_equal(expected, returned) + end + + should "add mock function declaration for function without arguments" do + function = {:name => "Maple", :args_string => "void", :return_type => "void"} + expected = [ "\n", + "typedef void (* CMOCK_Maple_CALLBACK)(int NumCalls);\n", + "void Maple_StubWithCallback(CMOCK_Maple_CALLBACK Callback);\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + should "add mock function declaration for function with arguments" do + function = {:name => "Maple", :args_string => "int* tofu", :return_type => "void"} + expected = [ "\n", + "typedef void (* CMOCK_Maple_CALLBACK)(int* tofu, int NumCalls);\n", + "void Maple_StubWithCallback(CMOCK_Maple_CALLBACK Callback);\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + should "add mock function declaration for function with return values" do + function = {:name => "Maple", :args_string => "int* tofu", :return_type => "char*"} + expected = [ "\n", + "typedef char* (* CMOCK_Maple_CALLBACK)(int* tofu, int NumCalls);\n", + "void Maple_StubWithCallback(CMOCK_Maple_CALLBACK Callback);\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + should "add mock function implementation for functions of style 'void func(void)'" do + function = {:name => "Apple", :args => [], :args_string => "void", :return_type => "void"} + expected = ["\n", + " if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallsExpected++;\n", + " Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallCount++);\n", + " return;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + should "add mock function implementation for functions of style 'int func(void)'" do + function = {:name => "Apple", :args => [], :args_string => "void", :return_type => "int"} + expected = ["\n", + " if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallsExpected++;\n", + " return Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallCount++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + should "add mock function implementation for functions of style 'void func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return_type => "void"} + expected = ["\n", + " if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallsExpected++;\n", + " Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallCount++);\n", + " return;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + should "add mock function implementation for functions of style 'int16_t func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return_type => "int16_t"} + expected = ["\n", + " if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallsExpected++;\n", + " return Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallCount++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + should "add mock interfaces for functions " do + function = {:name => "Lemon", + :args => [{ :type => "char*", :name => "pescado"}], + :args_string => "char* pescado", + :return_type => "int", + :return_string => "int toReturn" } + + expected = ["\n", + "void Lemon_StubWithCallback(CMOCK_Lemon_CALLBACK Callback)\n", + "{\n", + " Mock.Lemon_CallbackFunctionPointer = Callback;\n", + "}\n" + ].join + returned = @cmock_generator_plugin_callback.mock_interfaces(function) + assert_equal(expected, returned) + end + + should "add mock destroy for functions" do + function = {:name => "Peach", :args => [], :return_type => "void" } + expected = ["\n", + " Mock.Peach_CallbackFunctionPointer = NULL;\n" ].join + returned = @cmock_generator_plugin_callback.mock_destroy(function) + assert_equal(expected, returned) + end +end diff --git a/test/unit/cmock_generator_plugin_cexception_test.rb b/test/unit/cmock_generator_plugin_cexception_test.rb index 6cebfbd..26ffa0f 100644 --- a/test/unit/cmock_generator_plugin_cexception_test.rb +++ b/test/unit/cmock_generator_plugin_cexception_test.rb @@ -14,6 +14,7 @@ class CMockGeneratorPluginCexceptionTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_plugin_cexception.config) assert_equal(@utils, @cmock_generator_plugin_cexception.utils) + assert_equal(7, @cmock_generator_plugin_cexception.priority) end should "include the cexception library" do @@ -58,17 +59,13 @@ class CMockGeneratorPluginCexceptionTest < Test::Unit::TestCase assert_equal(expected, returned) end - should "add nothing during implementation prefix" do - assert(!@cmock_generator_plugin_cexception.respond_to?(:mock_implementation_prefix)) - end - should "add a mock implementation" do function = {:name => "Cherry", :args => [], :return_type => "void"} expected = ["\n", " if ((Mock.Cherry_ThrowOnCallCount != Mock.Cherry_ThrowOnCallCount_Tail) &&\n", " (Mock.Cherry_ThrowValue != Mock.Cherry_ThrowValue_Tail))\n", " {\n", - " if (*Mock.Cherry_ThrowOnCallCount && \n", + " if (*Mock.Cherry_ThrowOnCallCount &&\n", " (Mock.Cherry_CallCount == *Mock.Cherry_ThrowOnCallCount))\n", " {\n", " EXCEPTION_T toThrow = *Mock.Cherry_ThrowValue;\n", diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index 64bf652..32cc337 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -27,6 +27,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase assert_equal(@config, @cmock_generator_plugin_expect.config) assert_equal(@utils, @cmock_generator_plugin_expect.utils) assert_equal(nil, @cmock_generator_plugin_expect.unity_helper) + assert_equal(5, @cmock_generator_plugin_expect.priority) end should "not include any additional include files" do @@ -129,10 +130,6 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase returned = @cmock_generator_plugin_expect.mock_function_declarations(function) assert_equal(expected, returned) end - - should "not require anything for implementation prefix" do - assert(!@cmock_generator_plugin_expect.respond_to?(:mock_implementation_prefix)) - end should "add mock function implementation for functions of style 'void func(void)'" do function = {:name => "Apple", :args => [], :return_type => "void"} diff --git a/test/unit/cmock_generator_plugin_ignore_test.rb b/test/unit/cmock_generator_plugin_ignore_test.rb index d897f00..7e5d962 100644 --- a/test/unit/cmock_generator_plugin_ignore_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_test.rb @@ -14,6 +14,7 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase should "have set up internal accessors correctly on init" do assert_equal(@config, @cmock_generator_plugin_ignore.config) assert_equal(@utils, @cmock_generator_plugin_ignore.utils) + assert_equal(2, @cmock_generator_plugin_ignore.priority) end should "not have any additional include file requirements" do @@ -41,18 +42,18 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase assert_equal(expected, returned) end - should "add required code to implementation prefix with void function" do + should "add required code to implementation with void function" do function = {:name => "Mold", :args_string => "void", :return_type => "void"} expected = [" if (Mock.Mold_IgnoreBool)\n", " {\n", " return;\n", " }\n" ].join - returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function) + returned = @cmock_generator_plugin_ignore.mock_implementation(function) assert_equal(expected, returned) end - should "add required code to implementation prefix with return functions" do + should "add required code to implementation with return functions" do function = {:name => "Fungus", :args_string => "void", :return_type => "int"} expected = [" if (Mock.Fungus_IgnoreBool)\n", " {\n", @@ -70,14 +71,10 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase " }\n", " }\n" ].join - returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function) + returned = @cmock_generator_plugin_ignore.mock_implementation(function) assert_equal(expected, returned) end - should "have nothing new for mock implementation" do - assert(!@cmock_generator_plugin_ignore.respond_to?(:mock_implementation)) - end - should "add a new mock interface for ignoring when function had no return value" do function = {:name => "Slime", :args => [], :args_string => "void", :return_type => "void"} expected = ["\n",