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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use std::cell::Cell;
use std::path::Path;
use std::time::{Instant};
use colored_truecolor::Colorize;

/// Unit
pub struct Unit {
    assertions: Cell<i32>,
    failures: Cell<i32>,
    start: Instant,
}

impl Unit {
    ///
    /// Unit Constructor
    ///
    /// - `description` A short description to describe the test
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// Unit::new("Test description here");
    /// ```
    ///
    pub fn new(description: &str) -> Unit
    {
        println!("{}", description.magenta().bold());
        Self {
            assertions: Cell::from(0),
            failures: Cell::from(0),
            start: Instant::now(),
        }
    }

    ///
    /// Run a test
    ///
    /// - `tdd` The test to execute
    /// ```
    /// use unit::Unit;
    ///
    /// Unit::new("pythagore").check( 3*3 + 4*4 == 5*5).end();
    /// ```
    ///
    pub fn check(self, tdd: bool) -> Unit
    {
        if tdd
        {
            self.assertions.set(self.assertions.get() + 1);
            print!("{}", ".".green().bold());
        } else {
            self.failures.set(self.failures.get() + 1);
            print!("{}", "F".red().bold());
        }
        return self;
    }

    ///
    /// Check if actual match true
    ///
    ///
    /// - `actual` The pointer to the function to check if she return true
    /// ```
    /// use std::env;
    /// use unit::Unit;
    ///
    /// fn is_linux() -> bool
    /// {
    ///     return env::consts::OS == "linux";
    /// }
    ///
    /// Unit::new("Check if it's a linux pc").ok(&is_linux).end();
    /// ```
    pub fn ok(self, actual: &dyn Fn() -> bool) -> Unit
    {
        return self.check(actual());
    }

    ///
    /// Check if actual match false
    ///
    /// - `actual` The pointer to the function to check if she return false
    ///
    /// ```
    /// use std::env;
    /// use unit::Unit;
    ///
    /// fn is_windows() -> bool
    /// {
    ///     return env::consts::OS == "windows";
    /// }
    ///
    /// Unit::new("Check if it's a windows pc").ko(&is_windows).end();
    /// ```
    ///
    pub fn ko(self, actual: &dyn Fn() -> bool) -> Unit
    {
        return self.check(!actual());
    }

    ///
    /// Check if the function return the expected number
    ///
    /// - `actual` The pointer to the function to test
    /// - `expected` The expected return result
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn hypot()-> i32
    /// {
    ///     return 5*5;
    /// }
    ///
    /// Unit::new("Verify the multiplication of the number 5 by 5").equals(&hypot,25)
    /// .end();
    /// ```
    ///
    pub fn equals(self, actual: &dyn Fn() -> i32, expected: i32) -> Unit
    {
        return self.check(actual() == expected);
    }

    ///
    /// Check if the function not return the expected number
    ///
    /// - `actual`      The pointer to the function to test
    /// - `expected`    The expected value
    ///
    /// ```
    ///
    /// use unit::Unit;
    ///
    /// fn get_age()-> i32
    /// {
    ///     return 30;
    /// }
    ///
    /// Unit::new("The age it's not equals to 200 years").unequals(&get_age,200);
    /// ```
    ///
    pub fn unequals(self, actual: &dyn Fn() -> i32, expected: i32) -> Unit
    {
        return self.check(actual() != expected);
    }

