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

fix: pop_back was not properly popping the last token when it was the last #19

Merged
merged 1 commit into from
Oct 13, 2023
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
21 changes: 21 additions & 0 deletions src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ impl Delete for Value {
ptr.delete(self)
}
}

#[cfg(test)]
mod tests {
use serde_json::json;

use super::*;

#[test]
fn test_issue_18() {
let mut data = json!(
{
"Example": 21,
"test": "test"
}
);
let pointer = Pointer::new(["Example"]);
println!("{}", pointer);
pointer.delete(&mut data);
assert_eq!(json!({"test": "test"}), data);
}
}
31 changes: 19 additions & 12 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Pointer {
}
}
/// Parses `value` as a JSON Pointer.
///
///
/// # Errors
/// returns `MalformedPointerError` if `value` is not able to be parsed as a
/// valid JSON Pointer.
Expand Down Expand Up @@ -160,18 +160,15 @@ impl Pointer {
.map_or(Some((&self.inner[1..], "")), Option::Some)
.map(|(f, b)| (f.to_owned(), b.to_owned()))
.map(|(front, back)| {
if !front.is_empty() {
self.inner = String::from("/") + &front;
} else {
if back.is_empty() {
self.inner = String::default();
Token::from_encoded(front)
} else {
self.inner = String::from("/") + &front;
Token::from_encoded(back)
}
Token::from_encoded(back)

})

// self.rsplit_once().map(|(front, back)| {
// self.inner = maybe_prepend_slash(&front);

// })
}
/// Removes and returns the first `Token` in the `Pointer` if it exists.
pub fn pop_front(&mut self) -> Option<Token> {
Expand Down Expand Up @@ -1326,6 +1323,16 @@ mod tests {
ptr, "/foo/bar/~1baz",
"pointer should equal \"/foo/bar/~1baz\" after push_back"
);

let mut ptr = Pointer::new(["foo", "bar"]);
assert_eq!(ptr.pop_back(), Some("bar".into()));
assert_eq!(ptr, "/foo", "pointer should equal \"/foo\" after pop_back");
assert_eq!(
ptr.pop_back(),
Some("foo".into()),
"\"foo\" should have been popped from the back"
);
assert_eq!(ptr, "", "pointer should equal \"\" after pop_back");
}

#[test]
Expand Down Expand Up @@ -1368,8 +1375,8 @@ mod tests {
assert_eq!(ptr.pop_back(), Some("bar".into()));
assert_eq!(ptr, "/foo");
assert_eq!(ptr.count(), 1);

ptr.pop_front();
assert_eq!(ptr.pop_front(), Some("foo".into()));
assert_eq!(ptr, "");
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ impl Token {
///
/// ## Parameters
/// - `len` - current length of the array / vector.
/// - `max_size` - a optional maximum allowed size for the array / vector.
///
/// ## Errors
/// - `IndexError::Parse` - if the token is not a valid index.
/// - `IndexError::OutOfBounds` - if the token is a valid index but exceeds
/// `max_size` or `len`.
/// `len`.
///
/// ## Examples
///```
Expand Down