@@ -275,51 +275,138 @@ mod test {
277 fn test_get_surround_pos() {
277 fn test_get_surround_pos() {
279+ let (doc, selection, expectations) =
280+ rope_with_selections_and_expectations(
278− let doc = Rope::from("(some) (chars)\n(newline)");
281+ "(some) (chars)\n(newline)",
279− let slice = doc.slice(..);
282+ "_ ^ _ _ ^ _\n_ ^ _"
280− let selection = Selection::new(
281− SmallVec::from_slice(&[Range::point(2), Range::point(9), Range::point(20)]),
285− // cursor on s[o]me, c[h]ars, newl[i]ne
286 assert_eq!(
285 assert_eq!(
287− get_surround_pos(slice, &selection, Some('('), 1)
286+ get_surround_pos(doc.slice(..), &selection, Some('('), 1).unwrap(),
288− .unwrap()
287+ expectations
290− &[0, 5, 7, 13, 15, 23]
295− fn test_get_surround_pos_bail() {
292+ fn test_get_surround_pos_bail_different_surround_chars() {
294+ let (doc, selection, _) =
295+ rope_with_selections_and_expectations(
296− let doc = Rope::from("[some]\n(chars)xx\n(newline)");
296+ "[some]\n(chars)xx\n(newline)",
297− let slice = doc.slice(..);
297+ " ^ \n ^ \n "
300− Selection::new(SmallVec::from_slice(&[Range::point(2), Range::point(9)]), 0);
301− // cursor on s[o]me, c[h]ars
302 assert_eq!(
300 assert_eq!(
303− get_surround_pos(slice, &selection, Some('('), 1),
301+ get_surround_pos(doc.slice(..), &selection, Some('('), 1),
304− Err(Error::PairNotFound) // different surround chars
302+ Err(Error::PairNotFound)
307+ fn test_get_surround_pos_bail_overlapping_surround_chars() {
309+ let (doc, selection, _) =
310+ rope_with_selections_and_expectations(
311+ "[some]\n(chars)xx\n(newline)",
307− let selection = Selection::new(
308− SmallVec::from_slice(&[Range::point(14), Range::point(24)]),
311− // cursor on [x]x, newli[n]e
312 assert_eq!(
315 assert_eq!(
313− get_surround_pos(slice, &selection, Some('('), 1),
316+ get_surround_pos(doc.slice(..), &selection, Some('('), 1),
314 Err(Error::PairNotFound) // overlapping surround chars
317 Err(Error::PairNotFound) // overlapping surround chars
322+ fn test_get_surround_pos_bail_cursor_overlap() {
324+ let (doc, selection, _) =
325+ rope_with_selections_and_expectations(
326+ "[some]\n(chars)xx\n(newline)",
318− Selection::new(SmallVec::from_slice(&[Range::point(2), Range::point(3)]), 0);
319− // cursor on s[o][m]e
320 assert_eq!(
330 assert_eq!(
321− get_surround_pos(slice, &selection, Some('['), 1),
331+ get_surround_pos(doc.slice(..), &selection, Some('['), 1),
322 Err(Error::CursorOverlap)
332 Err(Error::CursorOverlap)
337+ fn test_find_nth_pairs_pos_quote_success() {
339+ let (doc, selection, expectations) =
340+ rope_with_selections_and_expectations(
341+ "some 'quoted text' on this 'line'\n'and this one'",
345+ assert_eq!(2, expectations.len());
347+ find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1)
348+ .expect("find should succeed"),
349+ (expectations[0], expectations[1])
354+ fn test_find_nth_pairs_pos_nested_quote_success() {
356+ let (doc, selection, expectations) =
357+ rope_with_selections_and_expectations(
358+ "some 'nested 'quoted' text' on this 'line'\n'and this one'",
362+ assert_eq!(2, expectations.len());
364+ find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 2)
365+ .expect("find should succeed"),
366+ (expectations[0], expectations[1])
371+ fn test_find_nth_pairs_pos_inside_quote_ambiguous() {
373+ let (doc, selection, _) =
374+ rope_with_selections_and_expectations(
375+ "some 'nested 'quoted' text' on this 'line'\n'and this one'",
380+ find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1),
381+ Err(Error::CursorOnAmbiguousPair)
385+ // Create a Rope and a matching Selection using a specification language.
386+ // ^ is a single-point selection.
387+ // _ is an expected index. These are returned as a Vec<usize> for use in assertions.
388+ fn rope_with_selections_and_expectations(
391+ ) -> (Rope, Selection, Vec<usize>) {
392+ if text.len() != spec.len() {
393+ panic!("specification must match text length -- are newlines aligned?");
396+ let rope = Rope::from(text);
398+ let selections: SmallVec<[Range; 1]> = spec
401+ .map(|(i, _)| Range::point(i))
404+ let expectations: Vec<usize> = spec
410+ (rope, Selection::new(selections, 0), expectations)