public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Michael D Kinney" <michael.d.kinney@intel.com>
To: devel@edk2.groups.io
Cc: Sean Brogan <sean.brogan@microsoft.com>,
	Bret Barkelew <Bret.Barkelew@microsoft.com>,
	Jiewen Yao <jiewen.yao@intel.com>
Subject: [Patch 15/15] UnitTestFramewokPkg/SampleUnitTest: Use UT_EXPECT_ASSERT_FAILURE()
Date: Sun, 14 Jun 2020 17:19:18 -0700	[thread overview]
Message-ID: <20200615001918.22164-16-michael.d.kinney@intel.com> (raw)
In-Reply-To: <20200615001918.22164-1-michael.d.kinney@intel.com>

https://bugzilla.tianocore.org/show_bug.cgi?id=2801

Add samples using UT_EXPECT_ASSERT_FAILURE() for both positive
test cases where the ASSERT() is triggered and detected correctly
and negative test cases where an ASSERT() is never triggered.

These new test cases also generate passing test results when
ASSERT()s are disabled.

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>
---
 .../Sample/SampleUnitTest/SampleUnitTest.c    | 109 ++++++++++++++++++
 1 file changed, 109 insertions(+)

diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c
index 37d5747bca..424b7abf88 100644
--- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c
+++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTest.c
@@ -181,6 +181,102 @@ GlobalPointerShouldBeChangeable (
   return UNIT_TEST_PASSED;
 }
 
