Skip to content

Commit 11d7890

Browse files
committed
Move compile-fail tests to doctests
1 parent b85397e commit 11d7890

File tree

8 files changed

+92
-123
lines changed

8 files changed

+92
-123
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ version = "0.2.0"
1212
readme = "README.md"
1313

1414
[dependencies]
15+
num-traits = "0.2"
1516
proc-macro2 = "0.2.1"
1617
quote = "0.4.2"
1718
syn = "0.12.7"
1819

1920
[dev-dependencies]
20-
compiletest_rs = "0.3.5"
21-
num-traits = "0.2"
2221
num = "0.1"
2322

2423
[features]

ci/test_full.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ set -ex
44

55
echo Testing num-derive on rustc ${TRAVIS_RUST_VERSION}
66

7-
# num-derive should build everywhere.
7+
# num-derive should build and test everywhere.
88
cargo build --verbose --features="$FEATURES"
9-
10-
# We have no features to test...
11-
12-
13-
if [ "$TRAVIS_RUST_VERSION" != nightly ]; then exit; fi
14-
15-
# num-derive testing requires compiletest_rs, which requires nightly
169
cargo test --verbose --features="$FEATURES"

src/lib.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,51 @@ use syn::{Data, Fields, Ident};
5353
/// Derives [`num_traits::FromPrimitive`][from] for simple enums.
5454
///
5555
/// [from]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.FromPrimitive.html
56+
///
57+
/// # Examples
58+
///
59+
/// Simple enums can be derived:
60+
///
61+
/// ```rust
62+
/// # #[macro_use]
63+
/// # extern crate num_derive;
64+
///
65+
/// #[derive(FromPrimitive)]
66+
/// enum Color {
67+
/// Red,
68+
/// Blue,
69+
/// Green = 42,
70+
/// }
71+
/// # fn main() {}
72+
/// ```
73+
///
74+
/// Enums that contain data are not allowed:
75+
///
76+
/// ```compile_fail
77+
/// # #[macro_use]
78+
/// # extern crate num_derive;
79+
///
80+
/// #[derive(FromPrimitive)]
81+
/// enum Color {
82+
/// Rgb(u8, u8, u8),
83+
/// Hsv(u8, u8, u8),
84+
/// }
85+
/// # fn main() {}
86+
/// ```
87+
///
88+
/// Structs are not allowed:
89+
///
90+
/// ```compile_fail
91+
/// # #[macro_use]
92+
/// # extern crate num_derive;
93+
/// #[derive(FromPrimitive)]
94+
/// struct Color {
95+
/// r: u8,
96+
/// g: u8,
97+
/// b: u8,
98+
/// }
99+
/// # fn main() {}
100+
/// ```
56101
#[proc_macro_derive(FromPrimitive)]
57102
pub fn from_primitive(input: TokenStream) -> TokenStream {
58103
let ast: syn::DeriveInput = syn::parse(input).unwrap();
@@ -112,6 +157,51 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
112157
/// Derives [`num_traits::ToPrimitive`][to] for simple enums.
113158
///
114159
/// [to]: https://docs.rs/num-traits/0.2/num_traits/cast/trait.ToPrimitive.html
160+
///
161+
/// # Examples
162+
///
163+
/// Simple enums can be derived:
164+
///
165+
/// ```rust
166+
/// # #[macro_use]
167+
/// # extern crate num_derive;
168+
///
169+
/// #[derive(ToPrimitive)]
170+
/// enum Color {
171+
/// Red,
172+
/// Blue,
173+
/// Green = 42,
174+
/// }
175+
/// # fn main() {}
176+
/// ```
177+
///
178+
/// Enums that contain data are not allowed:
179+
///
180+
/// ```compile_fail
181+
/// # #[macro_use]
182+
/// # extern crate num_derive;
183+
///
184+
/// #[derive(ToPrimitive)]
185+
/// enum Color {
186+
/// Rgb(u8, u8, u8),
187+
/// Hsv(u8, u8, u8),
188+
/// }
189+
/// # fn main() {}
190+
/// ```
191+
///
192+
/// Structs are not allowed:
193+
///
194+
/// ```compile_fail
195+
/// # #[macro_use]
196+
/// # extern crate num_derive;
197+
/// #[derive(ToPrimitive)]
198+
/// struct Color {
199+
/// r: u8,
200+
/// g: u8,
201+
/// b: u8,
202+
/// }
203+
/// # fn main() {}
204+
/// ```
115205
#[proc_macro_derive(ToPrimitive)]
116206
pub fn to_primitive(input: TokenStream) -> TokenStream {
117207
let ast: syn::DeriveInput = syn::parse(input).unwrap();

tests/compile-fail/from-primitive/derive_on_struct.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/compile-fail/from-primitive/enum_with_associated_data.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/compile-fail/to-primitive/derive_on_struct.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/compile-fail/to-primitive/enum_with_associated_data.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/compiletest.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)