GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 59.7% 43 / 0 / 72
Functions: 40.0% 6 / 0 / 15
Branches: 60.3% 79 / 0 / 131

src/utilities/filetypes/StandardsJSON.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 "StandardsJSON.hpp"
7 #include "StandardsJSON_Impl.hpp"
8
9 #include "../core/Json.hpp"
10 #include <json/json.h>
11
12 #include <memory>
13 #include <utility>
14
15 namespace openstudio {
16 namespace detail {
17
18 ✗ StandardsJSON_Impl::StandardsJSON_Impl() = default;
19
20 7 StandardsJSON_Impl::StandardsJSON_Impl(const std::string& s) {
21
1/1
✓ Branch 1 taken 7 times.
7 std::istringstream ss(s);
22
23
1/1
✓ Branch 1 taken 7 times.
7 Json::CharReaderBuilder rbuilder;
24
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
21 rbuilder["collectComments"] = false;
25 7 std::string formattedErrors;
26
27
1/1
✓ Branch 1 taken 7 times.
7 bool parsingSuccessful = Json::parseFromStream(rbuilder, ss, &m_standardsRoot, &formattedErrors);
28
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!parsingSuccessful) {
29 ✗ LOG_AND_THROW("StandardsJSON cannot be processed, " << formattedErrors);
30 }
31
32
3/3
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 4 times.
7 if (!isValidStandardsJSON()) {
33
13/13
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 25 taken 3 times.
✓ Branch 28 taken 3 times.
✓ Branch 31 taken 3 times.
✓ Branch 34 taken 3 times.
✓ Branch 38 taken 3 times.
✓ Branch 41 taken 3 times.
3 LOG_AND_THROW("StandardsJSON isn't valid");
34 }
35 16 }
36
37 7 bool StandardsJSON_Impl::isValidStandardsJSON() const {
38
39
3/3
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 6 times.
7 if (!m_standardsRoot.isObject()) {
40
5/5
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
1 LOG(Warn, "Root isn't an object");
41 1 return false;
42 }
43
44
2/3
✓ Branch 1 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
6 if (m_standardsRoot.getMemberNames().empty()) {
45 ✗ LOG(Warn, "Root doesn't have at least one entry");
46 ✗ return false;
47 }
48
49
2/2
✓ Branch 1 taken 6 times.
✓ Branch 5 taken 6 times.
6 std::string primaryKey = m_standardsRoot.getMemberNames()[0];
50
2/3
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
6 if (!(openstudio::checkType(m_standardsRoot, primaryKey, Json::arrayValue))) {
51 ✗ LOG(Warn, "First entry should be an array");
52 ✗ return false;
53 }
54
55
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 const Json::Value m_standardsArr = m_standardsRoot.get(primaryKey, Json::arrayValue);
56
3/3
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 5 times.
6 if (m_standardsArr.empty()) {
57
5/5
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
1 LOG(Warn, "First entry should be an array with at least one element");
58 1 return false;
59 }
60
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 const Json::Value firstArrEntry = m_standardsArr[0];
61
2/3
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
5 if (!firstArrEntry.isObject()) {
62 ✗ LOG(Warn, "Second level entry should be an object");
63 ✗ return false;
64 }
65 // There shouldn't be any nested levels below that
66
7/7
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 31 times.
✓ Branch 10 taken 30 times.
✓ Branch 13 taken 35 times.
✓ Branch 15 taken 31 times.
✓ Branch 16 taken 4 times.
65 for (const auto& v : firstArrEntry) {
67
7/8
✓ Branch 1 taken 31 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 30 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 30 times.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 30 times.
31 if (v.isArray() || v.isObject()) {
68
5/5
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
1 LOG(Warn, "There shouldn't be any nested objects");
69 1 return false;
70 }
71 }
72
73 4 return true;
74 6 }
75
76 ✗ Json::Value StandardsJSON_Impl::toJSON() const {
77 ✗ return m_standardsRoot;
78 }
79
80 ✗ std::string StandardsJSON_Impl::string() const {
81 ✗ const Json::StreamWriterBuilder wbuilder;
82 ✗ std::string output = Json::writeString(wbuilder, m_standardsRoot);
83 ✗ return output;
84 ✗ }
85
86 3 boost::optional<Json::Value> StandardsJSON_Impl::getPrimaryKey(const std::string& primaryKey) const {
87
88 3 boost::optional<Json::Value> result;
89
90 // Check that primary key is indeed in there, and that the resulting object is an array
91
2/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 if (openstudio::checkKeyAndType(m_standardsRoot, primaryKey, Json::arrayValue)) {
92
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 result = m_standardsRoot.get(primaryKey, Json::arrayValue);
93 } else {
94 ✗ LOG(Warn, "Cannot find the primary key '" << primaryKey << "' in the StandardsJSON");
95 }
96
97 3 return result;
98 ✗ }
99
100 } // namespace detail
101
102 ✗ StandardsJSON::StandardsJSON() : m_impl(std::make_shared<detail::StandardsJSON_Impl>()) {}
103
104 7 StandardsJSON::StandardsJSON(const std::string& s) : m_impl(std::make_shared<detail::StandardsJSON_Impl>(s)) {}
105
106 ✗ StandardsJSON::StandardsJSON(std::shared_ptr<detail::StandardsJSON_Impl> impl) : m_impl(std::move(impl)) {}
107
108 4 boost::optional<StandardsJSON> StandardsJSON::load(const std::string& s) {
109 4 boost::optional<StandardsJSON> result;
110 try {
111
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
4 result = StandardsJSON(s);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } catch (const std::exception&) {
113 3 }
114 4 return result;
115 ✗ }
116
117 ✗ Json::Value StandardsJSON::toJSON() const {
118 ✗ return getImpl<detail::StandardsJSON_Impl>()->toJSON();
119 }
120
121 ✗ std::string StandardsJSON::string() const {
122 ✗ return getImpl<detail::StandardsJSON_Impl>()->string();
123 }
124
125 ✗ StandardsJSON StandardsJSON::clone() const {
126 ✗ return {std::make_shared<detail::StandardsJSON_Impl>(*m_impl)};
127 }
128
129 3 boost::optional<Json::Value> StandardsJSON::getPrimaryKey(const std::string& primaryKey) const {
130
1/1
✓ Branch 3 taken 3 times.
3 return getImpl<detail::StandardsJSON_Impl>()->getPrimaryKey(primaryKey);
131 }
132
133 // ostream operator
134 ✗ std::ostream& operator<<(std::ostream& os, const StandardsJSON& standardsJSON) {
135 ✗ os << standardsJSON.string();
136 ✗ return os;
137 }
138
139 } // namespace openstudio
140