ferro-rs 0.2.14

A Laravel-inspired web framework for Rust
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Fluent assertion API inspired by Jest's expect
//!
//! Provides a fluent API for assertions with clear expected/received output.

use std::fmt::Debug;

std::thread_local! {
    /// Thread-local storage for current test name (set by test! macro)
    pub static CURRENT_TEST_NAME: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
}

/// Set the current test name (called by test! macro)
pub fn set_current_test_name(name: Option<String>) {
    CURRENT_TEST_NAME.with(|cell| {
        *cell.borrow_mut() = name;
    });
}

/// Get the current test name for error messages
fn get_test_name() -> Option<String> {
    CURRENT_TEST_NAME.with(|cell| cell.borrow().clone())
}

/// Format the assertion failure header
fn format_header(location: &str) -> String {
    if let Some(name) = get_test_name() {
        format!("\nTest: \"{name}\"\n  at {location}\n")
    } else {
        format!("\nassertion failed at {location}\n")
    }
}

/// The main Expect wrapper for fluent assertions
pub struct Expect<T> {
    value: T,
    location: &'static str,
}

impl<T> Expect<T> {
    /// Create a new Expect wrapper (use the expect! macro instead)
    pub fn new(value: T, location: &'static str) -> Self {
        Self { value, location }
    }
}

// Equality matchers for Debug + PartialEq types
impl<T: Debug + PartialEq> Expect<T> {
    /// Assert that the value equals the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(actual).to_equal(expected);
    /// ```
    pub fn to_equal(&self, expected: T) {
        if self.value != expected {
            panic!(
                "{}\n  expect!(actual).to_equal(expected)\n\n  Expected: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                expected,
                self.value
            );
        }
    }

    /// Assert that the value does not equal the unexpected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(actual).to_not_equal(unexpected);
    /// ```
    pub fn to_not_equal(&self, unexpected: T) {
        if self.value == unexpected {
            panic!(
                "{}\n  expect!(actual).to_not_equal(value)\n\n  Expected NOT: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                unexpected,
                self.value
            );
        }
    }
}

// Boolean matchers
impl Expect<bool> {
    /// Assert that the value is true
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(condition).to_be_true();
    /// ```
    pub fn to_be_true(&self) {
        if !self.value {
            panic!(
                "{}\n  expect!(value).to_be_true()\n\n  Expected: true\n  Received: false\n",
                format_header(self.location)
            );
        }
    }

    /// Assert that the value is false
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(condition).to_be_false();
    /// ```
    pub fn to_be_false(&self) {
        if self.value {
            panic!(
                "{}\n  expect!(value).to_be_false()\n\n  Expected: false\n  Received: true\n",
                format_header(self.location)
            );
        }
    }
}

// Option matchers
impl<T: Debug> Expect<Option<T>> {
    /// Assert that the Option is Some
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(option).to_be_some();
    /// ```
    pub fn to_be_some(&self) {
        if self.value.is_none() {
            panic!(
                "{}\n  expect!(option).to_be_some()\n\n  Expected: Some(_)\n  Received: None\n",
                format_header(self.location)
            );
        }
    }

    /// Assert that the Option is None
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(option).to_be_none();
    /// ```
    pub fn to_be_none(&self) {
        if let Some(ref v) = self.value {
            panic!(
                "{}\n  expect!(option).to_be_none()\n\n  Expected: None\n  Received: Some({:?})\n",
                format_header(self.location),
                v
            );
        }
    }
}

// Option with PartialEq for to_contain
impl<T: Debug + PartialEq> Expect<Option<T>> {
    /// Assert that the Option contains the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(Some(5)).to_contain_value(5);
    /// ```
    pub fn to_contain_value(&self, expected: T) {
        match &self.value {
            Some(v) if *v == expected => {}
            Some(v) => {
                panic!(
                    "{}\n  expect!(option).to_contain_value(expected)\n\n  Expected: Some({:?})\n  Received: Some({:?})\n",
                    format_header(self.location),
                    expected,
                    v
                );
            }
            None => {
                panic!(
                    "{}\n  expect!(option).to_contain_value(expected)\n\n  Expected: Some({:?})\n  Received: None\n",
                    format_header(self.location),
                    expected
                );
            }
        }
    }
}

