]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
1 | use super::*; |
2 | ||
3 | use crate::json::JsonEmitter; | |
dfeec247 | 4 | use rustc_span::source_map::{FilePathMapping, SourceMap}; |
e74abb32 | 5 | |
60c5eb7d XL |
6 | use crate::emitter::{ColorConfig, HumanReadableErrorType}; |
7 | use crate::Handler; | |
e74abb32 | 8 | use rustc_serialize::json::decode; |
dfeec247 | 9 | use rustc_span::{BytePos, Span}; |
e74abb32 XL |
10 | |
11 | use std::str; | |
12 | ||
3dfed10e | 13 | #[derive(Decodable, Debug, PartialEq, Eq)] |
e74abb32 XL |
14 | struct TestData { |
15 | spans: Vec<SpanTestData>, | |
16 | } | |
17 | ||
3dfed10e | 18 | #[derive(Decodable, Debug, PartialEq, Eq)] |
e74abb32 XL |
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 | ||
60c5eb7d XL |
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 | ||
e74abb32 XL |
42 | /// Test the span yields correct positions in JSON. |
43 | fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { | |
44 | let expected_output = TestData { spans: vec![expected_output] }; | |
45 | ||
136023e0 | 46 | rustc_span::create_default_session_globals_then(|| { |
e74abb32 XL |
47 | let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); |
48 | sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned()); | |
49 | ||
50 | let output = Arc::new(Mutex::new(Vec::new())); | |
51 | let je = JsonEmitter::new( | |
52 | Box::new(Shared { data: output.clone() }), | |
53 | None, | |
54 | sm, | |
55 | true, | |
56 | HumanReadableErrorType::Short(ColorConfig::Never), | |
f035d41b | 57 | None, |
e74abb32 XL |
58 | false, |
59 | ); | |
60 | ||
61 | let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1)); | |
62 | let handler = Handler::with_emitter(true, None, Box::new(je)); | |
63 | handler.span_err(span, "foo"); | |
64 | ||
65 | let bytes = output.lock().unwrap(); | |
66 | let actual_output = str::from_utf8(&bytes).unwrap(); | |
67 | let actual_output: TestData = decode(actual_output).unwrap(); | |
68 | ||
69 | assert_eq!(expected_output, actual_output) | |
70 | }) | |
71 | } | |
72 | ||
73 | #[test] | |
74 | fn empty() { | |
75 | test_positions( | |
76 | " ", | |
77 | (0, 1), | |
78 | SpanTestData { | |
79 | byte_start: 0, | |
80 | byte_end: 1, | |
81 | line_start: 1, | |
82 | column_start: 1, | |
83 | line_end: 1, | |
84 | column_end: 2, | |
85 | }, | |
86 | ) | |
87 | } | |
88 | ||
89 | #[test] | |
90 | fn bom() { | |
91 | test_positions( | |
92 | "\u{feff} ", | |
93 | (0, 1), | |
94 | SpanTestData { | |
95 | byte_start: 3, | |
96 | byte_end: 4, | |
97 | line_start: 1, | |
98 | column_start: 1, | |
99 | line_end: 1, | |
100 | column_end: 2, | |
101 | }, | |
102 | ) | |
103 | } | |
104 | ||
105 | #[test] | |
106 | fn lf_newlines() { | |
107 | test_positions( | |
108 | "\nmod foo;\nmod bar;\n", | |
109 | (5, 12), | |
110 | SpanTestData { | |
111 | byte_start: 5, | |
112 | byte_end: 12, | |
113 | line_start: 2, | |
114 | column_start: 5, | |
115 | line_end: 3, | |
116 | column_end: 3, | |
117 | }, | |
118 | ) | |
119 | } | |
120 | ||
121 | #[test] | |
122 | fn crlf_newlines() { | |
123 | test_positions( | |
124 | "\r\nmod foo;\r\nmod bar;\r\n", | |
125 | (5, 12), | |
126 | SpanTestData { | |
127 | byte_start: 6, | |
128 | byte_end: 14, | |
129 | line_start: 2, | |
130 | column_start: 5, | |
131 | line_end: 3, | |
132 | column_end: 3, | |
133 | }, | |
134 | ) | |
135 | } | |
136 | ||
137 | #[test] | |
138 | fn crlf_newlines_with_bom() { | |
139 | test_positions( | |
140 | "\u{feff}\r\nmod foo;\r\nmod bar;\r\n", | |
141 | (5, 12), | |
142 | SpanTestData { | |
143 | byte_start: 9, | |
144 | byte_end: 17, | |
145 | line_start: 2, | |
146 | column_start: 5, | |
147 | line_end: 3, | |
148 | column_end: 3, | |
149 | }, | |
150 | ) | |
151 | } | |
152 | ||
153 | #[test] | |
154 | fn span_before_crlf() { | |
155 | test_positions( | |
156 | "foo\r\nbar", | |
157 | (2, 3), | |
158 | SpanTestData { | |
159 | byte_start: 2, | |
160 | byte_end: 3, | |
161 | line_start: 1, | |
162 | column_start: 3, | |
163 | line_end: 1, | |
164 | column_end: 4, | |
165 | }, | |
166 | ) | |
167 | } | |
168 | ||
169 | #[test] | |
170 | fn span_on_crlf() { | |
171 | test_positions( | |
172 | "foo\r\nbar", | |
173 | (3, 4), | |
174 | SpanTestData { | |
175 | byte_start: 3, | |
176 | byte_end: 5, | |
177 | line_start: 1, | |
178 | column_start: 4, | |
179 | line_end: 2, | |
180 | column_end: 1, | |
181 | }, | |
182 | ) | |
183 | } | |
184 | ||
185 | #[test] | |
186 | fn span_after_crlf() { | |
187 | test_positions( | |
188 | "foo\r\nbar", | |
189 | (4, 5), | |
190 | SpanTestData { | |
191 | byte_start: 5, | |
192 | byte_end: 6, | |
193 | line_start: 2, | |
194 | column_start: 1, | |
195 | line_end: 2, | |
196 | column_end: 2, | |
197 | }, | |
198 | ) | |
199 | } |