test_toolbox/
actual.rs

1/// Macro to define different actual variables for `debug` and `release`
2///
3/// This is useful when testing logic with differing actual variable definitions based on build.
4///
5/// Basically erogonomifies the following ...
6///
7/// ```rust
8/// # use cfg_if::cfg_if;
9/// cfg_if! {
10///    if #[cfg(not(debug_assertions))] {
11///        let expected = Default::default();
12///    } else {
13///        let expected = 42;
14///    }
15/// }
16/// cfg_if! {
17///    if #[cfg(not(debug_assertions))] {
18///        let actual: usize = Default::default();
19///    } else {
20///        let actual: usize;
21///    }
22/// }
23///
24/// #[cfg(debug_assertions)]
25/// { actual = sut(); }
26///
27/// assert_eq!(expected, actual);
28/// # #[cfg(debug_assertions)]
29/// # fn sut() -> usize { 42 }
30/// ```
31/// with ..
32/// ```rust
33/// # use test_toolbox::actual;
34/// # use test_toolbox::expect;
35/// expect! { expected = Default::default(), 42 }
36/// actual! { @dbg actual: usize }
37///
38/// #[cfg(debug_assertions)]
39/// { actual = sut(); }
40///
41/// assert_eq!(expected, actual);
42/// # #[cfg(debug_assertions)]
43/// # fn sut() -> usize { 42 }
44/// ```
45///
46/// # Features
47///
48/// *
49/// *
50///
51/// # Examples
52///
53/// * debug; uninitialized actual variable
54///   release; actual variable initialized to default
55///
56/// ```rust
57/// # use test_toolbox::actual;
58/// # use test_toolbox::expect;
59/// expect! { expected: usize = Default::default(), 42 }
60/// actual! { @dbg actual: usize }
61///
62/// #[cfg(debug_assertions)]
63/// { actual = sut(); }
64///
65/// assert_eq!(expected, actual);
66/// # #[cfg(debug_assertions)]
67/// # fn sut() -> usize { 42 }
68/// ```
69///
70/// * release; uninitialized actual variable
71///   debug; actual variable initialized to default
72///
73/// ```rust
74/// # use test_toolbox::actual;
75/// # use test_toolbox::expect;
76/// expect! { expected: usize = 42, Default::default() }
77/// actual! { @rls actual: usize }
78///
79/// #[cfg(not(debug_assertions))]
80/// { actual = sut(); }
81///
82/// assert_eq!(expected, actual);
83/// # #[cfg(not(debug_assertions))]
84/// # fn sut() -> usize { 42 }
85/// ```
86///
87///  * debug; uninitialized actual variable
88///    release; actual variable initialized to a value
89///
90/// ```rust
91/// # use test_toolbox::actual;
92/// # use test_toolbox::expect;
93/// expect! { expected: usize = 2, 42 }
94/// actual! { @dbg actual: usize; 2 }
95///
96/// #[cfg(debug_assertions)]
97/// { actual = sut(); }
98///
99/// assert_eq!(expected, actual);
100/// # #[cfg(debug_assertions)]
101/// # fn sut() -> usize { 42 }
102/// ```
103///
104/// * release; uninitialized actual variable
105///   debug; actual variable initialized to a value
106///
107/// ```rust
108/// # use test_toolbox::actual;
109/// # use test_toolbox::expect;
110/// expect! { expected: usize = 42, 2 }
111/// actual! { @rls actual: usize; 2 }
112///
113/// #[cfg(not(debug_assertions))]
114/// { actual = sut(); }
115///
116/// assert_eq!(expected, actual);
117/// # #[cfg(not(debug_assertions))]
118/// # fn sut() -> usize { 42 }
119/// ```
120///
121/// * debug; mutable actual variable initialized to a value
122///   release; immutable actual variable initialized to a value
123///
124/// ```rust
125/// # use test_toolbox::actual;
126/// # use test_toolbox::expect;
127/// expect! { expected = "", "Forty Two" }
128/// actual! { @dbg mut actual: String; String::new() }
129///
130/// #[cfg(debug_assertions)]
131/// { actual.push_str(&sut()); }
132///
133/// assert_eq!(expected, &actual);
134/// # #[cfg(debug_assertions)]
135/// # fn sut() -> String { String::from("Forty Two") }
136/// ```
137///
138/// * release; mutable actual variable initialized to a value
139///   debug; immutable actual variable initialized to a value
140///
141/// ```rust
142/// # use test_toolbox::actual;
143/// # use test_toolbox::expect;
144/// expect! { expected = "Forty Two", "" }
145/// actual! { @rls mut actual: String; String::new() }
146///
147/// #[cfg(not(debug_assertions))]
148/// { actual.push_str(&sut()); }
149///
150/// assert_eq!(expected, &actual);
151/// # #[cfg(not(debug_assertions))]
152/// # fn sut() -> String { String::from("Forty Two") }
153/// ```
154#[macro_export]
155macro_rules! actual {
156    // debug; uninitialized actual variable
157    // release; actual variable initialized to default
158    (@dbg $var:ident: $typ:ty) => {
159        $crate::cfg_if::cfg_if! {
160            if #[cfg(not(debug_assertions))] {
161                let $var: $typ = Default::default();
162            } else {
163                let $var: $typ;
164            }
165        }
166    };
167    // release; uninitialized actual variable
168    // debug; actual variable initialized to default
169    (@rls $var:ident: $typ:ty) => {
170        $crate::cfg_if::cfg_if! {
171            if #[cfg(not(debug_assertions))] {
172                let $var: $typ;
173            } else {
174                let $var: $typ = Default::default();
175            }
176        }
177    };
178    // debug; uninitialized actual variable
179    // release; actual variable initialized to a value
180    (@dbg $var:ident: $typ:ty; $val:expr) => {
181        $crate::cfg_if::cfg_if! {
182            if #[cfg(not(debug_assertions))] {
183                let $var: $typ = $val;
184            } else {
185                let $var: $typ;
186            }
187        }
188    };
189    // release; uninitialized actual variable
190    // debug; actual variable initialized to a value
191    (@rls $var:ident: $typ:ty; $val:expr) => {
192        $crate::cfg_if::cfg_if! {
193            if #[cfg(not(debug_assertions))] {
194                let $var: $typ;
195            } else {
196                let $var: $typ = $val;
197            }
198        }
199    };
200    // debug; mutable actual variable initialized to a value
201    // release; immutable actual variable initialized to a value
202    (@dbg mut $var:ident: $typ:ty; $exp:expr) => {
203        $crate::cfg_if::cfg_if! {
204            if #[cfg(not(debug_assertions))] {
205                let $var = $exp;
206            } else {
207                let mut $var = $exp;
208            }
209        }
210    };
211    // release; mutable actual variable initialized to a value
212    // debug; immutable actual variable initialized to a value
213    (@rls mut $var:ident: $typ:ty; $exp:expr) => {
214        $crate::cfg_if::cfg_if! {
215            if #[cfg(not(debug_assertions))] {
216                let mut $var = $exp;
217            } else {
218                let $var = $exp;
219            }
220        }
221    };
222}