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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![allow(dead_code)]
mod test_produces_at_least_the_same {
use eager2::eager_macro;
/*
Test that a declared macro will work as if it was produced with 'macro_rules'
when not called through'eager'
*/
#[eager_macro]
macro_rules! test_macro{
{1} =>{ 1 };
(2) => ( 2 );
[3] => [ 3 ];
{4} => ( 4 );
(5) => { 5 };
[6] => [ 6 ];
{7} => [ 7 ];
(8) => { 8 };
[9] => ( 9 );
}
#[test]
fn test() {
assert_eq!(1, test_macro! {1});
assert_eq!(2, test_macro! {2});
assert_eq!(3, test_macro! {3});
assert_eq!(4, test_macro! {4});
assert_eq!(5, test_macro! {5});
assert_eq!(6, test_macro! {6});
assert_eq!(7, test_macro! {7});
assert_eq!(8, test_macro! {8});
assert_eq!(9, test_macro! {9});
}
}
mod test_eager {
use eager2::{eager, eager_macro};
/*
Test that a declared macro will work as if it was produced with 'macro_rules'
when not called through'eager'
*/
eager! {
#[eager_macro]
macro_rules! test_macro {
{1} =>{ 1 };
(2) => ( 2 );
[3] => [ 3 ];
{4} => ( 4 );
(5) => { 5 };
[6] => [ 6 ];
{7} => [ 7 ];
(8) => { 8 };
[9] => ( 9 );
}}
#[test]
fn test() {
assert_eq!(1, test_macro! {1});
assert_eq!(2, test_macro! {2});
assert_eq!(3, test_macro! {3});
assert_eq!(4, test_macro! {4});
assert_eq!(5, test_macro! {5});
assert_eq!(6, test_macro! {6});
assert_eq!(7, test_macro! {7});
assert_eq!(8, test_macro! {8});
assert_eq!(9, test_macro! {9});
}
}
mod test_produces_eager_macro {
use eager2::{eager, eager_macro};
/*
Test that a declared macro will work with eager!
*/
#[eager_macro]
macro_rules! test_macro{
{1} => { + 1 };
{2} => {eager!{1 test_macro!(1)}};
{3} => {eager!{1 test_macro!(1) test_macro!(1)}};
{4} => {test_macro!(2) + test_macro!(2)};
}
#[test]
fn test() {
assert_eq!(2, test_macro! {2});
assert_eq!(3, test_macro! {3});
assert_eq!(4, test_macro! {4});
}
}
mod test_eager_vs_non_eager_expansion_order {
use eager2::{eager, eager_macro};
/*
Test that the expanded macro has the eager versions of each rule first.
This is required because the other way around may result in the eager
calls not using the correct rule.
For example, if 'mac1' below is expanded to:
macro_rules! mac1{
{
$($to_encapsulate:tt)*
}=>{
{$($to_encapsulate)*}
}
<and then the eager version>
}
In this case eager! would not work because when it calls the macro (mac1), the pure
rule will match the initial '@eager', which is not intended.
*/
#[eager_macro]
macro_rules! mac1 {
{
$($to_encapsulate:tt)*
}=>{
{$($to_encapsulate)*}
}
}
macro_rules! mac2 {
($some:ident) => {
eager! {
struct $some
mac1!{x: u32}
}
};
}
mac2! {SomeStruct}
}
mod test_attributes {
/*
Tests that can assign attributes to declared macros.
*/
#[macro_use]
mod test_mod {
use eager2::eager_macro;
#[eager_macro]
#[macro_export]
#[doc(hidden)] // Whether this gets the correct effect cannot be tested
macro_rules! test_macro_1 {
() => {
1
};
}
#[macro_export]
#[doc(hidden)] // Whether this gets the correct effect cannot be tested
#[eager_macro]
macro_rules! test_macro_2 {
() => {
2
};
}
}
#[test]
fn test() {
assert_eq!(1, test_macro_1!());
assert_eq!(2, test_macro_2!());
}
}
mod test_rustdoc {
/*
Tests that can assign rustdoc to the declared macros.
Whether the docs are generated correctly cannot be tested through usual
rust testing methods. But we can at least test that the docs may be present.
*/
#[macro_use]
mod test_mod {
use eager2::eager_macro;
#[eager_macro]
#[macro_export]
///
/// Some docs
///
macro_rules! test_macro_3 {
() => {
3
};
}
#[macro_export]
///
/// Some docs
///
#[eager_macro]
macro_rules! test_macro_4 {
() => {
4
};
}
}
#[test]
fn test() {
assert_eq!(3, test_macro_3!());
assert_eq!(4, test_macro_4!());
}
}