Skip to content

Commit

Permalink
Add a Negation token type to PointerTemplate
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Feb 12, 2025
1 parent 78f9c0a commit 8c911bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ template <typename PointerT> class GenericPointerTemplate {
auto operator==(const Condition &) const noexcept -> bool = default;
auto operator<(const Condition &) const noexcept -> bool { return false; }
};
struct Negation {
auto operator==(const Negation &) const noexcept -> bool = default;
auto operator<(const Negation &) const noexcept -> bool { return false; }
};
using Regex = typename PointerT::Value::String;
using Token = typename PointerT::Token;
using Container =
std::vector<std::variant<Wildcard, Condition, Regex, Token>>;
std::vector<std::variant<Wildcard, Condition, Negation, Regex, Token>>;

/// This constructor creates an empty JSON Pointer template. For example:
///
Expand Down
5 changes: 5 additions & 0 deletions src/core/jsonpointer/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ auto stringify(const PointerT &pointer,
stream.put(internal::token_pointer_tilde<CharT>);
stream.put('?');
stream.put(internal::token_pointer_tilde<CharT>);
} else if (std::holds_alternative<typename PointerT::Negation>(token)) {
stream.put(internal::token_pointer_slash<CharT>);
stream.put(internal::token_pointer_tilde<CharT>);
stream.put('!');
stream.put(internal::token_pointer_tilde<CharT>);
} else {
stringify_token<CharT, Traits, Allocator, typename PointerT::Token>(
std::get<typename PointerT::Token>(token), stream,
Expand Down
42 changes: 42 additions & 0 deletions test/jsonpointer/jsonpointer_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ TEST(JSONPointer_template, equality_with_condition_wildcard_false) {
EXPECT_NE(left, right);
}

TEST(JSONPointer_template, equality_with_negation_wildcard_true) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};
const sourcemeta::core::Pointer suffix{"baz"};

sourcemeta::core::PointerTemplate left{prefix};
left.emplace_back(sourcemeta::core::PointerTemplate::Negation{});
left.push_back(suffix);

sourcemeta::core::PointerTemplate right{prefix};
right.emplace_back(sourcemeta::core::PointerTemplate::Negation{});
right.push_back(suffix);

EXPECT_EQ(left, right);
}

TEST(JSONPointer_template, equality_with_negation_wildcard_false) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};
const sourcemeta::core::Pointer suffix{"baz"};

sourcemeta::core::PointerTemplate left{prefix};
left.emplace_back(sourcemeta::core::PointerTemplate::Negation{});
left.push_back(suffix);

sourcemeta::core::PointerTemplate right{prefix};
right.push_back(suffix);
right.emplace_back(sourcemeta::core::PointerTemplate::Negation{});

EXPECT_NE(left, right);
}

TEST(JSONPointer_template, pop_back) {
const sourcemeta::core::Pointer base{"foo", "bar"};
sourcemeta::core::PointerTemplate pointer{base};
Expand Down Expand Up @@ -244,6 +274,18 @@ TEST(JSONPointer_template, stringify_condition) {
EXPECT_EQ(stream.str(), "/foo/bar/~?~");
}

TEST(JSONPointer_template, stringify_negation) {
const sourcemeta::core::Pointer prefix{"foo", "bar"};

sourcemeta::core::PointerTemplate pointer{prefix};
pointer.emplace_back(sourcemeta::core::PointerTemplate::Negation{});

std::ostringstream stream;
sourcemeta::core::stringify(pointer, stream);

EXPECT_EQ(stream.str(), "/foo/bar/~!~");
}

TEST(JSONPointer_template, concat_move) {
const sourcemeta::core::Pointer pointer_left{"foo"};
const sourcemeta::core::Pointer pointer_right{"bar", "baz"};
Expand Down

0 comments on commit 8c911bc

Please sign in to comment.