From fcb69aa56c737ccdb1942d4e534f0dbe3ef3b167 Mon Sep 17 00:00:00 2001 From: Chance Date: Fri, 13 Oct 2023 13:07:08 -0400 Subject: [PATCH] fix: `pop_back` was not properly popping the last token when it was the last; fixes #18 --- src/delete.rs | 21 +++++++++++++++++++++ src/pointer.rs | 31 +++++++++++++++++++------------ src/token.rs | 3 +-- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/delete.rs b/src/delete.rs index a22eeab..b5f3862 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -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); + } +} diff --git a/src/pointer.rs b/src/pointer.rs index 0a69641..d3825b9 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -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. @@ -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 { @@ -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] @@ -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] diff --git a/src/token.rs b/src/token.rs index 7765ff0..26692f0 100644 --- a/src/token.rs +++ b/src/token.rs @@ -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 ///```