Skip to content

Commit

Permalink
DEBUG
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Feb 12, 2025
1 parent 50b3417 commit 8b72f38
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ template <typename PointerT> class GenericPointerTemplate {
/// ```
template <class... Args> auto emplace_back(Args &&...args) -> reference {
// It is a logical error to push a token after a key wildcard
assert(this->data.empty() ||
assert(this->empty() ||
!std::holds_alternative<Wildcard>(this->data.back()) ||
std::get<Wildcard>(this->data.back()) != Wildcard::Key);
return this->data.emplace_back(args...);
Expand All @@ -133,7 +133,7 @@ template <typename PointerT> class GenericPointerTemplate {
/// ```
auto push_back(const PointerT &other) -> void {
// It is a logical error to push a token after a key wildcard
assert(this->data.empty() ||
assert(this->empty() ||
!std::holds_alternative<Wildcard>(this->data.back()) ||
std::get<Wildcard>(this->data.back()) != Wildcard::Key);
this->data.reserve(this->data.size() + other.size());
Expand All @@ -150,7 +150,7 @@ template <typename PointerT> class GenericPointerTemplate {
/// pointer.pop_back();
/// ```
auto pop_back() -> void {
assert(!this->data.empty());
assert(!this->empty());
this->data.pop_back();
}

Expand Down Expand Up @@ -182,6 +182,20 @@ template <typename PointerT> class GenericPointerTemplate {
return result;
}

/// Check if a JSON Pointer template is empty.
/// For example:
///
/// ```cpp
/// #include <sourcemeta/core/jsonpointer.h>
/// #include <cassert>
///
/// const sourcemeta::core::PointerTemplate empty_pointer;
/// assert(empty_pointer.empty());
/// ```
[[nodiscard]] auto empty() const noexcept -> bool {
return this->data.empty();
}

/// Compare JSON Pointer template instances
auto operator==(const GenericPointerTemplate<PointerT> &other) const noexcept
-> bool {
Expand Down
10 changes: 6 additions & 4 deletions src/core/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ static auto repopulate_instance_locations(
sourcemeta::core::SchemaFrame::Instances::mapped_type &destination,
const std::optional<sourcemeta::core::PointerTemplate> &accumulator)
-> void {
if (cache_entry.orphan
// TODO: Implement an .empty() method
&& cache_entry.instance_location == sourcemeta::core::PointerTemplate{}) {
if (cache_entry.orphan && cache_entry.instance_location.empty()) {
return;
} else if (cache_entry.parent.has_value()) {
} else if (cache_entry.parent.has_value() &&
// Don't consider bases from the root subschema, as if that
// subschema has any instance location other than "", then it
// indicates a recursive reference
!cache_entry.parent.value().empty()) {
const auto match{instances.find(cache_entry.parent.value())};
if (match == instances.cend()) {
return;
Expand Down
11 changes: 11 additions & 0 deletions test/jsonpointer/jsonpointer_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,14 @@ TEST(JSONPointer_template, concat_move) {

EXPECT_EQ(result, expected);
}

TEST(JSONPointer_template, empty_true) {
const sourcemeta::core::PointerTemplate pointer;
EXPECT_TRUE(pointer.empty());
}

TEST(JSONPointer_template, empty_false) {
const sourcemeta::core::Pointer base{"foo"};
const sourcemeta::core::PointerTemplate pointer{base};
EXPECT_FALSE(pointer.empty());
}
40 changes: 17 additions & 23 deletions test/jsonschema/jsonschema_frame_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2157,20 +2157,17 @@ TEST(JSONSchema_frame_2020_12, dynamic_ref_multiple_targets) {
frame, "https://www.example.com", "https://www.example.com", "",
"https://www.example.com", "", POINTER_TEMPLATES("", "/bar"),
std::nullopt);

// TODO: Instance locations here are wrong
// EXPECT_FRAME_STATIC_2020_12_RESOURCE(
// frame, "https://www.example.com/foo", "https://www.example.com",
// "/properties/foo", "https://www.example.com/foo", "",
// POINTER_TEMPLATES("/foo", "/bar"), "");
EXPECT_FRAME_STATIC_2020_12_RESOURCE(
frame, "https://www.example.com/foo", "https://www.example.com",
"/properties/foo", "https://www.example.com/foo", "",
POINTER_TEMPLATES("/foo", "/bar"), "");

// Subschemas

// TODO: Instance locations here are wrong
// EXPECT_FRAME_STATIC_2020_12_SUBSCHEMA(
// frame, "https://www.example.com#/properties/foo",
// "https://www.example.com", "/properties/foo",
// "https://www.example.com/foo", "", POINTER_TEMPLATES("/foo", "/bar"), "");
EXPECT_FRAME_STATIC_2020_12_SUBSCHEMA(
frame, "https://www.example.com#/properties/foo",
"https://www.example.com", "/properties/foo",
"https://www.example.com/foo", "", POINTER_TEMPLATES("/foo", "/bar"), "");
EXPECT_FRAME_STATIC_2020_12_SUBSCHEMA(
frame, "https://www.example.com#/properties/bar",
"https://www.example.com", "/properties/bar", "https://www.example.com",
Expand Down Expand Up @@ -2216,27 +2213,24 @@ TEST(JSONSchema_frame_2020_12, dynamic_ref_multiple_targets) {

// Anchors

// TODO: Static variants should not cover dynamic cases
EXPECT_FRAME_STATIC_2020_12_ANCHOR(
frame, "https://www.example.com#test", "https://www.example.com", "",
"https://www.example.com", "", POINTER_TEMPLATES("", "/bar"),
std::nullopt);

// TODO: Instance locations here are wrong
// EXPECT_FRAME_STATIC_2020_12_ANCHOR(
// frame, "https://www.example.com/foo#test", "https://www.example.com",
// "/properties/foo", "https://www.example.com/foo", "",
// POINTER_TEMPLATES("/foo", "/bar"), "");
EXPECT_FRAME_STATIC_2020_12_ANCHOR(
frame, "https://www.example.com/foo#test", "https://www.example.com",
"/properties/foo", "https://www.example.com/foo", "",
POINTER_TEMPLATES("/foo", "/bar"), "");

EXPECT_FRAME_DYNAMIC_2020_12_ANCHOR(
frame, "https://www.example.com#test", "https://www.example.com", "",
"https://www.example.com", "", POINTER_TEMPLATES("", "/bar"),
std::nullopt);

// TODO: Instance locations here are wrong
// EXPECT_FRAME_DYNAMIC_2020_12_ANCHOR(
// frame, "https://www.example.com/foo#test", "https://www.example.com",
// "/properties/foo", "https://www.example.com/foo", "",
// POINTER_TEMPLATES("/foo", "/bar"), "");
EXPECT_FRAME_DYNAMIC_2020_12_ANCHOR(
frame, "https://www.example.com/foo#test", "https://www.example.com",
"/properties/foo", "https://www.example.com/foo", "",
POINTER_TEMPLATES("/foo", "/bar"), "");

// References

Expand Down

0 comments on commit 8b72f38

Please sign in to comment.