Skip to content

Commit 2b8bab0

Browse files
author
Jonathan Turner
committed
Move test helper functions to consolidated codemap testing
1 parent b68e079 commit 2b8bab0

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed
File renamed without changes.

src/libsyntax/codemap.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ mod tests {
830830
use syntax_pos::*;
831831
use errors::{Level, CodeSuggestion};
832832
use errors::emitter::EmitterWriter;
833+
use errors::snippet::SnippetData;
833834
use std::sync::{Arc, Mutex};
834835
use std::io::{self, Write};
835836
use std::str::from_utf8;
@@ -1077,6 +1078,69 @@ mod tests {
10771078
blork.rs:1:1: 1:12\n `first line.`\n");
10781079
}
10791080

1081+
/// Returns the span corresponding to the `n`th occurrence of
1082+
/// `substring` in `source_text`.
1083+
trait CodeMapExtension {
1084+
fn span_substr(&self,
1085+
file: &Rc<FileMap>,
1086+
source_text: &str,
1087+
substring: &str,
1088+
n: usize)
1089+
-> Span;
1090+
}
1091+
1092+
impl CodeMapExtension for CodeMap {
1093+
fn span_substr(&self,
1094+
file: &Rc<FileMap>,
1095+
source_text: &str,
1096+
substring: &str,
1097+
n: usize)
1098+
-> Span
1099+
{
1100+
println!("span_substr(file={:?}/{:?}, substring={:?}, n={})",
1101+
file.name, file.start_pos, substring, n);
1102+
let mut i = 0;
1103+
let mut hi = 0;
1104+
loop {
1105+
let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
1106+
panic!("source_text `{}` does not have {} occurrences of `{}`, only {}",
1107+
source_text, n, substring, i);
1108+
});
1109+
let lo = hi + offset;
1110+
hi = lo + substring.len();
1111+
if i == n {
1112+
let span = Span {
1113+
lo: BytePos(lo as u32 + file.start_pos.0),
1114+
hi: BytePos(hi as u32 + file.start_pos.0),
1115+
expn_id: NO_EXPANSION,
1116+
};
1117+
assert_eq!(&self.span_to_snippet(span).unwrap()[..],
1118+
substring);
1119+
return span;
1120+
}
1121+
i += 1;
1122+
}
1123+
}
1124+
}
1125+
1126+
fn splice(start: Span, end: Span) -> Span {
1127+
Span {
1128+
lo: start.lo,
1129+
hi: end.hi,
1130+
expn_id: NO_EXPANSION,
1131+
}
1132+
}
1133+
1134+
fn make_string(lines: &[RenderedLine]) -> String {
1135+
lines.iter()
1136+
.flat_map(|rl| {
1137+
rl.text.iter()
1138+
.map(|s| &s.text[..])
1139+
.chain(Some("\n"))
1140+
})
1141+
.collect()
1142+
}
1143+
10801144
fn init_expansion_chain(cm: &CodeMap) -> Span {
10811145
// Creates an expansion chain containing two recursive calls
10821146
// root -> expA -> expA -> expB -> expB -> end

src/libsyntax/test.rs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -692,66 +692,3 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
692692
vec![field("desc", desc_expr),
693693
field("testfn", testfn_expr)])
694694
}
695-
696-
/// Returns the span corresponding to the `n`th occurrence of
697-
/// `substring` in `source_text`.
698-
trait CodeMapExtension {
699-
fn span_substr(&self,
700-
file: &Rc<FileMap>,
701-
source_text: &str,
702-
substring: &str,
703-
n: usize)
704-
-> Span;
705-
}
706-
707-
impl CodeMapExtension for CodeMap {
708-
fn span_substr(&self,
709-
file: &Rc<FileMap>,
710-
source_text: &str,
711-
substring: &str,
712-
n: usize)
713-
-> Span
714-
{
715-
println!("span_substr(file={:?}/{:?}, substring={:?}, n={})",
716-
file.name, file.start_pos, substring, n);
717-
let mut i = 0;
718-
let mut hi = 0;
719-
loop {
720-
let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
721-
panic!("source_text `{}` does not have {} occurrences of `{}`, only {}",
722-
source_text, n, substring, i);
723-
});
724-
let lo = hi + offset;
725-
hi = lo + substring.len();
726-
if i == n {
727-
let span = Span {
728-
lo: BytePos(lo as u32 + file.start_pos.0),
729-
hi: BytePos(hi as u32 + file.start_pos.0),
730-
expn_id: NO_EXPANSION,
731-
};
732-
assert_eq!(&self.span_to_snippet(span).unwrap()[..],
733-
substring);
734-
return span;
735-
}
736-
i += 1;
737-
}
738-
}
739-
}
740-
741-
fn splice(start: Span, end: Span) -> Span {
742-
Span {
743-
lo: start.lo,
744-
hi: end.hi,
745-
expn_id: NO_EXPANSION,
746-
}
747-
}
748-
749-
fn make_string(lines: &[RenderedLine]) -> String {
750-
lines.iter()
751-
.flat_map(|rl| {
752-
rl.text.iter()
753-
.map(|s| &s.text[..])
754-
.chain(Some("\n"))
755-
})
756-
.collect()
757-
}

0 commit comments

Comments
 (0)