YARP
Yet Another Robot Platform
TestCase.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <robottestingframework/Arguments.h>
12 #include <robottestingframework/TestAssert.h>
13 
14 #include <yarp/os/Network.h>
15 #include <yarp/os/Property.h>
16 #include <yarp/os/ResourceFinder.h>
17 
18 #include <cstring>
19 
20 
22 {
23 public:
25 };
26 
27 
29  ::robottestingframework::TestCase(name),
30  mPriv(new Private)
31 {
32 }
33 
35 {
36  delete mPriv;
37 }
38 
39 
41 {
42  // check yarp network
43  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(mPriv->yarp.checkNetwork(),
44  "YARP network does not seem to be available, is the yarp server accessible?");
45 
46  // loading environment properties by parsing the string value
47  // from getEnvironment().
48  std::string strEnv = getEnvironment();
49  yarp::os::Property envprop;
50  envprop.fromArguments(strEnv.c_str());
51  bool useSuiteContext = envprop.check("context");
52 
53  // load the config file and update the environment if available
54  // E.g., "--from mytest.ini"
56  if(useSuiteContext) {
57  rf.setDefaultContext(envprop.find("context").asString().c_str());
58  } else {
59  rf.setDefaultContext("RobotTesting");
60  }
61  rf.configure(argc, argv);
62  yarp::os::Property property;
63 
64  if(rf.check("from")) {
65 
66  std::string cfgname = rf.find("from").asString();
67  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(cfgname.size(),
68  "Empty value was set for the '--from' property");
69 
70  // loading configuration file indicated by --from
71  std::string cfgfile = rf.findFileByName(cfgname.c_str());
72 
73  bool useTestContext = rf.check("context");
74 
75  // if the config file cannot be found from default context or
76  // there is not any context, use the robotname environment as context
77  if(!useSuiteContext && !useTestContext && !cfgfile.size() && envprop.check("robotname")) {
78  rf.setDefaultContext(envprop.find("robotname").asString().c_str());
79  cfgfile = rf.findFileByName(cfgname.c_str());
80  }
81  ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(cfgfile.size(),
82  ::robottestingframework::Asserter::format("Cannot find configuration file %s", cfgfile.c_str()));
83  ROBOTTESTINGFRAMEWORK_TEST_REPORT(::robottestingframework::Asserter::format("Loading configuration from %s", cfgfile.c_str()));
84  // update the properties with environment
85  property.fromConfigFile(cfgfile.c_str(), envprop);
86  } else {
87  property.fromString(rf.toString().c_str());
88  }
89 
90  return setup(property);
91 }
92 
94 {
95  return false;
96 }
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
A class for storing options and configuration information.
Definition: Property.h:37
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1034
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
void fromArguments(const char *arguments, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1071
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:237
The YarpTestCase is a helper class to facilitate loading the tests settings which are developed for Y...
Definition: TestCase.h:34
bool setup(int argc, char **argv) override
Definition: TestCase.cpp:40
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
Definition: environment.h:31