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