File: parser_config.cc

package info (click to toggle)
sdformat 12.3.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,980 kB
  • sloc: cpp: 54,706; python: 3,729; javascript: 704; ruby: 366; sh: 97; ansic: 30; makefile: 16
file content (241 lines) | stat: -rw-r--r-- 7,735 bytes parent folder | download
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
/*
 * Copyright (C) 2020 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 <gtest/gtest.h>

#include "sdf/Filesystem.hh"
#include "sdf/Model.hh"
#include "sdf/ParserConfig.hh"
#include "sdf/Root.hh"
#include "sdf/SDFImpl.hh"
#include "sdf/World.hh"
#include "sdf/parser.hh"

#include "test_config.h"

/////////////////////////////////////////////////
/// Test global config
TEST(ParserConfig, GlobalConfig)
{
  // The directory used in AddURIPath must exist in the filesystem, so we'll use
  // the source directory
  const std::string testDir = sdf::testing::SourceFile();

  sdf::addURIPath("file://", testDir);
  sdf::setFindCallback(
      [](const std::string &)
      {
        return "test/dir2";
      });

  const auto &config = sdf::ParserConfig::GlobalConfig();
  auto it = config.URIPathMap().find("file://");
  ASSERT_NE(config.URIPathMap().end(), it);
  ASSERT_EQ(1u, it->second.size());
  EXPECT_EQ(it->second.front(), testDir);

  ASSERT_TRUE(sdf::ParserConfig::GlobalConfig().FindFileCallback());
  EXPECT_EQ("test/dir2",
      sdf::ParserConfig::GlobalConfig().FindFileCallback()("empty"));
  // sdf::findFile requires explicitly enabling callbacks
  EXPECT_EQ("test/dir2", sdf::findFile("empty", false, true));
  EXPECT_EQ("test/dir2", sdf::findFile("empty", true, true));
}

/////////////////////////////////////////////////
/// Test using a non global config with functions like sdf::addURIPath and
/// sdf::setFindCallback
TEST(ParserConfig, NonGlobalConfig)
{
  // Reset global config
  sdf::ParserConfig::GlobalConfig() = sdf::ParserConfig();

  sdf::ParserConfig config;
  // The directory used in AddURIPath must exist in the filesystem, so we'll use
  // the source directory
  const std::string testDir = sdf::testing::SourceFile();
  config.AddURIPath("file://", testDir);
  config.SetFindCallback(
      [](const std::string &)
      {
        return "test/dir2";
      });

  auto it = config.URIPathMap().find("file://");
  ASSERT_NE(config.URIPathMap().end(), it);
  ASSERT_EQ(1u, it->second.size());
  EXPECT_EQ(it->second.front(), testDir);

  ASSERT_TRUE(config.FindFileCallback());
  EXPECT_EQ("test/dir2", config.FindFileCallback()("empty"));
  EXPECT_EQ("test/dir2", sdf::findFile("empty", false, true, config));
  EXPECT_EQ("test/dir2", sdf::findFile("empty", true, true, config));

  EXPECT_TRUE(sdf::ParserConfig::GlobalConfig().URIPathMap().empty());
  EXPECT_FALSE(sdf::ParserConfig::GlobalConfig().FindFileCallback());
}

/////////////////////////////////////////////////
TEST(ParserConfig, ParseWithNonGlobalConfig)
{
  // Reset global config
  sdf::ParserConfig::GlobalConfig() = sdf::ParserConfig();

  // Case 1: Use of sdf::setFindCallback
  {
    const std::string testFile = sdf::testing::TestFile("sdf", "includes.sdf");

    auto findFileCb = [](const std::string &_uri)
    {
      return sdf::testing::TestFile("integration", "model", _uri);
    };

    sdf::ParserConfig config;
    config.SetFindCallback(findFileCb);

    // Parsing testFile without setting FindFileCallback on the global
    // ParserConfig should fail
    {
      sdf::Errors errors;
      sdf::SDFPtr sdf = sdf::readFile(testFile, errors);
      ASSERT_NE(nullptr, sdf);
      ASSERT_NE(nullptr, sdf->Root());
      ASSERT_TRUE(sdf->Root()->HasElement("world"));
      auto world = sdf->Root()->GetElement("world");
      EXPECT_FALSE(world->HasElement("model"));
      std::size_t uriErrorCount = std::count_if(errors.begin(), errors.end(),
          [](const auto &_err)
          {
            return _err.Code() == sdf::ErrorCode::URI_LOOKUP;
          });
      EXPECT_EQ(7u, uriErrorCount);
    }

    {
      sdf::Errors errors;
      sdf::Root root;
      errors = root.Load(testFile);
      auto world = root.WorldByIndex(0);
      ASSERT_NE(nullptr, world);
      EXPECT_EQ(0u, world->ModelCount());
      std::size_t uriErrorCount = std::count_if(errors.begin(), errors.end(),
          [](const auto &_err)
          {
            return _err.Code() == sdf::ErrorCode::URI_LOOKUP;
          });
      EXPECT_EQ(7u, uriErrorCount);
    }


    // Parsing should succeed when using a ParserConfig with the appropriate
    // findFile callback assigned
    {
      sdf::Errors errors;
      sdf::SDFPtr sdf = sdf::readFile(testFile, config, errors);
      ASSERT_NE(nullptr, sdf);
      ASSERT_NE(nullptr, sdf->Root());
      ASSERT_TRUE(sdf->Root()->HasElement("world"));
      auto world = sdf->Root()->GetElement("world");
      EXPECT_TRUE(world->HasElement("model"));
      EXPECT_TRUE(errors.empty());
    }

    {
      sdf::Errors errors;
      sdf::Root root;
      errors = root.Load(testFile, config);
      auto world = root.WorldByIndex(0);
      ASSERT_NE(nullptr, world);
      EXPECT_EQ(3u, world->ModelCount());
      EXPECT_TRUE(errors.empty());
    }
  }

  // Case 2: Use of sdf::addURIPath
  {
    const std::string testSdfString = R"(
    <sdf version="1.6">
      <world name="default">
        <include>
          <uri>testScheme://box</uri> <!-- NOLINT -->
        </include>
      </world>
    </sdf>)";

    sdf::ParserConfig config;
    config.AddURIPath("testScheme://",
        sdf::testing::TestFile("integration", "model"));

    // Parsing testSdfString without setting addURIPath on the global
    // ParserConfig should fail
    {
      sdf::Errors errors;
      sdf::SDFPtr sdf = std::make_shared<sdf::SDF>();
      sdf::init(sdf);
      sdf::readString(testSdfString, sdf, errors);
      ASSERT_NE(nullptr, sdf);
      ASSERT_NE(nullptr, sdf->Root());
      ASSERT_TRUE(sdf->Root()->HasElement("world"));
      auto world = sdf->Root()->GetElement("world");
      EXPECT_FALSE(world->HasElement("model"));
      std::size_t uriErrorCount = std::count_if(errors.begin(), errors.end(),
          [](const auto &_err)
          {
            return _err.Code() == sdf::ErrorCode::URI_LOOKUP;
          });
      EXPECT_EQ(1u, uriErrorCount);
    }
    {
      sdf::Errors errors;
      sdf::Root root;
      errors = root.LoadSdfString(testSdfString);
      auto world = root.WorldByIndex(0);
      ASSERT_NE(nullptr, world);
      EXPECT_EQ(0u, world->ModelCount());
      std::size_t uriErrorCount = std::count_if(errors.begin(), errors.end(),
          [](const auto &_err)
          {
            return _err.Code() == sdf::ErrorCode::URI_LOOKUP;
          });
      EXPECT_EQ(1u, uriErrorCount);
    }

    // Parsing should succeed when using a ParserConfig with the appropriate
    // uri map
    {
      sdf::Errors errors;
      sdf::SDFPtr sdf = std::make_shared<sdf::SDF>();
      sdf::init(sdf);
      sdf::readString(testSdfString, config, sdf, errors);
      ASSERT_NE(nullptr, sdf);
      ASSERT_NE(nullptr, sdf->Root());
      ASSERT_TRUE(sdf->Root()->HasElement("world"));
      auto world = sdf->Root()->GetElement("world");
      EXPECT_TRUE(world->HasElement("model"));
      EXPECT_TRUE(errors.empty());
    }
    {
      sdf::Errors errors;
      sdf::Root root;
      errors = root.LoadSdfString(testSdfString, config);
      auto world = root.WorldByIndex(0);
      ASSERT_NE(nullptr, world);
      EXPECT_EQ(1u, world->ModelCount());
      EXPECT_TRUE(errors.empty());
    }
  }
}