]> git.proxmox.com Git - rustc.git/blob - src/librustc_errors/json/tests.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_errors / 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_serialize::json::decode;
9 use rustc_span::{BytePos, Span};
10
11 use std::str;
12
13 #[derive(Decodable, Debug, PartialEq, Eq)]
14 struct TestData {
15 spans: Vec<SpanTestData>,
16 }
17
18 #[derive(Decodable, Debug, PartialEq, Eq)]
19 struct SpanTestData {
20 pub byte_start: u32,
21 pub byte_end: u32,
22 pub line_start: u32,
23 pub column_start: u32,
24 pub line_end: u32,
25 pub column_end: u32,
26 }
27
28 struct Shared<T> {
29 data: Arc<Mutex<T>>,
30 }
31
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)
35 }
36
37 fn flush(&mut self) -> io::Result<()> {
38 self.data.lock().unwrap().flush()
39 }
40 }
41
42 fn with_default_session_globals(f: impl FnOnce()) {
43 let session_globals = rustc_span::SessionGlobals::new(rustc_span::edition::DEFAULT_EDITION);
44 rustc_span::SESSION_GLOBALS.set(&session_globals, f);
45 }
46
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] };
50
51 with_default_session_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());
54
55 let output = Arc::new(Mutex::new(Vec::new()));
56 let je = JsonEmitter::new(
57 Box::new(Shared { data: output.clone() }),
58 None,
59 sm,
60 true,
61 HumanReadableErrorType::Short(ColorConfig::Never),
62 None,
63 false,
64 );
65
66 let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
67 let handler = Handler::with_emitter(true, None, Box::new(je));
68 handler.span_err(span, "foo");
69
70 let bytes = output.lock().unwrap();
71 let actual_output = str::from_utf8(&bytes).unwrap();
72 let actual_output: TestData = decode(actual_output).unwrap();
73
74 assert_eq!(expected_output, actual_output)
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 }