Move test helper functions to consolidated codemap testing

This commit is contained in:
Jonathan Turner 2016-06-23 10:50:05 -04:00
parent b68e079522
commit 2b8bab095d
3 changed files with 64 additions and 63 deletions

View File

@ -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

View File

@ -692,66 +692,3 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
vec![field("desc", desc_expr),
field("testfn", testfn_expr)])
}
/// 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()
}