Skip to content

Commit

Permalink
Replace PROJECT_SOURCE_DIR in tests
Browse files Browse the repository at this point in the history
This allows the test files to be loaded in a more abstracted way,
so that it is less dependant on the way that CMake works.

Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
mjcarroll committed Jan 12, 2021
1 parent 2cf2c8c commit eea81be
Show file tree
Hide file tree
Showing 36 changed files with 268 additions and 257 deletions.
4 changes: 2 additions & 2 deletions src/Converter_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1899,9 +1899,9 @@ TEST(Converter, MuchNewerVersion)
}

const std::string CONVERT_DOC_15_16 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.6", "1_5.convert");
sdf::testing::SourceFile("sdf", "1.6", "1_5.convert");
const std::string CONVERT_DOC_16_17 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.7", "1_6.convert");
sdf::testing::SourceFile("sdf", "1.7", "1_6.convert");

/////////////////////////////////////////////////
/// Test conversion of imu in 1.5 to 1.6
Expand Down
9 changes: 3 additions & 6 deletions src/FrameSemantics_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"model_frame_attached_to.sdf");
sdf::testing::TestFile("sdf", "model_frame_attached_to.sdf");

// Load the SDF file
sdf::Root root;
Expand Down Expand Up @@ -103,8 +102,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_Model)
TEST(FrameSemantics, buildFrameAttachedToGraph_World)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"world_frame_attached_to.sdf");
sdf::testing::TestFile("sdf", "world_frame_attached_to.sdf");

// Load the SDF file
sdf::Root root;
Expand Down Expand Up @@ -198,8 +196,7 @@ TEST(FrameSemantics, buildFrameAttachedToGraph_World)
TEST(FrameSemantics, buildPoseRelativeToGraph)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"model_frame_relative_to_joint.sdf");
sdf::testing::TestFile("sdf", "model_frame_relative_to_joint.sdf");

// Load the SDF file
sdf::Root root;
Expand Down
74 changes: 31 additions & 43 deletions src/parser_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ sdf::SDFPtr InitSDF()
/// Checks emitted warnings for custom/unknown elements in log file
TEST(Parser, CustomUnknownElements)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path = pathBase +"/custom_and_unknown_elements.sdf";
const auto path = sdf::testing::TestFile(
"sdf", "custom_and_unknown_elements.sdf");

sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
Expand Down Expand Up @@ -100,10 +99,9 @@ TEST(Parser, CustomUnknownElements)
/////////////////////////////////////////////////
TEST(Parser, ReusedSDFVersion)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path17 = pathBase +"/model_link_relative_to.sdf";
const std::string path16 = pathBase +"/joint_complete.sdf";
const auto path17 = sdf::testing::TestFile(
"sdf", "model_link_relative_to.sdf");
const auto path16 = sdf::testing::TestFile("sdf", "joint_complete.sdf");

// Call readFile API that always converts
sdf::SDFPtr sdf = InitSDF();
Expand All @@ -123,9 +121,7 @@ TEST(Parser, ReusedSDFVersion)
/////////////////////////////////////////////////
TEST(Parser, readFileConversions)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";
const std::string path = pathBase +"/joint_complete.sdf";
const auto path = sdf::testing::TestFile("sdf", "joint_complete.sdf");

