diff options
author | Michael D Kinney <michael.d.kinney@intel.com> | 2020-06-10 18:09:16 -0700 |
---|---|---|
committer | mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> | 2020-07-15 05:25:21 +0000 |
commit | 133891b712a9ceda0fbdb9649b559773e4cdc6db (patch) | |
tree | 3475a4b7569fd02157807b3bf77f880e2ad6c651 /MdePkg/Include | |
parent | 75e92c13541672a3af349067a040b9dda3d4452d (diff) | |
download | edk2-133891b712a9ceda0fbdb9649b559773e4cdc6db.zip edk2-133891b712a9ceda0fbdb9649b559773e4cdc6db.tar.gz edk2-133891b712a9ceda0fbdb9649b559773e4cdc6db.tar.bz2 |
MdePkg/Include: Add UT_EXPECT_ASSERT_FAILURE() to UnitTestLib
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2801
Add the UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) macro
to the UnitTestLib that can be used to check if a function
under test triggers an ASSERT() condition. If an ASSERT()
condition is triggered, then the macro returns. If the
ASSERT() condition is not triggered, then the current unit
test fails with a status of UNIT_TEST_ERROR_TEST_FAILED.
If ASSERT()s are disabled, then this check for ASSERT()
behavior is not possible, and the check is skipped.
The global variable gUnitTestExpectAssertFailureJumpBuffer
is added to the UnitTestLib to save/restore context when
the UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) macro
is used. The UT_EXPECT_ASSERT_FAILURE() macro uses the
SetJump() service with this global variable. The UnitTestLib
service UnitTestDebugAssert() uses the LongJump() service
with this global to restore context if an ASSERT() is
triggered by the code under test.
Add UnitTestExpectAssertFailure() to the UnitTestLib class.
The UnitTestExpectAssertFailure() is called from the new
UT_EXPECT_ASSERT_FAILURE() macro after the status of this
macro check is known.
Add UnitTestDebugAssert() to the UnitTestLib class. The
UnitTestDebugAssert() service is the same as the DebugLib
DebugAssert() service and is invoked from the DebugLib
_ASSERT() macro if unit testing is enabled. This allows the
Unit Test Framework to know when code under test triggers an
ASSERT() condition.
Cc: Liming Gao <liming.gao@intel.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Diffstat (limited to 'MdePkg/Include')
-rw-r--r-- | MdePkg/Include/Library/UnitTestLib.h | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/MdePkg/Include/Library/UnitTestLib.h b/MdePkg/Include/Library/UnitTestLib.h index a437458..9917549 100644 --- a/MdePkg/Include/Library/UnitTestLib.h +++ b/MdePkg/Include/Library/UnitTestLib.h @@ -442,6 +442,56 @@ SaveFrameworkState ( }
/**
+ This macro uses the framework assertion logic to check whether a function call
+ triggers an ASSERT() condition. The BaseLib SetJump()/LongJump() services
+ are used to establish a safe return point when an ASSERT() is triggered.
+ If an ASSERT() is triggered, unit test execution continues and Status is set
+ to UNIT_TEST_PASSED. Otherwise, a unit test case failure is raised and
+ Status is set to UNIT_TEST_ERROR_TEST_FAILED.
+
+ If ASSERT() macros are disabled, then the test case is skipped and a warning
+ message is added to the unit test log. Status is set to UNIT_TEST_SKIPPED.
+
+ @param[in] FunctionCall Function call that is expected to trigger ASSERT().
+ @param[out] Status Pointer to a UNIT_TEST_STATUS return value. This
+ is an optional parameter that may be NULL.
+**/
+#if defined (EDKII_UNIT_TEST_FRAMEWORK_ENABLED)
+ #include <Library/BaseLib.h>
+
+ ///
+ /// Pointer to jump buffer used with SetJump()/LongJump() to test if a
+ /// function under test generates an expected ASSERT() condition.
+ ///
+ extern BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer;
+
+ #define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) \
+ do { \
+ UNIT_TEST_STATUS UnitTestJumpStatus; \
+ BASE_LIBRARY_JUMP_BUFFER UnitTestJumpBuffer; \
+ UnitTestJumpStatus = UNIT_TEST_SKIPPED; \
+ if (DebugAssertEnabled ()) { \
+ gUnitTestExpectAssertFailureJumpBuffer = &UnitTestJumpBuffer; \
+ if (SetJump (gUnitTestExpectAssertFailureJumpBuffer) == 0) { \
+ FunctionCall; \
+ UnitTestJumpStatus = UNIT_TEST_ERROR_TEST_FAILED; \
+ } else { \
+ UnitTestJumpStatus = UNIT_TEST_PASSED; \
+ } \
+ gUnitTestExpectAssertFailureJumpBuffer = NULL; \
+ } \
+ if (!UnitTestExpectAssertFailure ( \
+ UnitTestJumpStatus, \
+ __FUNCTION__, __LINE__, __FILE__, \
+ #FunctionCall, Status)) { \
+ return UNIT_TEST_ERROR_TEST_FAILED; \
+ } \
+ } while (FALSE)
+#else
+ #define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) FunctionCall;
+#endif
+
+/**
If Expression is TRUE, then TRUE is returned.
If Expression is FALSE, then an assert is triggered and the location of the
assert provided by FunctionName, LineNumber, FileName, and Description are
@@ -691,6 +741,46 @@ UnitTestAssertNotNull ( );
/**
+ If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return
+ TRUE because an ASSERT() was expected when FunctionCall was executed and an
+ ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a
+ warning message and return TRUE because ASSERT() macros are disabled. If
+ UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and
+ return FALSE because an ASSERT() was expected when FunctionCall was executed,
+ but no ASSERT() conditions were triggered. The log messages contain
+ FunctionName, LineNumber, and FileName strings to provide the location of the
+ UT_EXPECT_ASSERT_FAILURE() macro.
+
+ @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() that
+ is either pass, skipped, or failed.
+ @param[in] FunctionName Null-terminated ASCII string of the function
+ executing the UT_EXPECT_ASSERT_FAILURE() macro.
+ @param[in] LineNumber The source file line number of the the function
+ executing the UT_EXPECT_ASSERT_FAILURE() macro.
+ @param[in] FileName Null-terminated ASCII string of the filename
+ executing the UT_EXPECT_ASSERT_FAILURE() macro.
+ @param[in] FunctionCall Null-terminated ASCII string of the function call
+ executed by the UT_EXPECT_ASSERT_FAILURE() macro.
+ @param[out] ResultStatus Used to return the UnitTestStatus value to the
+ caller of UT_EXPECT_ASSERT_FAILURE(). This is
+ optional parameter that may be NULL.
+
+ @retval TRUE UnitTestStatus is UNIT_TEST_PASSED.
+ @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED.
+ @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.
+**/
+BOOLEAN
+EFIAPI
+UnitTestExpectAssertFailure (
+ IN UNIT_TEST_STATUS UnitTestStatus,
+ IN CONST CHAR8 *FunctionName,
+ IN UINTN LineNumber,
+ IN CONST CHAR8 *FileName,
+ IN CONST CHAR8 *FunctionCall,
+ OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL
+ );
+
+/**
Test logging macro that records an ERROR message in the test framework log.
Record is associated with the currently executing test case.
|