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 }
Network.h
yarp::robottestingframework::TestCase
The YarpTestCase is a helper class to facilitate loading the tests settings which are developed for Y...
Definition: TestCase.h:34
yarp::conf::environment::getEnvironment
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
Definition: environment.h:31
yarp::robottestingframework::TestCase::~TestCase
virtual ~TestCase()
Definition: TestCase.cpp:34
yarp::os::Property::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1034
yarp::os::Property::fromArguments
void fromArguments(const char *arguments, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1071
yarp::robottestingframework::TestCase::Private::yarp
yarp::os::Network yarp
Definition: TestCase.cpp:24
yarp::os::ResourceFinder::setDefaultContext
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
Definition: ResourceFinder.h:91
yarp::os::ResourceFinder::configure
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Definition: ResourceFinder.cpp:803
Property.h
yarp::robottestingframework::TestCase::Private
Definition: TestCase.cpp:22
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::ResourceFinder::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: ResourceFinder.cpp:926
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
yarp::os::ResourceFinder::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: ResourceFinder.cpp:920
yarp::robottestingframework::TestCase::TestCase
TestCase(std::string name)
Definition: TestCase.cpp:28
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp::robottestingframework::TestCase::setup
bool setup(int argc, char **argv) override
Definition: TestCase.cpp:40
yarp::os::ResourceFinder::findFileByName
std::string findFileByName(const std::string &name)
Find the full path to a file.
Definition: ResourceFinder.cpp:860
ResourceFinder.h
TestCase.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::os::ResourceFinder::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: ResourceFinder.cpp:944
yarp::os::ResourceFinder
Helper class for finding config files and other external resources.
Definition: ResourceFinder.h:33