+UNIT_TEST_STATUS
+EFIAPI
+DetectExpectedAssert (
+  IN UNIT_TEST_CONTEXT  Context
+  )
+{
+  //
+  // This test passes because it directly triggers an ASSERT().
+  //
+  UT_EXPECT_ASSERT_FAILURE (ASSERT (FALSE), NULL);
+
+  //
+  // 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 UT_EXPECT_ASSERT_FAILURE() returns without an error.
+  //
+  UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (101), NULL);
+
+  return UNIT_TEST_PASSED;
+}
+
+UNIT_TEST_STATUS
+EFIAPI
+DetectNoAssertsWorker1 (
+  IN UNIT_TEST_CONTEXT  Context
+  )
+{
+  UNIT_TEST_STATUS  Status;
+
+  //
+  // If ASSERT()s are enabled, then the following test case will not triggered
+  // an ASSERT() and UT_EXPECT_ASSERT_FAILURE() exits the function from the
+  // macro with status UNIT_TEST_ERROR_TEST_FAILED.
+  //
+  // If ASSERT()s are disabled, then UNIT_TEST_ERROR_TEST_FAILED() is ignored
+  // and Status is set to UNIT_TEST_SKIPPED.
+  //
+  // If ASSERT()s are enabled and this ASSERT() is incorrectly triggered, then
+  // UT_EXPECT_ASSERT_FAILURE() returns with Status set to UNIT_TEST_PASSED.
+  //
+  UT_EXPECT_ASSERT_FAILURE (ASSERT (TRUE), &Status);
+
+  return Status;
+}
+
+UNIT_TEST_STATUS
+EFIAPI
+DetectNoAssertsWorker2 (
+  IN UNIT_TEST_CONTEXT  Context
+  )
+{
+  UNIT_TEST_STATUS  Status;
+
+  //
+  // If ASSERT()s are enabled, then the following test case will not triggered
+  // an ASSERT() and UT_EXPECT_ASSERT_FAILURE() exits the function from the
+  // macro with status UNIT_TEST_ERROR_TEST_FAILED.
+  //
+  // If ASSERT()s are disabled, then UNIT_TEST_ERROR_TEST_FAILED() is ignored
+  // and Status is set to UNIT_TEST_SKIPPED.
+  //
+  // If ASSERT()s are enabled and this ASSERT() is incorrectly triggered, then
+  // UT_EXPECT_ASSERT_FAILURE() returns with Status set to UNIT_TEST_PASSED.
+  //
+  // DecimalToBcd() only generates an ASSERT() if the value passed in is >= 100.
+  //
+  UT_EXPECT_ASSERT_FAILURE (DecimalToBcd8 (99), &Status);
+
+  return Status;
+}
+
+UNIT_TEST_STATUS
+EFIAPI
+DetectNoAsserts (
+  IN UNIT_TEST_CONTEXT  Context
+  )
+{
+  UNIT_TEST_STATUS  Status;
+
+  //
+  // This call is expected to return UNIT_TEST_ERROR_TEST_FAILED or
+  // UNIT_TEST_SKIPPED.
+  //
+  Status = DetectNoAssertsWorker1 (Context);
+  UT_ASSERT_FALSE (Status == UNIT_TEST_PASSED);
+
+  //
+  // This call is expected to return UNIT_TEST_ERROR_TEST_FAILED or
+  // UNIT_TEST_SKIPPED.
+  //
+  Status = DetectNoAssertsWorker2 (Context);
+  UT_ASSERT_FALSE (Status == UNIT_TEST_PASSED);
+
+  return UNIT_TEST_PASSED;
+}
+
 /**
   Initialize the unit test framework, suite, and unit tests for the
   sample unit tests and run the unit tests.
@@ -199,6 +295,7 @@ UefiTestMain (
   UNIT_TEST_FRAMEWORK_HANDLE  Framework;
   UNIT_TEST_SUITE_HANDLE      SimpleMathTests;
   UNIT_TEST_SUITE_HANDLE      GlobalVarTests;
+  UNIT_TEST_SUITE_HANDLE      DebugAssertTests;
 
   Framework = NULL;
 
@@ -236,6 +333,18 @@ UefiTestMain (
   AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL);
   AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL);
 
+  //
+  // Populate the DebugLib ASSERT() Unit Test Suite.
+  //
+  Status = CreateUnitTestSuite (&DebugAssertTests, Framework, "DebugLib ASSERT() Tests", "Sample.DebugLibAssert", NULL, NULL);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for DebugAssertTests\n"));
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  AddTestCase (DebugAssertTests, "Detect Expected ASSERT()", "DetectExpectedAssert", DetectExpectedAssert, NULL, NULL, NULL);
+  AddTestCase (DebugAssertTests, "Detect no ASSERT() triggered", "DetectNoAsserts", DetectNoAsserts, NULL, NULL, NULL);
+
   //
   // Execute the tests.
   //
-- 
2.21.0.windows.1


      parent reply	other threads:[~2020-06-15  0:19 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-15  0:19 [Patch 00/15] UnitTestFrameworkPkg: Enhancements and bug fixes Michael D Kinney
2020-06-15  0:19 ` [Patch 01/15] BaseTools/Python: Allow HOST_APPLICATION to use NULL libraries Michael D Kinney
2020-06-15  6:22   ` [edk2-devel] " Yuwei Chen
2020-06-15  0:19 ` [Patch 02/15] MdePkg/BaseCpuLibNull: Add Null version of CpuLib for host testing Michael D Kinney
2020-07-09 14:03   ` Liming Gao
2020-06-15  0:19 ` [Patch 03/15] MdePkg/BaseCacheMaintenanceLibNull: Add Null instance " Michael D Kinney
2020-07-09 14:03   ` Liming Gao
2020-06-15  0:19 ` [Patch 04/15] MdePkg/BaseLib: Break out IA32/X64 GCC inline privileged functions Michael D Kinney
2020-07-09 14:07   ` Liming Gao
2020-07-09 17:00     ` Michael D Kinney
2020-07-10  8:06       ` Liming Gao
2020-07-10 16:24         ` Michael D Kinney
2020-06-15  0:19 ` [Patch 05/15] MdePkg/Library/BaseLib: Add BaseLib instance for host based unit tests Michael D Kinney
2020-06-15  0:19 ` [Patch 06/15] UnitTestFrameworkPkg: Use host libraries from MdePkg Michael D Kinney
2020-06-15  0:19 ` [Patch 07/15] UnitTestFrameworkPkg: Enable source level debug for host tests Michael D Kinney
2020-06-15  0:19 ` [Patch 08/15] UnitTestFrameworkPkg: Set host application stack size to 256KB Michael D Kinney
2020-06-15  0:19 ` [Patch 09/15] UnitTestFrameworkPkg: Change target mode DebugLib mapping Michael D Kinney
2020-06-15  0:19 ` [Patch 10/15] UnitTestFrameworkPkg/UnitTestLib: Move print log into cleanup Michael D Kinney
2020-06-15  0:19 ` [Patch 11/15] UnitTestFrameworkPkg/UnitTestLib: Fix target mode log messages Michael D Kinney
2020-06-15  0:19 ` [Patch 12/15] UnitTestFrameworkPkg/UnitTestLib: Add checks for ASSERT() Michael D Kinney
2020-06-15  0:19 ` [Patch 13/15] MdePkg/Include: Hook DebugLib _ASSERT() for unit tests Michael D Kinney
2020-06-15  0:19 ` [Patch 14/15] MdePkg/Include: Add UT_EXPECT_ASSERT_FAILURE() to UnitTestLib Michael D Kinney
2020-06-15  0:19 ` Michael D Kinney [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200615001918.22164-16-michael.d.kinney@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox