Skip to content

Commit

Permalink
Add more tests for Reporter and ReporterInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
loganharbour authored and aeslaughter committed Jun 2, 2021
1 parent dc411b5 commit ebf35b0
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/include/reporters/TestReporter.h
Expand Up @@ -81,3 +81,13 @@ class TestGetReporterDeclaredInInitialSetupReporter : public GeneralReporter
const Real & _value_declared_in_initial_setup;
Real & _the_value_of_the_reporter;
};

class TestDeclareErrorsReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareErrorsReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override {}
};
29 changes: 29 additions & 0 deletions test/include/userobjects/ReporterInterfaceErrorTest.h
@@ -0,0 +1,29 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "GeneralUserObject.h"

/**
* A UserObject that tests errors produced by the ReporterInterface.
*/
class ReporterInterfaceErrorTest : public GeneralUserObject
{
public:
static InputParameters validParams();

ReporterInterfaceErrorTest(const InputParameters & params);

void initialSetup() override;

void initialize() override {};
void execute() override {};
void finalize()override{};
};
34 changes: 34 additions & 0 deletions test/include/userobjects/ReporterSpecialTypeTest.h
@@ -0,0 +1,34 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "GeneralUserObject.h"

/**
* A UserObject that tests the requesting of Reporter values
* that are actually declared later to be Postprocessor
* and VectorPostprocessor values.
*/
class ReporterSpecialTypeTest : public GeneralUserObject
{
public:
static InputParameters validParams();

ReporterSpecialTypeTest(const InputParameters & params);

void initialSetup() override;
void initialize() override{};
void execute() override{};
void finalize() override{};

private:
bool isPostprocessor(const std::string & param_name) const;
bool isVectorPostprocessor(const std::string & param_name) const;
};
36 changes: 36 additions & 0 deletions test/src/reporters/TestReporter.C
Expand Up @@ -13,6 +13,7 @@ registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);

InputParameters
TestDeclareReporter::validParams()
Expand Down Expand Up @@ -173,3 +174,38 @@ TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}

InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");

params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");

return params;
}

TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
66 changes: 66 additions & 0 deletions test/src/userobjects/ReporterInterfaceErrorTest.C
@@ -0,0 +1,66 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ReporterInterfaceErrorTest.h"

registerMooseObject("MooseTestApp", ReporterInterfaceErrorTest);

InputParameters
ReporterInterfaceErrorTest::validParams()
{
InputParameters params = GeneralUserObject::validParams();

params.addParam<ReporterName>("reporter", "Test parameter for a ReporterName");

params.addParam<bool>("missing_parameter", false, "Test the error for a missing parameter");
params.addParam<bool>("bad_parameter_type", false, "Test the error for a bad parameter type");
params.addParam<bool>(
"other_type_requested",
false,
"True to test the error for the Reporter value being registered with another type");
params.addParam<bool>("missing",
false,
"Test the error after reporters are added and requesting a reporter value "
"that does not exist");
params.addParam<bool>("missing_by_name",
false,
"Test the error after reporters are added and requesting a reporter value "
"by name that does not exist");
params.addParam<bool>("has_early", false, "Test the error for seeing if a Reporter exists too early");
params.addParam<bool>(
"has_early_by_name", false, "Test the error for seeing if a Reporter exists by name too early");
return params;
}

ReporterInterfaceErrorTest::ReporterInterfaceErrorTest(const InputParameters & params)
: GeneralUserObject(params)
{
if (getParam<bool>("missing_parameter"))
getReporterValue<int>("bad_param");
if (getParam<bool>("bad_parameter_type"))
getReporterValue<int>("missing_parameter");
if (getParam<bool>("other_type_requested"))
{
getReporterValueByName<Real>("some_reporter/some_value");
getReporterValueByName<int>("some_reporter/some_value");
}
if (getParam<bool>("has_early"))
hasReporterValue<Real>("reporter");
if (getParam<bool>("has_early_by_name"))
hasReporterValueByName<Real>("some_reporter/some_value");
}

void
ReporterInterfaceErrorTest::initialSetup()
{
if (getParam<bool>("missing"))
getReporterValue<int>("reporter");
if (getParam<bool>("missing_by_name"))
getReporterValueByName<int>("some_reporter/some_value");
}
67 changes: 67 additions & 0 deletions test/src/userobjects/ReporterSpecialTypeTest.C
@@ -0,0 +1,67 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ReporterSpecialTypeTest.h"

registerMooseObject("MooseTestApp", ReporterSpecialTypeTest);

InputParameters
ReporterSpecialTypeTest::validParams()
{
InputParameters params = GeneralUserObject::validParams();

params.addRequiredParam<ReporterName>(
"pp_reporter", "Test parameter that should represent a Postprocessor value");
params.addRequiredParam<ReporterName>(
"vpp_reporter", "Test parameter that should represent a VectorPostprocessor value");

return params;
}

