Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chanced committed Jul 2, 2024
1 parent 81e6d30 commit 17f082a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand Down
4 changes: 2 additions & 2 deletions src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod tests {
*/
#[test]
#[cfg(feature = "json")]
fn test_delete_json() {
fn delete_json() {
Test::all([
// 0
Test {
Expand Down Expand Up @@ -280,7 +280,7 @@ mod tests {
*/
#[test]
#[cfg(feature = "toml")]
fn test_delete_toml() {
fn delete_toml() {
use toml::{toml, Table, Value};

Test::all([
Expand Down
20 changes: 10 additions & 10 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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(), "-");
}
}
103 changes: 82 additions & 21 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,30 @@ impl PartialEq<&str> for Pointer {
&&self.0 == other
}
}

impl<'p> PartialEq<String> for &'p Pointer {
fn eq(&self, other: &String) -> bool {
self.0.eq(other)
}
}
impl PartialEq<str> for Pointer {
fn eq(&self, other: &str) -> bool {
&self.0 == other
}
}

impl PartialEq<Pointer> for &str {
fn eq(&self, other: &Pointer) -> bool {
*self == (&other.0)
}
}

impl PartialEq<Pointer> for String {
fn eq(&self, other: &Pointer) -> bool {
self == &other.0
}
}


impl PartialEq<Pointer> for str {
fn eq(&self, other: &Pointer) -> bool {
self == &other.0
Expand Down Expand Up @@ -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("/")),
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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"]),
Expand All @@ -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()));
Expand All @@ -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()));

Expand All @@ -1093,17 +1110,35 @@ 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);
let into: PointerBuf = "/foo/bar/~0~1".try_into().unwrap();
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!({
Expand All @@ -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": {
Expand All @@ -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");
Expand All @@ -1161,15 +1196,15 @@ 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());
}
}

#[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"]));
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 17f082a

Please sign in to comment.