How To Use
 All Modules Pages
C++ sample: Implementation of a simulated object
// Assume you have an arbitrary error type within your SUT
// (for simplicity here modeled as int)
using SpecificErrorType = int;
namespace TestToolBox
{
inline std::string MakeReturnValue(
SpecificErrorType& out_retVal,
bool in_simulateFailure,
std::string const & in_methodName, // not used here
TTB::SimulatedObjectBase const * in_pObject) // not used here
{
out_retVal = SpecificErrorType();
if (in_simulateFailure) out_retVal = -1;
return ""; // optionally give info for test output
};
}
class SomeSimulatedObject : public TTB::RegisteredSimulatedObject
{
public:
SomeSimulatedObject(std::string const & in_name)
: TTB::RegisteredSimulatedObject(in_name, TTB_CLASS_NAME)
{}
void DoSomething()
{
bool xValDefined = _IsOptionSet("xVal", TTB_FUNC_NAME);
double xVal = _GetOptionValue<double>("xVal", TTB_FUNC_NAME);
TTB_METHOD_CALL1(" xVal=" << xVal << (xValDefined ? "" : " (xVal not defined)"));
}
void DoSomethingElse() const
{
double xVal = _GetOptionValue<double>("xVal", TTB_FUNC_NAME);
TTB_METHOD_CALL1("xVal=" << xVal);
}
void DoSomethingWithAccessToCommandLine() const
{
auto yVal = TestToolBox::TheEnvironment()->GetCommandLineOptionVal<double>("-yVal");
TTB_METHOD_CALL1("yVal=" << yVal);
}
SpecificErrorType CalculateSomething(int in_val, int& out_val)
{
// Remark: 3rd param is a lambda function which is called
// immediately before leaving function (i.e. after a possible halt
// of function call is continued)
return TTB_METHOD_CALL_RET3(SpecificErrorType, "in_val=" << in_val,
{
// Calculate out val
if (_WasError("CalculateSomething"))
out_val = -99;
else
out_val = 2 * in_val;
// optional output string
return TTB_STRING_S("out_val=" << out_val);
});
}
};