YARP
Yet Another Robot Platform
TestCase.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
7 
8 #include <robottestingframework/Arguments.h>
9 #include <robottestingframework/TestAssert.h>
10 
11 #include <yarp/os/Network.h>
12 #include <yarp/os/Property.h>
13 #include <yarp/os/ResourceFinder.h>
14 
15 #include <cstring>
16 
17 
19 {
20 public:
22 };
23 
24 
26  ::robottestingframework::TestCase(name),
27  mPriv(new Private)
28 {
29 }
30 
32 {
33  delete mPriv;
34 }
35 
36 
38 {
39  // check yarp network
40  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(mPriv->yarp.checkNetwork(),
41  "YARP network does not seem to be available, is the yarp server accessible?");
42 
43  // loading environment properties by parsing the string value
44  // from getEnvironment().
45  std::string strEnv = getEnvironment();
46  yarp::os::Property envprop;
47  envprop.fromArguments(strEnv.c_str());
48  bool useSuiteContext = envprop.check("context");
49 
50  // load the config file and update the environment if available
51  // E.g., "--from mytest.ini"
53  if(useSuiteContext) {
54  rf.setDefaultContext(envprop.find("context").asString().c_str());
55  } else {
56  rf.setDefaultContext("RobotTesting");
57  }
58  rf.configure(argc, argv);
59  yarp::os::Property property;
60 
61  if(rf.check("from")) {
62 
63  std::string cfgname = rf.find("from").asString();
64  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(cfgname.size(),
65  "Empty value was set for the '--from' property");
66 
67  // loading configuration file indicated by --from
68  std::string cfgfile = rf.findFileByName(cfgname.c_str());
69 
70  bool useTestContext = rf.check("context");
71 
72  // if the config file cannot be found from default context or
73  // there is not any context, use the robotname environment as context
74  if(!useSuiteContext && !useTestContext && !cfgfile.size() && envprop.check("robotname")) {
75  rf.setDefaultContext(envprop.find("robotname").asString().c_str());
76  cfgfile = rf.findFileByName(cfgname.c_str());
77  }
78  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(cfgfile.size(),
79  ::robottestingframework::Asserter::format("Cannot find configuration file %s", cfgfile.c_str()));
80  ROBOTTESTINGFRAMEWORK_TEST_REPORT(::robottestingframework::Asserter::format("Loading configuration from %s", cfgfile.c_str()));
81  // update the properties with environment
82  property.fromConfigFile(cfgfile.c_str(), envprop);
83  } else {
84  property.fromString(rf.toString().c_str());
85  }
86 
87  return setup(property);
88 }
89 
91 {
92  return false;
93 }
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:783
A class for storing options and configuration information.
Definition: Property.h:34
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1051
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1041
void fromArguments(const char *arguments, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1088
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
std::string toString() const override
Return a standard text representation of the content of the object.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string findFileByName(const std::string &name)
Find the full path to a file.
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
The YarpTestCase is a helper class to facilitate loading the tests settings which are developed for Y...
Definition: TestCase.h:31
bool setup(int argc, char **argv) override
Definition: TestCase.cpp:37
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:309