2016-09-01 17:29:24 +00:00
|
|
|
extern crate cargotest;
|
|
|
|
extern crate hamcrest;
|
|
|
|
|
|
|
|
use cargotest::is_nightly;
|
|
|
|
use cargotest::support::{project, execs};
|
|
|
|
use hamcrest::assert_that;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn noop() {
|
|
|
|
if !is_nightly() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = project("client")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "client"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[dependencies.noop]
|
|
|
|
path = "../noop"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro)]
|
2016-09-01 17:29:24 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate noop;
|
|
|
|
|
|
|
|
#[derive(Noop)]
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
"#);
|
|
|
|
let noop = project("noop")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "noop"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2016-09-01 17:29:24 +00:00
|
|
|
"#)
|
|
|
|
.file("src/lib.rs", r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro, proc_macro_lib)]
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Noop)]
|
2016-09-01 17:29:24 +00:00
|
|
|
pub fn noop(input: TokenStream) -> TokenStream {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
noop.build();
|
|
|
|
|
|
|
|
assert_that(client.cargo_process("build"),
|
|
|
|
execs().with_status(0));
|
|
|
|
assert_that(client.cargo("build"),
|
|
|
|
execs().with_status(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn impl_and_derive() {
|
|
|
|
if !is_nightly() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = project("client")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "client"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[dependencies.transmogrify]
|
|
|
|
path = "../transmogrify"
|
|
|
|
"#)
|
|
|
|
.file("src/main.rs", r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro)]
|
2016-09-01 17:29:24 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate transmogrify;
|
|
|
|
|
|
|
|
trait ImplByTransmogrify {
|
|
|
|
fn impl_by_transmogrify(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Transmogrify)]
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::new();
|
|
|
|
assert!(x.impl_by_transmogrify());
|
|
|
|
println!("{:?}", x);
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
let transmogrify = project("transmogrify")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "transmogrify"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2016-09-01 17:29:24 +00:00
|
|
|
"#)
|
|
|
|
.file("src/lib.rs", r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro, proc_macro_lib)]
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Transmogrify)]
|
2016-09-01 17:29:24 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn transmogrify(input: TokenStream) -> TokenStream {
|
|
|
|
assert_eq!(input.to_string(), "struct X;\n");
|
|
|
|
|
|
|
|
"
|
|
|
|
impl X {
|
|
|
|
fn new() -> Self {
|
|
|
|
X { success: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplByTransmogrify for X {
|
|
|
|
fn impl_by_transmogrify(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct X {
|
|
|
|
success: bool,
|
|
|
|
}
|
|
|
|
".parse().unwrap()
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
transmogrify.build();
|
|
|
|
|
|
|
|
assert_that(client.cargo_process("build"),
|
|
|
|
execs().with_status(0));
|
|
|
|
assert_that(client.cargo("run"),
|
|
|
|
execs().with_status(0).with_stdout("X { success: true }"));
|
|
|
|
}
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-10-04 16:58:28 +00:00
|
|
|
fn plugin_and_proc_macro() {
|
2016-09-01 17:42:23 +00:00
|
|
|
if !is_nightly() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let questionable = project("questionable")
|
|
|
|
.file("Cargo.toml", r#"
|
|
|
|
[package]
|
|
|
|
name = "questionable"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
plugin = true
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2016-09-01 17:42:23 +00:00
|
|
|
"#)
|
|
|
|
.file("src/lib.rs", r#"
|
|
|
|
#![feature(plugin_registrar, rustc_private)]
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro, proc_macro_lib)]
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
extern crate rustc_plugin;
|
|
|
|
use rustc_plugin::Registry;
|
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
#[plugin_registrar]
|
|
|
|
pub fn plugin_registrar(reg: &mut Registry) {}
|
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Questionable)]
|
2016-09-01 17:42:23 +00:00
|
|
|
pub fn questionable(input: TokenStream) -> TokenStream {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
let msg = " lib.plugin and lib.proc-macro cannot both be true";
|
2016-09-01 17:42:23 +00:00
|
|
|
assert_that(questionable.cargo_process("build"),
|
|
|
|
execs().with_status(101).with_stderr_contains(msg));
|
|
|
|
}
|