]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_errors/src/json/tests.rs
Add 'src/tools/rust-analyzer/' from commit '977e12a0bdc3e329af179ef3a9d466af9eb613bb'
[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         );
63
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");
67
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);
73
74         assert_eq!(expected_output, spans[0])
75     })
76 }
77
78 #[test]
79 fn empty() {
80     test_positions(
81         " ",
82         (0, 1),
83         SpanTestData {
84             byte_start: 0,
85             byte_end: 1,
86             line_start: 1,
87             column_start: 1,
88             line_end: 1,
89             column_end: 2,
90         },
91     )
92 }
93
94 #[test]
95 fn bom() {
96     test_positions(
97         "\u{feff} ",
98         (0, 1),
99         SpanTestData {
100             byte_start: 3,
101             byte_end: 4,
102             line_start: 1,
103             column_start: 1,
104             line_end: 1,
105             column_end: 2,
106         },
107     )
108 }
109
110 #[test]
111 fn lf_newlines() {
112     test_positions(
113         "\nmod foo;\nmod bar;\n",
114         (5, 12),
115         SpanTestData {
116             byte_start: 5,
117             byte_end: 12,
118             line_start: 2,
119             column_start: 5,
120             line_end: 3,
121             column_end: 3,
122         },
123     )
124 }
125
126 #[test]
127 fn crlf_newlines() {
128     test_positions(
129         "\r\nmod foo;\r\nmod bar;\r\n",
130         (5, 12),
131         SpanTestData {
132             byte_start: 6,
133             byte_end: 14,
134             line_start: 2,
135             column_start: 5,
136             line_end: 3,
137             column_end: 3,
138         },
139     )
140 }
141
142 #[test]
143 fn crlf_newlines_with_bom() {
144     test_positions(
145         "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
146         (5, 12),
147         SpanTestData {
148             byte_start: 9,
149             byte_end: 17,
150             line_start: 2,
151             column_start: 5,
152             line_end: 3,
153             column_end: 3,
154         },
155     )
156 }
157
158 #[test]
159 fn span_before_crlf() {
160     test_positions(
161         "foo\r\nbar",
162         (2, 3),
163         SpanTestData {
164             byte_start: 2,
165             byte_end: 3,
166             line_start: 1,
167             column_start: 3,
168             line_end: 1,
169             column_end: 4,
170         },
171     )
172 }
173
174 #[test]
175 fn span_on_crlf() {
176     test_positions(
177         "foo\r\nbar",
178         (3, 4),
179         SpanTestData {
180             byte_start: 3,
181             byte_end: 5,
182             line_start: 1,
183             column_start: 4,
184             line_end: 2,
185             column_end: 1,
186         },
187     )
188 }
189
190 #[test]
191 fn span_after_crlf() {
192     test_positions(
193         "foo\r\nbar",
194         (4, 5),
195         SpanTestData {
196             byte_start: 5,
197             byte_end: 6,
198             line_start: 2,
199             column_start: 1,
200             line_end: 2,
201             column_end: 2,
202         },
203     )
204 }