summary refs log tree commit diff
path: root/src/libsyntax/codemap.rs
diff options
context:
space:
mode:
authorJonathan Turner <jturner@mozilla.com>2016-06-23 10:50:05 -0400
committerJonathan Turner <jturner@mozilla.com>2016-06-23 10:50:05 -0400
commit2b8bab095d99a7797f3bed9072175bd256093749 (patch)
treedbefbb48bb2a8602f39f6d91668f697f1711287a /src/libsyntax/codemap.rs
parentb68e079522e7bcaf57587ea0502a0ed94a600bc0 (diff)
downloadrust-2b8bab095d99a7797f3bed9072175bd256093749.tar.gz
rust-2b8bab095d99a7797f3bed9072175bd256093749.zip
Move test helper functions to consolidated codemap testing
Diffstat (limited to 'src/libsyntax/codemap.rs')
-rw-r--r--src/libsyntax/codemap.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs
index da9ba0a464d..1e3e93f5146 100644
--- a/src/libsyntax/codemap.rs
+++ b/src/libsyntax/codemap.rs
@@ -830,6 +830,7 @@ mod tests {
     use syntax_pos::*;
     use errors::{Level, CodeSuggestion};
     use errors::emitter::EmitterWriter;
+    use errors::snippet::SnippetData;
     use std::sync::{Arc, Mutex};
     use std::io::{self, Write};
     use std::str::from_utf8;
@@ -1077,6 +1078,69 @@ mod tests {
                     blork.rs:1:1: 1:12\n  `first line.`\n");
     }
 
+    /// Returns the span corresponding to the `n`th occurrence of
+    /// `substring` in `source_text`.
+    trait CodeMapExtension {
+        fn span_substr(&self,
+                    file: &Rc<FileMap>,
+                    source_text: &str,
+                    substring: &str,
+                    n: usize)
+                    -> Span;
+    }
+
+    impl CodeMapExtension for CodeMap {
+        fn span_substr(&self,
+                    file: &Rc<FileMap>,
+                    source_text: &str,
+                    substring: &str,
+                    n: usize)
+                    -> Span
+        {
+            println!("span_substr(file={:?}/{:?}, substring={:?}, n={})",
+                    file.name, file.start_pos, substring, n);
+            let mut i = 0;
+            let mut hi = 0;
+            loop {
+                let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
+                    panic!("source_text `{}` does not have {} occurrences of `{}`, only {}",
+                        source_text, n, substring, i);
+                });
+                let lo = hi + offset;
+                hi = lo + substring.len();
+                if i == n {
+                    let span = Span {
+                        lo: BytePos(lo as u32 + file.start_pos.0),
+                        hi: BytePos(hi as u32 + file.start_pos.0),
+                        expn_id: NO_EXPANSION,
+                    };
+                    assert_eq!(&self.span_to_snippet(span).unwrap()[..],
+                            substring);
+                    return span;
+                }
+                i += 1;
+            }
+        }
+    }
+
+    fn splice(start: Span, end: Span) -> Span {
+        Span {
+            lo: start.lo,
+            hi: end.hi,
+            expn_id: NO_EXPANSION,
+        }
+    }
+
+    fn make_string(lines: &[RenderedLine]) -> String {
+        lines.iter()
+            .flat_map(|rl| {
+                rl.text.iter()
+                        .map(|s| &s.text[..])
+                        .chain(Some("\n"))
+            })
+            .collect()
+    }
+
     fn init_expansion_chain(cm: &CodeMap) -> Span {
         // Creates an expansion chain containing two recursive calls
         // root -> expA -> expA -> expB -> expB -> end