https://mooseframework.inl.gov
ActionUnitTest.h
Go to the documentation of this file.
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 
78 class ActionUnitTest : public ::testing::Test
79 {
80 public:
84  ActionUnitTest(const std::string & app_name)
85  : _app(Moose::createMooseApp(app_name, 0, nullptr)),
86  _factory(_app->getFactory()),
87  _action_factory(_app->getActionFactory())
88  {
90  }
91 
92  void SetUp() override
93  {
94  buildActions();
95  runActions();
96  }
97 
98 protected:
100  virtual void buildActions() = 0;
101 
104  {
105  InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
106  mesh_params.set<MooseEnum>("dim") = "3";
107  mesh_params.set<unsigned int>("nx") = 2;
108  mesh_params.set<unsigned int>("ny") = 2;
109  mesh_params.set<unsigned int>("nz") = 2;
110  _mesh = _factory.createUnique<MooseMesh>("GeneratedMesh", "name1", mesh_params);
111  _mesh->setMeshBase(_mesh->buildMeshBaseObject());
112  _mesh->buildMesh();
113 
114  InputParameters problem_params = _factory.getValidParams("FEProblem");
115  problem_params.set<MooseMesh *>("mesh") = _mesh.get();
116  problem_params.set<std::string>(MooseBase::name_param) = "name2";
117  _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
118 
120 
121  _app->actionWarehouse().problemBase() = _fe_problem;
122  }
123 
129  virtual void runActions() = 0;
130 
132  template <typename T>
133  T & addObject(const std::string & type, const std::string & name, InputParameters & params);
134 
135  std::unique_ptr<MooseMesh> _mesh;
136  std::shared_ptr<MooseApp> _app;
139  std::shared_ptr<FEProblem> _fe_problem;
140 };
141 
142 template <typename T>
143 T &
144 ActionUnitTest::addObject(const std::string & type,
145  const std::string & name,
146  InputParameters & params)
147 {
148  auto objects = _fe_problem->addObject<T>(type, name, params);
149  mooseAssert(objects.size() == 1, "Doesn't work with threading");
150  return *objects[0];
151 }
std::string name(const ElemQuality q)
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
Base class for building basic unit tests for MOOSE actions.
T & addObject(const std::string &type, const std::string &name, InputParameters &params)
Convenience routine for adding an object in an Actions test.
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
std::shared_ptr< MooseApp > _app
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
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
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void buildMinimalObjects()
Some base objects we often need to have the action act on them.
void SetUp() override
Factory & _factory
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:48
virtual void runActions()=0
Override this to make the actions execute NOTE: many tasks will auto-register their task at creation...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
virtual void buildActions()=0
Override this to create the action(s) you want to test.
std::shared_ptr< FEProblem > _fe_problem
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
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
ActionFactory & _action_factory
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
std::unique_ptr< MooseMesh > _mesh
ActionUnitTest(const std::string &app_name)