www.mooseframework.org
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 ()
 

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 61 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 67 of file MooseObjectUnitTest.h.

68  : _app(AppFactory::createAppShared(app_name, 0, nullptr)), _factory(_app->getFactory())
69  {
70  buildObjects();
71  }
std::shared_ptr< MooseApp > _app
static MooseAppPtr createAppShared(const std::string &app_type, int argc, char **argv, MPI_Comm comm_word=MPI_COMM_WORLD)
Helper function for creating a MooseApp from command-line arguments.
Definition: AppFactory.C:35

Member Function Documentation

◆ buildObjects()

void MooseObjectUnitTest::buildObjects ( )
inlineprotected

Definition at line 74 of file MooseObjectUnitTest.h.

Referenced by MooseObjectUnitTest().

75  {
76  InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
77  mesh_params.set<std::string>("_object_name") = "name1";
78  mesh_params.set<std::string>("_type") = "GeneratedMesh";
79  mesh_params.set<MooseEnum>("dim") = "3";
80  _mesh = libmesh_make_unique<GeneratedMesh>(mesh_params);
81 
82  InputParameters problem_params = _factory.getValidParams("FEProblem");
83  problem_params.set<MooseMesh *>("mesh") = _mesh.get();
84  problem_params.set<std::string>("_object_name") = "name2";
85  _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
86 
87  _fe_problem->createQRules(QGAUSS, FIRST, FIRST, FIRST);
88  }
std::shared_ptr< FEProblem > _fe_problem
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, 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
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:25
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
std::unique_ptr< MooseMesh > _mesh
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:72
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:32

Member Data Documentation

◆ _app

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

Definition at line 91 of file MooseObjectUnitTest.h.

◆ _factory

Factory& MooseObjectUnitTest::_factory
protected

Definition at line 92 of file MooseObjectUnitTest.h.

Referenced by buildObjects().

◆ _fe_problem

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

Definition at line 93 of file MooseObjectUnitTest.h.

Referenced by buildObjects().

◆ _mesh

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

Definition at line 90 of file MooseObjectUnitTest.h.

Referenced by buildObjects().


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