Skip to content

Commit fda0359

Browse files
committed
Add tests for libsyntax JSON format
1 parent 2daa404 commit fda0359

File tree

3 files changed

+195
-2
lines changed

3 files changed

+195
-2
lines changed

src/libsyntax/json.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use std::sync::{Arc, Mutex};
2525

2626
use rustc_serialize::json::{as_json, as_pretty_json};
2727

28+
#[cfg(test)]
29+
mod tests;
30+
2831
pub struct JsonEmitter {
2932
dst: Box<dyn Write + Send>,
3033
registry: Option<Registry>,

src/libsyntax/json/tests.rs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
use super::*;
2+
3+
use crate::tests::{Shared};
4+
use crate::json::JsonEmitter;
5+
use crate::source_map::{SourceMap, FilePathMapping};
6+
use crate::with_default_globals;
7+
8+
use errors::emitter::{HumanReadableErrorType, ColorConfig};
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+
33+
let expected_output = TestData { spans: vec![expected_output] };
34+
35+
with_default_globals(|| {
36+
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
37+
sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
38+
39+
let output = Arc::new(Mutex::new(Vec::new()));
40+
let je = JsonEmitter::new(
41+
Box::new(Shared { data: output.clone() }),
42+
None,
43+
sm,
44+
true,
45+
HumanReadableErrorType::Short(ColorConfig::Never),
46+
false
47+
);
48+
49+
let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));
50+
let handler = Handler::with_emitter(true, None, Box::new(je));
51+
handler.span_err(span, "foo");
52+
53+
let bytes = output.lock().unwrap();
54+
let actual_output = str::from_utf8(&bytes).unwrap();
55+
let actual_output : TestData = decode(actual_output).unwrap();
56+
57+
println!("expected output:\n------\n{:#?}------", expected_output);
58+
println!("actual output:\n------\n{:#?}------", actual_output);
59+
60+
assert!(expected_output == actual_output)
61+
})
62+
}
63+
64+
#[test]
65+
fn empty() {
66+
test_positions(
67+
" ",
68+
(0, 1),
69+
SpanTestData {
70+
byte_start: 0,
71+
byte_end: 1,
72+
line_start: 1,
73+
column_start: 1,
74+
line_end: 1,
75+
column_end: 2,
76+
}
77+
)
78+
}
79+
80+
#[test]
81+
fn bom() {
82+
test_positions(
83+
"\u{feff} ",
84+
(0, 1),
85+
SpanTestData {
86+
byte_start: 3,
87+
byte_end: 4,
88+
line_start: 1,
89+
column_start: 1,
90+
line_end: 1,
91+
column_end: 2,
92+
}
93+
)
94+
}
95+
96+
#[test]
97+
fn lf_newlines() {
98+
test_positions(
99+
"\nmod foo;\nmod bar;\n",
100+
(5, 12),
101+
SpanTestData {
102+
byte_start: 5,
103+
byte_end: 12,
104+
line_start: 2,
105+
column_start: 5,
106+
line_end: 3,
107+
column_end: 3,
108+
}
109+
)
110+
}
111+
112+
#[test]
113+
fn crlf_newlines() {
114+
test_positions(
115+
"\r\nmod foo;\r\nmod bar;\r\n",
116+
(5, 12),
117+
SpanTestData {
118+
byte_start: 6,
119+
byte_end: 14,
120+
line_start: 2,
121+
column_start: 5,
122+
line_end: 3,
123+
column_end: 3,
124+
}
125+
)
126+
}
127+
128+
#[test]
129+
fn crlf_newlines_with_bom() {
130+
test_positions(
131+
"\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
132+
(5, 12),
133+
SpanTestData {
134+
byte_start: 9,
135+
byte_end: 17,
136+
line_start: 2,
137+
column_start: 5,
138+
line_end: 3,
139+
column_end: 3,
140+
}
141+
)
142+
}
143+
144+
#[test]
145+
fn span_before_crlf() {
146+
test_positions(
147+
"foo\r\nbar",
148+
(2, 3),
149+
SpanTestData {
150+
byte_start: 2,
151+
byte_end: 3,
152+
line_start: 1,
153+
column_start: 3,
154+
line_end: 1,
155+
column_end: 4,
156+
}
157+
)
158+
}
159+
160+
#[test]
161+
fn span_on_crlf() {
162+
test_positions(
163+
"foo\r\nbar",
164+
(3, 4),
165+
SpanTestData {
166+
byte_start: 3,
167+
byte_end: 5,
168+
line_start: 1,
169+
column_start: 4,
170+
line_end: 2,
171+
column_end: 1,
172+
}
173+
)
174+
}
175+
176+
#[test]
177+
fn span_after_crlf() {
178+
test_positions(
179+
"foo\r\nbar",
180+
(4, 5),
181+
SpanTestData {
182+
byte_start: 5,
183+
byte_end: 6,
184+
line_start: 2,
185+
column_start: 1,
186+
line_end: 2,
187+
column_end: 2,
188+
}
189+
)
190+
}

src/libsyntax/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ struct SpanLabel {
110110
label: &'static str,
111111
}
112112

113-
struct Shared<T: Write> {
114-
data: Arc<Mutex<T>>,
113+
crate struct Shared<T: Write> {
114+
pub data: Arc<Mutex<T>>,
115115
}
116116

117117
impl<T: Write> Write for Shared<T> {

0 commit comments

Comments
 (0)