From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 65F877803D7 for ; Tue, 13 Feb 2024 17:26:23 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=OwwM44ilh3SfU41BboHcbOmUEnx+lAnxUEwE/pbA+Ck=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1707845182; v=1; b=seY4XM9+74KzcashYlYib43ry+oYr/IPH3Pz/qfSd5rkkvPgWBOcUp95DlvGXKk7cLGBUFXS YrYRDHj6nRl6UoIxot9cwZRVIHL4R31EO/yhFLefYp1GiAIjFdvMiQ2z6l1LUo4dj8/fr8z3wbp bP+QZCzZJtklzH4LV7klWF/E= X-Received: by 127.0.0.2 with SMTP id N2hQYY7687511xHzZrrZIs04; Tue, 13 Feb 2024 09:26:22 -0800 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by mx.groups.io with SMTP id smtpd.web11.17844.1707845177295409701 for ; Tue, 13 Feb 2024 09:26:18 -0800 X-IronPort-AV: E=McAfee;i="6600,9927,10982"; a="1733194" X-IronPort-AV: E=Sophos;i="6.06,157,1705392000"; d="scan'208";a="1733194" X-Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2024 09:26:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,157,1705392000"; d="scan'208";a="7698062" X-Received: from mdkinney-mobl.amr.corp.intel.com ([10.209.108.163]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2024 09:26:18 -0800 From: "Michael D Kinney" To: devel@edk2.groups.io Cc: Michael Kubacki , Sean Brogan , Michael Kubacki Subject: [edk2-devel] [edk2-stable202402][Patch v4 6/7] UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW() Date: Tue, 13 Feb 2024 09:26:11 -0800 Message-Id: <20240213172612.636-7-michael.d.kinney@intel.com> In-Reply-To: <20240213172612.636-1-michael.d.kinney@intel.com> References: <20240213172612.636-1-michael.d.kinney@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,michael.d.kinney@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: FfTJn3TJRkC6NMWN61j7kBLJx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=seY4XM9+; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io 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 Cc: Sean Brogan Signed-off-by: Michael D Kinney Reviewed-by: Michael Kubacki --- UnitTestFrameworkPkg/ReadMe.md | 2 +- .../SampleGoogleTest/SampleGoogleTest.cpp | 36 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/UnitTestFrameworkPkg/ReadMe.md b/UnitTestFrameworkPkg/ReadMe.md index d6a3e0c15a2b..d28cb5cb0a5d 100644 --- a/UnitTestFrameworkPkg/ReadMe.md +++ b/UnitTestFrameworkPkg/ReadMe.md @@ -59,7 +59,7 @@ reviewed. The paths to the SecureBootVariableLib unit tests are: | Unit Test Source Language | C | C++ | | Register Test Suite | YES | Auto | | Register Test Case | YES | Auto | -| Death/Expected Assert Tests | YES | YES | +| Expected Assert Tests | YES | YES | | Setup/Teardown Hooks | YES | YES | | Value-Parameterized Tests | NO | YES | | Typed Tests | NO | YES | diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp index 94cbf2cf0b3c..2c2765e1e5ab 100644 --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp @@ -7,7 +7,7 @@ **/ -#include +#include extern "C" { #include #include @@ -229,7 +229,7 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) { } /** - Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s. + Sample unit test using the EXPECT_ANY_THROW() macro to test expected ASSERT()s. **/ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) { // @@ -242,14 +242,35 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) { // // This test passes because it directly triggers an ASSERT(). // - ASSERT_DEATH (ASSERT (FALSE), ""); + EXPECT_ANY_THROW (ASSERT (FALSE)); // // This test passes because DecimalToBcd() generates an ASSERT() if the // value passed in is >= 100. The expected ASSERT() is caught by the unit - // test framework and ASSERT_DEATH() returns without an error. + // test framework and EXPECT_ANY_THROW() returns without an error. // - ASSERT_DEATH (DecimalToBcd8 (101), ""); + EXPECT_ANY_THROW (DecimalToBcd8 (101)); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and throws the C++ exception of type std::runtime_error. + // EXPECT_THROW() returns without an error. + // + EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error); + + // + // This test passes because DecimalToBcd() generates an ASSERT() if the + // value passed in is >= 100. The expected ASSERT() is caught by the unit + // test framework and throws the C++ exception of type std::runtime_error with + // a message that includes the filename, linenumber, and the expression that + // triggered the ASSERT(). + // + // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds() to + // throw a C++ exception of type std::runtime_error with a message that + // includes the expression of "Value < 100" that triggered the ASSERT(). + // + EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100"); } INSTANTIATE_TEST_SUITE_P ( @@ -266,6 +287,11 @@ TEST (MacroTestsMessages, MacroTraceMessage) { // Example of logging. // SCOPED_TRACE ("SCOPED_TRACE message\n"); + + // + // Always pass + // + ASSERT_TRUE (TRUE); } int -- 2.40.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#115398): https://edk2.groups.io/g/devel/message/115398 Mute This Topic: https://groups.io/mt/104336763/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-