https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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 {
79 }
std::shared_ptr< MooseApp > _app
std::unique_ptr< MooseApp > createMooseApp(const std::string &default_app_type, int argc, char *argv[])
Create a MooseApp from command-line arguments.
Definition MooseMain.C:27

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::shared_ptr< FEProblem > _fe_problem
std::string name(const ElemQuality q)

◆ buildObjects()

void MooseObjectUnitTest::buildObjects ( )
inlineprotected

Definition at line 82 of file MooseObjectUnitTest.h.

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>(MooseBase::name_param) = "name2";
96 _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
97
99
100 _app->actionWarehouse().problemBase() = _fe_problem;
101 }
virtual void createQRules(libMesh::QuadratureType type, libMesh::Order order, libMesh::Order volume_order=libMesh::INVALID_ORDER, libMesh::Order face_order=libMesh::INVALID_ORDER, SubdomainID block=Moose::ANY_BLOCK_ID, bool allow_negative_qweights=true)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition FEProblem.h:21
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:142
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition Factory.C:68
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:128
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
static const std::string name_param
The name of the parameter that contains the object name.
Definition MooseBase.h:55
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
void setMeshBase(std::unique_ptr< MeshBase > mesh_base)
Method to set the mesh_base object.
Definition MooseMesh.C:2922
std::unique_ptr< MooseMesh > _mesh

Referenced by MooseObjectUnitTest().

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: