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