From fcb7a7972106c7c59a35a11911c470de532366ca Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 23 Dec 2016 17:44:28 -0700 Subject: [PATCH] Some unit tests for the RelativeIndexable methods --- src/characters.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/characters.rs b/src/characters.rs index fe13ec8..596da1b 100644 --- a/src/characters.rs +++ b/src/characters.rs @@ -117,3 +117,38 @@ impl RelativeIndexable for str { index } } + +#[test] +fn index_before_is_well_behaved_for_ascii() { + let s = "abc"; + + // Sanity + assert_eq!(s.index_before(0), 0); + assert_eq!(s.index_before(2), 1); + + // An index beyond the string bounds returns the index of the last character in the string. + { + let idx = s.index_before(4); + assert_eq!(idx, 2); + assert!(s.is_char_boundary(idx)); + let last_char = &s[idx ..]; + assert_eq!(last_char.len(), 1); + assert_eq!(last_char.chars().nth(0), Some('c')); + } +} + +#[test] +fn index_after_is_well_behaved_for_ascii() { + let s = "abc"; + + // Sanity + assert_eq!(s.index_after(0), 1); + assert_eq!(s.index_after(2), 3); + + // An index beyond the string bounds returns the length of the string + { + let idx = s.index_after(4); + assert_eq!(idx, s.len()); + assert!(s.is_char_boundary(idx)); + } +}