Skip to content

Commit

Permalink
test now passes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielepanozzo committed Oct 8, 2023
1 parent dd94261 commit 4143a7d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
22 changes: 22 additions & 0 deletions data/rules_03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"pointer": "/",
"type": "object",
"required": ["field1"]
},
{
"pointer": "/field1",
"type": "float",
"min": 45
},
{
"pointer": "/object1",
"type": "object",
"required": ["field1"]
},
{
"pointer": "/object1/field1",
"type": "float",
"min": 45
}
]
19 changes: 11 additions & 8 deletions src/jse/jse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ namespace jse
{
if (key == "/")
return pointer;
else if (pointer == "/")
return key;
else
return key + "/" + pointer;
return key + pointer;
}

} // namespace
Expand All @@ -81,7 +83,7 @@ namespace jse
{
// check if the rules have any include
bool include_present = false;
for (auto rule : current)
for (const auto& rule : current)
if (rule.at("type") == "include")
include_present = true;

Expand All @@ -91,7 +93,7 @@ namespace jse

json enriched;
// otherwise, do a round of replacement
for (auto rule : current)
for (const auto& rule : current)
{
// copy all rules that are not include
if (rule.at("type") != "include")
Expand All @@ -103,26 +105,27 @@ namespace jse
{
bool replaced = false;
// the include file could be in any of the include directories
for (auto dir : dirs)
for (const auto& dir : dirs)
{
string spec_file = rule.at("spec_file");
string f = dir + spec_file;
string f = dir + "/" + spec_file;
// check if the file exists
if (std::filesystem::is_regular_file(f))
{
std::ifstream ifs(f);
json include_rules = json::parse(ifs);

// loop over all rules to add the prefix
for (auto i_rule : include_rules)
for (auto& i_rule : include_rules)
{
string prefix = rule.at("pointer");
string pointer = i_rule.at("pointer");
i_rule.at("pointer") = prepend_pointer(pointer,prefix);
string new_pointer = prepend_pointer(pointer,prefix);
i_rule.at("pointer") = new_pointer;
}

// save modified rules
for (auto i_rule : include_rules)
for (const auto& i_rule : include_rules)
enriched.push_back(i_rule);

// one substitution is enough, give up the search over include dirs
Expand Down
19 changes: 9 additions & 10 deletions tests/test_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,25 @@ TEST_CASE("include_rule", "[validator]")
"pointer": "/",
"type": "include",
"spec_file": "rules_02.json"
},
{
"pointer": "/object1",
"type": "include",
"spec_file": "rules_02.json"
}
]
)"_json;
// ,
// {
// "pointer": "/object1",
// "type": "include",
// "spec_file": "rules_02.json"
// }




JSE jse;
jse.include_directories.push_back(root_path);
json new_rules = jse.inject_include(rules);

std::ifstream ifs2(root_path + "/rules_01.json");
std::ifstream ifs2(root_path + "/rules_03.json");
json matching = json::parse(ifs2);

INFO(new_rules);
INFO(matching);

INFO(jse.log2str());
REQUIRE(new_rules == matching);
}
Expand Down

0 comments on commit 4143a7d

Please sign in to comment.