Skip to content

Commit 5d7b659

Browse files
committed
Add tests
1 parent 50fcd45 commit 5d7b659

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![feature(proc_macro_diagnostic, proc_macro_span)]
5+
#![crate_type = "proc-macro"]
6+
7+
extern crate proc_macro;
8+
9+
use proc_macro::{TokenStream, TokenTree, Span};
10+
11+
fn lit_span(tt: TokenTree) -> (Span, String) {
12+
match tt {
13+
TokenTree::Literal(..) |
14+
TokenTree::Group(..) => (tt.span(), tt.to_string().trim().into()),
15+
_ => panic!("expected a literal in token tree, got: {:?}", tt)
16+
}
17+
}
18+
19+
#[proc_macro]
20+
pub fn assert_span_pos(input: TokenStream) -> TokenStream {
21+
let mut tokens = input.into_iter();
22+
let (sp1, str1) = lit_span(tokens.next().expect("first argument"));
23+
let _ = tokens.next();
24+
let (_sp2, str2) = lit_span(tokens.next().expect("second argument"));
25+
26+
let line: usize = str1.parse().unwrap();
27+
let col: usize = str2.parse().unwrap();
28+
29+
let sp1s = sp1.start();
30+
if (line, col) != (sp1s.line, sp1s.column) {
31+
let msg = format!("line/column mismatch: ({}, {}) != ({}, {})", line, col,
32+
sp1s.line, sp1s.column);
33+
sp1.error(msg).emit();
34+
}
35+
36+
"".parse().unwrap()
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build:assert-span-pos.rs
2+
// ignore-tidy-tab
3+
extern crate assert_span_pos;
4+
5+
assert_span_pos::assert_span_pos!(5, 35);
6+
7+
// Test space indentation
8+
assert_span_pos::assert_span_pos!(8, 39);
9+
// Test tab indentation
10+
assert_span_pos::assert_span_pos!(10, 36);
11+
12+
// Test that the macro actually emits an error on a mismatch:
13+
assert_span_pos::assert_span_pos!(0, 35); //~ ERROR line/column mismatch: (0, 35) != (13, 35)
14+
assert_span_pos::assert_span_pos!(14, 0); //~ ERROR line/column mismatch: (14, 0) != (14, 35)
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: line/column mismatch: (0, 35) != (13, 35)
2+
--> $DIR/span-absolute-posititions.rs:13:35
3+
|
4+
LL | assert_span_pos::assert_span_pos!(0, 35);
5+
| ^
6+
7+
error: line/column mismatch: (14, 0) != (14, 35)
8+
--> $DIR/span-absolute-posititions.rs:14:35
9+
|
10+
LL | assert_span_pos::assert_span_pos!(14, 0);
11+
| ^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)