aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-10-31 08:43:20 +0000
committerSeivan Heidari <[email protected]>2019-10-31 08:43:20 +0000
commit8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (patch)
tree744cf81075d394e2f9c06afb07642a2601800dda /crates/test_utils
parent49562d36b97ddde34cf7585a8c2e8f232519b657 (diff)
parentd067afb064a7fa67b172abf561b7d80740cd6f18 (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs34
1 files changed, 31 insertions, 3 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index c40943b63..1244ea8cf 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -1,4 +1,10 @@
1//! FIXME: write short doc here 1//! Assorted testing utilities.
2//!
3//! Most notable things are:
4//!
5//! * Rich text comparison, which outputs a diff.
6//! * Extracting markup (mainly, `<|>` markers) out of fixture strings.
7//! * marks (see the eponymous module).
2 8
3#[macro_use] 9#[macro_use]
4pub mod marks; 10pub mod marks;
@@ -43,7 +49,7 @@ pub fn extract_offset(text: &str) -> (TextUnit, String) {
43 } 49 }
44} 50}
45 51
46pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { 52fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> {
47 let cursor_pos = text.find(CURSOR_MARKER)?; 53 let cursor_pos = text.find(CURSOR_MARKER)?;
48 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len()); 54 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
49 new_text.push_str(&text[..cursor_pos]); 55 new_text.push_str(&text[..cursor_pos]);
@@ -59,12 +65,34 @@ pub fn extract_range(text: &str) -> (TextRange, String) {
59 } 65 }
60} 66}
61 67
62pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { 68fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
63 let (start, text) = try_extract_offset(text)?; 69 let (start, text) = try_extract_offset(text)?;
64 let (end, text) = try_extract_offset(&text)?; 70 let (end, text) = try_extract_offset(&text)?;
65 Some((TextRange::from_to(start, end), text)) 71 Some((TextRange::from_to(start, end), text))
66} 72}
67 73
74pub enum RangeOrOffset {
75 Range(TextRange),
76 Offset(TextUnit),
77}
78
79impl From<RangeOrOffset> for TextRange {
80 fn from(selection: RangeOrOffset) -> Self {
81 match selection {
82 RangeOrOffset::Range(it) => it,
83 RangeOrOffset::Offset(it) => TextRange::from_to(it, it),
84 }
85 }
86}
87
88pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
89 if let Some((range, text)) = try_extract_range(text) {
90 return (RangeOrOffset::Range(range), text);
91 }
92 let (offset, text) = extract_offset(text);
93 (RangeOrOffset::Offset(offset), text)
94}
95
68/// Extracts ranges, marked with `<tag> </tag>` paris from the `text` 96/// Extracts ranges, marked with `<tag> </tag>` paris from the `text`
69pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) { 97pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
70 let open = format!("<{}>", tag); 98 let open = format!("<{}>", tag);