Skip to content

Commit 39a9b55

Browse files
committed
Add test case for which syn currently fails
1 parent decfedb commit 39a9b55

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

macros/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
2525
let source = input.to_string();
2626

2727
let ast = syn::parse_item(&source).unwrap();
28-
// panic!("{:?}", ast);
2928

3029
let name = &ast.ident;
31-
let variants: &[_] = match ast.body {
30+
let variants = match ast.body {
3231
Enum(ref variants) => variants,
3332
_ => panic!("#[derive(FromPrimitive)] works only with enums, struct given!"),
3433
};

macros/tests/test_macro.rs renamed to macros/tests/trivial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum Color {
2222
}
2323

2424
#[test]
25-
fn test_from_primitive() {
25+
fn test_from_primitive_for_trivial_case() {
2626
let v: [Option<Color>; 4] = [num::FromPrimitive::from_u64(0),
2727
num::FromPrimitive::from_u64(1),
2828
num::FromPrimitive::from_u64(2),

macros/tests/with_custom_values.rs

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

0 commit comments

Comments
 (0)