Fix handling of string arguments.

Since 2f0a44d7ce, string arguments were being tested with
UNITY_TEST_ASSERT_EQUAL_PTR, not UNITY_TEST_ASSERT_EQUAL_STRING.
Before that change, we would call divine_ptr(arg_type) where
arg_type had the argument name stripped, i.e. "const char *".
Now we are passing it the name as well, i.e. "const char *str"
and it was not prepared to handle that.
This commit is contained in:
John Lindgren
2017-08-31 17:44:08 -04:00
parent 693c658780
commit 42dab4836d
2 changed files with 39 additions and 7 deletions
+5 -4
View File
@@ -146,16 +146,17 @@ class CMockHeaderParser
return args if (arg =~ /^\s*((\.\.\.)|(void))\s*$/) # we're done if we reach void by itself or ...
arg_array = arg.split
arg_elements = arg_array - @c_attributes # split up words and remove known attributes
args << { :type => (arg_type = arg_elements[0..-2].join(' ')),
args << { :type => arg_elements[0..-2].join(' '),
:name => arg_elements[-1]
}.merge(divine_ptr_and_const(arg))
end
return args
end
def divine_ptr(arg_type)
return false unless arg_type.include? '*'
return false if arg_type.gsub(/(const|char|\*|\s)+/,'').empty?
def divine_ptr(arg)
return false unless arg.include? '*'
# treat "const char *" and similar as a string, not a pointer
return false if /(^|\s)(const\s+)?char(\s+const)?\s*\*(?!.*\*)/ =~ arg
return true
end
+34 -3
View File
@@ -1052,7 +1052,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
assert_equal(expected, result[:functions])
end
it "handle arrays and treat them as pointers" do
it "handle arrays and treat them as pointers or strings" do
source = "void KeyOperated(CUSTOM_TYPE thing1[], int thing2 [ ], char thing3 [][2 ][ 3], int* thing4[4])"
expected = [{:var_arg=>nil,
:return=>{ :type => "void",
@@ -1068,7 +1068,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
:contains_ptr? => true,
:args=>[ {:type=>"CUSTOM_TYPE*", :name=>"thing1", :ptr? => true, :const? => false, :const_ptr? => false},
{:type=>"int*", :name=>"thing2", :ptr? => true, :const? => false, :const_ptr? => false},
{:type=>"char*", :name=>"thing3", :ptr? => true, :const? => false, :const_ptr? => false}, #THIS one will likely change in the future when we improve multidimensional array support
{:type=>"char*", :name=>"thing3", :ptr? => false, :const? => false, :const_ptr? => false}, #THIS one will likely change in the future when we improve multidimensional array support
{:type=>"int**", :name=>"thing4", :ptr? => true, :const? => false, :const_ptr? => false} #THIS one will likely change in the future when we improve multidimensional array support
],
:args_string=>"CUSTOM_TYPE* thing1, int* thing2, char* thing3, int** thing4",
@@ -1407,7 +1407,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
:contains_ptr? => true,
:args=>[ {:type=>"sqlite3_stmt*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false},
{:type=>"int", :name=>"cmock_arg3", :ptr? => false, :const? => false, :const_ptr? => false},
{:type=>"char*", :name=>"cmock_arg4", :ptr? => true, :const? => true, :const_ptr? => false},
{:type=>"char*", :name=>"cmock_arg4", :ptr? => false, :const? => true, :const_ptr? => false},
{:type=>"int", :name=>"n", :ptr? => false, :const? => false, :const_ptr? => false},
{:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false}
],
@@ -1544,4 +1544,35 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
end
end
it "divines ptr correctly for string types" do
truth_table = [
# argument ptr
[ "char s", false ],
[ "const char s", false ],
[ "char const s", false ],
[ "char *s", false ],
[ "const char *s", false ],
[ "char const *s", false ],
[ "char *const s", false ],
[ "const char *const s", false ],
[ "char const *const s", false ],
[ "char **s", true ],
[ "const char **s", true ],
[ "char const **s", true ],
[ "char *const *s", true ],
[ "const char *const *s", true ],
[ "char const *const *s", true ],
[ "char **const s", true ],
[ "const char **const s", true ],
[ "char const **const s", true ],
[ "char *const *const s", true ],
[ "const char *const *const s", true ],
[ "char const *const *const s", true ]
]
truth_table.each do |entry|
assert_equal(@parser.divine_ptr(entry[0]), entry[1])
end
end
end