diff --git a/src/assign.rs b/src/assign.rs index 7890b16..11361f8 100644 --- a/src/assign.rs +++ b/src/assign.rs @@ -598,7 +598,7 @@ mod tests { #[test] #[cfg(feature = "json")] - fn test_assign_json() { + fn assign_json() { use alloc::vec; use serde_json::json; Test::all([ @@ -757,7 +757,7 @@ mod tests { #[test] #[cfg(feature = "toml")] - fn test_assign_toml() { + fn assign_toml() { use alloc::vec; use toml::{toml, Table, Value}; Test::all([ diff --git a/src/delete.rs b/src/delete.rs index 8db5850..15be7dc 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -212,7 +212,7 @@ mod tests { */ #[test] #[cfg(feature = "json")] - fn test_delete_json() { + fn delete_json() { Test::all([ // 0 Test { @@ -280,7 +280,7 @@ mod tests { */ #[test] #[cfg(feature = "toml")] - fn test_delete_toml() { + fn delete_toml() { use toml::{toml, Table, Value}; Test::all([ diff --git a/src/index.rs b/src/index.rs index 6747548..8be86ba 100644 --- a/src/index.rs +++ b/src/index.rs @@ -223,33 +223,33 @@ mod tests { } #[test] - fn test_index_try_from_token_num() { + fn index_try_from_token_num() { let token = Token::new("3"); let index = Index::try_from(&token).unwrap(); assert_eq!(index, Index::Num(3)); } #[test] - fn test_index_try_from_token_next() { + fn index_try_from_token_next() { let token = Token::new("-"); let index = Index::try_from(&token).unwrap(); assert_eq!(index, Index::Next); } #[test] - fn test_index_try_from_str_num() { + fn index_try_from_str_num() { let index = Index::try_from("42").unwrap(); assert_eq!(index, Index::Num(42)); } #[test] - fn test_index_try_from_str_next() { + fn index_try_from_str_next() { let index = Index::try_from("-").unwrap(); assert_eq!(index, Index::Next); } #[test] - fn test_index_try_from_string_num() { + fn index_try_from_string_num() { let index = Index::try_from(String::from("7")).unwrap(); assert_eq!(index, Index::Num(7)); } @@ -261,30 +261,30 @@ mod tests { } #[test] - fn test_index_for_len_incl_valid() { + fn index_for_len_incl_valid() { assert_eq!(Index::Num(0).for_len_incl(1), Ok(0)); assert_eq!(Index::Next.for_len_incl(2), Ok(2)); } #[test] - fn test_index_for_len_incl_out_of_bounds() { + fn index_for_len_incl_out_of_bounds() { assert!(Index::Num(2).for_len_incl(1).is_err()); } #[test] - fn test_index_for_len_unchecked() { + fn index_for_len_unchecked() { assert_eq!(Index::Num(10).for_len_unchecked(5), 10); assert_eq!(Index::Next.for_len_unchecked(3), 3); } #[test] - fn test_display_index_num() { + fn display_index_num() { let index = Index::Num(5); assert_eq!(index.to_string(), "5"); } #[test] - fn test_display_index_next() { + fn display_index_next() { assert_eq!(Index::Next.to_string(), "-"); } } diff --git a/src/pointer.rs b/src/pointer.rs index fea41ef..01f1add 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -445,13 +445,30 @@ impl PartialEq<&str> for Pointer { &&self.0 == other } } - +impl<'p> PartialEq for &'p Pointer { + fn eq(&self, other: &String) -> bool { + self.0.eq(other) + } +} impl PartialEq for Pointer { fn eq(&self, other: &str) -> bool { &self.0 == other } } +impl PartialEq for &str { + fn eq(&self, other: &Pointer) -> bool { + *self == (&other.0) + } +} + +impl PartialEq for String { + fn eq(&self, other: &Pointer) -> bool { + self == &other.0 + } +} + + impl PartialEq for str { fn eq(&self, other: &Pointer) -> bool { self == &other.0 @@ -871,19 +888,19 @@ mod tests { let _ = Pointer::from_static("foo/bar"); } #[test] - fn test_strip_suffix() { + fn strip_suffix() { let p = Pointer::new("/example/pointer/to/some/value"); let stripped = p.strip_suffix(Pointer::new("/to/some/value")).unwrap(); assert_eq!(stripped, "/example/pointer"); } #[test] - fn test_strip_prefix() { + fn strip_prefix() { let p = Pointer::new("/example/pointer/to/some/value"); let stripped = p.strip_prefix(Pointer::new("/example/pointer")).unwrap(); assert_eq!(stripped, "/to/some/value"); } #[test] - fn test_parse() { + fn parse() { let tests = [ ("", Ok("")), ("/", Ok("/")), @@ -932,7 +949,7 @@ mod tests { } #[test] - fn test_push_pop_back() { + fn push_pop_back() { let mut ptr = PointerBuf::default(); assert_eq!(ptr, "", "default, root pointer should equal \"\""); assert_eq!(ptr.count(), 0, "default pointer should have 0 tokens"); @@ -963,7 +980,7 @@ mod tests { } #[test] - fn test_replace_token() { + fn replace_token() { let mut ptr = PointerBuf::try_from("/test/token").unwrap(); let res = ptr.replace_token(0, "new".into()); @@ -979,7 +996,7 @@ mod tests { } #[test] - fn test_push_pop_front() { + fn push_pop_front() { let mut ptr = PointerBuf::default(); assert_eq!(ptr, ""); assert_eq!(ptr.count(), 0); @@ -1051,7 +1068,7 @@ mod tests { } #[test] - fn test_formatting() { + fn formatting() { assert_eq!(PointerBuf::from_tokens(["foo", "bar"]), "/foo/bar"); assert_eq!( PointerBuf::from_tokens(["~/foo", "~bar", "/baz"]), @@ -1062,7 +1079,7 @@ mod tests { } #[test] - fn test_last() { + fn last() { let ptr = Pointer::from_static("/foo/bar"); assert_eq!(ptr.last(), Some("bar".into())); @@ -1081,7 +1098,7 @@ mod tests { } #[test] - fn test_first() { + fn first() { let ptr = Pointer::from_static("/foo/bar"); assert_eq!(ptr.first(), Some("foo".into())); @@ -1093,7 +1110,7 @@ mod tests { } #[test] - fn test_pointerbuf_try_from() { + fn pointerbuf_try_from() { let ptr = PointerBuf::from_tokens(["foo", "bar", "~/"]); assert_eq!(PointerBuf::try_from("/foo/bar/~0~1").unwrap(), ptr); @@ -1101,9 +1118,27 @@ mod tests { assert_eq!(ptr, into); } + #[test] + fn default() { + let ptr = PointerBuf::default(); + assert_eq!(ptr, ""); + assert_eq!(ptr.count(), 0); + + let ptr = <&Pointer>::default(); + assert_eq!(ptr, ""); + } + + #[test] + #[cfg(all(feature = "serde", feature = "json"))] + fn to_json_value() { + use serde_json::Value; + let ptr = Pointer::from_static("/foo/bar"); + assert_eq!(ptr.to_json_value(), Value::String(String::from("/foo/bar"))); + } + #[cfg(all(feature = "resolve", feature = "json"))] #[test] - fn test_resolve() { + fn resolve() { // full tests in resolve.rs use serde_json::json; let value = json!({ @@ -1120,7 +1155,7 @@ mod tests { #[cfg(all(feature = "delete", feature = "json"))] #[test] - fn test_delete() { + fn delete() { use serde_json::json; let mut value = json!({ "foo": { @@ -1144,7 +1179,7 @@ mod tests { #[cfg(all(feature = "assign", feature = "json"))] #[test] - fn test_assign() { + fn assign() { use serde_json::json; let mut value = json!({}); let ptr = Pointer::from_static("/foo/bar"); @@ -1161,7 +1196,7 @@ mod tests { } #[test] - fn test_get() { + fn get() { let ptr = Pointer::from_static("/0/1/2/3/4/5/6/7/8/9"); for i in 0..10 { assert_eq!(ptr.get(i).unwrap().decoded(), i.to_string()); @@ -1169,7 +1204,7 @@ mod tests { } #[test] - fn test_replace_token_success() { + fn replace_token_success() { let mut ptr = PointerBuf::from_tokens(["foo", "bar", "baz"]); assert!(ptr.replace_token(1, "qux".into()).is_ok()); assert_eq!(ptr, PointerBuf::from_tokens(["foo", "qux", "baz"])); @@ -1182,21 +1217,21 @@ mod tests { } #[test] - fn test_replace_token_out_of_bounds() { + fn replace_token_out_of_bounds() { let mut ptr = PointerBuf::from_tokens(["foo", "bar"]); assert!(ptr.replace_token(2, "baz".into()).is_err()); assert_eq!(ptr, PointerBuf::from_tokens(["foo", "bar"])); // Ensure original pointer is unchanged } #[test] - fn test_replace_token_with_empty_string() { + fn replace_token_with_empty_string() { let mut ptr = PointerBuf::from_tokens(["foo", "bar", "baz"]); assert!(ptr.replace_token(1, "".into()).is_ok()); assert_eq!(ptr, PointerBuf::from_tokens(["foo", "", "baz"])); } #[test] - fn test_replace_token_in_empty_pointer() { + fn replace_token_in_empty_pointer() { let mut ptr = PointerBuf::default(); assert!(ptr.replace_token(0, "foo".into()).is_err()); assert_eq!(ptr, PointerBuf::default()); // Ensure the pointer remains empty @@ -1399,7 +1434,7 @@ mod tests { #[cfg(all(feature = "json", feature = "std", feature = "serde"))] #[test] - fn test_serde() { + fn serde() { use serde::Deserialize; let ptr = PointerBuf::from_tokens(["foo", "bar"]); let json = serde_json::to_string(&ptr).unwrap(); @@ -1416,10 +1451,36 @@ mod tests { assert_eq!(p, ptr); let s = serde_json::to_string(p).unwrap(); assert_eq!(json, s); + + let invalid = serde_json::from_str::<&Pointer>("\"foo/bar\""); + assert!(invalid.is_err()); + assert_eq!( + invalid.unwrap_err().to_string(), + "failed to parse json pointer\n\ncaused by:\njson pointer is malformed as it does not start with a backslash ('/') at line 1 column 9" + ); + } + + #[test] + fn to_owned(){ + let ptr = Pointer::from_static("/bread/crumbs"); + let buf = ptr.to_owned(); + assert_eq!(buf, "/bread/crumbs"); + } + + + #[test] + #[allow(clippy::cmp_owned)] + fn partial_eq() { + let p = Pointer::from_static("/bread/crumbs"); + assert!(p == "/bread/crumbs"); + assert!(p == String::from("/bread/crumbs")); + assert!(p == p.to_owned()); + assert!(p == Pointer::from_static("/bread/crumbs")); + assert!(p != Pointer::from_static("/not/bread/crumbs/")); } #[test] - fn test_intersection() { + fn intersection() { struct Test { base: &'static str, a_suffix: &'static str, diff --git a/src/resolve.rs b/src/resolve.rs index f11bbd9..822600c 100644 --- a/src/resolve.rs +++ b/src/resolve.rs @@ -454,7 +454,7 @@ mod tests { #[test] #[cfg(feature = "json")] - fn test_resolve_json() { + fn resolve_json() { use serde_json::json; let data = &json!({ @@ -476,7 +476,7 @@ mod tests { " ": 7, "m~n": 8 }); - // let data = &test_data; + // let data = &data; Test::all([ // 0 @@ -567,7 +567,7 @@ mod tests { */ #[test] #[cfg(feature = "toml")] - fn test_resolve_toml() { + fn resolve_toml() { use toml::{toml, Value}; let data = &Value::Table(toml! { @@ -588,7 +588,7 @@ mod tests { " " = 7 "m~n" = 8 }); - // let data = &test_data; + // let data = &data; Test::all([ // 0 diff --git a/src/token.rs b/src/token.rs index 0a3f14e..413e688 100644 --- a/src/token.rs +++ b/src/token.rs @@ -331,13 +331,13 @@ mod tests { use quickcheck_macros::quickcheck; #[test] - fn test_from() { + fn from() { assert_eq!(Token::from("/").encoded(), "~1"); assert_eq!(Token::from("~/").encoded(), "~0~1"); } #[test] - fn test_to_index() { + fn to_index() { assert_eq!(Token::new("-").to_index(), Ok(Index::Next)); assert_eq!(Token::new("0").to_index(), Ok(Index::Num(0))); assert_eq!(Token::new("2").to_index(), Ok(Index::Num(2))); @@ -346,13 +346,13 @@ mod tests { } #[test] - fn test_new() { + fn new() { assert_eq!(Token::new("~1").encoded(), "~01"); assert_eq!(Token::new("a/b").encoded(), "a~1b"); } #[test] - fn test_serde() { + fn serde() { let token = Token::from_encoded("foo~0").unwrap(); let json = serde_json::to_string(&token).unwrap(); assert_eq!(json, "\"foo~\""); @@ -361,7 +361,7 @@ mod tests { } #[test] - fn test_from_encoded() { + fn from_encoded() { assert_eq!(Token::from_encoded("~1").unwrap().encoded(), "~1"); assert_eq!(Token::from_encoded("~0~1").unwrap().encoded(), "~0~1"); let t = Token::from_encoded("a~1b").unwrap();