// Call readFile API that always converts
{
Expand Down Expand Up @@ -384,16 +380,13 @@ TEST(Parser, addNestedModel)
/////////////////////////////////////////////////
TEST(Parser, NameUniqueness)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";

// These tests are copies of the ones in ign_TEST.cc but use direct calls to
// name uniqueness validator functions instead of going through ign.

// Check an SDF file with sibling elements of the same type (world)
// that have duplicate names.
{
std::string path = pathBase +"/world_duplicate.sdf";
const auto path = sdf::testing::TestFile("sdf", "world_duplicate.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -406,7 +399,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of different types (model, light)
// that have duplicate names.
{
std::string path = pathBase +"/world_sibling_same_names.sdf";
const auto path = sdf::testing::TestFile("sdf",
"world_sibling_same_names.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -419,7 +413,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (link)
// that have duplicate names.
{
std::string path = pathBase +"/model_duplicate_links.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_duplicate_links.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -432,7 +427,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (joint)
// that have duplicate names.
{
std::string path = pathBase +"/model_duplicate_joints.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_duplicate_joints.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -445,7 +441,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of different types (link, joint)
// that have duplicate names.
{
std::string path = pathBase +"/model_link_joint_same_name.sdf";
const auto path = sdf::testing::TestFile("sdf",
"model_link_joint_same_name.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -458,7 +455,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (collision)
// that have duplicate names.
{
std::string path = pathBase +"/link_duplicate_sibling_collisions.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_sibling_collisions.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -471,7 +469,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with sibling elements of the same type (visual)
// that have duplicate names.
{
std::string path = pathBase +"/link_duplicate_sibling_visuals.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_sibling_visuals.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_FALSE(sdf::recursiveSiblingUniqueNames(sdf->Root()));
Expand All @@ -484,7 +483,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with cousin elements of the same type (collision)
// that have duplicate names. This is a valid file.
{
std::string path = pathBase +"/link_duplicate_cousin_collisions.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_cousin_collisions.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -498,7 +498,8 @@ TEST(Parser, NameUniqueness)
// Check an SDF file with cousin elements of the same type (visual)
// that have duplicate names. This is a valid file.
{
std::string path = pathBase +"/link_duplicate_cousin_visuals.sdf";
const auto path = sdf::testing::TestFile("sdf",
"link_duplicate_cousin_visuals.sdf");
sdf::SDFPtr sdf = InitSDF();
EXPECT_TRUE(sdf::readFile(path, sdf));
EXPECT_TRUE(sdf::recursiveSameTypeUniqueNames(sdf->Root()));
Expand All @@ -520,9 +521,6 @@ static bool contains(const std::string &_a, const std::string &_b)
/////////////////////////////////////////////////
TEST(Parser, SyntaxErrorInValues)
{
std::string pathBase = PROJECT_SOURCE_PATH;
pathBase += "/test/sdf";

// Capture sdferr output
std::stringstream buffer;
auto old = std::cerr.rdbuf(buffer.rdbuf());
Expand All @@ -532,7 +530,7 @@ TEST(Parser, SyntaxErrorInValues)
#endif

{
std::string path = pathBase +"/bad_syntax_pose.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_pose.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand All @@ -543,7 +541,7 @@ TEST(Parser, SyntaxErrorInValues)
{
// clear the contents of the buffer
buffer.str("");
std::string path = pathBase +"/bad_syntax_double.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_double.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand All @@ -554,7 +552,7 @@ TEST(Parser, SyntaxErrorInValues)
{
// clear the contents of the buffer
buffer.str("");
std::string path = pathBase +"/bad_syntax_vector.sdf";
const auto path = sdf::testing::TestFile("sdf", "bad_syntax_vector.sdf");
sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);

Expand Down Expand Up @@ -605,8 +603,8 @@ class ValueConstraintsFixture : public ::testing::Test
/// Check if minimum/maximum values are valided
TEST_F(ValueConstraintsFixture, ElementMinMaxValues)
{
std::string sdfDescPath = std::string(PROJECT_SOURCE_PATH) +
"/test/sdf/stricter_semantics_desc.sdf";
const auto sdfDescPath =
sdf::testing::TestFile("sdf", "stricter_semantics_desc.sdf");

auto sdfTest = std::make_shared<sdf::SDF>();
sdf::initFile(sdfDescPath, sdfTest);
Expand Down Expand Up @@ -677,19 +675,9 @@ TEST_F(ValueConstraintsFixture, ElementMinMaxValues)
/// Main
int main(int argc, char **argv)
{
// temporarily set HOME to build directory
#ifndef _WIN32
setenv("HOME", PROJECT_BINARY_DIR, 1);
#else
std::string buildDir = PROJECT_BINARY_DIR;
for (int i = 0; i < buildDir.size(); ++i)
{
if (buildDir[i] == '/')
buildDir[i] = '\\';
}
std::string homePath = "HOMEPATH=" + buildDir;
_putenv(homePath.c_str());
#endif
// temporarily set HOME
std::string homeDir;
sdf::testing::TestHomePath(homeDir);
sdf::Console::Clear();

::testing::InitGoogleTest(&argc, argv);
Expand Down
6 changes: 2 additions & 4 deletions test/integration/actor_dom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
TEST(DOMActor, LoadActors)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"world_complete.sdf");
sdf::testing::TestFile("sdf", "world_complete.sdf");

sdf::Root root;
sdf::Errors errors = root.Load(testFile);
Expand Down Expand Up @@ -145,8 +144,7 @@ TEST(DOMActor, CopySdfLoadedProperties)
// Verify that copying an actor also copies the underlying ElementPtr
// Joints and Links
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"world_complete.sdf");
sdf::testing::TestFile("sdf", "world_complete.sdf");

sdf::Root root;
sdf::Errors errors = root.Load(testFile);
Expand Down
10 changes: 5 additions & 5 deletions test/integration/audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

#include "test_config.h"

const std::string SDF_TEST_FILE =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration",
"audio.sdf");

//////////////////////////////////////////////////
TEST(SDFParser, AudioSDF_FullParameters_noThrow)
{
const auto sdfTestFile =
sdf::testing::TestFile("integration", "audio.sdf");

sdf::SDFPtr p(new sdf::SDF());
sdf::init(p);
ASSERT_TRUE(sdf::readFile(SDF_TEST_FILE, p));
ASSERT_TRUE(sdf::readFile(sdfTestFile, p));
}
8 changes: 4 additions & 4 deletions test/integration/cfm_damping_implicit_spring_damper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#include "test_config.h"

const std::string URDF_TEST_FILE =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration",
"cfm_damping_implicit_spring_damper.urdf");
sdf::testing::TestFile(
"integration", "cfm_damping_implicit_spring_damper.urdf");

const std::string SDF_TEST_FILE =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration",
"cfm_damping_implicit_spring_damper.sdf");
sdf::testing::TestFile(
"integration", "cfm_damping_implicit_spring_damper.sdf");

/////////////////////////////////////////////////
TEST(SDFParser, CFMDampingSDFTest)
Expand Down
6 changes: 2 additions & 4 deletions test/integration/collision_dom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ TEST(DOMCollision, NoName)
TEST(DOMCollision, DoublePendulum)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"double_pendulum.sdf");
sdf::testing::TestFile("sdf", "double_pendulum.sdf");

// Load the SDF file
sdf::Root root;
Expand Down Expand Up @@ -95,8 +94,7 @@ TEST(DOMCollision, DoublePendulum)
TEST(DOMCollision, LoadModelFramesRelativeToJoint)
{
const std::string testFile =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "sdf",
"model_frame_relative_to_joint.sdf");
sdf::testing::TestFile("sdf", "model_frame_relative_to_joint.sdf");

// Load the SDF file
sdf::Root root;
Expand Down
12 changes: 3 additions & 9 deletions test/integration/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
/// \brief Use different sdf versions for ParserStringConverter Test.
void ParserStringConverter(const std::string &_version);

const std::string CONVERT_DOC_15_16 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.6", "1_5.convert");
const std::string CONVERT_DOC_16_17 =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "sdf", "1.7", "1_6.convert");

/////////////////////////////////////////////////
/// Test conversion using the parser sdf file converter interface.
TEST(ConverterIntegration, ParserFileConverter)
{
std::string filename = sdf::filesystem::append(PROJECT_SOURCE_PATH, "test",
"integration", "audio.sdf");
const auto filename = sdf::testing::TestFile("integration", "audio.sdf");

sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);
Expand Down Expand Up @@ -78,8 +72,8 @@ TEST(ConverterIntegration, ParserFileConverter)
/// Convert to a previous SDF version
TEST(ConverterIntegration, convertFileToNotLatestVersion)
{
std::string filename = sdf::filesystem::append(PROJECT_SOURCE_PATH, "test",
"integration", "audio.sdf");
const auto filename = sdf::testing::TestFile(
"integration", "audio.sdf");

sdf::SDFPtr sdf(new sdf::SDF());
sdf::init(sdf);
Expand Down
6 changes: 2 additions & 4 deletions test/integration/disable_fixed_joint_reduction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
#include "test_config.h"

const std::string SDF_FIXED_JNT =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration",
"fixed_joint_reduction.urdf");
sdf::testing::TestFile("integration", "fixed_joint_reduction.urdf");

const std::string SDF_FIXED_JNT_NO_LUMPING =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "test", "integration",
"fixed_joint_reduction_disabled.urdf");
sdf::testing::TestFile("integration", "fixed_joint_reduction_disabled.urdf");

/////////////////////////////////////////////////
bool findJointInModel(const std::string &desired_joint_name, sdf::SDFPtr robot)
Expand Down
5 changes: 2 additions & 3 deletions test/integration/element_memory_leak.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ const std::string sdfString(
" </model>\n"
"</sdf>");

const std::string getMemInfoPath =
sdf::filesystem::append(PROJECT_SOURCE_PATH, "tools", "get_mem_info.py");
const std::string pythonMeminfo("python3 " + getMemInfoPath);
const auto getMemInfoPath = sdf::testing::SourceFile("tools", "get_mem_info.py");
const auto pythonMeminfo("python3 " + getMemInfoPath);

int getMemoryUsage()
{
Expand Down
Loading

0 comments on commit eea81be

Please sign in to comment.