LCOV - code coverage report
Current view: top level - include/utils - MooseUnitUtils.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: a4781e Lines: 10 15 66.7 %
Date: 2025-09-23 09:14:59 Functions: 45 45 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : 
      12             : #include "gtest/gtest.h"
      13             : 
      14             : #include "MooseUtils.h"
      15             : #include "Moose.h"
      16             : 
      17             : namespace Moose::UnitUtils
      18             : {
      19             : /**
      20             :  * A helper for asserting that calling something throws an exception.
      21             :  *
      22             :  * @param action A function that calls the thing that should throw
      23             :  * @param contains Optional argument to check that the assertion message contains this sub string
      24             :  * @param set_throw_on_error Set to true to set moose to throw on error
      25             :  */
      26             : template <class ExceptionType = std::exception, class Action = bool>
      27             : void
      28         148 : assertThrows(const Action & action,
      29             :              const std::optional<std::string> & contains = {},
      30             :              const bool set_throw_on_error = false)
      31             : {
      32             :   static_assert(std::is_base_of_v<std::exception, ExceptionType>, "Not an exception");
      33             : 
      34         148 :   std::unique_ptr<Moose::ScopedThrowOnError> scoped_throw_on_error;
      35         148 :   if (set_throw_on_error)
      36           1 :     scoped_throw_on_error = std::make_unique<Moose::ScopedThrowOnError>();
      37             : 
      38             :   try
      39             :   {
      40         148 :     action();
      41           0 :     FAIL() << "Expected " << MooseUtils::prettyCppType<ExceptionType>() << " not thrown";
      42             :   }
      43         296 :   catch (std::exception const & e)
      44             :   {
      45             :     if constexpr (!std::is_same_v<std::exception, ExceptionType>)
      46         145 :       if (!dynamic_cast<const ExceptionType *>(&e))
      47           0 :         FAIL() << "Threw " << demangle(typeid(e).name()) << " instead of "
      48           0 :                << MooseUtils::prettyCppType<ExceptionType>() << " with message '" << e.what()
      49           0 :                << "'";
      50             : 
      51         148 :     if (contains)
      52             :     {
      53         444 :       ASSERT_TRUE(std::string(e.what()).find(*contains) != std::string::npos)
      54           0 :           << "Exception \"" << e.what() << "\" does not contain \"" << *contains << "\"";
      55             :     }
      56             :   }
      57         148 : }
      58             : }

Generated by: LCOV version 1.14