3 use crate::json::JsonEmitter;
4 use syntax_pos::source_map::{FilePathMapping, SourceMap};
6 use crate::emitter::{ColorConfig, HumanReadableErrorType};
8 use rustc_serialize::json::decode;
9 use syntax_pos::{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 = syntax_pos::Globals::new(syntax_pos::edition::DEFAULT_EDITION);
44 syntax_pos::GLOBALS.set(&globals, || {
45 syntax_pos::GLOBALS.set(&globals, f)
49 /// Test the span yields correct positions in JSON.
50 fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
51 let expected_output = TestData { spans: vec![expected_output] };
53 with_default_globals(|| {
54 let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
55 sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
57 let output = Arc::new(Mutex::new(Vec::new()));
58 let je = JsonEmitter::new(
59 Box::new(Shared { data: output.clone() }),
63 HumanReadableErrorType::Short(ColorConfig::Never),
67 let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
68 let handler = Handler::with_emitter(true, None, Box::new(je));
69 handler.span_err(span, "foo");
71 let bytes = output.lock().unwrap();
72 let actual_output = str::from_utf8(&bytes).unwrap();
73 let actual_output: TestData = decode(actual_output).unwrap();
75 assert_eq!(expected_output, actual_output)
114 "\nmod foo;\nmod bar;\n",
130 "\r\nmod foo;\r\nmod bar;\r\n",
144 fn crlf_newlines_with_bom() {
146 "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
160 fn span_before_crlf() {
192 fn span_after_crlf() {