1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
extern crate proc_macro2;
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
extern crate rand;
#[cfg_attr(test, macro_use)]
extern crate syn;
#[macro_use]
extern crate synstructure;

use proc_macro2::TokenStream;

decl_derive!([Arbitrary] => arbitrary_derive);

fn arbitrary_derive(s: synstructure::Structure) -> TokenStream {
    let (g, body) = match s.variants().len() {
        // zero-variant enum
        0 => panic!("Cannot derive `Arbitrary` for an enum with no variants."),

        // struct or single-variant enum
        1 => {
            let body = s.variants()[0].construct(|_, _| quote! { ::quickcheck::Arbitrary::arbitrary(g) });
            let g = if let syn::Fields::Unit = s.variants()[0].ast().fields {
                quote!(_g)
            } else {
                quote!(g)
            };

            (g, body)
        },

        // multiple-variant enum
        _ => {
            let mut variant_tokens = TokenStream::new();

            for (count, variant) in s.variants().iter().enumerate() {
                let constructor = variant.construct(|_, _| quote! { ::quickcheck::Arbitrary::arbitrary(g) });
                variant_tokens.extend(quote! { #count => #constructor, });
            }

            let count = s.variants().len();

            let body = quote! {
                match ::rand::Rng::gen_range(g, 0, #count) {
                    #variant_tokens
                    _ => unreachable!()
                }
            };

            (quote!(g), body)
        },
    };

    s.gen_impl(quote! {
        gen impl ::quickcheck::Arbitrary for @Self {
            fn arbitrary<G: ::quickcheck::Gen>(#g: &mut G) -> Self {
                #body
            }
        }
    })
}

#[test]
fn test_arbitrary_unit_struct() {
    test_derive! {
        arbitrary_derive {
            #[derive(Clone)]
            struct ArbitraryTest;
        }
        expands to {
            #[allow(non_upper_case_globals)]
            const _DERIVE_quickcheck_Arbitrary_FOR_ArbitraryTest: () = {
                impl ::quickcheck::Arbitrary for ArbitraryTest {
                    fn arbitrary<G: ::quickcheck::Gen>(_g: &mut G) -> Self {
                        ArbitraryTest
                    }
                }
            };
        }
    }
}

#[test]
fn test_arbitrary_struct() {
    test_derive! {
        arbitrary_derive {
            #[derive(Clone)]
            struct ArbitraryTest(u8, bool);
        }
        expands to {
            #[allow(non_upper_case_globals)]
            const _DERIVE_quickcheck_Arbitrary_FOR_ArbitraryTest: () = {
                impl ::quickcheck::Arbitrary for ArbitraryTest {
                    fn arbitrary<G: ::quickcheck::Gen>(g: &mut G) -> Self {
                        ArbitraryTest(::quickcheck::Arbitrary::arbitrary(g),
                                      ::quickcheck::Arbitrary::arbitrary(g), )
                    }
                }
            };
        }
    }
}

#[test]
#[should_panic(expected = "Cannot derive `Arbitrary` for an enum with no variants.")]
fn test_arbitrary_zero_variant_enum() {
    let input = parse_quote! {
        #[derive(Clone)]
        enum ArbitraryTest {}
    };

    arbitrary_derive(synstructure::Structure::new(&input));
}

#[test]
fn test_arbitrary_enum() {
    test_derive! {
        arbitrary_derive {
            #[derive(Clone)]
            enum ArbitraryTest {
                A,
                B(usize, u32),
                C{ b: bool, d: (u16, u16) }
            }
        }
        expands to {
            #[allow(non_upper_case_globals)]
            const _DERIVE_quickcheck_Arbitrary_FOR_ArbitraryTest: () = {
                impl ::quickcheck::Arbitrary for ArbitraryTest {
                    fn arbitrary<G: ::quickcheck::Gen>(g: &mut G) -> Self {
                        match ::rand::Rng::gen_range(g, 0, 3usize) {
                            0usize => ArbitraryTest::A,
                            1usize => ArbitraryTest::B(::quickcheck::Arbitrary::arbitrary(g),
                                                       ::quickcheck::Arbitrary::arbitrary(g),
                                                      ),
                            2usize => ArbitraryTest::C {
                                    b : ::quickcheck::Arbitrary::arbitrary(g),
                                    d : ::quickcheck::Arbitrary::arbitrary(g),
                                },
                            _ => unreachable!()
                        }
                    }
                }
            };
        }
    }
}