Skip to content

Commit 306a291

Browse files
committed
tests: simplified declarations
1 parent d79e3d0 commit 306a291

File tree

4 files changed

+178
-84
lines changed

4 files changed

+178
-84
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ libc = "*"
203203
regex="*"
204204
rand="*"
205205
tempdir="*"
206+
choose = { path = "tests/choose" }
206207

207208
[[bin]]
208209
name = "uutils"

tests/choose/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "choose"
3+
version = "0.1.0"
4+
authors = ["Knight <[email protected]>"]
5+
6+
[lib]
7+
plugin = true

tests/choose/src/lib.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#![feature(plugin_registrar, rustc_private)]
2+
3+
extern crate syntax;
4+
extern crate rustc;
5+
extern crate rustc_plugin;
6+
7+
use syntax::ast;
8+
use syntax::ptr::P;
9+
use syntax::codemap::Span;
10+
use syntax::parse::{self, token};
11+
use syntax::tokenstream::TokenTree;
12+
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
13+
use syntax::ext::build::AstBuilder;
14+
use syntax::errors::FatalError;
15+
use syntax::util::small_vector::SmallVector;
16+
use rustc_plugin::Registry;
17+
18+
macro_rules! panictry {
19+
($e:expr) => ({
20+
match $e {
21+
Ok(e) => e,
22+
Err(mut e) => {
23+
e.emit();
24+
panic!(FatalError);
25+
}
26+
}
27+
})
28+
}
29+
30+
pub fn expand<'cx>(cx: &'cx mut ExtCtxt, name: String) -> P<ast::Item> {
31+
let src = format!("mod {};", name.as_str());
32+
let mut p = parse::new_parser_from_source_str(cx.parse_sess(), cx.cfg(), name, src);
33+
match panictry!(p.parse_item()) {
34+
Some(item) => item,
35+
None => {
36+
panic!(p.diagnostic().span_fatal(p.span,
37+
&format!("expected item, found `{}`", p.this_token_to_string())))
38+
}
39+
}
40+
}
41+
42+
fn intern(s: &str) -> token::InternedString {
43+
token::intern_and_get_ident(s)
44+
}
45+
46+
fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
47+
let mut parser = cx.new_parser_from_tts(args);
48+
let mut test_mods = SmallVector::zero();
49+
let cfg_str = intern("cfg");
50+
let feat_str = intern("feature");
51+
while !parser.eat(&token::Eof) {
52+
if let Ok(s) = parser.parse_ident() {
53+
let unix_only: bool;
54+
match s.to_string().as_str() {
55+
"unix" => unix_only = true,
56+
"generic" => unix_only = false,
57+
_ => {
58+
cx.span_err(sp, "only `unix` and `generic` are supported now");
59+
return DummyResult::any(sp);
60+
}
61+
}
62+
parser.eat(&token::FatArrow);
63+
parser.eat(&token::OpenDelim(token::Brace));
64+
while !parser.eat(&token::CloseDelim(token::Brace)) {
65+
match parser.parse_ident() {
66+
Ok(s) => {
67+
let mod_name = s.to_string();
68+
let mut attrs = vec![cx.attribute(sp,
69+
cx.meta_list(sp,
70+
cfg_str.clone(),
71+
vec![cx.meta_name_value(sp,
72+
feat_str.clone(),
73+
ast::LitKind::Str(intern(mod_name.trim_left_matches("test_")), ast::StrStyle::Cooked))]))];
74+
75+
if unix_only {
76+
attrs.push(cx.attribute(sp,
77+
cx.meta_list(sp,
78+
cfg_str.clone(),
79+
vec![cx.meta_word(sp, intern("unix"))])));
80+
}
81+
82+
test_mods.push(expand(cx, mod_name));
83+
}
84+
_ => {
85+
cx.span_err(sp, "expect a single identifier");
86+
return DummyResult::any(sp);
87+
}
88+
}
89+
parser.eat(&token::Semi);
90+
parser.eat(&token::Comma);
91+
}
92+
} else {
93+
cx.span_err(sp, "expect a single identifier");
94+
return DummyResult::any(sp);
95+
}
96+
}
97+
MacEager::items(test_mods)
98+
}
99+
100+
#[plugin_registrar]
101+
pub fn plugin_registrar(reg: &mut Registry) {
102+
reg.register_macro("choose", choose);
103+
}

