diff --git a/lib/cmock_generator_plugin_return_thru_ptr.rb b/lib/cmock_generator_plugin_return_thru_ptr.rb index 4a72265..d5e967c 100644 --- a/lib/cmock_generator_plugin_return_thru_ptr.rb +++ b/lib/cmock_generator_plugin_return_thru_ptr.rb @@ -4,7 +4,7 @@ class CMockGeneratorPluginReturnThruPtr def initialize(config, utils) @utils = utils - @priority = 9 + @priority = 1 end def instance_typedefs(function) diff --git a/test/system/test_interactions/return_thru_ptr_and_expect_any_args.yml b/test/system/test_interactions/return_thru_ptr_and_expect_any_args.yml new file mode 100644 index 0000000..031df9f --- /dev/null +++ b/test/system/test_interactions/return_thru_ptr_and_expect_any_args.yml @@ -0,0 +1,235 @@ +--- +:cmock: + :mock_path: test/mocks + :mock_prefix: mock_ + :treat_as: + abs_struct: PTR + intptr: INT* + :when_ptr: :smart + :plugins: + - :array + - :ignore_arg + - :return_thru_ptr + +:systest: + :types: | + typedef int *intptr; + + struct a_struct + { + int i1; + int i2; + int i3; + }; + + struct _abs_struct + { + int abs_i1; + int abs_i2; + }; + + typedef struct _abs_struct abs_struct; + + :mockable: | + void ptr_ret_int(int *r); + void ptr_ret_ints(int *r, int *s); + void ptr_ret_array(char r[], int len); + void ptr_ret_typedef(intptr r); + void ptr_ret_struct(struct a_struct *r); + void ptr_ret_abstract(abs_struct *r); + void ptr_ret_abstract_array(abs_struct *r, int len); + void ptr_ret_const_int(int *r, const int *s); + void ptr_ret_string(char *s); + + :source: + :header: | + #include + #define lengthof(x) (sizeof(x)/sizeof((x)[0])) + + :code: | + + :tests: + :common: | + void setUp(void) {} + void tearDown(void) {} + + :units: + - :pass: TRUE + :should: "handle a single int* argument" + :code: | + test() + { + int r = 1; + int res = 4; + + ptr_ret_int_Expect(&r); + ptr_ret_int_ReturnThruPtr_r(&res); + ptr_ret_int(&r); + TEST_ASSERT_EQUAL(4, r); + } + + - :pass: TRUE + :should: "handle multiple calls" + :code: | + test() + { + int r = 1; + int res1 = 4; + int res2 = 8; + int res3 = 16; + + ptr_ret_int_Expect(&r); + ptr_ret_int_ReturnThruPtr_r(&res1); + ptr_ret_int_Expect(&r); + ptr_ret_int_ReturnThruPtr_r(&res2); + ptr_ret_int_Expect(&r); + ptr_ret_int_ReturnThruPtr_r(&res3); + + ptr_ret_int(&r); + TEST_ASSERT_EQUAL(4, r); + ptr_ret_int(&r); + TEST_ASSERT_EQUAL(8, r); + ptr_ret_int(&r); + TEST_ASSERT_EQUAL(16, r); + + } + + - :pass: TRUE + :should: "ignore an argument" + :code: | + test() + { + int r = 1, s = 2; + int res = 4; + + ptr_ret_int_Expect(&r); + ptr_ret_int_IgnoreArg_r(); + ptr_ret_int_ReturnThruPtr_r(&res); + ptr_ret_int(&s); + TEST_ASSERT_EQUAL(4, s); + } + + - :pass: TRUE + :should: "ignore a null pointer argument" + :code: | + test() + { + int r = 1; + int res = 4; + + ptr_ret_int_Expect(NULL); + ptr_ret_int_IgnoreArg_r(); + ptr_ret_int_ReturnThruPtr_r(&res); + ptr_ret_int(&r); + TEST_ASSERT_EQUAL(4, r); + } + + - :pass: TRUE + :should: "handle multiple int* arguments" + :code: | + test() + { + int r, s = 0x0880AA55; + int r_res = 4; + int s_res = 6; + + ptr_ret_ints_Expect(&r, &s); + ptr_ret_ints_ReturnThruPtr_r(&r_res); + ptr_ret_ints_ReturnThruPtr_s(&s_res); + ptr_ret_ints(&r, &s); + TEST_ASSERT_EQUAL(4, r); + TEST_ASSERT_EQUAL(6, s); + } + + - :pass: TRUE + :should: "only return through pointer when asked to" + :code: | + test() + { + int r = 0x0880AA55; + int s = 0xAA55; + int r_res = 4; + + ptr_ret_ints_Expect(&r, &s); + ptr_ret_ints_ReturnThruPtr_r(&r_res); + ptr_ret_ints(&r, &s); + TEST_ASSERT_EQUAL(4, r); + TEST_ASSERT_EQUAL(0xAA55, s); + } + + - :pass: TRUE + :should: "return an array through a pointer correctly" + :code: | + test() + { + char r_a[] = "booboorooboo"; + char r_a_ret[] = "FEEFI"; + + ptr_ret_array_Expect(r_a, lengthof(r_a)); + ptr_ret_array_ReturnArrayThruPtr_r(r_a_ret, strlen(r_a_ret)); + ptr_ret_array(r_a, lengthof(r_a)); + TEST_ASSERT_EQUAL_STRING("FEEFIorooboo", r_a); + } + + - :pass: TRUE + :should: "handle structs" + :code: | + test() + { + struct a_struct r_s = { .i1 = 2, .i2 = 3, .i3 = 4, }; + struct a_struct r_s_ret = { .i1 = 8, .i2 = 16, .i3 = 32, }; + + ptr_ret_struct_Expect(&r_s); + ptr_ret_struct_ReturnThruPtr_r(&r_s_ret); + ptr_ret_struct(&r_s); + TEST_ASSERT_EQUAL_MEMORY(&r_s_ret, &r_s, sizeof(struct a_struct)); + } + + - :pass: TRUE + :should: "handle typedefs" + :code: | + test() + { + abs_struct r_as = {.abs_i1 = 0x1234, .abs_i2 = 0x4567}; + abs_struct r_as_ret = {.abs_i1 = 0xFFAA55, .abs_i2 = 0xAAFFAA}; + ptr_ret_abstract_Expect(&r_as); + ptr_ret_abstract_ReturnMemThruPtr_r(&r_as_ret, sizeof(abs_struct)); + ptr_ret_abstract(&r_as); + TEST_ASSERT_EQUAL_MEMORY(&r_as_ret, &r_as, sizeof(abs_struct)); + } + + - :pass: TRUE + :should: "only generate ReturnThruPtr definitions for non-const arguments" + :code: | + test() + { + #if !defined(ptr_ret_const_int_ReturnThruPtr_r) + TEST_FAIL_MESSAGE("ReturnThruPtr not defined for a pointer argument."); + #endif + + #if defined(ptr_ret_const_int_ReturnThruPtr_s) + TEST_FAIL_MESSAGE("ReturnThruPtr defined for a const pointer argument."); + #endif + } + + - :pass: TRUE + :should: "generate ReturnThruPtr definitions for string arguments" + :code: | + test() + { + #if !defined(ptr_ret_string_ReturnThruPtr_s) + TEST_FAIL_MESSAGE("ReturnThruPtr not defined for a string argument."); + #endif + } + + - :pass: TRUE + :should: "generate IgnoreArg definitions" + :code: | + test() + { + #if !defined(ptr_ret_array_IgnoreArg_r) \ + || !defined(ptr_ret_array_IgnoreArg_len) \ + || !defined(ptr_ret_const_int_IgnoreArg_s) + TEST_FAIL_MESSAGE("IgnoreArg not defined for an argument."); + #endif + } diff --git a/test/system/test_interactions/return_thru_ptr_ignore_arg.yml b/test/system/test_interactions/return_thru_ptr_ignore_arg.yml index 031df9f..8ed246b 100644 --- a/test/system/test_interactions/return_thru_ptr_ignore_arg.yml +++ b/test/system/test_interactions/return_thru_ptr_ignore_arg.yml @@ -8,7 +8,7 @@ :when_ptr: :smart :plugins: - :array - - :ignore_arg + - :expect_any_args - :return_thru_ptr :systest: @@ -21,13 +21,13 @@ int i2; int i3; }; - - struct _abs_struct + + struct _abs_struct { int abs_i1; int abs_i2; }; - + typedef struct _abs_struct abs_struct; :mockable: | @@ -61,7 +61,7 @@ { int r = 1; int res = 4; - + ptr_ret_int_Expect(&r); ptr_ret_int_ReturnThruPtr_r(&res); ptr_ret_int(&r); @@ -77,21 +77,21 @@ int res1 = 4; int res2 = 8; int res3 = 16; - + ptr_ret_int_Expect(&r); ptr_ret_int_ReturnThruPtr_r(&res1); ptr_ret_int_Expect(&r); ptr_ret_int_ReturnThruPtr_r(&res2); ptr_ret_int_Expect(&r); ptr_ret_int_ReturnThruPtr_r(&res3); - + ptr_ret_int(&r); TEST_ASSERT_EQUAL(4, r); ptr_ret_int(&r); TEST_ASSERT_EQUAL(8, r); ptr_ret_int(&r); TEST_ASSERT_EQUAL(16, r); - + } - :pass: TRUE @@ -99,11 +99,10 @@ :code: | test() { - int r = 1, s = 2; + int s = 2; int res = 4; - - ptr_ret_int_Expect(&r); - ptr_ret_int_IgnoreArg_r(); + + ptr_ret_int_ExpectAnyArgs(); ptr_ret_int_ReturnThruPtr_r(&res); ptr_ret_int(&s); TEST_ASSERT_EQUAL(4, s); @@ -116,9 +115,8 @@ { int r = 1; int res = 4; - - ptr_ret_int_Expect(NULL); - ptr_ret_int_IgnoreArg_r(); + + ptr_ret_int_ExpectAnyArgs(); ptr_ret_int_ReturnThruPtr_r(&res); ptr_ret_int(&r); TEST_ASSERT_EQUAL(4, r); @@ -132,7 +130,7 @@ int r, s = 0x0880AA55; int r_res = 4; int s_res = 6; - + ptr_ret_ints_Expect(&r, &s); ptr_ret_ints_ReturnThruPtr_r(&r_res); ptr_ret_ints_ReturnThruPtr_s(&s_res); @@ -149,7 +147,7 @@ int r = 0x0880AA55; int s = 0xAA55; int r_res = 4; - + ptr_ret_ints_Expect(&r, &s); ptr_ret_ints_ReturnThruPtr_r(&r_res); ptr_ret_ints(&r, &s); @@ -164,7 +162,7 @@ { char r_a[] = "booboorooboo"; char r_a_ret[] = "FEEFI"; - + ptr_ret_array_Expect(r_a, lengthof(r_a)); ptr_ret_array_ReturnArrayThruPtr_r(r_a_ret, strlen(r_a_ret)); ptr_ret_array(r_a, lengthof(r_a)); @@ -178,7 +176,7 @@ { struct a_struct r_s = { .i1 = 2, .i2 = 3, .i3 = 4, }; struct a_struct r_s_ret = { .i1 = 8, .i2 = 16, .i3 = 32, }; - + ptr_ret_struct_Expect(&r_s); ptr_ret_struct_ReturnThruPtr_r(&r_s_ret); ptr_ret_struct(&r_s); @@ -206,7 +204,7 @@ #if !defined(ptr_ret_const_int_ReturnThruPtr_r) TEST_FAIL_MESSAGE("ReturnThruPtr not defined for a pointer argument."); #endif - + #if defined(ptr_ret_const_int_ReturnThruPtr_s) TEST_FAIL_MESSAGE("ReturnThruPtr defined for a const pointer argument."); #endif @@ -223,13 +221,11 @@ } - :pass: TRUE - :should: "generate IgnoreArg definitions" + :should: "generate ExpectAnyArgs definitions" :code: | test() { - #if !defined(ptr_ret_array_IgnoreArg_r) \ - || !defined(ptr_ret_array_IgnoreArg_len) \ - || !defined(ptr_ret_const_int_IgnoreArg_s) - TEST_FAIL_MESSAGE("IgnoreArg not defined for an argument."); + #if !defined(ptr_ret_array_ExpectAnyArgs) + TEST_FAIL_MESSAGE("ExpectAnyArgs not defined for an argument."); #endif } diff --git a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb index db97701..1a1def0 100644 --- a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb +++ b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb @@ -2,7 +2,7 @@ # CMock Project - Automatic Mock Generation for C # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams # [Released under MIT License. Please refer to license.txt for details] -# ========================================== +# ========================================== require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" require 'cmock_generator_plugin_return_thru_ptr' @@ -21,7 +21,7 @@ class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase :contains_ptr? => false} # void Pine(int chicken, const int beef, int *tofu) - @complex_func = {:name => "Pine", + @complex_func = {:name => "Pine", :args => [{ :type => "int", :name => "chicken", :ptr? => false, @@ -35,7 +35,7 @@ class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase :name => "tofu", :ptr? => true, }], - :return => test_return[:void], + :return => test_return[:void], :contains_ptr? => true } #no strict ordering @@ -54,13 +54,13 @@ class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase @utils.expect.ptr_or_str?('int*').returns(true) @utils.expect.ptr_or_str?('int*').returns(true) end - + should "have set up internal accessors correctly on init" do assert_equal(@utils, @cmock_generator_plugin_return_thru_ptr.utils) - assert_equal(9, @cmock_generator_plugin_return_thru_ptr.priority) + assert_equal(1, @cmock_generator_plugin_return_thru_ptr.priority) end - - should "not include any additional include files" do + + should "not include any additional include files" do assert(!@cmock_generator_plugin_return_thru_ptr.respond_to?(:include_files)) end @@ -68,7 +68,7 @@ class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@void_func) assert_equal("", returned) end - + should "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do complex_func_expect() expected = " int ReturnThruPtr_tofu_Used;\n" + @@ -86,7 +86,7 @@ class CMockGeneratorPluginReturnThruPtrTest < Test::Unit::TestCase should "add a mock function declaration only for non-const pointer arguments" do complex_func_expect(); - + expected = "#define Pine_ReturnThruPtr_tofu(tofu)" + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, sizeof(*tofu))\n" +