summaryrefslogtreecommitdiff
path: root/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib
diff options
context:
space:
mode:
authorMichael D Kinney <michael.d.kinney@intel.com>2024-02-06 01:59:42 +0300
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2024-02-14 05:37:16 +0300
commit2d144d7e147beddbba85cd04bd2093b544e5cf0a (patch)
treec2b7b82c8efb95441d3da6e224888f187930b426 /UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib
parent312ccaf81b6343c9bcee1f8992086075353abeca (diff)
downloadedk2-2d144d7e147beddbba85cd04bd2093b544e5cf0a.tar.xz
UnitTestFrameworkPkg/UnitTestDebugAssertLib: Add GoogleTest support
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4683 Add an C++ implementation of UnitTestDebugAssert() API for host-based environments. GoogleTest based environments throw a C++ exception of type std::runtime_error when an ASSERT() is triggered with a description that contains the filename, line number, and the expression that triggered the ASSERT(). Cc: Michael Kubacki <mikuback@linux.microsoft.com> Cc: Sean Brogan <sean.brogan@microsoft.com> Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com> Reviewed-by: Michael Kubacki <michael.kubacki@microsoft.com>
Diffstat (limited to 'UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib')
-rw-r--r--UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp63
-rw-r--r--UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf36
-rw-r--r--UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni11
3 files changed, 110 insertions, 0 deletions
diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp
new file mode 100644
index 0000000000..a4405cc205
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp
@@ -0,0 +1,63 @@
+/** @file
+ Unit Test Debug Assert Library for host-based environments
+
+ Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <stdexcept>
+
+#ifdef NULL
+ #undef NULL
+#endif
+
+extern "C" {
+ #include <Uefi.h>
+ #include <UnitTestFrameworkTypes.h>
+ #include <Library/BaseLib.h>
+ #include <Library/UnitTestLib.h>
+
+ ///
+ /// Point to jump buffer used with SetJump()/LongJump() to test if a function
+ /// under test generates an expected ASSERT() condition.
+ ///
+ BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer = NULL;
+
+ /**
+ Unit test library replacement for DebugAssert() in DebugLib.
+
+ If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
+ If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
+
+ @param FileName The pointer to the name of the source file that generated the assert condition.
+ @param LineNumber The line number in the source file that generated the assert condition
+ @param Description The pointer to the description of the assert condition.
+
+ **/
+ VOID
+ EFIAPI
+ UnitTestDebugAssert (
+ IN CONST CHAR8 *FileName,
+ IN UINTN LineNumber,
+ IN CONST CHAR8 *Description
+ )
+ {
+ CHAR8 Message[256];
+
+ if (gUnitTestExpectAssertFailureJumpBuffer != NULL) {
+ UT_LOG_INFO ("Detected expected ASSERT: %a(%d): %a\n", FileName, LineNumber, Description);
+ LongJump (gUnitTestExpectAssertFailureJumpBuffer, 1);
+ } else {
+ if (GetActiveFrameworkHandle () != NULL) {
+ AsciiStrCpyS (Message, sizeof (Message), "Detected unexpected ASSERT(");
+ AsciiStrCatS (Message, sizeof (Message), Description);
+ AsciiStrCatS (Message, sizeof (Message), ")");
+ UnitTestAssertTrue (FALSE, "", LineNumber, FileName, Message);
+ } else {
+ snprintf (Message, sizeof (Message), "Detected unexpected ASSERT: %s(%d): %s\n", FileName, (INT32)(UINT32)LineNumber, Description);
+ throw std::runtime_error (Message);
+ }
+ }
+ }
+}
diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
new file mode 100644
index 0000000000..9a7673f179
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.inf
@@ -0,0 +1,36 @@
+## @file
+# Unit Test Debug Assert Library for host-based environments
+#
+# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = UnitTestDebugAssertLibHost
+ MODULE_UNI_FILE = UnitTestDebugAssertLibHost.uni
+ FILE_GUID = F097D67C-0340-49C8-AB30-ABC1B7D1C8D2
+ MODULE_TYPE = HOST_APPLICATION
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = NULL
+
+#
+# VALID_ARCHITECTURES = IA32 X64 ARM AARCH64
+#
+
+[Sources]
+ UnitTestDebugAssertLibHost.cpp
+
+[Packages]
+ MdePkg/MdePkg.dec
+ UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
+
+[LibraryClasses]
+ BaseLib
+ UnitTestLib
+
+[BuildOptions]
+ MSFT:*_*_*_CC_FLAGS == /c /EHs /Zi /Od /MT
+ GCC:*_*_IA32_CC_FLAGS == -g -c -fshort-wchar -fexceptions -O0 -m32 -malign-double -fno-pie
+ GCC:*_*_X64_CC_FLAGS == -g -c -fshort-wchar -fexceptions -O0 -m64 -fno-pie "-DEFIAPI=__attribute__((ms_abi))"
diff --git a/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni
new file mode 100644
index 0000000000..63d1753a85
--- /dev/null
+++ b/UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.uni
@@ -0,0 +1,11 @@
+// /** @file
+// Unit Test Debug Assert Library for host-based environments
+//
+// Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_MODULE_ABSTRACT #language en-US "Unit Test Debug Assert Library for host-based environments"
+
+#string STR_MODULE_DESCRIPTION #language en-US "Unit Test Debug Assert Library for host-based environments"