https://mooseframework.inl.gov
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MooseObjectUnitTest Class Reference

Base class for building basic unit tests for MOOSE objects that can live alone (like user objects, etc.) More...

#include <MooseObjectUnitTest.h>

Inheritance diagram for MooseObjectUnitTest:
[legend]

Public Member Functions

 MooseObjectUnitTest (const std::string &app_name)
 

Protected Member Functions

void buildObjects ()
 
template<typename T >
T & addObject (const std::string &type, const std::string &name, InputParameters &params)
 

Protected Attributes

std::unique_ptr< MooseMesh_mesh
 
std::shared_ptr< MooseApp_app
 
Factory_factory
 
std::shared_ptr< FEProblem_fe_problem
 

Detailed Description

Base class for building basic unit tests for MOOSE objects that can live alone (like user objects, etc.)

This class builds the basic objects that are needed in order to test a MOOSE object. Those are a mesh and an FEProblem. To build a unit test, inherit from this class and build your test using the following template:

In your .h file:

class MyUnitTest : public MooseObjectUnitTest { public: MyUnitTest() : MooseObjectUnitTest("MyAppUnitApp") { // if you are using the old registration system, you want to register your objects using this // call. Otherwise, you do not need it. registerObjects(_factory); buildObjects(); }

protected: void registerObjects(Factory & factory) { // register your objects as usual, we have to be in a method like this so that the register // macros work registerUserObject(MyObjectThatIAmTesting); }

void buildObjects() { // build your object like so InputParameters pars = _factory.getValidParams("MyObjectThatIAmTesting"); _fe_problem->addUserObject("MyObjectThatIAmTesting", "fp", uo_pars); _obj = &_fe_problem->getUserObject<MyObjectThatIAmTesting>("fp"); }

// member variable used later in the actual tests const MyObjectThatIAmTesting * _obj; };

In your .C file

TEST_F(MyObjectThatIAmTesting, test) { EXPECT_EQ("testing", _obj->method(par1, par2)); }

NOTE: Testing mesh-bound objects like Kernels, BCs, etc. is not possible with this class.

Definition at line 69 of file MooseObjectUnitTest.h.

Constructor & Destructor Documentation

◆ MooseObjectUnitTest()

MooseObjectUnitTest::MooseObjectUnitTest ( const std::string &  app_name)
inline
Parameters
app_nameThe name of client's application

Definition at line 75 of file MooseObjectUnitTest.h.

76  : _app(Moose::createMooseApp(app_name, 0, nullptr)), _factory(_app->getFactory())
77  {
78  buildObjects();
79  }
std::shared_ptr< MooseApp > createMooseApp(const std::string &default_app_type, int argc, char *argv[])
Create a MooseApp from command-line arguments.
Definition: MooseMain.C:27
std::shared_ptr< MooseApp > _app

Member Function Documentation

◆ addObject()

template<typename T >
T & MooseObjectUnitTest::addObject ( const std::string &  type,
const std::string &  name,
InputParameters params 
)
protected

Definition at line 114 of file MooseObjectUnitTest.h.

117 {
118  auto objects = _fe_problem->addObject<T>(type, name, params);
119  mooseAssert(objects.size() == 1, "Doesn't work with threading");
120  return *objects[0];
121 }
std::string name(const ElemQuality q)
std::shared_ptr< FEProblem > _fe_problem

◆ buildObjects()

void MooseObjectUnitTest::buildObjects ( )
inlineprotected

Definition at line 82 of file MooseObjectUnitTest.h.

Referenced by MooseObjectUnitTest().

83  {
84  InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
85  mesh_params.set<MooseEnum>("dim") = "3";
86  mesh_params.set<unsigned int>("nx") = 2;
87  mesh_params.set<unsigned int>("ny") = 2;
88  mesh_params.set<unsigned int>("nz") = 2;
89  _mesh = _factory.createUnique<MooseMesh>("GeneratedMesh", "name1", mesh_params);
90  _mesh->setMeshBase(_mesh->buildMeshBaseObject());
91  _mesh->buildMesh();
92 
93  InputParameters problem_params = _factory.getValidParams("FEProblem");
94  problem_params.set<MooseMesh *>("mesh") = _mesh.get();
95  problem_params.set<std::string>("_object_name") = "name2";
96  _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
97 
99 
100  _app->actionWarehouse().problemBase() = _fe_problem;
101  }
std::shared_ptr< FEProblem > _fe_problem
std::shared_ptr< MooseApp > _app
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
std::unique_ptr< MooseMesh > _mesh
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:111
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:87

Member Data Documentation

◆ _app

std::shared_ptr<MooseApp> MooseObjectUnitTest::_app
protected

Definition at line 107 of file MooseObjectUnitTest.h.

Referenced by buildObjects().

◆ _factory

Factory& MooseObjectUnitTest::_factory
protected

Definition at line 108 of file MooseObjectUnitTest.h.

Referenced by buildObjects().

◆ _fe_problem

std::shared_ptr<FEProblem> MooseObjectUnitTest::_fe_problem
protected

Definition at line 109 of file MooseObjectUnitTest.h.

Referenced by addObject(), and buildObjects().

◆ _mesh

std::unique_ptr<MooseMesh> MooseObjectUnitTest::_mesh
protected

Definition at line 106 of file MooseObjectUnitTest.h.

Referenced by buildObjects().


The documentation for this class was generated from the following file: