1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use codemap::{CodeMap, FilePathMapping};
13 use errors::emitter::EmitterWriter;
15 use std::io::prelude::*;
18 use std::sync::{Arc, Mutex};
20 use syntax_pos::{BytePos, NO_EXPANSION, Span, MultiSpan};
22 /// Identify a position in the text by the Nth occurrence of a string.
34 struct Shared<T: Write> {
38 impl<T: Write> Write for Shared<T> {
39 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
40 self.data.lock().unwrap().write(buf)
43 fn flush(&mut self) -> io::Result<()> {
44 self.data.lock().unwrap().flush()
48 fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
49 let output = Arc::new(Mutex::new(Vec::new()));
51 let code_map = Rc::new(CodeMap::new(FilePathMapping::empty()));
52 code_map.new_filemap_and_lines(Path::new("test.rs"), &file_text);
54 let primary_span = make_span(&file_text, &span_labels[0].start, &span_labels[0].end);
55 let mut msp = MultiSpan::from_span(primary_span);
56 for span_label in span_labels {
57 let span = make_span(&file_text, &span_label.start, &span_label.end);
58 msp.push_span_label(span, span_label.label.to_string());
59 println!("span: {:?} label: {:?}", span, span_label.label);
60 println!("text: {:?}", code_map.span_to_snippet(span));
63 let emitter = EmitterWriter::new(Box::new(Shared { data: output.clone() }),
64 Some(code_map.clone()),
66 let handler = Handler::with_emitter(true, false, Box::new(emitter));
67 handler.span_err(msp, "foo");
69 assert!(expected_output.chars().next() == Some('\n'),
70 "expected output should begin with newline");
71 let expected_output = &expected_output[1..];
73 let bytes = output.lock().unwrap();
74 let actual_output = str::from_utf8(&bytes).unwrap();
75 println!("expected output:\n------\n{}------", expected_output);
76 println!("actual output:\n------\n{}------", actual_output);
78 assert!(expected_output == actual_output)
81 fn make_span(file_text: &str, start: &Position, end: &Position) -> Span {
82 let start = make_pos(file_text, start);
83 let end = make_pos(file_text, end) + end.string.len(); // just after matching thing ends
84 assert!(start <= end);
85 Span::new(BytePos(start as u32), BytePos(end as u32), NO_EXPANSION)
88 fn make_pos(file_text: &str, pos: &Position) -> usize {
89 let mut remainder = file_text;
91 for _ in 0..pos.count {
92 if let Some(n) = remainder.find(&pos.string) {
94 remainder = &remainder[n + 1..];
96 panic!("failed to find {} instances of {:?} in {:?}",
189 label: "`X` is a good letter",
200 label: "`Y` is a good letter too",
213 | ||____^__- `Y` is a good letter too
215 | `X` is a good letter
238 label: "`X` is a good letter",
249 label: "`Y` is a good letter too",
261 | ||____-__^ `X` is a good letter
263 | `Y` is a good letter too
269 fn different_overlap() {
288 label: "`X` is a good letter",
299 label: "`Y` is a good letter too",
311 | ||____^ `X` is a good letter
313 | |_____- `Y` is a good letter too
319 fn triple_overlap() {
337 label: "`X` is a good letter",
348 label: "`Y` is a good letter too",
373 | |||____^__-__- `Z` label
375 | |____| `Y` is a good letter too
376 | `X` is a good letter
401 label: "`X` is a good letter",
412 label: "`Y` is a good letter too",
435 | | `X` is a good letter
437 | |____-______- `Y` is a good letter too
447 fn non_overlaping() {
466 label: "`X` is a good letter",
477 label: "`Y` is a good letter too",
486 | |____^ `X` is a good letter
490 | |__________- `Y` is a good letter too
496 fn overlaping_start_and_end() {
515 label: "`X` is a good letter",
526 label: "`Y` is a good letter too",
538 | | `X` is a good letter
541 | |___________- `Y` is a good letter too
547 fn multiple_labels_primary_without_message() {
574 label: "`a` is a good letter",
593 | ----^^^^-^^-- `a` is a good letter
599 fn multiple_labels_secondary_without_message() {
615 label: "`a` is a good letter",
634 | ^^^^-------^^ `a` is a good letter
640 fn multiple_labels_primary_without_message_2() {
656 label: "`b` is a good letter",
688 | `b` is a good letter
694 fn multiple_labels_secondary_without_message_2() {
721 label: "`b` is a good letter",
731 | `b` is a good letter
737 fn multiple_labels_secondary_without_message_3() {
753 label: "`a` is a good letter",
774 | `a` is a good letter
780 fn multiple_labels_without_message() {
821 fn multiple_labels_without_message_2() {
873 fn multiple_labels_with_message() {
889 label: "`a` is a good letter",
900 label: "`b` is a good letter",
910 | | `b` is a good letter
911 | `a` is a good letter
917 fn single_label_with_message() {
933 label: "`a` is a good letter",
941 | ^^^^^^^^^^^^^ `a` is a good letter
947 fn single_label_without_message() {
1006 label: "`X` is a good letter",
1017 label: "`Y` is a good letter too",
1029 | | `X` is a good letter
1036 | |___________- `Y` is a good letter too
1042 fn long_snippet_multiple_spans() {
1071 label: "`Y` is a good letter",
1082 label: "`Z` is a good letter too",
1100 | ||__________- `Z` is a good letter too
1104 | |_______^ `Y` is a good letter