1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
* Copyright 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include "sdf/SDFImpl.hh"
#include "sdf/parser.hh"
#include "sdf/Model.hh"
#include "sdf/Joint.hh"
#include "sdf/Link.hh"
#include "sdf/Root.hh"
#include "sdf/World.hh"
#include "sdf/Actor.hh"
#include "sdf/Light.hh"
#include "test_config.h"
//////////////////////////////////////////////////
std::string findFileCb(const std::string &_input)
{
return sdf::testing::TestFile("integration", "model", _input);
}
//////////////////////////////////////////////////
TEST(ElementTracing, NestedModels)
{
const std::string testFile =
sdf::testing::TestFile("sdf", "nested_model.sdf");
sdf::Root root;
auto errors = root.Load(testFile);
EXPECT_TRUE(errors.empty()) << errors;
// Get the first model
const sdf::Model *model = root.Model();
ASSERT_NE(nullptr, model);
const std::string xmlPath = "/sdf/model[@name=\"top_level_model\"]";
sdf::ElementPtr modelElem = model->Element();
ASSERT_NE(nullptr, modelElem);
EXPECT_EQ(testFile, modelElem->FilePath());
ASSERT_TRUE(modelElem->LineNumber().has_value());
EXPECT_EQ(2, modelElem->LineNumber().value());
EXPECT_EQ(xmlPath, modelElem->XmlPath());
EXPECT_EQ(2, static_cast<int>(model->LinkCount()));
EXPECT_EQ(1, static_cast<int>(model->JointCount()));
EXPECT_EQ(1, static_cast<int>(model->ModelCount()));
const std::string parentLinkXmlPath = xmlPath + "/link[@name=\"parent\"]";
const sdf::Link *parentLink = model->LinkByName("parent");
ASSERT_NE(nullptr, parentLink);
sdf::ElementPtr parentLinkElem = parentLink->Element();
ASSERT_NE(nullptr, parentLinkElem);
EXPECT_EQ(testFile, parentLinkElem->FilePath());
ASSERT_TRUE(parentLinkElem->LineNumber().has_value());
EXPECT_EQ(3, parentLinkElem->LineNumber().value());
EXPECT_EQ(parentLinkXmlPath, parentLinkElem->XmlPath());
const std::string childLinkXmlPath = xmlPath + "/link[@name=\"child\"]";
const sdf::Link *childLink = model->LinkByName("child");
ASSERT_NE(nullptr, childLink);
sdf::ElementPtr childLinkElem = childLink->Element();
ASSERT_NE(nullptr, childLinkElem);
EXPECT_EQ(testFile, childLinkElem->FilePath());
ASSERT_TRUE(childLinkElem->LineNumber().has_value());
EXPECT_EQ(4, childLinkElem->LineNumber().value());
EXPECT_EQ(childLinkXmlPath, childLinkElem->XmlPath());
const std::string jointXmlPath =
xmlPath + "/joint[@name=\"top_level_joint\"]";
const sdf::Joint *joint = model->JointByIndex(0);
ASSERT_NE(nullptr, joint);
sdf::ElementPtr jointElem = joint->Element();
ASSERT_NE(nullptr, jointElem);
EXPECT_EQ(testFile, jointElem->FilePath());
ASSERT_TRUE(jointElem->LineNumber().has_value());
EXPECT_EQ(11, jointElem->LineNumber().value());
EXPECT_EQ(jointXmlPath, jointElem->XmlPath());
const std::string nestedModelXmlPath =
xmlPath + "/model[@name=\"nested_model\"]";
const sdf::Model *nestedModel = model->ModelByName("nested_model");
ASSERT_NE(nullptr, nestedModel);
sdf::ElementPtr nestedModelElem = nestedModel->Element();
ASSERT_NE(nullptr, nestedModelElem);
EXPECT_EQ(testFile, nestedModelElem->FilePath());
ASSERT_TRUE(nestedModelElem->LineNumber().has_value());
EXPECT_EQ(5, nestedModelElem->LineNumber().value());
EXPECT_EQ(nestedModelXmlPath, nestedModelElem->XmlPath());
const std::string nestedLinkXmlPath =
nestedModelXmlPath + "/link[@name=\"nested_link01\"]";
const sdf::Link *nestedLink = nestedModel->LinkByName("nested_link01");
ASSERT_NE(nullptr, nestedLink);
sdf::ElementPtr nestedLinkElem = nestedLink->Element();
ASSERT_NE(nullptr, nestedLinkElem);
EXPECT_EQ(testFile, nestedLinkElem->FilePath());
ASSERT_TRUE(nestedLinkElem->LineNumber().has_value());
EXPECT_EQ(6, nestedLinkElem->LineNumber().value());
EXPECT_EQ(nestedLinkXmlPath, nestedLinkElem->XmlPath());
const std::string nestedNestedModelXmlPath =
nestedModelXmlPath + "/model[@name=\"nested_nested_model\"]";
const sdf::Model *nestedNestedModel =
nestedModel->ModelByName("nested_nested_model");
ASSERT_NE(nullptr, nestedNestedModel);
sdf::ElementPtr nestedNestedModelElem = nestedNestedModel->Element();
ASSERT_NE(nullptr, nestedNestedModelElem);
EXPECT_EQ(testFile, nestedNestedModelElem->FilePath());
ASSERT_TRUE(nestedNestedModelElem->LineNumber().has_value());
EXPECT_EQ(7, nestedNestedModelElem->LineNumber().value());
EXPECT_EQ(nestedNestedModelXmlPath, nestedNestedModelElem->XmlPath());
const std::string nestedNestedLinkXmlPath =
nestedNestedModelXmlPath + "/link[@name=\"nested_nested_link01\"]";
const sdf::Link *nestedNestedLink =
nestedNestedModel->LinkByName("nested_nested_link01");
ASSERT_NE(nullptr, nestedNestedLink);
sdf::ElementPtr nestedNestedLinkElem = nestedNestedLink->Element();
ASSERT_NE(nullptr, nestedNestedLinkElem);
EXPECT_EQ(testFile, nestedNestedLinkElem->FilePath());
ASSERT_TRUE(nestedNestedLinkElem->LineNumber().has_value());
EXPECT_EQ(8, nestedNestedLinkElem->LineNumber().value());
EXPECT_EQ(nestedNestedLinkXmlPath, nestedNestedLinkElem->XmlPath());
}
//////////////////////////////////////////////////
TEST(ElementTracing, includes)
{
sdf::setFindCallback(findFileCb);
const auto worldFile = sdf::testing::TestFile("sdf", "includes.sdf");
sdf::Root root;
sdf::Errors errors = root.Load(worldFile);
EXPECT_TRUE(errors.empty()) << errors;
const std::string xmlPath = "/sdf";
sdf::ElementPtr rootElem = root.Element();
ASSERT_NE(nullptr, rootElem);
EXPECT_EQ(worldFile, rootElem->FilePath());
ASSERT_TRUE(rootElem->LineNumber().has_value());
EXPECT_EQ(2, rootElem->LineNumber().value());
EXPECT_EQ(xmlPath, rootElem->XmlPath());
const std::string worldXmlPath = xmlPath + "/world[@name=\"default\"]";
const sdf::World *world = root.WorldByIndex(0);
ASSERT_NE(nullptr, world);
sdf::ElementPtr worldElem = world->Element();
ASSERT_NE(nullptr, worldElem);
EXPECT_EQ(worldFile, worldElem->FilePath());
ASSERT_TRUE(worldElem->LineNumber().has_value());
EXPECT_EQ(3, worldElem->LineNumber().value());
EXPECT_EQ(worldXmlPath, worldElem->XmlPath());
// Actors
const std::string actorFilePath = sdf::testing::TestFile(
"integration", "model", "test_actor", "model.sdf");
const std::string actorXmlPath = "/sdf/actor[@name=\"actor\"]";
const sdf::Actor *actor = world->ActorByIndex(0);
ASSERT_NE(nullptr, actor);
sdf::ElementPtr actorElem = actor->Element();
ASSERT_NE(nullptr, actorElem);
EXPECT_EQ(actorFilePath, actorElem->FilePath());
ASSERT_TRUE(actorElem->LineNumber().has_value());
EXPECT_EQ(4, actorElem->LineNumber().value());
EXPECT_EQ(actorXmlPath, actorElem->XmlPath());
const std::string overrideActorXmlPath =
"/sdf/actor[@name=\"override_actor_name\"]";
const sdf::Actor *overrideActor = world->ActorByIndex(1);
ASSERT_NE(nullptr, overrideActor);
sdf::ElementPtr overrideActorElem = overrideActor->Element();
ASSERT_NE(nullptr, overrideActorElem);
EXPECT_EQ(actorFilePath, overrideActorElem->FilePath());
ASSERT_TRUE(overrideActorElem->LineNumber().has_value());
EXPECT_EQ(4, overrideActorElem->LineNumber().value());
EXPECT_EQ(overrideActorXmlPath, overrideActorElem->XmlPath());
const std::string overrideActorPluginXmlPath =
worldXmlPath + "/include[6]/plugin[0]";
sdf::ElementPtr overrideActorPluginElem =
overrideActorElem->GetElement("plugin");
ASSERT_NE(nullptr, overrideActorPluginElem);
EXPECT_EQ(actorFilePath, overrideActorPluginElem->FilePath());
ASSERT_TRUE(overrideActorPluginElem->LineNumber().has_value());
EXPECT_EQ(40, overrideActorPluginElem->LineNumber().value());
EXPECT_EQ(overrideActorPluginXmlPath, overrideActorPluginElem->XmlPath());
// Lights
const std::string lightFilePath = sdf::testing::TestFile(
"integration", "model", "test_light", "model.sdf");
const std::string lightXmlPath = "/sdf/light[@name=\"point_light\"]";
const sdf::Light *light = world->LightByIndex(0);
ASSERT_NE(nullptr, light);
sdf::ElementPtr lightElem = light->Element();
ASSERT_NE(nullptr, lightElem);
EXPECT_EQ(lightFilePath, lightElem->FilePath());
ASSERT_TRUE(lightElem->LineNumber().has_value());
EXPECT_EQ(3, lightElem->LineNumber().value());
EXPECT_EQ(lightXmlPath, lightElem->XmlPath());
const std::string overrideLightXmlPath =
"/sdf/light[@name=\"override_light_name\"]";
const sdf::Light *overrideLight = world->LightByIndex(1);
ASSERT_NE(nullptr, overrideLight);
sdf::ElementPtr overrideLightElem = overrideLight->Element();
ASSERT_NE(nullptr, overrideLightElem);
EXPECT_EQ(lightFilePath, overrideLightElem->FilePath());
ASSERT_TRUE(overrideLightElem->LineNumber().has_value());
EXPECT_EQ(3, overrideLightElem->LineNumber().value());
EXPECT_EQ(overrideLightXmlPath, overrideLightElem->XmlPath());
// Models
std::string modelFilePath = sdf::testing::TestFile(
"integration", "model", "test_model", "model.sdf");
const std::string modelXmlPath = "/sdf/model[@name=\"test_model\"]";
const sdf::Model *model = world->ModelByIndex(0);
ASSERT_NE(nullptr, model);
sdf::ElementPtr modelElem = model->Element();
ASSERT_NE(nullptr, modelElem);
EXPECT_EQ(modelFilePath, modelElem->FilePath());
ASSERT_TRUE(modelElem->LineNumber().has_value());
EXPECT_EQ(4, modelElem->LineNumber().value());
EXPECT_EQ(modelXmlPath, modelElem->XmlPath());
const std::string overrideModelXmlPath =
"/sdf/model[@name=\"override_model_name\"]";
const sdf::Model *overrideModel = world->ModelByIndex(1);
ASSERT_NE(nullptr, overrideModel);
sdf::ElementPtr overrideModelElem = overrideModel->Element();
ASSERT_NE(nullptr, overrideModelElem);
EXPECT_EQ(modelFilePath, overrideModelElem->FilePath());
ASSERT_TRUE(overrideModelElem->LineNumber().has_value());
EXPECT_EQ(4, overrideModelElem->LineNumber().value());
EXPECT_EQ(overrideModelXmlPath, overrideModelElem->XmlPath());
const std::string overrideModelPluginXmlPath =
worldXmlPath + "/include[1]/plugin[0]";
sdf::ElementPtr overrideModelPluginElem =
overrideModelElem->GetElement("plugin");
ASSERT_NE(nullptr, overrideModelPluginElem);
EXPECT_EQ(modelFilePath, overrideModelPluginElem->FilePath());
ASSERT_TRUE(overrideModelPluginElem->LineNumber().has_value());
EXPECT_EQ(14, overrideModelPluginElem->LineNumber().value());
EXPECT_EQ(overrideModelPluginXmlPath, overrideModelPluginElem->XmlPath());
#ifdef _WIN32
// There is a / in the last argument of TestFile here, as sdf::findFile does
// not sanitize the file paths provided, which in this case is
// 'test_model/model.sdf', therefore Windows machines will assign the path
// '\\path\\to\\integration\\model\\test_model/model.sdf', instead of
// '\\path\\to\\integration\\model\\test_model\\model.sdf'.
// Reference issue #572.
modelFilePath = sdf::testing::TestFile(
"integration", "model", "test_model/model.sdf");
#endif
const std::string overrideModelWithFileXmlPath =
"/sdf/model[@name=\"test_model_with_file\"]";
const sdf::Model *overrideModelWithFile = world->ModelByIndex(2);
ASSERT_NE(nullptr, overrideModelWithFile);
sdf::ElementPtr overrideModelWithFileElem = overrideModelWithFile->Element();
ASSERT_NE(nullptr, overrideModelWithFileElem);
EXPECT_EQ(modelFilePath, overrideModelWithFileElem->FilePath());
ASSERT_TRUE(overrideModelWithFileElem->LineNumber().has_value());
EXPECT_EQ(4, overrideModelWithFileElem->LineNumber().value());
EXPECT_EQ(overrideModelWithFileXmlPath,
overrideModelWithFileElem->XmlPath());
}
|