// Result matchers
impl<T: Debug, E: Debug> Expect<Result<T, E>> {
    /// Assert that the Result is Ok
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(result).to_be_ok();
    /// ```
    pub fn to_be_ok(&self) {
        if let Err(ref e) = self.value {
            panic!(
                "{}\n  expect!(result).to_be_ok()\n\n  Expected: Ok(_)\n  Received: Err({:?})\n",
                format_header(self.location),
                e
            );
        }
    }

    /// Assert that the Result is Err
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(result).to_be_err();
    /// ```
    pub fn to_be_err(&self) {
        if let Ok(ref v) = self.value {
            panic!(
                "{}\n  expect!(result).to_be_err()\n\n  Expected: Err(_)\n  Received: Ok({:?})\n",
                format_header(self.location),
                v
            );
        }
    }
}

// String matchers
impl Expect<String> {
    /// Assert that the string contains the substring
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(string).to_contain("hello");
    /// ```
    pub fn to_contain(&self, substring: &str) {
        if !self.value.contains(substring) {
            panic!(
                "{}\n  expect!(string).to_contain(substring)\n\n  Expected to contain: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                substring,
                self.value
            );
        }
    }

    /// Assert that the string starts with the prefix
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(string).to_start_with("hello");
    /// ```
    pub fn to_start_with(&self, prefix: &str) {
        if !self.value.starts_with(prefix) {
            panic!(
                "{}\n  expect!(string).to_start_with(prefix)\n\n  Expected to start with: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                prefix,
                self.value
            );
        }
    }

    /// Assert that the string ends with the suffix
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(string).to_end_with("world");
    /// ```
    pub fn to_end_with(&self, suffix: &str) {
        if !self.value.ends_with(suffix) {
            panic!(
                "{}\n  expect!(string).to_end_with(suffix)\n\n  Expected to end with: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                suffix,
                self.value
            );
        }
    }

    /// Assert that the string has the expected length
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(string).to_have_length(5);
    /// ```
    pub fn to_have_length(&self, expected: usize) {
        let actual = self.value.len();
        if actual != expected {
            panic!(
                "{}\n  expect!(string).to_have_length({})\n\n  Expected length: {}\n  Actual length: {}\n  Value: {:?}\n",
                format_header(self.location),
                expected,
                expected,
                actual,
                self.value
            );
        }
    }

    /// Assert that the string is empty
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(string).to_be_empty();
    /// ```
    pub fn to_be_empty(&self) {
        if !self.value.is_empty() {
            panic!(
                "{}\n  expect!(string).to_be_empty()\n\n  Expected: \"\"\n  Received: {:?}\n",
                format_header(self.location),
                self.value
            );
        }
    }
}

// &str matchers
impl Expect<&str> {
    /// Assert that the string contains the substring
    pub fn to_contain(&self, substring: &str) {
        if !self.value.contains(substring) {
            panic!(
                "{}\n  expect!(string).to_contain(substring)\n\n  Expected to contain: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                substring,
                self.value
            );
        }
    }

    /// Assert that the string starts with the prefix
    pub fn to_start_with(&self, prefix: &str) {
        if !self.value.starts_with(prefix) {
            panic!(
                "{}\n  expect!(string).to_start_with(prefix)\n\n  Expected to start with: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                prefix,
                self.value
            );
        }
    }

    /// Assert that the string ends with the suffix
    pub fn to_end_with(&self, suffix: &str) {
        if !self.value.ends_with(suffix) {
            panic!(
                "{}\n  expect!(string).to_end_with(suffix)\n\n  Expected to end with: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                suffix,
                self.value
            );
        }
    }

