Skip to content

Commit 4143a7d

Browse files
test now passes
1 parent dd94261 commit 4143a7d

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

data/rules_03.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"pointer": "/",
4+
"type": "object",
5+
"required": ["field1"]
6+
},
7+
{
8+
"pointer": "/field1",
9+
"type": "float",
10+
"min": 45
11+
},
12+
{
13+
"pointer": "/object1",
14+
"type": "object",
15+
"required": ["field1"]
16+
},
17+
{
18+
"pointer": "/object1/field1",
19+
"type": "float",
20+
"min": 45
21+
}
22+
]

src/jse/jse.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ namespace jse
6161
{
6262
if (key == "/")
6363
return pointer;
64+
else if (pointer == "/")
65+
return key;
6466
else
65-
return key + "/" + pointer;
67+
return key + pointer;
6668
}
6769

6870
} // namespace
@@ -81,7 +83,7 @@ namespace jse
8183
{
8284
// check if the rules have any include
8385
bool include_present = false;
84-
for (auto rule : current)
86+
for (const auto& rule : current)
8587
if (rule.at("type") == "include")
8688
include_present = true;
8789

@@ -91,7 +93,7 @@ namespace jse
9193

9294
json enriched;
9395
// otherwise, do a round of replacement
94-
for (auto rule : current)
96+
for (const auto& rule : current)
9597
{
9698
// copy all rules that are not include
9799
if (rule.at("type") != "include")
@@ -103,26 +105,27 @@ namespace jse
103105
{
104106
bool replaced = false;
105107
// the include file could be in any of the include directories
106-
for (auto dir : dirs)
108+
for (const auto& dir : dirs)
107109
{
108110
string spec_file = rule.at("spec_file");
109-
string f = dir + spec_file;
111+
string f = dir + "/" + spec_file;
110112
// check if the file exists
111113
if (std::filesystem::is_regular_file(f))
112114
{
113115
std::ifstream ifs(f);
114116
json include_rules = json::parse(ifs);
115117

116118
// loop over all rules to add the prefix
117-
for (auto i_rule : include_rules)
119+
for (auto& i_rule : include_rules)
118120
{
119121
string prefix = rule.at("pointer");
120122
string pointer = i_rule.at("pointer");
121-
i_rule.at("pointer") = prepend_pointer(pointer,prefix);
123+
string new_pointer = prepend_pointer(pointer,prefix);
124+
i_rule.at("pointer") = new_pointer;
122125
}
123126

124127
// save modified rules
125-
for (auto i_rule : include_rules)
128+
for (const auto& i_rule : include_rules)
126129
enriched.push_back(i_rule);
127130

128131
// one substitution is enough, give up the search over include dirs

tests/test_validator.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,26 +227,25 @@ TEST_CASE("include_rule", "[validator]")
227227
"pointer": "/",
228228
"type": "include",
229229
"spec_file": "rules_02.json"
230+
},
231+
{
232+
"pointer": "/object1",
233+
"type": "include",
234+
"spec_file": "rules_02.json"
230235
}
231236
]
232237
)"_json;
233-
// ,
234-
// {
235-
// "pointer": "/object1",
236-
// "type": "include",
237-
// "spec_file": "rules_02.json"
238-
// }
239-
240-
241-
242238

243239
JSE jse;
244240
jse.include_directories.push_back(root_path);
245241
json new_rules = jse.inject_include(rules);
246242

247-
std::ifstream ifs2(root_path + "/rules_01.json");
243+
std::ifstream ifs2(root_path + "/rules_03.json");
248244
json matching = json::parse(ifs2);
249245

246+
INFO(new_rules);
247+
INFO(matching);
248+
250249
INFO(jse.log2str());
251250
REQUIRE(new_rules == matching);
252251
}

0 commit comments

Comments
 (0)