]> xenbits.xensource.com Git - people/aperard/ovmf.git/commitdiff
UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW()
authorMichael D Kinney <michael.d.kinney@intel.com>
Sat, 3 Feb 2024 01:15:29 +0000 (17:15 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Wed, 14 Feb 2024 02:37:16 +0000 (02:37 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4683

Update GoogleTest samples to use EXPECT_ANY_THROW() instead
of ASSERT_DEATH(). ASSERT_DEATH() is a very slow method to
detect an expected ASSERT() condition. Throwing an exception
from ASSERT() and using EXPECT_ANY_THROW() is several orders
of magnitude faster.

Update GoogleTest sample with example of using EXPECT_THROW()
and EXPECT_THAT() to check for more specific ASSERT() conditions
that allow unit test cases to test functions that contain
more than one ASSERT() statement and verify that the expected
ASSERT() is the one that was actually triggered.

Update library mappings so target-based unit tests use
UnitTestDebugAssertLib.inf and host-based unit tests use
UnitTestDebugAssertLibHost.inf

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>
UnitTestFrameworkPkg/ReadMe.md
UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp

index d6a3e0c15a2ba883abbc3b0f1e4039423cb86dbd..d28cb5cb0a5d503d70bbb090eb0e85d48512e0b3 100644 (file)
@@ -59,7 +59,7 @@ reviewed. The paths to the SecureBootVariableLib unit tests are:
 | Unit Test Source Language   |     C     |    C++     |\r
 | Register Test Suite         |    YES    |    Auto    |\r
 | Register Test Case          |    YES    |    Auto    |\r
-| Death/Expected Assert Tests |    YES    |    YES     |\r
+| Expected Assert Tests       |    YES    |    YES     |\r
 | Setup/Teardown Hooks        |    YES    |    YES     |\r
 | Value-Parameterized Tests   |    NO     |    YES     |\r
 | Typed Tests                 |    NO     |    YES     |\r
index 94cbf2cf0b3cc0d1b96cc68fac6246ae2b2c9be1..2c2765e1e5ab0b2ad3defa0b8a72a4bc9813e0d3 100644 (file)
@@ -7,7 +7,7 @@
 \r
 **/\r
 \r
-#include <gtest/gtest.h>\r
+#include <Library/GoogleTestLib.h>\r
 extern "C" {\r
   #include <Uefi.h>\r
   #include <Library/BaseLib.h>\r
@@ -229,7 +229,7 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) {
 }\r
 \r
 /**\r
-  Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s.\r
+  Sample unit test using the EXPECT_ANY_THROW() macro to test expected ASSERT()s.\r
 **/\r
 TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {\r
   //\r
@@ -242,14 +242,35 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {
   //\r
   // This test passes because it directly triggers an ASSERT().\r
   //\r
-  ASSERT_DEATH (ASSERT (FALSE), "");\r
+  EXPECT_ANY_THROW (ASSERT (FALSE));\r
 \r
   //\r
   // This test passes because DecimalToBcd() generates an ASSERT() if the\r
   // value passed in is >= 100.  The expected ASSERT() is caught by the unit\r
-  // test framework and ASSERT_DEATH() returns without an error.\r
+  // test framework and EXPECT_ANY_THROW() returns without an error.\r
   //\r
-  ASSERT_DEATH (DecimalToBcd8 (101), "");\r
+  EXPECT_ANY_THROW (DecimalToBcd8 (101));\r
+\r
+  //\r
+  // This test passes because DecimalToBcd() generates an ASSERT() if the\r
+  // value passed in is >= 100.  The expected ASSERT() is caught by the unit\r
+  // test framework and throws the C++ exception of type std::runtime_error.\r
+  // EXPECT_THROW() returns without an error.\r
+  //\r
+  EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error);\r
+\r
+  //\r
+  // This test passes because DecimalToBcd() generates an ASSERT() if the\r
+  // value passed in is >= 100.  The expected ASSERT() is caught by the unit\r
+  // test framework and throws the C++ exception of type std::runtime_error with\r
+  // a message that includes the filename, linenumber, and the expression that\r
+  // triggered the ASSERT().\r
+  //\r
+  // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds() to\r
+  // throw a C++ exception of type std::runtime_error with a message that\r
+  // includes the expression of "Value < 100" that triggered the ASSERT().\r
+  //\r
+  EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100");\r
 }\r
 \r
 INSTANTIATE_TEST_SUITE_P (\r
@@ -266,6 +287,11 @@ TEST (MacroTestsMessages, MacroTraceMessage) {
   // Example of logging.\r
   //\r
   SCOPED_TRACE ("SCOPED_TRACE message\n");\r
+\r
+  //\r
+  // Always pass\r
+  //\r
+  ASSERT_TRUE (TRUE);\r
 }\r
 \r
 int\r