    ///
    /// Check if two strings are equals
    ///
    /// - `actual`      The string actual
    /// - `expected`    The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let admin = String::from("taishingi");
    ///
    /// Unit::new("Check if the admin username is correct").identical(&admin,"taishingi").end();
    /// ```
    ///
    pub fn identical(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.eq(expected));
    }

    ///
    /// Check if the actual value is empty
    ///
    /// - `actual` The string to check if is empty
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let users:[&str;3] = ["","nautilus","power"];
    ///
    /// Unit::new("Check if user password is empty").empty(users[0]).end();
    /// ```
    pub fn empty(self, actual: &str) -> Unit
    {
        return self.check(actual == "");
    }

    ///
    /// Check if the actual value is not empty
    ///
    /// - `actual` The string to check if is not empty
    /// ```
    /// use unit::Unit;
    ///
    /// let nums:[&str;3] = ["911","14",""];
    ///
    /// Unit::new("Check if the phone number is defined").not_empty(nums[1]).end();
    ///
    /// ```
    ///
    pub fn not_empty(self, actual: &str) -> Unit
    {
        return self.check(actual != "");
    }

    ///
    ///
    /// Check if two strings are unequals
    ///
    /// - `actual`      The actual value
    /// - `expected`    The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let cars:[&str;3] = ["volvo","citroen","peugeot"];
    ///
    /// Unit::new("Check if users cars has not a ferrari car").different(cars[0],"ferrari").end();
    /// ```
    ///
    pub fn different(self, actual: &str, expected: &str) -> Unit
    {
        return self.check(actual != expected);
    }

    ///
    /// Check if a string start with the expected value
    ///
    /// - `actual` The data to compare
    /// - `expected` The expected data at the beginning of actual
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let phrase = String::from("A superb woman kiss me last night.");
    ///
    /// Unit::new("Check the beginning of a phrase").begin(&phrase, "A").end();
    /// ```
    ///
    pub fn begin(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.starts_with(expected));
    }

    ///
    /// Check if the string finnish wish the expected value
    ///
    /// - `actual` The data to compare
    /// - `expected` The expected data at the end of actual
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let phrase = String::from("A musical who loves programming.");
    ///
    /// Unit::new("Check the end of a phrase").finnish(&phrase, "programming.").end();
    ///
    /// ```
    pub fn finnish(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.ends_with(expected));
    }

    ///
    /// Check if the callback f return 0
    ///
    /// - `f` The pointer to the function to check
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn s()-> i32
    /// {
    ///     return if 2%2 == 0 {0 } else { 1 };
    /// }
    ///
    /// Unit::new("Check if 2 is divisible by 2").success(&s).end();
    /// ```
    ///
    pub fn success(self, f: &dyn Fn() -> i32) -> Unit
    {
        return self.check(f() == 0);
    }

    ///
    /// Check if the callback f return 1
    ///
    /// - `f` The pointer to the callback to check
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn f()-> i32
    /// {
    ///     return if 4%3 == 0 { 0 } else { 1 };
    /// }
    ///
    /// Unit::new("Check if 2 is divisible by 3").failure(&f).end();
    /// ```
    ///
    ///
    pub fn failure(self, f: &dyn Fn() -> i32) -> Unit
    {
        return self.check(f() == 1);
    }

    ///
    /// Check a theory
    ///
    /// - `description`     The theory description
    /// - `f`               The pointer to the function to check
    /// - `expected`        The expected theory value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn matrix()->bool
    /// {
    ///     return false;
    /// }
    ///
    /// Unit::new("We are in the matrices ?").theory("All are real ?",&matrix,false);
    /// ```
    ///
    pub fn theory(self, description: &str, f: &dyn Fn() -> bool, expected: bool) -> Unit
    {
        println!("\n{} {} {}\n", "[".blue().bold(), description.cyan().bold(), "]".blue().bold());
        let r = self.check(f() == expected);
        print!("\n");
        return r;
    }

    ///
    /// Group a test case
    ///
    /// - `description`     The description to the test group
    /// - `f`               The callback to execute
    ///
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn hypot()-> i32
    /// {
    ///     return 25;
    /// }
    ///
    /// fn rectangle(u: Unit)-> Unit
    /// {
    ///     return u.equals(&hypot,3*3+4*4);
    /// }
    ///
    /// Unit::new("Test the pythagore theorem")
    /// .describe("The triangle must be rectangle",&rectangle).end();
    /// ```
    ///
    pub fn describe(self, description: &str, f: &dyn Fn(Unit) -> Unit) -> Unit
    {
        println!("\n{} {} {}\n", "[".blue().bold(), description.cyan().bold(), "]".blue().bold());
        let r = f(self);
        print!("\n");
        return r;
    }

    ///
    /// Check is the string is defined (not empty)
    ///
    /// - `actual` The value to check
    /// ```
    /// use unit::Unit;
    ///
    /// let s = String::from("Cleopatra love cesar");
    ///
    /// Unit::new("The string is empty ?").defined(&s).end();
    /// ```
    pub fn defined(self, actual: &String) -> Unit
    {
        return self.check(!actual.is_empty());
    }

    ///
    /// Check if the actual path exist
    ///
    /// - `actual` The path to check the existence
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// Unit::new("Check if the directory exist").exist("/home").end();
    /// ```
    ///
    pub fn exist(self, actual: &str) -> Unit
    {
        return self.check(Path::new(actual).exists());
    }

    ///
    ///
    /// Check if the actual string is not equal to expected
    ///
    /// - `actual`      The actual value to check
    /// - `expected`    The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let password = "1234"; /// not good ! but better than 0000.
    ///
    /// let bad_password = String::from("0000");
    ///
    /// Unit::new("Check if password don't match 0000").not_equal(&bad_password,password).end();
    /// ```
    ///
    pub fn not_equal(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.ne(expected));
    }

    ///
    ///
    /// Check if actual string is equal to expected
    ///
    /// - `actual`      The actual value to check
    /// - `expected`    The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let expected = "MxPx";
    ///
    /// let pass= String::from("MxPx");
    ///
    /// Unit::new("I've founded the password ?").expect(&pass,expected).end();
    /// ```
    ///
    pub fn expect(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.eq(expected));
    }

    ///
    /// Check if actual is equal to expected
    ///
    /// - `actual` The actual value
    /// - `expected` The expected number
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// Unit::new("birthday month...").expect_number(02,02).end();
    /// ```
    ///
    pub fn expect_number(self, actual: usize, expected: usize) -> Unit
    {
        return self.check(actual == expected);
    }

    ///
    /// Check if expected is found in the actual string
    ///
    /// - `actual` The actual value
    /// - `expected` The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let poem = String::from("Habibi i love you forever");
    ///
    /// Unit::new("Check if forever exist in the poem").found(&poem,"forever").end();
    /// ```
    ///
    pub fn found(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.find(expected) != None);
    }

    ///
    /// Check if expected value is not founded in the actual string
    ///
    ///
    /// - `actual` The string to parse
    /// - `expected` The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let poem = String::from("Habibi i love you forever");
    ///
    /// Unit::new("Check if habibi not exist in the poem").not_founded(&poem,"habibi").end();
    /// ```
    ///
    pub fn not_founded(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.find(expected) == None);
    }

    /// Check if expected value is ascii
    pub fn ascii(self, actual: String) -> Unit
    {
        return self.check(actual.is_ascii());
    }

    ///
    /// Check if a string contains a string
    ///
    /// - `actual` The actual value
    /// - `expected` The expected value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// let x = String::from("500000 pieces d'or");
    ///
    /// Unit::new("Check if a coffer is there").contains(&x,"or").end();
    ///
    /// ```
    ///
    ///
    pub fn contains(self, actual: &String, expected: &str) -> Unit
    {
        return self.check(actual.contains(expected));
    }

    ///
    /// Check if the actual value is superior to the expected value
    ///
    /// - `actual`      The actual value to check
    /// - `expected`    The superior's value expected for the actual value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn get_fans()->i32
    /// {
    ///     return 5000;
    /// }
    ///
    /// Unit::new("We have how many of fans").superior(&get_fans,500).end();
    /// ```
    pub fn superior(self, actual: &dyn Fn() -> i32, expected: i32) -> Unit
    {
        return self.check(actual() > expected);
    }

    ///
    /// Check if the actual value is inferior to the expected value
    ///
    /// - `actual` The pointer to the function
    /// - `expected` The inferior's value for the actual value
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// fn log_size()-> i32
    /// {
    ///     return 410;
    /// }
    ///
    /// Unit::new("Server log more full ?").inferior(&log_size,500).end();
    /// ```
    ///
    ///
    pub fn inferior(self, actual: &dyn Fn() -> i32, expected: i32) -> Unit
    {
        return self.check(actual() < expected);
    }

    ///
    /// Test if actual is between the min and the max value
    ///
    ///
    /// - `actual` The function pointer to the function to check
    /// - `min` The minimum value
    /// - `max` The maximum value
    /// -
    /// ```
    /// use unit::Unit;
    /// fn h()-> i32
    /// {
    ///     return 5*5;
    /// }
    /// Unit::new("test if a number is between 0 and 100").between(&h,24,26).end();
    /// ```
    pub fn between(self, actual: &dyn Fn() -> i32, min: i32, max: i32) -> Unit
    {
        let x = actual();
        return self.check(x < max && x > min);
    }

    ///
    /// End of the tests
    ///
    /// ```
    /// use unit::Unit;
    ///
    /// Unit::new("The superb description").expect_number(1,1).end();
    /// ```
    pub fn end(self) -> i32
    {
        let asserts = self.assertions.get();
        let fails = self.failures.get();
        let total = asserts + fails;

        let mut i = ((asserts * 100) / total) / 2;
        print!("\n{}", "[".white().bold());
        while i > 0 {
            print!("{}", "#".green().bold());
            i = i - 1;
        }
        i = ((fails * 100) / total) / 2;
        while i > 0 {
            print!("{}", "#".red().bold());
            i = i - 1;
        }
        print!("{} {} {} {} {} {:?} {}", "]-[".white().bold(), asserts.to_string().green().bold(), "]-[".white().bold(), fails.to_string().red().bold(), "]-[".white().bold(), self.start.elapsed(), "]".white().bold());
        return if self.failures.get() > 0 { 1 } else { 0 };
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    fn kool() -> bool
    {
        return false;
    }

    fn look() -> bool
    {
        return true;
    }

    fn f() -> i32
    {
        return 1;
    }

    fn theorem() -> bool
    {
        return 4 * 4 + 3 * 3 == 5 * 5;
    }

    fn s() -> i32
    {
        return 0;
    }

    fn status(u: Unit) -> Unit
    {
        return u.failure(&f).success(&s);
    }

    fn diff(u: Unit) -> Unit
    {
        return u.unequals(&f, 2).different("a", "b");
    }

    fn files(u: Unit) -> Unit
    {
        return u.exist(".");
    }

    fn numbers(u: Unit) -> Unit
    {
        return u.equals(&s, 0).equals(&f, 1).unequals(&f, 2).between(&s, -1, 10).superior(&f, 0).inferior(&f, 10);
    }

    fn test_string(u: Unit) -> Unit
    {
        let phrase = String::from("The humanity develop the linux kernel.");
        return u.empty("").not_empty(" ").identical(&phrase, "The humanity develop the linux kernel.").contains(&phrase, "linux").begin(&phrase, "The").finnish(&phrase, "kernel.");
    }

    fn boolean(u: Unit) -> Unit
    {
        return u.ok(&look).ko(&kool);
    }

    #[test]
    fn it_works() {
        let u = Unit::new("Test the unit framework");
        u.describe("Test all boolean", &boolean)
            .describe("Test numbers", &numbers)
            .describe("Test success and failure", &status)
            .describe("Test differences", &diff)
            .describe("Test string", &test_string)
            .describe("Test files", &files)
            .theory("Test pythagore", &theorem, true)
            .end();
    }
}