From 286b4596d605ceaecc1a4d170632c172ecdbdb54 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Fri, 4 Sep 2020 16:17:01 -0700 Subject: [PATCH] Trim whitespace from uri element text This copies the whitespace test from #359 but takes a narrower approach by only trimming whitespace from `` element text. Closes #322. Signed-off-by: Steve Peters --- src/Material.cc | 1 + src/parser.cc | 3 +- test/integration/CMakeLists.txt | 1 + test/integration/whitespace.cc | 64 +++++++++++++++++++++++++++++++++ test/sdf/whitespace.sdf | 40 +++++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 test/integration/whitespace.cc create mode 100644 test/sdf/whitespace.sdf diff --git a/src/Material.cc b/src/Material.cc index dd465a942..c509d0d5b 100644 --- a/src/Material.cc +++ b/src/Material.cc @@ -135,6 +135,7 @@ Errors Material::Load(sdf::ElementPtr _sdf) std::pair uriPair = elem->Get("uri", ""); if (uriPair.first == "__default__") uriPair.first = ""; + uriPair.first = sdf::trim(uriPair.first); if (!uriPair.second || uriPair.first.empty()) { diff --git a/src/parser.cc b/src/parser.cc index 3bcc7bc71..84b741961 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -915,7 +915,8 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf, Errors &_errors) if (elemXml->FirstChildElement("uri")) { - std::string uri = elemXml->FirstChildElement("uri")->GetText(); + std::string uri = + sdf::trim(elemXml->FirstChildElement("uri")->GetText()); modelPath = sdf::findFile(uri, true, true); // Test the model path diff --git a/test/integration/CMakeLists.txt b/test/integration/CMakeLists.txt index d039ba030..42fd7d6e8 100644 --- a/test/integration/CMakeLists.txt +++ b/test/integration/CMakeLists.txt @@ -40,6 +40,7 @@ set(tests urdf_gazebo_extensions.cc urdf_joint_parameters.cc visual_dom.cc + whitespace.cc world_dom.cc ) diff --git a/test/integration/whitespace.cc b/test/integration/whitespace.cc new file mode 100644 index 000000000..8389e7296 --- /dev/null +++ b/test/integration/whitespace.cc @@ -0,0 +1,64 @@ +/* + * Copyright 2019 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 +#include +#include + +#include "sdf/Actor.hh" +#include "sdf/Collision.hh" +#include "sdf/Filesystem.hh" +#include "sdf/Geometry.hh" +#include "sdf/Light.hh" +#include "sdf/Link.hh" +#include "sdf/Mesh.hh" +#include "sdf/Model.hh" +#include "sdf/parser.hh" +#include "sdf/Root.hh" +#include "sdf/SDFImpl.hh" +#include "sdf/Visual.hh" +#include "sdf/World.hh" +#include "test_config.h" + +const auto g_testPath = sdf::filesystem::append(PROJECT_SOURCE_PATH, "test"); +const auto g_modelsPath = + sdf::filesystem::append(g_testPath, "integration", "model"); + +///////////////////////////////////////////////// +std::string findFileCb(const std::string &_input) +{ + return sdf::filesystem::append(g_testPath, "integration", "model", _input); +} + +////////////////////////////////////////////////// +TEST(WhitespaceTest, Whitespace) +{ + sdf::setFindCallback(findFileCb); + + const auto worldFile = + sdf::filesystem::append(g_testPath, "sdf", "whitespace.sdf"); + + sdf::Root root; + sdf::Errors errors = root.Load(worldFile); + for (auto e : errors) + std::cout << e.Message() << std::endl; + EXPECT_TRUE(errors.empty()); + + ASSERT_NE(nullptr, root.Element()); + EXPECT_EQ(worldFile, root.Element()->FilePath()); + EXPECT_EQ("1.6", root.Element()->OriginalVersion()); +} diff --git a/test/sdf/whitespace.sdf b/test/sdf/whitespace.sdf new file mode 100644 index 000000000..fc7cf95c9 --- /dev/null +++ b/test/sdf/whitespace.sdf @@ -0,0 +1,40 @@ + + + + + + test_model + + + + + test_model + + override_model_name + + + + test_light + + + + + test_light + + override_light_name + 4 5 6 0 0 0 + + + + test_actor + + + + + test_actor + + override_actor_name + + + +