ReporterSpecialTypeTest::ReporterSpecialTypeTest(const InputParameters & params)
: GeneralUserObject(params)
{
getReporterValue<Real>("pp_reporter");
getReporterValue<VectorPostprocessorValue>("vpp_reporter");

for (const std::string & name : {"pp_reporter", "vpp_reporter"})
if (isPostprocessor(name) || isVectorPostprocessor(name))
mooseError("Is a special type");
}

void
ReporterSpecialTypeTest::initialSetup()
{
if (!hasUserObjectByName<Postprocessor>(getReporterName("pp_reporter").getObjectName()))
mooseError("Is not a pp");
if (!hasUserObjectByName<VectorPostprocessor>(getReporterName("vpp_reporter").getObjectName()))
mooseError("Is not a vpp");
if (!isPostprocessor("pp_reporter"))
mooseError("Not a pp special type");
if (!isVectorPostprocessor("vpp_reporter"))
mooseError("Not a vpp special type");
}

bool
ReporterSpecialTypeTest::isPostprocessor(const std::string & param_name) const
{
return _fe_problem.getReporterData()
.getReporterStateBase(getReporterName(param_name))
.getReporterName()
.isPostprocessor();
}

bool
ReporterSpecialTypeTest::isVectorPostprocessor(const std::string & param_name) const
{
return _fe_problem.getReporterData()
.getReporterStateBase(getReporterName(param_name))
.getReporterName()
.isVectorPostprocessor();
}
21 changes: 21 additions & 0 deletions test/tests/interfaces/reporterinterface/ri_errors.i
@@ -0,0 +1,21 @@
[Mesh]
[gmg]
type = GeneratedMeshGenerator
dim = 1
[]
[]

[UserObjects]
[error_test]
type = ReporterInterfaceErrorTest
reporter = dummy/value
[]
[]

[Problem]
solve = false
[]

[Executioner]
type = Steady
[]
74 changes: 74 additions & 0 deletions test/tests/interfaces/reporterinterface/tests
@@ -0,0 +1,74 @@
[Tests]
design = 'ReporterInterface.md'
issues = '#17512'

[param_errors]
requirement = 'The system shall report a reasonable error when requesting a Reporter value from a parameter when'

[missing_parameter]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/missing_parameter=true'
expect_err = 'When getting a Reporter, failed to get a parameter with the name "bad_param".'

detail = 'the parameter is not found and'
[]
[bad_parameter_type]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/bad_parameter_type=true'
expect_err = 'Supplied parameter with name "missing_parameter" of type "bool" is not an expected type for getting a Reporter.'

detail = 'the parameter does not represent a Reporter'
[]
[]

[other_type_requested_error]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/other_type_requested=true'
expect_err = 'While requesting a Reporter value with the name "some_value" and type "int",.*a Reporter with the same name has been requested with a different type.'

requirement = 'The system shall report a reasonable error when requesting a Reporter value when a Reporter with the same name exists with a different type.'
[]

[missing_errors]
requirement = 'The system shall report a reasonable error when requesting a Reporter value'
[param]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/missing=true'
expect_err = 'reporter\)\:(.*)A Reporter value with the name "dummy/value" and type "int" was not found..'

detail = 'by parameter and the Reporter value does not exist and'
[]
[name]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/missing_by_name=true'
expect_err = 'A Reporter value with the name "some_reporter/some_value" and type "int" was not found.'

detail = 'by Reporter name and the Reporter value does not exist.'
[]
[]

[has_errors]
requirement = 'The system shall report a reasonable error when it is too early to request if a Reporter value exists'
[param]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/has_early=true'
expect_err = 'Cannot call hasReporterValue\(\) until all Reporters have been constructed.'

detail = 'by parameter name and'
[]
[name]
type = RunException
input = 'ri_errors.i'
cli_args = 'UserObjects/error_test/has_early_by_name=true'
expect_err = 'Cannot call hasReporterValueByName\(\) until all Reporters have been constructed.'

detail = 'by Reporter name.'
[]
[]
[]
19 changes: 19 additions & 0 deletions test/tests/reporters/base/errors.i
@@ -0,0 +1,19 @@
[Mesh]
[gmg]
type = GeneratedMeshGenerator
dim = 1
[]
[]

[Reporters/error_test]
type = TestDeclareErrorsReporter
value = value_name
[]

[Problem]
solve = false
[]

[Executioner]
type = Steady
[]
2 changes: 2 additions & 0 deletions test/tests/reporters/base/gold/special_types_out.csv
@@ -0,0 +1,2 @@
time,pp
1,1
2 changes: 2 additions & 0 deletions test/tests/reporters/base/gold/special_types_out_vpp_0001.csv
@@ -0,0 +1,2 @@
value
2

0 comments on commit ebf35b0

Please sign in to comment.