Skip to content

Commit

Permalink
tests: simplified declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
knight42 committed Aug 25, 2016
1 parent d79e3d0 commit d795b94
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ libc = "*"
regex="*"
rand="*"
tempdir="*"
choose = { path = "tests/choose" }

[[bin]]
name = "uutils"
Expand Down
7 changes: 7 additions & 0 deletions tests/choose/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "choose"
version = "0.1.0"
authors = ["Knight <anonymousknight96@gmail.com>"]

[lib]
plugin = true
102 changes: 102 additions & 0 deletions tests/choose/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#![feature(plugin_registrar, rustc_private)]

extern crate syntax;
extern crate rustc_plugin;

use syntax::ast;
use syntax::ptr::P;
use syntax::codemap::Span;
use syntax::parse::{self, token};
use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder;
use syntax::errors::FatalError;
use syntax::util::small_vector::SmallVector;
use rustc_plugin::Registry;

macro_rules! panictry {
($e:expr) => ({
match $e {
Ok(e) => e,
Err(mut e) => {
e.emit();
panic!(FatalError);
}
}
})
}

pub fn expand<'cx>(cx: &'cx mut ExtCtxt, name: String) -> P<ast::Item> {
let src = format!("mod {};", name.as_str());
let mut p = parse::new_parser_from_source_str(cx.parse_sess(), cx.cfg(), name, src);
match panictry!(p.parse_item()) {
Some(item) => item,
None => {
panic!(p.diagnostic().span_fatal(p.span,
&format!("expected item, found `{}`", p.this_token_to_string())))
}
}
}

fn intern(s: &str) -> token::InternedString {
token::intern_and_get_ident(s)
}

fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
let mut parser = cx.new_parser_from_tts(args);
let mut test_mods = SmallVector::zero();
let cfg_str = intern("cfg");
let feat_str = intern("feature");
while !parser.eat(&token::Eof) {
if let Ok(s) = parser.parse_ident() {
let unix_only;
match s.to_string().as_str() {
"unix" => unix_only = true,
"generic" => unix_only = false,
_ => {
cx.span_err(sp, "only `unix` and `generic` are supported now");
return DummyResult::any(sp);
}
}
parser.eat(&token::FatArrow);
parser.eat(&token::OpenDelim(token::Brace));
while !parser.eat(&token::CloseDelim(token::Brace)) {
match parser.parse_ident() {
Ok(s) => {
let mod_name = s.to_string();
let mut attrs = vec![cx.attribute(sp,
cx.meta_list(sp,
cfg_str.clone(),
vec![cx.meta_name_value(sp,
feat_str.clone(),
ast::LitKind::Str(intern(mod_name.trim_left_matches("test_")), ast::StrStyle::Cooked))]))];

if unix_only {
attrs.push(cx.attribute(sp,
cx.meta_list(sp,
cfg_str.clone(),
vec![cx.meta_word(sp, intern("unix"))])));
}

test_mods.push(expand(cx, mod_name));
}
_ => {
cx.span_err(sp, "expect a single identifier");
return DummyResult::any(sp);
}
}
parser.eat(&token::Semi);
parser.eat(&token::Comma);
}
} else {
cx.span_err(sp, "expect a single identifier");
return DummyResult::any(sp);
}
}
MacEager::items(test_mods)
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("choose", choose);
}
151 changes: 67 additions & 84 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,71 @@
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(choose))]

#[macro_use]
mod common;

// For conditional compilation
macro_rules! unix_only {
($($fea:expr, $m:ident);+) => {
$(
#[cfg(unix)]
#[cfg(feature = $fea)]
mod $m;
)+
};
}
unix_only! {
"chmod", test_chmod;
"chown", test_chown;
"chgrp", test_chgrp;
"install", test_install;
"mv", test_mv;
"pathchk", test_pathchk;
"pinky", test_pinky;
"stdbuf", test_stdbuf;
"touch", test_touch;
"unlink", test_unlink;
"who", test_who;
// Be aware of the trailing semicolon after the last item
"stat", test_stat
}


macro_rules! generic {
($($fea:expr, $m:ident);+) => {
$(
#[cfg(feature = $fea)]
mod $m;
)+
};
}
generic! {
"base32", test_base32;
"base64", test_base64;
"basename", test_basename;
"cat", test_cat;
"cksum", test_cksum;
"comm", test_comm;
"cp", test_cp;
"cut", test_cut;
"dircolors", test_dircolors;
"dirname", test_dirname;
"echo", test_echo;
"env", test_env;
"expr", test_expr;
"factor", test_factor;
"false", test_false;
"fold", test_fold;
"hashsum", test_hashsum;
"head", test_head;
"link", test_link;
"ln", test_ln;
"ls", test_ls;
"mkdir", test_mkdir;
"mktemp", test_mktemp;
"nl", test_nl;
"od", test_od;
"paste", test_paste;
"printf", test_printf;
"ptx", test_ptx;
"pwd", test_pwd;
"readlink", test_readlink;
"realpath", test_realpath;
"rm", test_rm;
"rmdir", test_rmdir;
"seq", test_seq;
"sort", test_sort;
"split", test_split;
"sum", test_sum;
"tac", test_tac;
"tail", test_tail;
"test", test_test;
"tr", test_tr;
"true", test_true;
"truncate", test_truncate;
"tsort", test_tsort;
"unexpand", test_unexpand;
"uniq", test_uniq;
// Be aware of the trailing semicolon after the last item
"wc", test_wc
choose! {
unix => {
test_chmod
test_chown
test_chgrp
test_install
test_mv
test_pathchk
test_pinky
test_stdbuf
test_touch
test_unlink
test_who
test_stat
}
generic => {
test_base32
test_base64
test_basename
test_cat
test_cksum
test_comm
test_cp
test_cut
test_dircolors
test_dirname
test_echo
test_env
test_expr
test_factor
test_false
test_fold
test_hashsum
test_head
test_link
test_ln
test_ls
test_mkdir
test_mktemp
test_nl
test_od
test_paste
test_printf
test_ptx
test_pwd
test_readlink
test_realpath
test_rm
test_rmdir
test_seq
test_sort
test_split
test_sum
test_tac
test_tail
test_test
test_tr
test_true
test_truncate
test_tsort
test_unexpand
test_uniq
test_wc
}
}

0 comments on commit d795b94

Please sign in to comment.