From 4143a7d2a28a6fc574c985ddc19fcae7396836ee Mon Sep 17 00:00:00 2001 From: Daniele Panozzo Date: Sun, 8 Oct 2023 16:41:32 -0400 Subject: [PATCH] test now passes --- data/rules_03.json | 22 ++++++++++++++++++++++ src/jse/jse.cpp | 19 +++++++++++-------- tests/test_validator.cpp | 19 +++++++++---------- 3 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 data/rules_03.json diff --git a/data/rules_03.json b/data/rules_03.json new file mode 100644 index 0000000..ed9a42c --- /dev/null +++ b/data/rules_03.json @@ -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 + } +] \ No newline at end of file diff --git a/src/jse/jse.cpp b/src/jse/jse.cpp index 9d5bf61..44710f2 100644 --- a/src/jse/jse.cpp +++ b/src/jse/jse.cpp @@ -61,8 +61,10 @@ namespace jse { if (key == "/") return pointer; + else if (pointer == "/") + return key; else - return key + "/" + pointer; + return key + pointer; } } // namespace @@ -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; @@ -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") @@ -103,10 +105,10 @@ 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)) { @@ -114,15 +116,16 @@ namespace jse 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 diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index 8d26333..04193d7 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -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); }