From 39a9b55565c09cc806a73aab8e3ab6c3e6260bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Sun, 18 Sep 2016 22:40:58 +0200 Subject: [PATCH] Add test case for which `syn` currently fails --- macros/src/lib.rs | 3 +- macros/tests/{test_macro.rs => trivial.rs} | 2 +- macros/tests/with_custom_values.rs | 33 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) rename macros/tests/{test_macro.rs => trivial.rs} (95%) create mode 100644 macros/tests/with_custom_values.rs diff --git a/macros/src/lib.rs b/macros/src/lib.rs index dba3da1983..09b6647a6b 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -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!"), }; diff --git a/macros/tests/test_macro.rs b/macros/tests/trivial.rs similarity index 95% rename from macros/tests/test_macro.rs rename to macros/tests/trivial.rs index 12452661f1..709fc2c8fd 100644 --- a/macros/tests/test_macro.rs +++ b/macros/tests/trivial.rs @@ -22,7 +22,7 @@ enum Color { } #[test] -fn test_from_primitive() { +fn test_from_primitive_for_trivial_case() { let v: [Option; 4] = [num::FromPrimitive::from_u64(0), num::FromPrimitive::from_u64(1), num::FromPrimitive::from_u64(2), diff --git a/macros/tests/with_custom_values.rs b/macros/tests/with_custom_values.rs new file mode 100644 index 0000000000..7373570bdb --- /dev/null +++ b/macros/tests/with_custom_values.rs @@ -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 or the MIT license +// , 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; 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]); +}