]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/json/tests.rs
Rollup merge of #66477 - ALSchwalm:clarify-transmute-copy, r=Centril
[rust.git] / src / librustc_errors / json / tests.rs
1 use super::*;
2
3 use crate::json::JsonEmitter;
4 use syntax_pos::source_map::{FilePathMapping, SourceMap};
5
6 use crate::emitter::{ColorConfig, HumanReadableErrorType};
7 use crate::Handler;
8 use rustc_serialize::json::decode;
9 use syntax_pos::{BytePos, Span};
10
11 use std::str;
12
13 #[derive(RustcDecodable, Debug, PartialEq, Eq)]
14 struct TestData {
15     spans: Vec<SpanTestData>,
16 }
17
18 #[derive(RustcDecodable, 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_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)
46     })
47 }
48
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] };
52
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());
56
57         let output = Arc::new(Mutex::new(Vec::new()));
58         let je = JsonEmitter::new(
59             Box::new(Shared { data: output.clone() }),
60             None,
61             sm,
62             true,
63             HumanReadableErrorType::Short(ColorConfig::Never),
64             false,
65         );
66
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");
70
71         let bytes = output.lock().unwrap();
72         let actual_output = str::from_utf8(&bytes).unwrap();
73         let actual_output: TestData = decode(actual_output).unwrap();
74
75         assert_eq!(expected_output, actual_output)
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 }