3 use crate::json::JsonEmitter;
4 use rustc_span::source_map::{FilePathMapping, SourceMap};
6 use crate::emitter::{ColorConfig, HumanReadableErrorType};
8 use rustc_serialize::json::decode;
9 use rustc_span::{BytePos, Span};
13 #[derive(RustcDecodable, Debug, PartialEq, Eq)]
15 spans: Vec<SpanTestData>,
18 #[derive(RustcDecodable, Debug, PartialEq, Eq)]
23 pub column_start: u32,
32 impl<T: Write> Write for Shared<T> {
33 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
34 self.data.lock().unwrap().write(buf)
37 fn flush(&mut self) -> io::Result<()> {
38 self.data.lock().unwrap().flush()
42 fn with_default_globals(f: impl FnOnce()) {
43 let globals = rustc_span::Globals::new(rustc_span::edition::DEFAULT_EDITION);
44 rustc_span::GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals, f))
47 /// Test the span yields correct positions in JSON.
48 fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
49 let expected_output = TestData { spans: vec![expected_output] };
51 with_default_globals(|| {
52 let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
53 sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
55 let output = Arc::new(Mutex::new(Vec::new()));
56 let je = JsonEmitter::new(
57 Box::new(Shared { data: output.clone() }),
61 HumanReadableErrorType::Short(ColorConfig::Never),
65 let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
66 let handler = Handler::with_emitter(true, None, Box::new(je));
67 handler.span_err(span, "foo");
69 let bytes = output.lock().unwrap();
70 let actual_output = str::from_utf8(&bytes).unwrap();
71 let actual_output: TestData = decode(actual_output).unwrap();
73 assert_eq!(expected_output, actual_output)
112 "\nmod foo;\nmod bar;\n",
128 "\r\nmod foo;\r\nmod bar;\r\n",
142 fn crlf_newlines_with_bom() {
144 "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
158 fn span_before_crlf() {
190 fn span_after_crlf() {