Skip to content

Commit

Permalink
Add test case for which syn currently fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Sep 18, 2016
1 parent decfedb commit 39a9b55
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 1 addition & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
let source = input.to_string();

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

let name = &ast.ident;
let variants: &[_] = match ast.body {
let variants = match ast.body {
Enum(ref variants) => variants,
_ => panic!("#[derive(FromPrimitive)] works only with enums, struct given!"),
};
Expand Down
2 changes: 1 addition & 1 deletion macros/tests/test_macro.rs → macros/tests/trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum Color {
}

#[test]
fn test_from_primitive() {
fn test_from_primitive_for_trivial_case() {
let v: [Option<Color>; 4] = [num::FromPrimitive::from_u64(0),
num::FromPrimitive::from_u64(1),
num::FromPrimitive::from_u64(2),
Expand Down
33 changes: 33 additions & 0 deletions macros/tests/with_custom_values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_macro)]

extern crate num;
#[macro_use]
extern crate num_macros;

#[derive(Debug, PartialEq, FromPrimitive)]
enum Color {
Red,
Blue = 5,
Green,
}

#[test]
fn test_from_primitive_for_enum_with_custom_value() {
let v: [Option<Color>; 4] = [num::FromPrimitive::from_u64(0),
num::FromPrimitive::from_u64(5),
num::FromPrimitive::from_u64(6),
num::FromPrimitive::from_u64(3)];

assert_eq!(v,
[Some(Color::Red), Some(Color::Blue), Some(Color::Green), None]);
}

0 comments on commit 39a9b55

Please sign in to comment.