Skip to content

Commit f2fe204

Browse files
author
Jonathan Turner
committed
Consolidate codemap tests and fix more errors for travis
1 parent 51deb4f commit f2fe204

File tree

6 files changed

+828
-863
lines changed

6 files changed

+828
-863
lines changed

src/librustc/session/config.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1421,12 +1421,11 @@ mod tests {
14211421
use middle::cstore::DummyCrateStore;
14221422
use session::config::{build_configuration, build_session_options};
14231423
use session::build_session;
1424-
1424+
use errors;
14251425
use std::rc::Rc;
14261426
use getopts::{getopts, OptGroup};
14271427
use syntax::attr;
14281428
use syntax::attr::AttrMetaMethods;
1429-
use syntax::diagnostics;
14301429

14311430
fn optgroups() -> Vec<OptGroup> {
14321431
super::rustc_optgroups().into_iter()
@@ -1443,7 +1442,7 @@ mod tests {
14431442
Ok(m) => m,
14441443
Err(f) => panic!("test_switch_implies_cfg_test: {}", f)
14451444
};
1446-
let registry = diagnostics::registry::Registry::new(&[]);
1445+
let registry = errors::registry::Registry::new(&[]);
14471446
let sessopts = build_session_options(matches);
14481447
let sess = build_session(sessopts, &dep_graph, None, registry, Rc::new(DummyCrateStore));
14491448
let cfg = build_configuration(&sess);
@@ -1463,7 +1462,7 @@ mod tests {
14631462
panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f)
14641463
}
14651464
};
1466-
let registry = diagnostics::registry::Registry::new(&[]);
1465+
let registry = errors::registry::Registry::new(&[]);
14671466
let sessopts = build_session_options(matches);
14681467
let sess = build_session(sessopts, &dep_graph, None, registry,
14691468
Rc::new(DummyCrateStore));
@@ -1480,7 +1479,7 @@ mod tests {
14801479
let matches = getopts(&[
14811480
"-Awarnings".to_string()
14821481
], &optgroups()).unwrap();
1483-
let registry = diagnostics::registry::Registry::new(&[]);
1482+
let registry = errors::registry::Registry::new(&[]);
14841483
let sessopts = build_session_options(&matches);
14851484
let sess = build_session(sessopts, &dep_graph, None, registry,
14861485
Rc::new(DummyCrateStore));
@@ -1492,7 +1491,7 @@ mod tests {
14921491
"-Awarnings".to_string(),
14931492
"-Dwarnings".to_string()
14941493
], &optgroups()).unwrap();
1495-
let registry = diagnostics::registry::Registry::new(&[]);
1494+
let registry = errors::registry::Registry::new(&[]);
14961495
let sessopts = build_session_options(&matches);
14971496
let sess = build_session(sessopts, &dep_graph, None, registry,
14981497
Rc::new(DummyCrateStore));
@@ -1503,7 +1502,7 @@ mod tests {
15031502
let matches = getopts(&[
15041503
"-Adead_code".to_string()
15051504
], &optgroups()).unwrap();
1506-
let registry = diagnostics::registry::Registry::new(&[]);
1505+
let registry = errors::registry::Registry::new(&[]);
15071506
let sessopts = build_session_options(&matches);
15081507
let sess = build_session(sessopts, &dep_graph, None, registry,
15091508
Rc::new(DummyCrateStore));

src/librustc_errors/emitter.rs

+1-254
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl EmitterWriter {
358358
Ok(())
359359
}
360360

361-
fn highlight_lines(&mut self,
361+
pub fn highlight_lines(&mut self,
362362
msp: &MultiSpan,
363363
lvl: Level)
364364
-> io::Result<()>
@@ -619,256 +619,3 @@ impl Write for Destination {
619619
}
620620
}
621621
}
622-
623-
624-
#[cfg(test)]
625-
mod test {
626-
use errors::{Level, CodeSuggestion};
627-
use super::EmitterWriter;
628-
use codemap::CodeMap;
629-
use syntax_pos::{mk_sp, Span, MultiSpan, BytePos, NO_EXPANSION};
630-
use std::sync::{Arc, Mutex};
631-
use std::io::{self, Write};
632-
use std::str::from_utf8;
633-
use std::rc::Rc;
634-
635-
struct Sink(Arc<Mutex<Vec<u8>>>);
636-
impl Write for Sink {
637-
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
638-
Write::write(&mut *self.0.lock().unwrap(), data)
639-
}
640-
fn flush(&mut self) -> io::Result<()> { Ok(()) }
641-
}
642-
643-
/// Given a string like " ^~~~~~~~~~~~ ", produces a span
644-
/// coverting that range. The idea is that the string has the same
645-
/// length as the input, and we uncover the byte positions. Note
646-
/// that this can span lines and so on.
647-
fn span_from_selection(input: &str, selection: &str) -> Span {
648-
assert_eq!(input.len(), selection.len());
649-
let left_index = selection.find('~').unwrap() as u32;
650-
let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index);
651-
Span { lo: BytePos(left_index), hi: BytePos(right_index + 1), expn_id: NO_EXPANSION }
652-
}
653-
654-
// Diagnostic doesn't align properly in span where line number increases by one digit
655-
#[test]
656-
fn test_hilight_suggestion_issue_11715() {
657-
let data = Arc::new(Mutex::new(Vec::new()));
658-
let cm = Rc::new(CodeMap::new());
659-
let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), None, cm.clone());
660-
let content = "abcdefg
661-
koksi
662-
line3
663-
line4
664-
cinq
665-
line6
666-
line7
667-
line8
668-
line9
669-
line10
670-
e-lä-vän
671-
tolv
672-
dreizehn
673-
";
674-
let file = cm.new_filemap_and_lines("dummy.txt", None, content);
675-
let start = file.lines.borrow()[10];
676-
let end = file.lines.borrow()[11];
677-
let sp = mk_sp(start, end);
678-
let lvl = Level::Error;
679-
println!("highlight_lines");
680-
ew.highlight_lines(&sp.into(), lvl).unwrap();
681-
println!("done");
682-
let vec = data.lock().unwrap().clone();
683-
let vec: &[u8] = &vec;
684-
let str = from_utf8(vec).unwrap();
685-
println!("r#\"\n{}\"#", str);
686-
assert_eq!(str, &r#"
687-
--> dummy.txt:11:1
688-
|>
689-
11 |> e-lä-vän
690-
|> ^
691-
"#[1..]);
692-
}
693-
694-
#[test]
695-
fn test_single_span_splice() {
696-
// Test that a `MultiSpan` containing a single span splices a substition correctly
697-
let cm = CodeMap::new();
698-
let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
699-
let selection = " \n ~~\n~~~\n~~~~~ \n \n";
700-
cm.new_filemap_and_lines("blork.rs", None, inputtext);
701-
let sp = span_from_selection(inputtext, selection);
702-
let msp: MultiSpan = sp.into();
703-
704-
// check that we are extracting the text we thought we were extracting
705-
assert_eq!(&cm.span_to_snippet(sp).unwrap(), "BB\nCCC\nDDDDD");
706-
707-
let substitute = "ZZZZZZ".to_owned();
708-
let expected = "bbbbZZZZZZddddd";
709-
let suggest = CodeSuggestion {
710-
msp: msp,
711-
substitutes: vec![substitute],
712-
};
713-
assert_eq!(suggest.splice_lines(&cm), expected);
714-
}
715-
716-
#[test]
717-
fn test_multi_span_splice() {
718-
// Test that a `MultiSpan` containing multiple spans splices a substition correctly
719-
let cm = CodeMap::new();
720-
let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
721-
let selection1 = " \n \n \n \n ~ \n"; // intentionally out of order
722-
let selection2 = " \n ~~\n~~~\n~~~~~ \n \n";
723-
cm.new_filemap_and_lines("blork.rs", None, inputtext);
724-
let sp1 = span_from_selection(inputtext, selection1);
725-
let sp2 = span_from_selection(inputtext, selection2);
726-
let msp: MultiSpan = MultiSpan::from_spans(vec![sp1, sp2]);
727-
728-
let expected = "bbbbZZZZZZddddd\neXYZe";
729-
let suggest = CodeSuggestion {
730-
msp: msp,
731-
substitutes: vec!["ZZZZZZ".to_owned(),
732-
"XYZ".to_owned()]
733-
};
734-
735-
assert_eq!(suggest.splice_lines(&cm), expected);
736-
}
737-
738-
#[test]
739-
fn test_multispan_highlight() {
740-
let data = Arc::new(Mutex::new(Vec::new()));
741-
let cm = Rc::new(CodeMap::new());
742-
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())), None, cm.clone());
743-
744-
let inp = "_____aaaaaa____bbbbbb__cccccdd_";
745-
let sp1 = " ~~~~~~ ";
746-
let sp2 = " ~~~~~~ ";
747-
let sp3 = " ~~~~~ ";
748-
let sp4 = " ~~~~ ";
749-
let sp34 = " ~~~~~~~ ";
750-
751-
let expect_start = &r#"
752-
--> dummy.txt:1:6
753-
|>
754-
1 |> _____aaaaaa____bbbbbb__cccccdd_
755-
|> ^^^^^^ ^^^^^^ ^^^^^^^
756-
"#[1..];
757-
758-
let span = |sp, expected| {
759-
let sp = span_from_selection(inp, sp);
760-
assert_eq!(&cm.span_to_snippet(sp).unwrap(), expected);
761-
sp
762-
};
763-
cm.new_filemap_and_lines("dummy.txt", None, inp);
764-
let sp1 = span(sp1, "aaaaaa");
765-
let sp2 = span(sp2, "bbbbbb");
766-
let sp3 = span(sp3, "ccccc");
767-
let sp4 = span(sp4, "ccdd");
768-
let sp34 = span(sp34, "cccccdd");
769-
770-
let spans = vec![sp1, sp2, sp3, sp4];
771-
772-
let test = |expected, highlight: &mut FnMut()| {
773-
data.lock().unwrap().clear();
774-
highlight();
775-
let vec = data.lock().unwrap().clone();
776-
let actual = from_utf8(&vec[..]).unwrap();
777-
println!("actual=\n{}", actual);
778-
assert_eq!(actual, expected);
779-
};
780-
781-
let msp = MultiSpan::from_spans(vec![sp1, sp2, sp34]);
782-
test(expect_start, &mut || {
783-
diag.highlight_lines(&msp, Level::Error).unwrap();
784-
});
785-
test(expect_start, &mut || {
786-
let msp = MultiSpan::from_spans(spans.clone());
787-
diag.highlight_lines(&msp, Level::Error).unwrap();
788-
});
789-
}
790-
791-
#[test]
792-
fn test_huge_multispan_highlight() {
793-
let data = Arc::new(Mutex::new(Vec::new()));
794-
let cm = Rc::new(CodeMap::new());
795-
let mut diag = EmitterWriter::new(Box::new(Sink(data.clone())), None, cm.clone());
796-
797-
let inp = "aaaaa\n\
798-
aaaaa\n\
799-
aaaaa\n\
800-
bbbbb\n\
801-
ccccc\n\
802-
xxxxx\n\
803-
yyyyy\n\
804-
_____\n\
805-
ddd__eee_\n\
806-
elided\n\
807-
__f_gg";
808-
let file = cm.new_filemap_and_lines("dummy.txt", None, inp);
809-
810-
let span = |lo, hi, (off_lo, off_hi)| {
811-
let lines = file.lines.borrow();
812-
let (mut lo, mut hi): (BytePos, BytePos) = (lines[lo], lines[hi]);
813-
lo.0 += off_lo;
814-
hi.0 += off_hi;
815-
mk_sp(lo, hi)
816-
};
817-
let sp0 = span(4, 6, (0, 5));
818-
let sp1 = span(0, 6, (0, 5));
819-
let sp2 = span(8, 8, (0, 3));
820-
let sp3 = span(8, 8, (5, 8));
821-
let sp4 = span(10, 10, (2, 3));
822-
let sp5 = span(10, 10, (4, 6));
823-
824-
let expect0 = &r#"
825-
--> dummy.txt:5:1
826-
|>
827-
5 |> ccccc
828-
|> ^
829-
...
830-
9 |> ddd__eee_
831-
|> ^^^ ^^^
832-
10 |> elided
833-
11 |> __f_gg
834-
|> ^ ^^
835-
"#[1..];
836-
837-
let expect = &r#"
838-
--> dummy.txt:1:1
839-
|>
840-
1 |> aaaaa
841-
|> ^
842-
...
843-
9 |> ddd__eee_
844-
|> ^^^ ^^^
845-
10 |> elided
846-
11 |> __f_gg
847-
|> ^ ^^
848-
"#[1..];
849-
850-
macro_rules! test {
851-
($expected: expr, $highlight: expr) => ({
852-
data.lock().unwrap().clear();
853-
$highlight();
854-
let vec = data.lock().unwrap().clone();
855-
let actual = from_utf8(&vec[..]).unwrap();
856-
println!("actual:");
857-
println!("{}", actual);
858-
println!("expected:");
859-
println!("{}", $expected);
860-
assert_eq!(&actual[..], &$expected[..]);
861-
});
862-
}
863-
864-
let msp0 = MultiSpan::from_spans(vec![sp0, sp2, sp3, sp4, sp5]);
865-
let msp = MultiSpan::from_spans(vec![sp1, sp2, sp3, sp4, sp5]);
866-
867-
test!(expect0, || {
868-
diag.highlight_lines(&msp0, Level::Error).unwrap();
869-
});
870-
test!(expect, || {
871-
diag.highlight_lines(&msp, Level::Error).unwrap();
872-
});
873-
}
874-
}

src/librustc_errors/snippet/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use std::cmp;
1717
use std::rc::Rc;
1818
use std::mem;
1919

20-
mod test;
21-
2220
#[derive(Clone)]
2321
pub struct SnippetData {
2422
codemap: Rc<CodeMapper>,

0 commit comments

Comments
 (0)