Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement starts_with_initial for GenericPointer #1314

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,30 @@ template <typename PropertyT> class GenericPointer {
this->data.cbegin());
}

/// Check whether a JSON Pointer starts with the initial part of another JSON
/// Pointer. For example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/jsonpointer.h>
/// #include <cassert>
///
/// const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar", "baz"};
/// const sourcemeta::jsontoolkit::Pointer prefix{"foo", "bar", "qux"};
/// assert(pointer.starts_with_initial(prefix));
/// ```
auto starts_with_initial(const GenericPointer<PropertyT> &other) const
-> bool {
if (!other.empty()) {
for (std::size_t index = 0; index < other.size() - 1; index++) {
if (this->data[index] != other.data[index]) {
return false;
}
}
}

return true;
}

/// Replace a base of a JSON Pointer with another JSON Pointer. For example:
///
/// ```cpp
Expand Down
1 change: 1 addition & 0 deletions test/jsonpointer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_executable(sourcemeta_jsontoolkit_jsonpointer_unit
jsonpointer_to_uri_test.cc
jsonpointer_concat_test.cc
jsonpointer_starts_with_test.cc
jsonpointer_starts_with_initial_test.cc
jsonpointer_rebase_test.cc)
noa_add_default_options(PRIVATE sourcemeta_jsontoolkit_jsonpointer_unit)
target_link_libraries(sourcemeta_jsontoolkit_jsonpointer_unit
Expand Down
81 changes: 81 additions & 0 deletions test/jsonpointer/jsonpointer_starts_with_initial_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <gtest/gtest.h>

#include <sourcemeta/jsontoolkit/jsonpointer.h>

TEST(JSONPointer_starts_with_initial, property_prefix_true) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"foo"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, property_prefix_false) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"bar"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, index_prefix_false) {
const sourcemeta::jsontoolkit::Pointer pointer{0, 1};
const sourcemeta::jsontoolkit::Pointer prefix{1};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, property_same) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"foo", "bar"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, index_same) {
const sourcemeta::jsontoolkit::Pointer pointer{0, 1};
const sourcemeta::jsontoolkit::Pointer prefix{0, 1};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, property_different) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"bar", "baz"};
EXPECT_FALSE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, index_different) {
const sourcemeta::jsontoolkit::Pointer pointer{0, 1};
const sourcemeta::jsontoolkit::Pointer prefix{2, 3};
EXPECT_FALSE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, non_empty_empty) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo"};
const sourcemeta::jsontoolkit::Pointer prefix;
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, empty_non_empty) {
const sourcemeta::jsontoolkit::Pointer pointer;
const sourcemeta::jsontoolkit::Pointer prefix{"foo"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, empty_empty) {
const sourcemeta::jsontoolkit::Pointer pointer;
const sourcemeta::jsontoolkit::Pointer prefix;
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, initial_equal_true) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"foo", "bar", "baz"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, initial_equal_false) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar"};
const sourcemeta::jsontoolkit::Pointer prefix{"foo", "qux", "baz"};
EXPECT_FALSE(pointer.starts_with_initial(prefix));
}

TEST(JSONPointer_starts_with_initial, initial_subset) {
const sourcemeta::jsontoolkit::Pointer pointer{"foo", "bar", "qux"};
const sourcemeta::jsontoolkit::Pointer prefix{"foo", "bar", "baz"};
EXPECT_TRUE(pointer.starts_with_initial(prefix));
}