GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 35.0% 79 / 0 / 226
Functions: 20.0% 4 / 0 / 20
Branches: 23.7% 100 / 0 / 422

src/utilities/bcl/BCLMeasureArgument.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "BCLMeasureArgument.hpp"
7
8 #include "../core/Assert.hpp"
9
10 #include <pugixml.hpp>
11 #include <json/json.h>
12
13 namespace openstudio {
14
15 293 BCLMeasureArgument::BCLMeasureArgument(const pugi::xml_node& element) {
16
17 // TODO: escape name
18 // cppcheck-suppress useInitializationList
19
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 7 taken 293 times.
✓ Branch 10 taken 293 times.
293 m_name = element.child("name").text().as_string();
20
21
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 7 taken 293 times.
✓ Branch 10 taken 293 times.
293 m_displayName = element.child("display_name").text().as_string();
22
23 1465 auto getOptionaString = [&element](const char* name) -> boost::optional<std::string> {
24
4/4
✓ Branch 1 taken 1465 times.
✓ Branch 4 taken 1465 times.
✓ Branch 6 taken 287 times.
✓ Branch 7 taken 1178 times.
1465 if (auto subelement = element.child(name)) {
25
3/3
✓ Branch 1 taken 287 times.
✓ Branch 4 taken 287 times.
✓ Branch 7 taken 287 times.
287 const std::string text = subelement.text().as_string();
26
1/2
✓ Branch 1 taken 287 times.
✗ Branch 2 not taken.
287 if (!text.empty()) {
27
1/1
✓ Branch 1 taken 287 times.
287 return {text};
28 }
29 287 }
30 1178 return boost::none;
31 293 };
32
33
1/1
✓ Branch 1 taken 293 times.
293 m_description = getOptionaString("description");
34
35
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 7 taken 293 times.
✓ Branch 10 taken 293 times.
293 m_type = element.child("type").text().as_string();
36
37
1/1
✓ Branch 1 taken 293 times.
293 m_units = getOptionaString("units");
38
39 293 m_required = false;
40
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 7 taken 293 times.
✓ Branch 10 taken 293 times.
293 std::string test = element.child("required").text().as_string();
41
2/3
✓ Branch 1 taken 293 times.
✓ Branch 3 taken 293 times.
✗ Branch 4 not taken.
293 if (test == "true") {
42 293 m_required = true;
43 }
44
45 293 m_modelDependent = false;
46
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 7 taken 293 times.
✓ Branch 10 taken 293 times.
293 test = element.child("model_dependent").text().as_string();
47
2/3
✓ Branch 1 taken 293 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 293 times.
293 if (test == "true") {
48 ✗ m_modelDependent = true;
49 }
50
51
1/1
✓ Branch 1 taken 293 times.
293 m_defaultValue = getOptionaString("default_value");
52
53
4/4
✓ Branch 1 taken 293 times.
✓ Branch 4 taken 293 times.
✓ Branch 6 taken 98 times.
✓ Branch 7 taken 195 times.
293 if (auto subelement = element.child("choices")) {
54
6/6
✓ Branch 1 taken 98 times.
✓ Branch 6 taken 196 times.
✓ Branch 9 taken 196 times.
✓ Branch 12 taken 294 times.
✓ Branch 14 taken 196 times.
✓ Branch 15 taken 98 times.
294 for (auto& choiceElement : subelement.children("choice")) {
55
4/4
✓ Branch 1 taken 196 times.
✓ Branch 4 taken 196 times.
✓ Branch 7 taken 196 times.
✓ Branch 10 taken 196 times.
196 const std::string choiceValue = choiceElement.child("value").text().as_string();
56
1/2
✓ Branch 1 taken 196 times.
✗ Branch 2 not taken.
196 if (!choiceValue.empty()) { // Not exactly the same test as before, probably needs a better test and/or error reporting
57
1/1
✓ Branch 1 taken 196 times.
196 m_choiceValues.push_back(choiceValue);
58
1/1
✓ Branch 1 taken 196 times.
196 auto display_name = choiceElement.child("display_name");
59
2/3
✓ Branch 1 taken 196 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 196 times.
196 if (!display_name) {
60 // DLM: this is technically an invalid file, attempt to fix it here
61 ✗ m_choiceDisplayNames.push_back(choiceValue);
62 } else {
63
3/3
✓ Branch 1 taken 196 times.
✓ Branch 4 taken 196 times.
✓ Branch 7 taken 196 times.
196 m_choiceDisplayNames.emplace_back(display_name.text().as_string());
64 }
65 }
66 196 }
67 }
68
69
1/1
✓ Branch 1 taken 293 times.
293 m_minValue = getOptionaString("min_value");
70
1/1
✓ Branch 1 taken 293 times.
293 m_maxValue = getOptionaString("max_value");
71 293 }
72
73 10 BCLMeasureArgument::BCLMeasureArgument(const std::string& name, const std::string& displayName, const boost::optional<std::string>& description,
74 const std::string& type, const boost::optional<std::string>& units, bool required, bool modelDependent,
75 const boost::optional<std::string>& defaultValue, const std::vector<std::string>& choiceValues,
76 const std::vector<std::string>& choiceDisplayNames, const boost::optional<std::string>& minValue,
77 10 const boost::optional<std::string>& maxValue)
78 10 : m_name(name),
79
1/1
✓ Branch 1 taken 10 times.
10 m_displayName(displayName),
80
1/1
✓ Branch 1 taken 10 times.
10 m_description(description),
81
1/1
✓ Branch 1 taken 10 times.
10 m_type(type),
82
1/1
✓ Branch 1 taken 10 times.
10 m_units(units),
83 10 m_required(required),
84 10 m_modelDependent(modelDependent),
85
1/1
✓ Branch 1 taken 10 times.
10 m_defaultValue(defaultValue),
86
1/1
✓ Branch 1 taken 10 times.
10 m_choiceValues(choiceValues),
87
1/1
✓ Branch 1 taken 10 times.
10 m_choiceDisplayNames(choiceDisplayNames),
88
1/1
✓ Branch 1 taken 10 times.
10 m_minValue(minValue),
89
1/1
✓ Branch 1 taken 10 times.
10 m_maxValue(maxValue) {
90
2/6
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
10 if (!m_choiceValues.empty() && m_choiceDisplayNames.empty()) {
91 ✗ m_choiceDisplayNames = m_choiceValues;
92
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 } else if (m_choiceValues.size() != m_choiceDisplayNames.size()) {
93 ✗ throw openstudio::Exception("Sizes of choiceValues and choiceDisplayNames do not match");
94 }
95 10 }
96
97 ✗ std::string BCLMeasureArgument::name() const {
98 ✗ return m_name;
99 }
100
101 ✗ std::string BCLMeasureArgument::displayName() const {
102 ✗ return m_displayName;
103 }
104
105 ✗ boost::optional<std::string> BCLMeasureArgument::description() const {
106 ✗ return m_description;
107 }
108
109 ✗ std::string BCLMeasureArgument::type() const {
110 ✗ return m_type;
111 }
112
113 ✗ boost::optional<std::string> BCLMeasureArgument::units() const {
114 ✗ return m_units;
115 }
116
117 ✗ bool BCLMeasureArgument::required() const {
118 ✗ return m_required;
119 }
120
121 ✗ bool BCLMeasureArgument::modelDependent() const {
122 ✗ return m_modelDependent;
123 }
124
125 ✗ boost::optional<std::string> BCLMeasureArgument::defaultValue() const {
126 ✗ return m_defaultValue;
127 }
128
129 ✗ std::vector<std::string> BCLMeasureArgument::choiceValues() const {
130 ✗ return m_choiceValues;
131 }
132
133 ✗ std::vector<std::string> BCLMeasureArgument::choiceDisplayNames() const {
134 ✗ return m_choiceDisplayNames;
135 }
136
137 ✗ boost::optional<std::string> BCLMeasureArgument::minValue() const {
138 ✗ return m_minValue;
139 }
140
141 ✗ boost::optional<std::string> BCLMeasureArgument::maxValue() const {
142 ✗ return m_maxValue;
143 }
144
145 15 void BCLMeasureArgument::writeValues(pugi::xml_node& element) const {
146
1/1
✓ Branch 1 taken 15 times.
15 auto subElement = element.append_child("name");
147
1/1
✓ Branch 1 taken 15 times.
15 auto text = subElement.text();
148
1/1
✓ Branch 2 taken 15 times.
15 text.set(m_name.c_str());
149
150
1/1
✓ Branch 1 taken 15 times.
15 subElement = element.append_child("display_name");
151
1/1
✓ Branch 1 taken 15 times.
15 text = subElement.text();
152
1/1
✓ Branch 2 taken 15 times.
15 text.set(m_displayName.c_str());
153
154
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 if (m_description) {
155
1/1
✓ Branch 1 taken 15 times.
15 subElement = element.append_child("description");
156
1/1
✓ Branch 1 taken 15 times.
15 text = subElement.text();
157
2/2
✓ Branch 1 taken 15 times.
✓ Branch 5 taken 15 times.
15 text.set(m_description->c_str());
158 }
159
160
1/1
✓ Branch 1 taken 15 times.
15 subElement = element.append_child("type");
161
1/1
✓ Branch 1 taken 15 times.
15 text = subElement.text();
162
1/1
✓ Branch 2 taken 15 times.
15 text.set(m_type.c_str());
163
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
30 if (m_units) {
165 ✗ subElement = element.append_child("units");
166 ✗ text = subElement.text();
167 ✗ text.set(m_units->c_str());
168 }
169
170
1/1
✓ Branch 1 taken 15 times.
15 subElement = element.append_child("required");
171
1/1
✓ Branch 1 taken 15 times.
15 text = subElement.text();
172
2/3
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15 times.
15 text.set(m_required ? "true" : "false");
173
174
1/1
✓ Branch 1 taken 15 times.
15 subElement = element.append_child("model_dependent");
175
1/1
✓ Branch 1 taken 15 times.
15 text = subElement.text();
176
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 15 times.
15 text.set(m_modelDependent ? "true" : "false");
177
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
30 if (m_defaultValue) {
179 ✗ subElement = element.append_child("default_value");
180 ✗ text = subElement.text();
181 ✗ text.set(m_defaultValue->c_str());
182 }
183
184 15 const unsigned n = m_choiceValues.size();
185
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 4 not taken.
15 OS_ASSERT(n == m_choiceDisplayNames.size());
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (n > 0) {
187 ✗ auto choicesElement = element.append_child("choices");
188 ✗ for (unsigned i = 0; i < n; ++i) {
189 ✗ auto choiceElement = choicesElement.append_child("choice");
190
191 ✗ auto valueElement = choiceElement.append_child("value");
192 ✗ text = valueElement.text();
193 ✗ text.set(m_choiceValues[i].c_str());
194
195 ✗ auto displayNameElement = choiceElement.append_child("display_name");
196 ✗ text = displayNameElement.text();
197 ✗ text.set(m_choiceDisplayNames[i].c_str());
198 }
199 }
200
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
30 if (m_minValue) {
202 ✗ subElement = element.append_child("min_value");
203 ✗ text = subElement.text();
204 ✗ text.set(m_minValue->c_str());
205 }
206
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
30 if (m_maxValue) {
208 ✗ subElement = element.append_child("max_value");
209 ✗ text = subElement.text();
210 ✗ text.set(m_maxValue->c_str());
211 }
212 15 }
213
214 ✗ Json::Value BCLMeasureArgument::toJSON() const {
215 ✗ Json::Value root;
216
217 ✗ root["name"] = m_name;
218 ✗ root["display_name"] = m_displayName;
219
220 ✗ if (m_description) {
221 ✗ root["description"] = *m_description;
222 }
223
224 ✗ root["type"] = m_type;
225
226 ✗ if (m_units) {
227 ✗ root["units"] = *m_units;
228 }
229
230 ✗ root["required"] = m_required;
231 ✗ root["model_dependent"] = m_modelDependent;
232
233 ✗ if (m_defaultValue) {
234 ✗ root["default_value"] = *m_defaultValue;
235 }
236
237 ✗ const size_t n = m_choiceValues.size();
238 ✗ OS_ASSERT(n == m_choiceDisplayNames.size());
239 ✗ if (n > 0) {
240 ✗ auto& choices = root["choices"];
241 ✗ for (size_t i = 0; i < n; ++i) {
242 ✗ auto& choice = choices["choice"];
243 ✗ choice["value"] = m_choiceValues[i];
244 ✗ choice["display_name"] = m_choiceDisplayNames[i];
245 }
246 }
247
248 ✗ if (m_minValue) {
249 ✗ root["min_value"] = *m_minValue;
250 }
251
252 ✗ if (m_maxValue) {
253 ✗ root["max_value"] = *m_maxValue;
254 }
255
256 ✗ return root;
257 ✗ }
258
259 ✗ std::string BCLMeasureArgument::toJSONString() const {
260 ✗ return toJSON().toStyledString();
261 }
262
263 ✗ bool BCLMeasureArgument::operator==(const BCLMeasureArgument& other) const {
264 ✗ if (m_name != other.name()) {
265 ✗ return false;
266 }
267
268 ✗ if (m_displayName != other.displayName()) {
269 ✗ return false;
270 }
271
272 ✗ if (m_description && !other.description()) {
273 ✗ return false;
274 ✗ } else if (!m_description && other.description()) {
275 ✗ return false;
276 ✗ } else if (m_description && other.description()) {
277 ✗ if (m_description.get() != other.description().get()) {
278 ✗ return false;
279 }
280 }
281
282 ✗ if (m_type != other.type()) {
283 ✗ return false;
284 }
285
286 ✗ if (m_units && !other.units()) {
287 ✗ return false;
288 ✗ } else if (!m_units && other.units()) {
289 ✗ return false;
290 ✗ } else if (m_units && other.units()) {
291 ✗ if (m_units.get() != other.units().get()) {
292 ✗ return false;
293 }
294 }
295
296 ✗ if (m_required != other.required()) {
297 ✗ return false;
298 }
299
300 ✗ if (m_modelDependent != other.modelDependent()) {
301 ✗ return false;
302 }
303
304 ✗ if (m_defaultValue && !other.defaultValue()) {
305 ✗ return false;
306 ✗ } else if (!m_defaultValue && other.defaultValue()) {
307 ✗ return false;
308 ✗ } else if (m_defaultValue && other.defaultValue()) {
309 ✗ if (m_defaultValue.get() != other.defaultValue().get()) {
310 ✗ return false;
311 }
312 }
313
314 ✗ unsigned n = m_choiceValues.size();
315 ✗ std::vector<std::string> otherChoiceValues = other.choiceValues();
316 ✗ if (otherChoiceValues.size() != n) {
317 ✗ return false;
318 }
319 ✗ for (unsigned i = 0; i < n; ++i) {
320 ✗ if (m_choiceValues[i] != otherChoiceValues[i]) {
321 ✗ return false;
322 }
323 }
324
325 ✗ n = m_choiceDisplayNames.size();
326 ✗ std::vector<std::string> otherChoiceDisplayNames = other.choiceDisplayNames();
327 ✗ if (otherChoiceDisplayNames.size() != n) {
328 ✗ return false;
329 }
330 ✗ for (unsigned i = 0; i < n; ++i) {
331 ✗ if (m_choiceDisplayNames[i] != otherChoiceDisplayNames[i]) {
332 ✗ return false;
333 }
334 }
335
336 ✗ if (m_minValue && !other.minValue()) {
337 ✗ return false;
338 ✗ } else if (!m_minValue && other.minValue()) {
339 ✗ return false;
340 ✗ } else if (m_minValue && other.minValue()) {
341 ✗ if (m_minValue.get() != other.minValue().get()) {
342 ✗ return false;
343 }
344 }
345
346 ✗ if (m_maxValue && !other.maxValue()) {
347 ✗ return false;
348 ✗ } else if (!m_maxValue && other.maxValue()) {
349 ✗ return false;
350 ✗ } else if (m_maxValue && other.maxValue()) {
351 ✗ if (m_maxValue.get() != other.maxValue().get()) {
352 ✗ return false;
353 }
354 }
355
356 ✗ return true;
357 ✗ }
358
359 ✗ std::ostream& operator<<(std::ostream& os, const BCLMeasureArgument& argument) {
360 ✗ pugi::xml_document doc;
361 ✗ auto element = doc.append_child("Argument");
362 ✗ argument.writeValues(element);
363
364 ✗ doc.save(os, " ");
365 ✗ return os;
366 ✗ }
367
368 } // namespace openstudio
369