LCOV - code coverage report
Current view: top level - include/base - MooseObjectUnitTest.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 8601ad Lines: 24 24 100.0 %
Date: 2025-07-18 13:27:08 Functions: 5 5 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 "MooseMesh.h"
      15             : #include "FEProblem.h"
      16             : #include "AppFactory.h"
      17             : #include "MooseMain.h"
      18             : 
      19             : /**
      20             :  * Base class for building basic unit tests for MOOSE objects that can live alone (like user
      21             :  * objects, etc.)
      22             :  *
      23             :  * This class builds the basic objects that are needed in order to test a MOOSE object. Those are a
      24             :  * mesh and an FEProblem.  To build a unit test, inherit from this class and build your test using
      25             :  * the following template:
      26             :  *
      27             :  * In your .h file:
      28             :  *
      29             :  * class MyUnitTest : public MooseObjectUnitTest
      30             :  * {
      31             :  * public:
      32             :  *   MyUnitTest() : MooseObjectUnitTest("MyAppUnitApp")
      33             :  *   {
      34             :  *     // if you are using the old registration system, you want to register your objects using this
      35             :  *     // call. Otherwise, you do not need it.
      36             :  *     registerObjects(_factory);
      37             :  *     buildObjects();
      38             :  *   }
      39             :  *
      40             :  * protected:
      41             :  *   void registerObjects(Factory & factory)
      42             :  *   {
      43             :  *     // register your objects as usual, we have to be in a method like this so that the register
      44             :  *     // macros work
      45             :  *     registerUserObject(MyObjectThatIAmTesting);
      46             :  *   }
      47             :  *
      48             :  *   void buildObjects()
      49             :  *   {
      50             :  *     // build your object like so
      51             :  *     InputParameters pars = _factory.getValidParams("MyObjectThatIAmTesting");
      52             :  *     _fe_problem->addUserObject("MyObjectThatIAmTesting", "fp", uo_pars);
      53             :  *     _obj = &_fe_problem->getUserObject<MyObjectThatIAmTesting>("fp");
      54             :  *   }
      55             :  *
      56             :  *   // member variable used later in the actual tests
      57             :  *   const MyObjectThatIAmTesting * _obj;
      58             :  * };
      59             :  *
      60             :  * In your .C file
      61             :  *
      62             :  * TEST_F(MyObjectThatIAmTesting, test)
      63             :  * {
      64             :  *   EXPECT_EQ("testing", _obj->method(par1, par2));
      65             :  * }
      66             :  *
      67             :  * NOTE: Testing mesh-bound objects like Kernels, BCs, etc. is not possible with this class.
      68             :  */
      69             : class MooseObjectUnitTest : public ::testing::Test
      70             : {
      71             : public:
      72             :   /**
      73             :    * @param app_name The name of client's application
      74             :    */
      75          28 :   MooseObjectUnitTest(const std::string & app_name)
      76          28 :     : _app(Moose::createMooseApp(app_name, 0, nullptr)), _factory(_app->getFactory())
      77             :   {
      78          28 :     buildObjects();
      79          28 :   }
      80             : 
      81             : protected:
      82          28 :   void buildObjects()
      83             :   {
      84          28 :     InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
      85          28 :     mesh_params.set<MooseEnum>("dim") = "3";
      86          28 :     mesh_params.set<unsigned int>("nx") = 2;
      87          28 :     mesh_params.set<unsigned int>("ny") = 2;
      88          28 :     mesh_params.set<unsigned int>("nz") = 2;
      89          28 :     _mesh = _factory.createUnique<MooseMesh>("GeneratedMesh", "name1", mesh_params);
      90          28 :     _mesh->setMeshBase(_mesh->buildMeshBaseObject());
      91          28 :     _mesh->buildMesh();
      92             : 
      93          28 :     InputParameters problem_params = _factory.getValidParams("FEProblem");
      94          28 :     problem_params.set<MooseMesh *>("mesh") = _mesh.get();
      95          28 :     problem_params.set<std::string>("_object_name") = "name2";
      96          28 :     _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
      97             : 
      98          28 :     _fe_problem->createQRules(libMesh::QGAUSS, libMesh::FIRST, libMesh::FIRST, libMesh::FIRST);
      99             : 
     100          28 :     _app->actionWarehouse().problemBase() = _fe_problem;
     101          28 :   }
     102             : 
     103             :   template <typename T>
     104             :   T & addObject(const std::string & type, const std::string & name, InputParameters & params);
     105             : 
     106             :   std::unique_ptr<MooseMesh> _mesh;
     107             :   std::shared_ptr<MooseApp> _app;
     108             :   Factory & _factory;
     109             :   std::shared_ptr<FEProblem> _fe_problem;
     110             : };
     111             : 
     112             : template <typename T>
     113             : T &
     114           6 : MooseObjectUnitTest::addObject(const std::string & type,
     115             :                                const std::string & name,
     116             :                                InputParameters & params)
     117             : {
     118           6 :   auto objects = _fe_problem->addObject<T>(type, name, params);
     119             :   mooseAssert(objects.size() == 1, "Doesn't work with threading");
     120          12 :   return *objects[0];
     121           6 : }

Generated by: LCOV version 1.14