- handles pointers (but not yet arrays)

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@63 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2009-02-22 00:06:43 +00:00
parent 795d23bad2
commit 868e951c0e
7 changed files with 118 additions and 11 deletions
+15
View File
@@ -25,3 +25,18 @@ bool AdcConductor_JustHereToTest(void)
return AdcModel_DoNothingExceptTestASpecialType(ExampleStruct);
}
bool AdcConductor_AlsoHereToTest(void)
{
EXAMPLE_STRUCT_T example = AdcModel_DoNothingExceptReturnASpecialType();
return ((example.x == 99) && (example.y == 1));
}
bool AdcConductor_YetAnotherTest(void)
{
uint32 example = 3;
return AdModel_DoNothingExceptTestPointers(&example);
}
+2
View File
@@ -5,5 +5,7 @@ void AdcConductor_Init(void);
void AdcConductor_Run(void);
bool AdcConductor_JustHereToTest(void);
bool AdcConductor_AlsoHereToTest(void);
bool AdcConductor_YetAnotherTest(void);
#endif // _ADCCONDUCTOR_H
+12
View File
@@ -18,3 +18,15 @@ bool AdcModel_DoNothingExceptTestASpecialType(EXAMPLE_STRUCT_T ExampleStruct)
{
//This doesn't really do anything. it's only here to make sure I can compare a struct.
}
bool AdModel_DoNothingExceptTestPointers(uint32* pExample)
{
//This doesn't really do anything. it's only here to make sure I can compare a pointer value.
}
EXAMPLE_STRUCT_T AdcModel_DoNothingExceptReturnASpecialType(void)
{
EXAMPLE_STRUCT_T example; //again, this just is here to test that I can return a struct
example.x = 99;
example.y = 1;
return example;
}
+2
View File
@@ -5,5 +5,7 @@ bool AdcModel_DoGetSample(void);
void AdcModel_ProcessInput(uint16 millivolts);
bool AdcModel_DoNothingExceptTestASpecialType(EXAMPLE_STRUCT_T ExampleStruct);
bool AdModel_DoNothingExceptTestPointers(uint32* pExample);
EXAMPLE_STRUCT_T AdcModel_DoNothingExceptReturnASpecialType(void);
#endif // _ADCMODEL_H
+40
View File
@@ -79,3 +79,43 @@ void testJustHereToTest_Should_ProperlyPassAStructAndVerifyIt(void)
// TEST_ASSERT_TRUE(AdcConductor_JustHereToTest());
//}
void test_AdcConductor_AlsoHereToTest_Should_ProperlyReturnAStructAsExpected1(void)
{
EXAMPLE_STRUCT_T TestStruct;
TestStruct.x = 99;
TestStruct.y = 1;
AdcModel_DoNothingExceptReturnASpecialType_ExpectAndReturn(TestStruct);
TEST_ASSERT_TRUE(AdcConductor_AlsoHereToTest());
}
void test_AdcConductor_AlsoHereToTest_Should_ProperlyReturnAStructAsExpected2(void)
{
EXAMPLE_STRUCT_T TestStruct;
TestStruct.x = 98;
TestStruct.y = 1;
AdcModel_DoNothingExceptReturnASpecialType_ExpectAndReturn(TestStruct);
TEST_ASSERT_FALSE(AdcConductor_AlsoHereToTest());
}
void test_AdcConductor_YetAnotherTest_Should_VerifyThatPointersToStructsAreTestable(void)
{
uint32 TestNum = 3;
AdModel_DoNothingExceptTestPointers_ExpectAndReturn(&TestNum, TRUE);
TEST_ASSERT_TRUE(AdcConductor_YetAnotherTest());
}
//void test_AdcConductor_YetAnotherTest_Should_FailIfYouUncommentThisTestBecauseTheValuePointedToIsWrong(void)
//{
// uint32 TestNum = 7;
//
// AdModel_DoNothingExceptTestPointers_ExpectAndReturn(&TestNum, FALSE);
//
// TEST_ASSERT_FALSE(AdcConductor_YetAnotherTest());
//}
+20 -9
View File
@@ -6,6 +6,7 @@ class CMockGeneratorUtils
def initialize(config, helpers={})
@config = config
@tab = @config.tab
@ptr_handling = @config.when_ptr_star
@helpers = helpers
end
@@ -67,24 +68,34 @@ class CMockGeneratorUtils
end
def code_verify_an_arg_expectation(function, arg_type, actual)
expect = expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for parameter '#{actual}'.\"")
lines = ["\n"]
lines << "#{@tab}if (Mock.#{function[:name]}_Expected_#{actual} != Mock.#{function[:name]}_Expected_#{actual}_HeadTail)\n"
lines << "#{@tab}{\n"
lines << "#{@tab}#{@tab}#{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n"
lines << "#{@tab}#{@tab}Mock.#{function[:name]}_Expected_#{actual}++;\n"
lines << "#{@tab}#{@tab}#{expect};\n"
lines << expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for parameter '#{actual}'.\"","#{@tab}#{@tab}")
lines << "#{@tab}}\n"
lines.flatten
end
def expect_helper(c_type, expected, actual, msg)
unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type)
unity_msg = (unity_func =~ /_MESSAGE/) ? ", #{msg}" : ''
if (unity_func == "TEST_ASSERT_EQUAL_MEMORY_MESSAGE")
full_expected = (expected.strip[0] == 42) ? expected.slice(1..-1) : "&(#{expected})"
return "#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg})"
def expect_helper(c_type, expected, actual, msg, indent)
if ((c_type.strip[0] == 42) and (@ptr_handling == :compare_ptr))
unity_func = "TEST_ASSERT_EQUAL_INT_MESSAGE"
else
return "#{unity_func}(#{expected}, #{actual}#{unity_msg})"
unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type)
end
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})"
return "#{indent}#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg});\n"
when /_ARRAY/
return [ "#{indent}if (*p_expected == NULL)\n",
"#{indent}#{@tab}{ TEST_ASSERT_NULL(#{actual}); }\n",
"#{indent}else\n",
"#{indent}#{@tab}{ #{unity_func}(#{expected}, #{actual}, 1#{unity_msg}); }\n" ]
else
return "#{indent}#{unity_func}(#{expected}, #{actual}#{unity_msg});\n"
end
end
end
+27 -2
View File
@@ -5,6 +5,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
def setup
create_mocks :config, :unity_helper
@config.expect.tab.returns(" ")
@config.expect.when_ptr_star.returns(:compare_ptr)
@cmock_generator_utils = CMockGeneratorUtils.new(@config)
end
@@ -20,6 +21,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
should "have set up internal accessors correctly on init, complete with passed helpers" do
create_mocks :config
@config.expect.tab.returns(" ")
@config.expect.when_ptr_star.returns(:compare_ptr)
@cmock_generator_utils = CMockGeneratorUtils.new(@config, {:A, :B})
assert_equal(@config, @cmock_generator_utils.config)
assert_equal(" ", @cmock_generator_utils.tab)
@@ -131,7 +133,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
assert_equal(expected, returned)
end
should "make handle expected for non character strings" do
should "make handle expected when no helpers are available" do
function = { :name => "CanOpener", :rettype => "uint64"}
var_type = "uint16"
var_name = "CorkScrew"
@@ -168,7 +170,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
assert_equal(expected, returned)
end
should "make handle expected for custom types" do
should "make handle expected for custom types from unity helper" do
function = { :name => "TeaPot", :rettype => "uint64"}
var_type = "MANDELBROT_SET_T"
var_name = "TeaSpoon"
@@ -207,4 +209,27 @@ 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 array compares, which involves extra work" do
function = { :name => "Blender", :rettype => "uint16*"}
var_type = "FRUIT*"
var_name = "Strawberry"
@cmock_generator_utils.helpers = {:unity_helper => @unity_helper}
@unity_helper.expect.get_helper(var_type).returns("TEST_ASSERT_EQUAL_FRUIT_ARRAY_MESSAGE")
expected = ["\n",
" if (Mock.Blender_Expected_Strawberry != Mock.Blender_Expected_Strawberry_HeadTail)\n",
" {\n",
" FRUIT** p_expected = Mock.Blender_Expected_Strawberry;\n",
" Mock.Blender_Expected_Strawberry++;\n",
" if (*p_expected == NULL)\n",
" { TEST_ASSERT_NULL(Strawberry); }\n",
" else\n",
" { TEST_ASSERT_EQUAL_FRUIT_ARRAY_MESSAGE(*p_expected, Strawberry, 1, \"Function 'Blender' called with unexpected value for parameter 'Strawberry'.\"); }\n",
" }\n"
]
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
end