Skip to content

Commit 0c89b89

Browse files
committed
Add compiletest crate to test against invalid derivations
1 parent b038b79 commit 0c89b89

File tree

6 files changed

+112
-1
lines changed

6 files changed

+112
-1
lines changed

macros/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ quote = "0.1.3"
1414
syn = "0.6.0"
1515

1616
[dev-dependencies]
17+
compiletest_rs = "0.2.2"
1718

1819
[dev-dependencies.num]
1920
path = ".."

macros/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate rustc_macro;
1919
use rustc_macro::TokenStream;
2020

2121
use syn::Body::Enum;
22+
use syn::VariantData::Unit;
2223

2324
#[rustc_macro_derive(FromPrimitive)]
2425
pub fn from_primitive(input: TokenStream) -> TokenStream {
@@ -29,13 +30,22 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
2930

3031
let variants = match ast.body {
3132
Enum(ref variants) => variants,
32-
_ => panic!("`FromPrimitive` can be applied only to the enums, {} is not an enum", name),
33+
_ => {
34+
panic!("`FromPrimitive` can be applied only to the enums, {} is not an enum",
35+
name)
36+
}
3337
};
3438

3539
let mut idx = 0;
3640
let variants: Vec<_> = variants.iter()
3741
.map(|variant| {
3842
let ident = &variant.ident;
43+
match variant.data {
44+
Unit => (),
45+
_ => {
46+
panic!("`FromPrimitive` can be applied only to unitary enums, {}::{} is either struct or tuple", name, ident)
47+
},
48+
}
3949
if let Some(val) = variant.discriminant {
4050
idx = val.value;
4151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
3+
// file at the top-level directory of this distribution and at
4+
// http://rust-lang.org/COPYRIGHT.
5+
//
6+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
// option. This file may not be copied, modified, or distributed
10+
// except according to those terms.
11+
12+
#![feature(rustc_macro)]
13+
14+
extern crate num;
15+
#[macro_use]
16+
extern crate num_macros;
17+
18+
#[derive(Debug, PartialEq, FromPrimitive)] //~ ERROR
19+
struct Color {
20+
r: u8,
21+
g: u8,
22+
b: u8,
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
3+
// file at the top-level directory of this distribution and at
4+
// http://rust-lang.org/COPYRIGHT.
5+
//
6+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
// option. This file may not be copied, modified, or distributed
10+
// except according to those terms.
11+
12+
#![feature(rustc_macro)]
13+
14+
extern crate num;
15+
#[macro_use]
16+
extern crate num_macros;
17+
18+
#[derive(Debug, PartialEq, FromPrimitive)] //~ ERROR
19+
enum Color {
20+
Rgb(u8, u8, u8),
21+
Hsv(u8, u8, u8),
22+
}
23+
24+
fn main() {}

macros/tests/compiletest.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
extern crate compiletest_rs as compiletest;
2+
3+
use std::path::PathBuf;
4+
use std::env::var;
5+
6+
fn run_mode(mode: &'static str) {
7+
let mut config = compiletest::default_config();
8+
9+
let cfg_mode = mode.parse().ok().expect("Invalid mode");
10+
11+
config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_owned());
12+
if let Ok(name) = var::<&str>("TESTNAME") {
13+
let s : String = name.to_owned();
14+
config.filter = Some(s)
15+
}
16+
config.mode = cfg_mode;
17+
config.src_base = PathBuf::from(format!("tests/{}", mode));
18+
19+
compiletest::run_tests(&config);
20+
}
21+
22+
#[test]
23+
fn compile_test() {
24+
run_mode("compile-fail");
25+
}

macros/tests/empty_enum.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
3+
// file at the top-level directory of this distribution and at
4+
// http://rust-lang.org/COPYRIGHT.
5+
//
6+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
// option. This file may not be copied, modified, or distributed
10+
// except according to those terms.
11+
12+
#![feature(rustc_macro)]
13+
14+
extern crate num;
15+
#[macro_use]
16+
extern crate num_macros;
17+
18+
#[derive(Debug, PartialEq, FromPrimitive)]
19+
enum Color {}
20+
21+
#[test]
22+
fn test_empty_enum() {
23+
let v: [Option<Color>; 1] = [num::FromPrimitive::from_u64(0)];
24+
25+
assert_eq!(v, [None]);
26+
}

0 commit comments

Comments
 (0)