chanced / jsonptr

JSON Pointer (RFC 6901) implementation for Rust
Apache License 2.0
44 stars 4 forks source link

`Pointer::intersection` fails if one pointer equals or ends with `"/"` #44

Closed chanced closed 1 month ago

chanced commented 2 months ago
    #[test]
    fn test_intersection_issue_44() {
        let base = PointerBuf::new();
        let suffix_0 = Pointer::from_static("/").to_buf();
        let suffix_1 = Pointer::from_static("/a/b/c").to_buf();
        println!("suffix_1: {suffix_1:?}");
        let mut a = base.clone();
        a.append(&suffix_0);
        let mut b = base.clone();
        b.append(&suffix_1);
        let isect = a.intersection(&b);
        assert_eq!(isect, base);
    }

edit: (above was fixed)

This test is also failing:

        let base = PointerBuf::must_parse("/a");
        let suffix_0 = PointerBuf::must_parse("/");
        let suffix_1 = PointerBuf::must_parse("/suffix_1");
        let mut a = base.clone();
        a.append(&suffix_0);
        let mut b = base.clone();
        b.append(&suffix_1);
        let isect = a.intersection(&b);
        assert_eq!(
            isect, base,
            "intersection failed\n\n expected: \"{base}\"\n   actual: \"{isect}\""
        );
 expected: "/a"
   actual: "/a/"
chanced commented 2 months ago

This test is also failing:

        let base = PointerBuf::must_parse("/a");
        let suffix_0 = PointerBuf::must_parse("/");
        let suffix_1 = PointerBuf::must_parse("/suffix_1");
        let mut a = base.clone();
        a.append(&suffix_0);
        let mut b = base.clone();
        b.append(&suffix_1);
        let isect = a.intersection(&b);
        assert_eq!(
            isect, base,
            "intersection failed\n\n expected: \"{base}\"\n   actual: \"{isect}\""
        );
 expected: "/a"
   actual: "/a/"
  left: Pointer("/a/")
 right: PointerBuf("/a")
chanced commented 2 months ago

@asmello I dont know how easily this can be solved with the current logic. We may need to loop through the tokens, comparing them instead of comparing bytes.

asmello commented 2 months ago

Ah, I see the problem... Comparing tokens is probably the way to go, though the case where one of the pointers is root needs special care. Thankfully this is now fairly efficient, since Tokens are borrowed.

chanced commented 2 months ago

I have it partially working using comparison on the "add-tests" branch (pr #48). There's a case that's failing but I don't immediately recognize why.