]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/json/tests.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / libsyntax / json / tests.rs
CommitLineData
e74abb32
XL
1use super::*;
2
3use crate::json::JsonEmitter;
4use crate::source_map::{FilePathMapping, SourceMap};
5use crate::tests::Shared;
6use crate::with_default_globals;
7
8use errors::emitter::{ColorConfig, HumanReadableErrorType};
9use errors::Handler;
10use rustc_serialize::json::decode;
11use syntax_pos::{BytePos, Span};
12
13use std::str;
14
15#[derive(RustcDecodable, Debug, PartialEq, Eq)]
16struct TestData {
17 spans: Vec<SpanTestData>,
18}
19
20#[derive(RustcDecodable, Debug, PartialEq, Eq)]
21struct 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.
31fn 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]
61fn 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]
77fn 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]
93fn 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]
109fn 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]
125fn 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]
141fn 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]
157fn 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]
173fn 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}