- fixed an error in pointer handling. beefed up tests

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@142 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2009-08-27 23:07:07 +00:00
parent 1ed579f206
commit a875949914
5 changed files with 204 additions and 4 deletions
+3 -1
View File
@@ -59,7 +59,9 @@ class CMockGeneratorUtils
case(unity_func)
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE"
full_expected = (expected =~ /^\*/) ? expected.slice(1..-1) : "&(#{expected})"
return " #{unity_func}((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n"
return " TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n"
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY"
return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(#{expected}), (void*)#{arg}, sizeof(#{c_type.sub('*','')})#{unity_msg}); }\n"
when /_ARRAY/
return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { #{unity_func}(#{expected}, #{arg}, 1#{unity_msg}); }\n"
else
+6 -2
View File
@@ -8,10 +8,14 @@ class CMockUnityHelperParser
end
def get_helper(ctype)
lookup = ctype.gsub(/const\s+/,'').strip.gsub(/\s+/,'_')
lookup = ctype.gsub(/(?:^|(\S?)(\s*)|(\W))const(?:$|(\s*)(\S)|(\W))/,'\1\3\5\6').strip.gsub(/\s+/,'_')
return @c_types[lookup] if (@c_types[lookup])
raise("Don't know how to test #{ctype} and memory tests are disabled!") unless @config.memcmp_if_unknown
return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE'
if (ctype =~ /\*/)
return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY'
else
return 'TEST_ASSERT_EQUAL_MEMORY_MESSAGE'
end
end
private ###########################
@@ -0,0 +1,166 @@
---
:cmock:
:plugins:
- # none
:systest:
:types: |
typedef struct _POINT_T {
int x;
int y;
} POINT_T;
:mockable: |
void foo(POINT_T* a);
POINT_T* bar(void);
void fooa(POINT_T a[]);
void foos(const char const * a);
const char const * bars(void);
:source:
:header: |
void function_a(void);
void function_b(void);
:code: |
void function_a(void)
{
foo(bar());
}
void function_b(void) {
fooa(bar());
}
void function_c(void) {
foos(bars());
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
:units:
- :pass: TRUE
:should: 'handle the situation where we pass nulls to pointers'
:code: |
test()
{
bar_ExpectAndReturn(NULL);
foo_Expect(NULL);
function_a();
}
- :pass: TRUE
:should: 'handle the situation where we pass single object with expect'
:code: |
test()
{
POINT_T pt = {1, 2};
POINT_T ex = {1, 2};
bar_ExpectAndReturn(&pt);
foo_Expect(&ex);
function_a();
}
- :pass: FALSE
:should: 'handle the situation where we pass single object with expect and it is wrong'
:code: |
test()
{
POINT_T pt = {1, 2};
POINT_T ex = {1, 3};
bar_ExpectAndReturn(&pt);
foo_Expect(&ex);
function_a();
}
- :pass: FALSE
:should: 'handle the situation where we pass nulls to pointers and fail'
:code: |
test()
{
POINT_T pt = {1, 2};
bar_ExpectAndReturn(&pt);
foo_Expect(NULL);
function_a();
}
- :pass: TRUE
:should: 'handle the situation where we pass nulls to arrays'
:code: |
test()
{
bar_ExpectAndReturn(NULL);
fooa_Expect(NULL);
function_b();
}
- :pass: TRUE
:should: 'handle the situation where we pass single array element with expect'
:code: |
test()
{
POINT_T pt = {1, 2};
POINT_T ex = {1, 2};
bar_ExpectAndReturn(&pt);
fooa_Expect(&ex);
function_b();
}
- :pass: FALSE
:should: 'handle the situation where we pass single array element with expect and it is wrong'
:code: |
test()
{
POINT_T pt = {1, 2};
POINT_T ex = {1, 3};
bar_ExpectAndReturn(&pt);
fooa_Expect(&ex);
function_b();
}
- :pass: FALSE
:should: 'handle the situation where we pass nulls to arrays and fail'
:code: |
test()
{
POINT_T pt = {1, 2};
bar_ExpectAndReturn(&pt);
fooa_Expect(NULL);
function_b();
}
- :pass: TRUE
:should: 'handle standard c string as null terminated on not do crappy memory compares of a byte, passing'
:code: |
test()
{
bars_ExpectAndReturn("This is a\0 silly string");
foos_Expect("This is a\0 wacky string");
function_c();
}
- :pass: FALSE
:should: 'handle standard c string as null terminated on not do crappy memory compares of a byte, finding failures'
:code: |
test()
{
bars_ExpectAndReturn("This is a silly string");
foos_Expect("This is a wacky string");
function_c();
}
...
+23
View File
@@ -245,6 +245,29 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
should "make handle default types with memory compares and arrays, which involves extra work" do
function = { :name => "Toaster", :return_type => "uint64"}
var_type = "SOME_STRUCT*"
var_name = "Bread"
@cmock_generator_utils.helpers = {:unity_helper => @unity_helper}
@unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY")
expected = ["\n",
" if (Mock.Toaster_Expected_Bread != Mock.Toaster_Expected_Bread_Tail)\n",
" {\n",
" SOME_STRUCT** p_expected = Mock.Toaster_Expected_Bread;\n",
" Mock.Toaster_Expected_Bread++;\n",
" if (*p_expected == NULL)\n",
" { TEST_ASSERT_NULL(Bread); }\n",
" else\n",
" { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(*p_expected), (void*)Bread, sizeof(SOME_STRUCT), \"Function 'Toaster' called with unexpected value for argument 'Bread'.\"); }\n",
" }\n"
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
should "make handle default types with array compares, which involves extra work" do
function = { :name => "Blender", :return_type => "uint16*"}
+6 -1
View File
@@ -170,10 +170,15 @@ class CMockUnityHelperParserTest < Test::Unit::TestCase
'SPINACH' => "TEST_ASSERT_EQUAL_SPINACH",
}
["UINT16","UINT8*","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype|
["UINT16","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype|
@config.expect.memcmp_if_unknown.returns(true)
assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE", @parser.get_helper(ctype))
end
["UINT8*","SPINACH_T*"].each do |ctype|
@config.expect.memcmp_if_unknown.returns(true)
assert_equal("TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY", @parser.get_helper(ctype))
end
end
should "raise error when asked to fetch helper of type not on my list and not allowed to mem check" do