From ffbb92854ff805204932c9275cd80cc20f069b2d Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Wed, 24 Jun 2009 02:27:32 +0000 Subject: [PATCH] - finished strict ordering git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@135 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_generator.rb | 10 ++ lib/cmock_generator_plugin_expect.rb | 11 +- .../enforce_strict_ordering.yml | 100 ++++++++++++++++-- 3 files changed, 111 insertions(+), 10 deletions(-) diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 8667606..9e487f1 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -107,6 +107,7 @@ class CMockGenerator if (@ordered) file << "extern int GlobalExpectCount;\n" file << "extern int GlobalVerifyOrder;\n" + file << "extern char* GlobalOrderError;\n" end file << "\n" end @@ -115,6 +116,10 @@ class CMockGenerator file << "void #{@mock_name}_Verify(void)\n{\n" file << "#{@tab}TEST_ASSERT_EQUAL(0, Mock.allocFailure);\n" file << functions.collect {|function| @plugins.run(:mock_verify, function)}.join + if (@ordered) + file << "#{@tab}if (GlobalOrderError)\n" + file << "#{@tab}#{@tab}TEST_FAIL(GlobalOrderError);\n" + end file << "}\n\n" end @@ -131,6 +136,11 @@ class CMockGenerator if (@ordered) file << "#{@tab}GlobalExpectCount = 0;\n" file << "#{@tab}GlobalVerifyOrder = 0;\n" + file << "#{@tab}if (GlobalOrderError)\n" + file << "#{@tab}{\n" + file << "#{@tab}#{@tab}free(GlobalOrderError);\n" + file << "#{@tab}#{@tab}GlobalOrderError = NULL;\n" + file << "#{@tab}}\n" end file << "}\n\n" end diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 5a3ba3e..9bf21ec 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -66,7 +66,14 @@ class CMockGeneratorPluginExpect "#{@tab}#{@tab}++GlobalVerifyOrder;\n", "#{@tab}#{@tab}if (Mock.#{function[:name]}_CallOrder != Mock.#{function[:name]}_CallOrder_Tail)\n", "#{@tab}#{@tab}#{@tab}Mock.#{function[:name]}_CallOrder++;\n", - @utils.expect_helper('int', '*p_expected', 'GlobalVerifyOrder', "\"Function '#{function[:name]}' Called Out Of Order.\"","#{@tab}#{@tab}"), + #@utils.expect_helper('int', '*p_expected', 'GlobalVerifyOrder', "\"Function '#{function[:name]}' Called Out Of Order.\"","#{@tab}#{@tab}"), + "#{@tab}#{@tab}if ((*p_expected != GlobalVerifyOrder) && (GlobalOrderError == NULL))\n", + "#{@tab}#{@tab}{\n", + "#{@tab}#{@tab}#{@tab}const char* ErrStr = \"Function '#{function[:name]}' Called Out Of Order.\";\n", + "#{@tab}#{@tab}#{@tab}GlobalOrderError = malloc(#{"Function '#{function[:name]}' Called Out Of Order.".size+1});\n", + "#{@tab}#{@tab}#{@tab}if (GlobalOrderError)\n", + "#{@tab}#{@tab}#{@tab}#{@tab}strcpy(GlobalOrderError, ErrStr);\n", + "#{@tab}#{@tab}}\n", "#{@tab}}\n" ] end @@ -115,7 +122,7 @@ class CMockGeneratorPluginExpect end def mock_verify(function) - return ["#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"] + ["#{@tab}TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"] end def mock_destroy(function) diff --git a/test/system/test_interactions/enforce_strict_ordering.yml b/test/system/test_interactions/enforce_strict_ordering.yml index d4b3451..3a46ec8 100644 --- a/test/system/test_interactions/enforce_strict_ordering.yml +++ b/test/system/test_interactions/enforce_strict_ordering.yml @@ -2,7 +2,8 @@ :cmock: :enforce_strict_ordering: 1 :plugins: - - # none + - ignore + - cexception :systest: :types: | @@ -11,14 +12,18 @@ typedef signed int custom_type; :mockable: | + #include "Exception.h" UINT32 foo(custom_type a); UINT32 bar(custom_type b); + void baz(custom_type c); :source: - :header: | + :header: | + #include "Exception.h" UINT32 function_a(int a, int b); void function_b(void); void function_c(void); + void function_d(void); :code: | UINT32 function_a(int a, int b) @@ -26,7 +31,16 @@ return foo((custom_type)a) + bar((custom_type)b); } - void function_b(void) { } + void function_b(void) + { + baz((custom_type)1); + foo((custom_type)2); + bar((custom_type)3); + baz((custom_type)4); + foo((custom_type)5); + bar((custom_type)6); + baz((custom_type)7); + } void function_c(void) { @@ -37,8 +51,29 @@ foo((custom_type)5); } + void function_d(void) + { + EXCEPTION_T e; + Try + { + foo((custom_type)1); + } + Catch(e) {} + Try + { + bar((custom_type)2); + } + Catch(e) {} + Try + { + foo((custom_type)3); + } + Catch(e) {} + } + :tests: :common: | + #include "CException.h" void setUp(void) {} void tearDown(void) {} @@ -61,14 +96,16 @@ foo_ExpectAndReturn((custom_type)1, 10); TEST_ASSERT_EQUAL(30, function_a(1, 2)); } - + - :pass: FALSE - :should: 'fail because bar() is called but is not expected' + :should: 'fail because bar() is called twice but is expected once' :code: | test() { - bar_ExpectAndReturn((custom_type)1, 10); - function_b(); + foo_ExpectAndReturn((custom_type)1, 10); + bar_ExpectAndReturn((custom_type)2, 20); + bar_ExpectAndReturn((custom_type)3, 30); + TEST_ASSERT_EQUAL(30, function_a(1, 2)); } - :pass: FALSE @@ -82,7 +119,7 @@ } - :pass: TRUE - :should: 'fail because bar and foo called out of order' + :should: 'pass because bar and foo called in order with multiple params' :code: | test() { @@ -120,4 +157,51 @@ function_c(); } + - :pass: TRUE + :should: 'pass because we are properly ignoring baz' + :code: | + test() + { + baz_Ignore(); + foo_ExpectAndReturn((custom_type)2, 10); + bar_ExpectAndReturn((custom_type)3, 20); + foo_ExpectAndReturn((custom_type)5, 10); + bar_ExpectAndReturn((custom_type)6, 10); + function_b(); + } + + - :pass: FALSE + :should: 'fail because bar and foo out of order, even though baz is ignored' + :code: | + test() + { + baz_Ignore(); + foo_ExpectAndReturn((custom_type)2, 10); + foo_ExpectAndReturn((custom_type)5, 10); + bar_ExpectAndReturn((custom_type)3, 20); + bar_ExpectAndReturn((custom_type)6, 10); + function_b(); + } + + - :pass: TRUE + :should: 'pass when using cexception, as long as the order is right' + :code: | + test() + { + foo_ExpectAndThrow((custom_type)1, 10); + bar_ExpectAndReturn((custom_type)2, 20); + foo_ExpectAndReturn((custom_type)3, 10); + function_d(); + } + + - :pass: FALSE + :should: 'fail when an throw call is made out of order' + :code: | + test() + { + bar_ExpectAndReturn((custom_type)2, 20); + foo_ExpectAndThrow((custom_type)1, 10); + foo_ExpectAndReturn((custom_type)3, 10); + function_d(); + } ...