extern crate keytree; use keytree::path::{UniquePath, NonUniquePath, PathError}; // Helper function to concisely generate path // fn p(s: &str) -> NonUniquePath { NonUniquePath::from(s).unwrap() } // Path tests #[test] fn non_unique_debug() { assert_eq!(p("a::b").debug(), "a[0]::b[..]"); } #[test] fn unique_debug() { let a = p("a::b").unique(3); assert_eq!(a.debug(), "a[0]::b[3]"); } #[test] fn empty_unique_debug() { let a = UniquePath::new(); assert_eq!(a.debug(), "(empty)"); } #[test] fn path_empty() { assert!(UniquePath::new().is_empty()) } #[test] fn badly_formed_string_1() { let s = "a::::c"; assert_eq!( NonUniquePath::from(s), Err(PathError::Parse(String::from(s), 3)), ) } #[test] fn badly_formed_string_2() { let s = "a::c:"; assert_eq!( NonUniquePath::from(s), Err(PathError::Parse(String::from(s), 5)), ) } #[test] fn badly_formed_string_3() { let s = "::a::c"; assert_eq!( NonUniquePath::from(s), Err(PathError::Parse(String::from(s), 0)), ) } #[test] fn badly_formed_string_4() { let s = ":a::c"; assert_eq!( NonUniquePath::from(s), Err(PathError::Parse(String::from(s), 0)), ) } #[test] fn path_is_empty_1() { assert!(UniquePath::new().is_empty()) } #[test] fn append_to_non_unique() { let a = UniquePath::new(); let b = p("a::b"); assert_eq!( a.append_non_unique(&b).debug(), "a[0]::b[..]" ); } #[test] fn append_empty_and_unique() { let a = UniquePath::new(); let b = p("a::b").unique(3); assert_eq!( a.append_unique(&b).debug(), "a[0]::b[3]" ); } #[test] fn append_to_empty_and_unique() { let a = p("a::b").unique(2); let b = UniquePath::new(); assert_eq!( a.append_unique(&b).debug(), "a[0]::b[2]" ); } #[test] fn append_empty_and_nonunique() { let a = UniquePath::new(); let b = p("a::b"); assert_eq!( a.append_non_unique(&b).debug(), "a[0]::b[..]" ); } #[test] fn append_to_empty() { let a = p("a::b").unique(0); let b = UniquePath::new(); assert_eq!( a.append_unique(&b).debug(), "a[0]::b[0]" ); } #[test] fn non_unique_truncate_1() { let a = p("a::b"); assert!( a .truncate(0) .unwrap() .is_empty() ); } #[test] fn non_unique_truncate_2() { let a = p("a::b"); assert_eq!( a .truncate(1) .unwrap() .debug(), "a[0]" ); } #[test] fn unique_truncate() { let a = p("a::b::c").unique(0); assert!(a .truncate(0) .is_empty() ); } #[test] fn parse_whitespace_1() { let s = "a ::b"; assert_eq!( UniquePath::from(s), Err(PathError::Parse(String::from(s), 1)), ) } #[test] fn parse_whitespace_2() { let s = "a: :b"; assert_eq!( UniquePath::from(s), Err(PathError::Parse(String::from(s), 2)), ) } #[test] fn parse_whitespace_3() { let s = " a::b"; assert_eq!( UniquePath::from(s), Err(PathError::Parse(String::from(s), 0)), ) } #[test] fn parse_whitespace_4() { let s = "a::b "; assert_eq!( UniquePath::from(s), Err(PathError::Parse(String::from(s), 4)), ) } #[test] fn unique_to_non_unique() { assert_eq!( UniquePath::from("a::b") .unwrap() .non_unique() .debug(), "a[0]::b[..]" ) } #[test] fn non_unique_to_unique() { assert_eq!( NonUniquePath::from("a::b") .unwrap() .unique(3) .debug(), "a[0]::b[3]" ) } #[test] fn last_index() { let a = p("a::b::c").unique(2); assert_eq!( a.last_index(), 2, ) } #[test] fn set_last_index() { let mut a = p("a::b::c").unique(2); a.set_last_index(3); assert_eq!( a.debug(), "a[0]::b[0]::c[3]", ) } #[test] fn unique_empty_len() { assert!(UniquePath::new().len() == 0); } #[test] fn nonunique_empty_len() { assert_eq!( UniquePath::new().non_unique().len(), 0 ); } #[test] fn non_unique_len() { assert_eq!(p("a::b").len(), 2) } #[test] fn unique_equality_names() { let a = p("a::b").unique(0); let b = p("a::b").unique(0); assert!( a == b ) } #[test] fn unique_equality_names_2() { let a = p("a::ab").unique(0); let b = p("a::ac").unique(0); assert!( a < b ) } #[test] fn unique_equality_names_3() { let a = p("a::b").unique(0); let b = p("a::b::c").unique(0); assert!( a < b ) } #[test] fn unique_equality_indices_2() { let a = p("a::b").unique(0); let b = p("a::b").unique(1); assert!( a < b ) } #[test] fn non_unique_equality_names() { let a = p("a::b"); let b = p("a::b"); assert!( a == b ) } #[test] fn nonunique_equality_names_2() { let a = p("a::ab"); let b = p("a::ac"); assert!( a < b ) } #[test] fn nonunique_equality_names_3() { let a = p("a::b"); let b = p("a::b::c"); assert!( a < b ) } #[test] fn same_base_1() { let a = p("a::b").unique(0); let b = p("a::b").unique(1); assert!( a.eq_base(&b) ); } #[test] fn unique_remove_1() { let a = p("a::b").unique(0); assert_eq!( a.remove(1).debug(), "a[0]" ) } #[test] fn unique_remove_2() { let a = p("a").unique(0); assert_eq!( a.remove(1).debug(), "(empty)" ) } #[test] fn nonunique_tail_1() { let a = p("a::b"); assert_eq!( a.tail().debug(), "b[..]" ) } #[test] fn nonunique_tail_2() { let a = p("a"); assert_eq!( a.tail().debug(), "(empty)" ) }