3 use crate::json::JsonEmitter;
4 use rustc_span::source_map::{FilePathMapping, SourceMap};
6 use crate::emitter::{ColorConfig, HumanReadableErrorType};
8 use rustc_span::{BytePos, Span};
12 use serde::Deserialize;
14 #[derive(Deserialize, Debug, PartialEq, Eq)]
16 spans: Vec<SpanTestData>,
19 #[derive(Deserialize, Debug, PartialEq, Eq)]
24 pub column_start: u32,
33 impl<T: Write> Write for Shared<T> {
34 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
35 self.data.lock().unwrap().write(buf)
38 fn flush(&mut self) -> io::Result<()> {
39 self.data.lock().unwrap().flush()
43 /// Test the span yields correct positions in JSON.
44 fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
45 rustc_span::create_default_session_globals_then(|| {
46 let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
47 sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
49 crate::fallback_fluent_bundle(rustc_error_messages::DEFAULT_LOCALE_RESOURCES, false);
51 let output = Arc::new(Mutex::new(Vec::new()));
52 let je = JsonEmitter::new(
53 Box::new(Shared { data: output.clone() }),
59 HumanReadableErrorType::Short(ColorConfig::Never),
64 let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
65 let handler = Handler::with_emitter(true, None, Box::new(je));
66 handler.span_err(span, "foo");
68 let bytes = output.lock().unwrap();
69 let actual_output = str::from_utf8(&bytes).unwrap();
70 let actual_output: TestData = serde_json::from_str(actual_output).unwrap();
71 let spans = actual_output.spans;
72 assert_eq!(spans.len(), 1);
74 assert_eq!(expected_output, spans[0])
113 "\nmod foo;\nmod bar;\n",
129 "\r\nmod foo;\r\nmod bar;\r\n",
143 fn crlf_newlines_with_bom() {
145 "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
159 fn span_before_crlf() {
191 fn span_after_crlf() {