File: nested_multiple_elements_error.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 (175 lines) | stat: -rw-r--r-- 5,738 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
/*
 * Copyright 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 <iostream>
#include <string>
#include <gtest/gtest.h>

#include "sdf/Actor.hh"
#include "sdf/Filesystem.hh"
#include "sdf/Geometry.hh"
#include "sdf/Light.hh"
#include "sdf/Model.hh"
#include "sdf/Root.hh"
#include "sdf/World.hh"
#include "test_config.h"

const auto g_testPath = sdf::testing::TestFile();
const auto g_modelsPath =
    sdf::filesystem::append(g_testPath, "integration", "model");
const auto g_sdfPath = sdf::filesystem::append(g_testPath, "sdf");

/////////////////////////////////////////////////
std::string findFileCb(const std::string &_input)
{
  return sdf::filesystem::append(g_testPath, "integration", "model", _input);
}

//////////////////////////////////////////////////
// Check that an error is emitted if there are multiple models in an included
// file. Despite the error, the first model should be loaded.
TEST(IncludesTest, NestedMultipleModelsError)
{
  sdf::setFindCallback(findFileCb);

  const auto sdfFile =
    sdf::filesystem::append(g_modelsPath, "nested_multiple_models_error");

  sdf::Root root;
  sdf::Errors errors = root.Load(sdfFile);
  ASSERT_FALSE(errors.empty());
  EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());
  EXPECT_EQ("Found more than one model for <include>.", errors[0].Message());

  const auto * model = root.Model();
  ASSERT_NE(nullptr, model);
  EXPECT_EQ("nested_model", model->Name());

  ASSERT_EQ(1u, model->LinkCount());
  EXPECT_EQ("link1", model->LinkByIndex(0)->Name());

  EXPECT_EQ(nullptr, root.Actor());
  EXPECT_EQ(nullptr, root.Light());
}

//////////////////////////////////////////////////
// Check that an error is emitted if there are multiple actors in an included
// file. Despite the error, the first actor should be loaded.
TEST(IncludesTest, NestedMultipleActorsError)
{
  sdf::setFindCallback(findFileCb);

  const auto sdfFile =
    sdf::filesystem::append(g_modelsPath, "nested_multiple_actors_error");

  sdf::Root root;
  sdf::Errors errors = root.Load(sdfFile);
  ASSERT_FALSE(errors.empty());
  EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());
  EXPECT_EQ("Found more than one actor for <include>.", errors[0].Message());

  EXPECT_EQ(nullptr, root.Model());
  EXPECT_EQ(nullptr, root.Light());

  const auto * actor = root.Actor();
  ASSERT_NE(nullptr, actor);
  EXPECT_EQ("nested_actor", actor->Name());
  ASSERT_EQ(1u, actor->LinkCount());
  EXPECT_EQ("link1", actor->LinkByIndex(0)->Name());
}

//////////////////////////////////////////////////
// Check that an error is emitted if there are multiple lights in an included
// file. Despite the error, the first light should be loaded.
TEST(IncludesTest, NestedMultipleLightsError)
{
  sdf::setFindCallback(findFileCb);

  const auto sdfFile =
    sdf::filesystem::append(g_modelsPath, "nested_multiple_lights_error");

  sdf::Root root;
  sdf::Errors errors = root.Load(sdfFile);
  ASSERT_FALSE(errors.empty());
  EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());
  EXPECT_EQ("Found more than one light for <include>.", errors[0].Message());

  EXPECT_EQ(nullptr, root.Model());
  EXPECT_EQ(nullptr, root.Actor());

  const auto * light = root.Light();
  ASSERT_NE(nullptr, light);
  EXPECT_EQ("nested_light", light->Name());
  EXPECT_EQ(ignition::math::Vector3d(1, 0, 0), light->Direction());
}

//////////////////////////////////////////////////
// Check that an error is emitted if there are more than one of model, actor, or
// light in an included file. Despite the error, the
// the first model, actor, light should be loaded in that order of preference.
TEST(IncludesTest, NestedMultipleElementsError)
{
  sdf::setFindCallback(findFileCb);

  const auto sdfFile =
    sdf::filesystem::append(g_modelsPath, "nested_multiple_elements_error");

  sdf::Root root;
  sdf::Errors errors = root.Load(sdfFile);
  ASSERT_FALSE(errors.empty());
  EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());
  EXPECT_EQ(
      "Found other top level element <actor> in addition to <model> in include "
      "file.",
      errors[0].Message());

  EXPECT_EQ(nullptr, root.Light());
  EXPECT_EQ(nullptr, root.Actor());

  const auto * model = root.Model();
  ASSERT_NE(nullptr, model);
  EXPECT_EQ("nested_model", model->Name());
}

//////////////////////////////////////////////////
TEST(IncludesTest, NestedMultipleElementsErrorWorld)
{
  sdf::setFindCallback(findFileCb);

  const auto sdfFile =
    sdf::filesystem::append(
      g_sdfPath, "nested_multiple_elements_error_world.sdf");

  sdf::Root root;
  sdf::Errors errors = root.Load(sdfFile);
  ASSERT_FALSE(errors.empty());
  EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());

  EXPECT_EQ(nullptr, root.Light());
  EXPECT_EQ(nullptr, root.Actor());
  EXPECT_EQ(nullptr, root.Model());

  ASSERT_EQ(1u, root.WorldCount());
  const auto * world = root.WorldByIndex(0);
  ASSERT_NE(nullptr, world);
  ASSERT_EQ(1u, world->ModelCount());

  const auto model = world->ModelByIndex(0);
  EXPECT_EQ("nested_model", model->Name());
  ASSERT_EQ(1u, model->LinkCount());
  EXPECT_EQ("link", model->LinkByIndex(0)->Name());
}