cargo/tests/testsuite/proc_macro.rs

302 lines
6.6 KiB
Rust
Raw Normal View History

2016-09-01 17:29:24 +00:00
use cargotest::is_nightly;
2018-03-14 15:17:44 +00:00
use cargotest::support::{execs, project};
2016-09-01 17:29:24 +00:00
use hamcrest::assert_that;
#[test]
fn probe_cfg_before_crate_type_discovery() {
let client = project().at("client")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "client"
version = "0.0.1"
authors = []
[target.'cfg(not(stage300))'.dependencies.noop]
path = "../noop"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
#[macro_use]
extern crate noop;
#[derive(Noop)]
struct X;
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _noop = project().at("noop")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "noop"
version = "0.0.1"
authors = []
[lib]
proc-macro = true
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_derive(Noop)]
pub fn noop(_input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2018-03-14 15:17:44 +00:00
assert_that(client.cargo("build"), execs().with_status(0));
}
2016-09-01 17:29:24 +00:00
#[test]
fn noop() {
let client = project().at("client")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2016-09-01 17:29:24 +00:00
[package]
name = "client"
version = "0.0.1"
authors = []
[dependencies.noop]
path = "../noop"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
2016-09-01 17:29:24 +00:00
#[macro_use]
extern crate noop;
#[derive(Noop)]
struct X;
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _noop = project().at("noop")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2016-09-01 17:29:24 +00:00
[package]
name = "noop"
version = "0.0.1"
authors = []
[lib]
proc-macro = true
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
extern crate proc_macro;
use proc_macro::TokenStream;
2016-09-01 17:29:24 +00:00
#[proc_macro_derive(Noop)]
2016-11-17 20:21:13 +00:00
pub fn noop(_input: TokenStream) -> TokenStream {
"".parse().unwrap()
2016-09-01 17:29:24 +00:00
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2016-09-01 17:29:24 +00:00
2018-03-14 15:17:44 +00:00
assert_that(client.cargo("build"), execs().with_status(0));
assert_that(client.cargo("build"), execs().with_status(0));
2016-09-01 17:29:24 +00:00
}
#[test]
fn impl_and_derive() {
let client = project().at("client")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2016-09-01 17:29:24 +00:00
[package]
name = "client"
version = "0.0.1"
authors = []
[dependencies.transmogrify]
path = "../transmogrify"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
2016-09-01 17:29:24 +00:00
#[macro_use]
extern crate transmogrify;
trait ImplByTransmogrify {
fn impl_by_transmogrify(&self) -> bool;
}
2016-11-17 20:21:13 +00:00
#[derive(Transmogrify, Debug)]
struct X { success: bool }
2016-09-01 17:29:24 +00:00
fn main() {
let x = X::new();
assert!(x.impl_by_transmogrify());
println!("{:?}", x);
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _transmogrify = project().at("transmogrify")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
2016-09-01 17:29:24 +00:00
[package]
name = "transmogrify"
version = "0.0.1"
authors = []
[lib]
proc-macro = true
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
extern crate proc_macro;
use proc_macro::TokenStream;
2016-09-01 17:29:24 +00:00
#[proc_macro_derive(Transmogrify)]
2016-09-01 17:29:24 +00:00
#[doc(hidden)]
pub fn transmogrify(input: TokenStream) -> TokenStream {
"
impl X {
fn new() -> Self {
X { success: true }
}
}
impl ImplByTransmogrify for X {
fn impl_by_transmogrify(&self) -> bool {
true
}
}
".parse().unwrap()
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2016-09-01 17:29:24 +00:00
2018-03-14 15:17:44 +00:00
assert_that(client.cargo("build"), execs().with_status(0));
assert_that(
client.cargo("run"),
execs().with_status(0).with_stdout("X { success: true }"),
);
2016-09-01 17:29:24 +00:00
}
#[test]
fn plugin_and_proc_macro() {
if !is_nightly() {
return;
}
let questionable = project().at("questionable")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "questionable"
version = "0.0.1"
authors = []
[lib]
plugin = true
proc-macro = true
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(plugin_registrar, rustc_private)]
#![feature(proc_macro, proc_macro_lib)]
extern crate rustc_plugin;
use rustc_plugin::Registry;
extern crate proc_macro;
use proc_macro::TokenStream;
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {}
#[proc_macro_derive(Questionable)]
pub fn questionable(input: TokenStream) -> TokenStream {
input
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let msg = " lib.plugin and lib.proc-macro cannot both be true";
2018-03-14 15:17:44 +00:00
assert_that(
questionable.cargo("build"),
execs().with_status(101).with_stderr_contains(msg),
);
}
#[test]
fn proc_macro_doctest() {
let foo = project()
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
[lib]
proc-macro = true
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::TokenStream;
/// ```
/// assert!(true);
/// ```
#[proc_macro_derive(Bar)]
pub fn derive(_input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
#[test]
fn a() {
assert!(true);
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
foo.cargo("test"),
execs()
.with_status(0)
.with_stdout_contains("test a ... ok")
.with_stdout_contains_n("test [..] ... ok", 2),
);
}