tests/tests.rs

Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,71 @@
1+
#![cfg_attr(test, feature(plugin))]
2+
#![cfg_attr(test, plugin(choose))]
3+
14
#[macro_use]
25
mod common;
36

4-
// For conditional compilation
5-
macro_rules! unix_only {
6-
($($fea:expr, $m:ident);+) => {
7-
$(
8-
#[cfg(unix)]
9-
#[cfg(feature = $fea)]
10-
mod $m;
11-
)+
12-
};
13-
}
14-
unix_only! {
15-
"chmod", test_chmod;
16-
"chown", test_chown;
17-
"chgrp", test_chgrp;
18-
"install", test_install;
19-
"mv", test_mv;
20-
"pathchk", test_pathchk;
21-
"pinky", test_pinky;
22-
"stdbuf", test_stdbuf;
23-
"touch", test_touch;
24-
"unlink", test_unlink;
25-
"who", test_who;
26-
// Be aware of the trailing semicolon after the last item
27-
"stat", test_stat
28-
}
29-
30-
31-
macro_rules! generic {
32-
($($fea:expr, $m:ident);+) => {
33-
$(
34-
#[cfg(feature = $fea)]
35-
mod $m;
36-
)+
37-
};
38-
}
39-
generic! {
40-
"base32", test_base32;
41-
"base64", test_base64;
42-
"basename", test_basename;
43-
"cat", test_cat;
44-
"cksum", test_cksum;
45-
"comm", test_comm;
46-
"cp", test_cp;
47-
"cut", test_cut;
48-
"dircolors", test_dircolors;
49-
"dirname", test_dirname;
50-
"echo", test_echo;
51-
"env", test_env;
52-
"expr", test_expr;
53-
"factor", test_factor;
54-
"false", test_false;
55-
"fold", test_fold;
56-
"hashsum", test_hashsum;
57-
"head", test_head;
58-
"link", test_link;
59-
"ln", test_ln;
60-
"ls", test_ls;
61-
"mkdir", test_mkdir;
62-
"mktemp", test_mktemp;
63-
"nl", test_nl;
64-
"od", test_od;
65-
"paste", test_paste;
66-
"printf", test_printf;
67-
"ptx", test_ptx;
68-
"pwd", test_pwd;
69-
"readlink", test_readlink;
70-
"realpath", test_realpath;
71-
"rm", test_rm;
72-
"rmdir", test_rmdir;
73-
"seq", test_seq;
74-
"sort", test_sort;
75-
"split", test_split;
76-
"sum", test_sum;
77-
"tac", test_tac;
78-
"tail", test_tail;
79-
"test", test_test;
80-
"tr", test_tr;
81-
"true", test_true;
82-
"truncate", test_truncate;
83-
"tsort", test_tsort;
84-
"unexpand", test_unexpand;
85-
"uniq", test_uniq;
86-
// Be aware of the trailing semicolon after the last item
87-
"wc", test_wc
7+
choose! {
8+
unix => {
9+
test_chmod
10+
test_chown
11+
test_chgrp
12+
test_install
13+
test_mv
14+
test_pathchk
15+
test_pinky
16+
test_stdbuf
17+
test_touch
18+
test_unlink
19+
test_who
20+
test_stat
21+
}
22+
generic => {
23+
test_base32
24+
test_base64
25+
test_basename
26+
test_cat
27+
test_cksum
28+
test_comm
29+
test_cp
30+
test_cut
31+
test_dircolors
32+
test_dirname
33+
test_echo
34+
test_env
35+
test_expr
36+
test_factor
37+
test_false
38+
test_fold
39+
test_hashsum
40+
test_head
41+
test_link
42+
test_ln
43+
test_ls
44+
test_mkdir
45+
test_mktemp
46+
test_nl
47+
test_od
48+
test_paste
49+
test_printf
50+
test_ptx
51+
test_pwd
52+
test_readlink
53+
test_realpath
54+
test_rm
55+
test_rmdir
56+
test_seq
57+
test_sort
58+
test_split
59+
test_sum
60+
test_tac
61+
test_tail
62+
test_test
63+
test_tr
64+
test_true
65+
test_truncate
66+
test_tsort
67+
test_unexpand
68+
test_uniq
69+
test_wc
70+
}
8871
}

0 commit comments

Comments
 (0)