]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_errors/src/json/tests.rs
Rollup merge of #105998 - RalfJung:no-unwind-panic-msg, r=thomcc
[rust.git] / compiler / rustc_errors / src / json / tests.rs
1 use super::*;
2
3 use crate::json::JsonEmitter;
4 use rustc_span::source_map::{FilePathMapping, SourceMap};
5
6 use crate::emitter::{ColorConfig, HumanReadableErrorType};
7 use crate::Handler;
8 use rustc_span::{BytePos, Span};
9
10 use std::str;
11
12 use serde::Deserialize;
13
14 #[derive(Deserialize, Debug, PartialEq, Eq)]
15 struct TestData {
16     spans: Vec<SpanTestData>,
17 }
18
19 #[derive(Deserialize, Debug, PartialEq, Eq)]
20 struct SpanTestData {
21     pub byte_start: u32,
22     pub byte_end: u32,
23     pub line_start: u32,
24     pub column_start: u32,
25     pub line_end: u32,
26     pub column_end: u32,
27 }
28
29 struct Shared<T> {
30     data: Arc<Mutex<T>>,
31 }
32
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)
36     }
37
38     fn flush(&mut self) -> io::Result<()> {
39         self.data.lock().unwrap().flush()
40     }
41 }
42
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());
48         let fallback_bundle =
49             crate::fallback_fluent_bundle(rustc_error_messages::DEFAULT_LOCALE_RESOURCES, false);
50
51         let output = Arc::new(Mutex::new(Vec::new()));
52         let je = JsonEmitter::new(
53             Box::new(Shared { data: output.clone() }),
54             None,
55             sm,
56             None,
57             fallback_bundle,
58             true,
59             HumanReadableErrorType::Short(ColorConfig::Never),
60             None,
61             false,
62             false,
63         );
64
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");
68
69         let bytes = output.lock().unwrap();
70         let actual_output = str::from_utf8(&bytes).unwrap();
71         let actual_output: TestData = serde_json::from_str(actual_output).unwrap();
72         let spans = actual_output.spans;
73         assert_eq!(spans.len(), 1);
74
75         assert_eq!(expected_output, spans[0])
76     })
77 }
78
79 #[test]
80 fn empty() {
81     test_positions(
82         " ",
83         (0, 1),
84         SpanTestData {
85             byte_start: 0,
86             byte_end: 1,
87             line_start: 1,
88             column_start: 1,
89             line_end: 1,
90             column_end: 2,
91         },
92     )
93 }
94
95 #[test]
96 fn bom() {
97     test_positions(
98         "\u{feff} ",
99         (0, 1),
100         SpanTestData {
101             byte_start: 3,
102             byte_end: 4,
103             line_start: 1,
104             column_start: 1,
105             line_end: 1,
106             column_end: 2,
107         },
108     )
109 }
110
111 #[test]
112 fn lf_newlines() {
113     test_positions(
114         "\nmod foo;\nmod bar;\n",
115         (5, 12),
116         SpanTestData {
117             byte_start: 5,
118             byte_end: 12,
119             line_start: 2,
120             column_start: 5,
121             line_end: 3,
122             column_end: 3,
123         },
124     )
125 }
126
127 #[test]
128 fn crlf_newlines() {
129     test_positions(
130         "\r\nmod foo;\r\nmod bar;\r\n",
131         (5, 12),
132         SpanTestData {
133             byte_start: 6,
134             byte_end: 14,
135             line_start: 2,
136             column_start: 5,
137             line_end: 3,
138             column_end: 3,
139         },
140     )
141 }
142
143 #[test]
144 fn crlf_newlines_with_bom() {
145     test_positions(
146         "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
147         (5, 12),
148         SpanTestData {
149             byte_start: 9,
150             byte_end: 17,
151             line_start: 2,
152             column_start: 5,
153             line_end: 3,
154             column_end: 3,
155         },
156     )
157 }
158
159 #[test]
160 fn span_before_crlf() {
161     test_positions(
162         "foo\r\nbar",
163         (2, 3),
164         SpanTestData {
165             byte_start: 2,
166             byte_end: 3,
167             line_start: 1,
168             column_start: 3,
169             line_end: 1,
170             column_end: 4,
171         },
172     )
173 }
174
175 #[test]
176 fn span_on_crlf() {
177     test_positions(
178         "foo\r\nbar",
179         (3, 4),
180         SpanTestData {
181             byte_start: 3,
182             byte_end: 5,
183             line_start: 1,
184             column_start: 4,
185             line_end: 2,
186             column_end: 1,
187         },
188     )
189 }
190
191 #[test]
192 fn span_after_crlf() {
193     test_positions(
194         "foo\r\nbar",
195         (4, 5),
196         SpanTestData {
197             byte_start: 5,
198             byte_end: 6,
199             line_start: 2,
200             column_start: 1,
201             line_end: 2,
202             column_end: 2,
203         },
204     )
205 }