    /// Assert that the string has the expected length
    pub fn to_have_length(&self, expected: usize) {
        let actual = self.value.len();
        if actual != expected {
            panic!(
                "{}\n  expect!(string).to_have_length({})\n\n  Expected length: {}\n  Actual length: {}\n  Value: {:?}\n",
                format_header(self.location),
                expected,
                expected,
                actual,
                self.value
            );
        }
    }

    /// Assert that the string is empty
    pub fn to_be_empty(&self) {
        if !self.value.is_empty() {
            panic!(
                "{}\n  expect!(string).to_be_empty()\n\n  Expected: \"\"\n  Received: {:?}\n",
                format_header(self.location),
                self.value
            );
        }
    }
}

// Vec matchers
impl<T: Debug + PartialEq> Expect<Vec<T>> {
    /// Assert that the Vec has the expected length
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(vec).to_have_length(3);
    /// ```
    pub fn to_have_length(&self, expected: usize) {
        let actual = self.value.len();
        if actual != expected {
            panic!(
                "{}\n  expect!(vec).to_have_length({})\n\n  Expected length: {}\n  Actual length: {}\n",
                format_header(self.location),
                expected,
                expected,
                actual
            );
        }
    }

    /// Assert that the Vec contains the item
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(vec).to_contain(&item);
    /// ```
    pub fn to_contain(&self, item: &T) {
        if !self.value.contains(item) {
            panic!(
                "{}\n  expect!(vec).to_contain(item)\n\n  Expected to contain: {:?}\n  Received: {:?}\n",
                format_header(self.location),
                item,
                self.value
            );
        }
    }

    /// Assert that the Vec is empty
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(vec).to_be_empty();
    /// ```
    pub fn to_be_empty(&self) {
        if !self.value.is_empty() {
            panic!(
                "{}\n  expect!(vec).to_be_empty()\n\n  Expected: []\n  Received: {:?}\n",
                format_header(self.location),
                self.value
            );
        }
    }
}

// Numeric comparison matchers using PartialOrd
#[allow(clippy::neg_cmp_op_on_partial_ord)]
impl<T: Debug + PartialOrd> Expect<T> {
    /// Assert that the value is greater than the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(10).to_be_greater_than(5);
    /// ```
    pub fn to_be_greater_than(&self, expected: T) {
        if !(self.value > expected) {
            panic!(
                "{}\n  expect!(value).to_be_greater_than(expected)\n\n  Expected: > {:?}\n  Received: {:?}\n",
                format_header(self.location),
                expected,
                self.value
            );
        }
    }

    /// Assert that the value is less than the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(5).to_be_less_than(10);
    /// ```
    pub fn to_be_less_than(&self, expected: T) {
        if !(self.value < expected) {
            panic!(
                "{}\n  expect!(value).to_be_less_than(expected)\n\n  Expected: < {:?}\n  Received: {:?}\n",
                format_header(self.location),
                expected,
                self.value
            );
        }
    }

    /// Assert that the value is greater than or equal to the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(10).to_be_greater_than_or_equal(10);
    /// ```
    pub fn to_be_greater_than_or_equal(&self, expected: T) {
        if !(self.value >= expected) {
            panic!(
                "{}\n  expect!(value).to_be_greater_than_or_equal(expected)\n\n  Expected: >= {:?}\n  Received: {:?}\n",
                format_header(self.location),
                expected,
                self.value
            );
        }
    }

    /// Assert that the value is less than or equal to the expected value
    ///
    /// # Example
    /// ```rust,ignore
    /// expect!(5).to_be_less_than_or_equal(5);
    /// ```
    pub fn to_be_less_than_or_equal(&self, expected: T) {
        if !(self.value <= expected) {
            panic!(
                "{}\n  expect!(value).to_be_less_than_or_equal(expected)\n\n  Expected: <= {:?}\n  Received: {:?}\n",
                format_header(self.location),
                expected,
                self.value
            );
        }
    }
}