Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/test.rs')
-rw-r--r--helix-core/src/test.rs157
1 files changed, 18 insertions, 139 deletions
diff --git a/helix-core/src/test.rs b/helix-core/src/test.rs
index 71c71cce..17523ed7 100644
--- a/helix-core/src/test.rs
+++ b/helix-core/src/test.rs
@@ -1,9 +1,7 @@
//! Test helpers.
use crate::{Range, Selection};
-use ropey::Rope;
use smallvec::SmallVec;
use std::cmp::Reverse;
-use unicode_segmentation::UnicodeSegmentation;
/// Convert annotated test string to test string and selection.
///
@@ -12,10 +10,6 @@ use unicode_segmentation::UnicodeSegmentation;
/// `#[` for primary selection with head after anchor followed by `|]#`.
/// `#(` for secondary selection with head after anchor followed by `|)#`.
///
-/// If the selection contains any LF or CRLF sequences, which are immediately
-/// followed by the same grapheme, then the subsequent one is removed. This is
-/// to allow representing having the cursor over the end of the line.
-///
/// # Examples
///
/// ```
@@ -36,23 +30,23 @@ use unicode_segmentation::UnicodeSegmentation;
pub fn print(s: &str) -> (String, Selection) {
let mut primary_idx = None;
let mut ranges = SmallVec::new();
- let mut iter = UnicodeSegmentation::graphemes(s, true).peekable();
+ let mut iter = s.chars().peekable();
let mut left = String::with_capacity(s.len());
'outer: while let Some(c) = iter.next() {
let start = left.chars().count();
- if c != "#" {
- left.push_str(c);
+ if c != '#' {
+ left.push(c);
continue;
}
let (is_primary, close_pair) = match iter.next() {
- Some("[") => (true, "]"),
- Some("(") => (false, ")"),
+ Some('[') => (true, ']'),
+ Some('(') => (false, ')'),
Some(ch) => {
left.push('#');
- left.push_str(ch);
+ left.push(ch);
continue;
}
None => break,
@@ -62,45 +56,24 @@ pub fn print(s: &str) -> (String, Selection) {
panic!("primary `#[` already appeared {:?} {:?}", left, s);
}
- let head_at_beg = iter.next_if_eq(&"|").is_some();
- let last_grapheme = |s: &str| {
- UnicodeSegmentation::graphemes(s, true)
- .next_back()
- .map(String::from)
- };
+ let head_at_beg = iter.next_if_eq(&'|').is_some();
while let Some(c) = iter.next() {
- let next = iter.peek();
- let mut prev = last_grapheme(left.as_str());
-
- if !(c == close_pair && next == Some(&"#")) {
- left.push_str(c);
+ if !(c == close_pair && iter.peek() == Some(&'#')) {
+ left.push(c);
continue;
}
if !head_at_beg {
- match &prev {
- Some(p) if p != "|" => {
- left.push_str(c);
- continue;
- }
- Some(p) if p == "|" => {
- left.pop().unwrap(); // pop the |
- prev = last_grapheme(left.as_str());
- }
- _ => (),
+ let prev = left.pop().unwrap();
+ if prev != '|' {
+ left.push(prev);
+ left.push(c);
+ continue;
}
}
iter.next(); // skip "#"
- let next = iter.peek();
-
- // skip explicit line end inside selection
- if (prev == Some(String::from("\r\n")) || prev == Some(String::from("\n")))
- && next.map(|n| String::from(*n)) == prev
- {
- iter.next();
- }
if is_primary {
primary_idx = Some(ranges.len());
@@ -145,16 +118,14 @@ pub fn print(s: &str) -> (String, Selection) {
/// use smallvec::smallvec;
///
/// assert_eq!(
-/// plain("abc", &Selection::new(smallvec![Range::new(0, 1), Range::new(3, 2)], 0)),
+/// plain("abc", Selection::new(smallvec![Range::new(0, 1), Range::new(3, 2)], 0)),
/// "#[a|]#b#(|c)#".to_owned()
/// );
/// ```
-pub fn plain<R: Into<Rope>>(s: R, selection: &Selection) -> String {
- let s = s.into();
+pub fn plain(s: &str, selection: Selection) -> String {
let primary = selection.primary_index();
- let mut out = String::with_capacity(s.len_bytes() + 5 * selection.len());
- out.push_str(&s.to_string());
-
+ let mut out = String::with_capacity(s.len() + 5 * selection.len());
+ out.push_str(s);
let mut insertion: Vec<_> = selection
.iter()
.enumerate()
@@ -167,9 +138,7 @@ pub fn plain<R: Into<Rope>>(s: R, selection: &Selection) -> String {
(false, false) => [(range.anchor, ")#"), (range.head, "#(|")],
}
})
- .map(|(char_idx, marker)| (s.char_to_byte(char_idx), marker))
.collect();
-
// insert in reverse order
insertion.sort_unstable_by_key(|k| Reverse(k.0));
for (i, s) in insertion {
@@ -293,94 +262,4 @@ mod test {
print("hello #[|πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦]# goodbye")
);
}
-
- #[test]
- fn plain_single() {
- assert_eq!("#[|h]#ello", plain("hello", &Selection::single(1, 0)));
- assert_eq!("#[h|]#ello", plain("hello", &Selection::single(0, 1)));
- assert_eq!("#[|hell]#o", plain("hello", &Selection::single(4, 0)));
- assert_eq!("#[hell|]#o", plain("hello", &Selection::single(0, 4)));
- assert_eq!("#[|hello]#", plain("hello", &Selection::single(5, 0)));
- assert_eq!("#[hello|]#", plain("hello", &Selection::single(0, 5)));
- }
-
- #[test]
- fn plain_multi() {
- assert_eq!(
- plain(
- "hello",
- &Selection::new(
- SmallVec::from_slice(&[Range::new(1, 0), Range::new(5, 4)]),
- 0
- )
- ),
- String::from("#[|h]#ell#(|o)#")
- );
- assert_eq!(
- plain(
- "hello",
- &Selection::new(
- SmallVec::from_slice(&[Range::new(0, 1), Range::new(4, 5)]),
- 0
- )
- ),
- String::from("#[h|]#ell#(o|)#")
- );
- assert_eq!(
- plain(
- "hello",
- &Selection::new(
- SmallVec::from_slice(&[Range::new(2, 0), Range::new(5, 3)]),
- 0
- )
- ),
- String::from("#[|he]#l#(|lo)#")
- );
- assert_eq!(
- plain(
- "hello\r\nhello\r\nhello\r\n",
- &Selection::new(
- SmallVec::from_slice(&[
- Range::new(7, 5),
- Range::new(21, 19),
- Range::new(14, 12)
- ]),
- 0
- )
- ),
- String::from("hello#[|\r\n]#hello#(|\r\n)#hello#(|\r\n)#")
- );
- }
-
- #[test]
- fn plain_multi_byte_code_point() {
- assert_eq!(
- plain("β€žβ€œ", &Selection::single(1, 0)),
- String::from("#[|β€ž]#β€œ")
- );
- assert_eq!(
- plain("β€žβ€œ", &Selection::single(2, 1)),
- String::from("β€ž#[|β€œ]#")
- );
- assert_eq!(
- plain("β€žβ€œ", &Selection::single(0, 1)),
- String::from("#[β€ž|]#β€œ")
- );
- assert_eq!(
- plain("β€žβ€œ", &Selection::single(1, 2)),
- String::from("β€ž#[β€œ|]#")
- );
- assert_eq!(
- plain("they said β€žhelloβ€œ", &Selection::single(11, 10)),
- String::from("they said #[|β€ž]#helloβ€œ")
- );
- }
-
- #[test]
- fn plain_multi_code_point_grapheme() {
- assert_eq!(
- plain("hello πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ goodbye", &Selection::single(13, 6)),
- String::from("hello #[|πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦]# goodbye")
- );
- }
}