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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#![cfg_attr(feature = "nightly", feature(specialization))]

use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
use std::rc::{Rc, Weak};

use std::fmt::Write;
use std::ops::DerefMut;

use itertools::Itertools;

#[macro_use]
mod colors;
pub mod cardinality;
mod dbg;
pub mod matchers;
#[macro_use]
pub mod clone;
pub mod type_info;

pub use crate::type_info::TypeInfo;
pub use dbg::DebugOnStable;

use crate::cardinality::{Cardinality, CardinalityCheckResult};
use crate::dbg::dbg;

thread_local! {
    // Mapping from mock_type_id of 'extern' block mock to corresponding mock object.
    // It is needed since mock is object but mocked functions are static.
    pub static EXTERN_MOCKS: RefCell<HashMap<usize, (usize, Rc<RefCell<ScenarioInternals>>)>> = RefCell::new(HashMap::new());
}

macro_rules! define_actions {
    ($action_clone:ident { $($Arg:ident),* }) => {
        type $action_clone<$($Arg,)* T> = Rc<RefCell<dyn FnMut($($Arg,)*) -> T>>;
    }
}

define_actions!(ActionClone0 { });
define_actions!(ActionClone1 { Arg0 });
define_actions!(ActionClone2 { Arg0, Arg1 });
define_actions!(ActionClone3 { Arg0, Arg1, Arg2 });
define_actions!(ActionClone4 { Arg0, Arg1, Arg2, Arg3 });

pub trait CallMatch {
    fn matches_args(&self, call: &Call) -> bool;
    fn matches(&self, call: &Call) -> bool {
        self.matches_target(call) && self.matches_method(call) && self.matches_args(call)
    }
    fn matches_target(&self, call: &Call) -> bool {
        self.get_mock_id() == call.method_data.mock_id
    }
    fn matches_generic_method(&self, call: &Call) -> bool {
        self.get_mock_type_id() == call.method_data.mock_type_id
            && self.get_method_name() == call.method_data.method_name
    }
    fn matches_method(&self, call: &Call) -> bool {
        self.get_mock_type_id() == call.method_data.mock_type_id
            && self.get_method_name() == call.method_data.method_name
            && self.get_type_param_ids() == &call.method_data.type_param_ids[..]
    }
    fn validate(&self, call: &Call) -> Vec<Result<(), String>>;
    fn get_mock_id(&self) -> usize;
    fn get_mock_type_id(&self) -> usize;
    fn get_method_name(&self) -> &'static str;
    fn get_type_param_ids(&self) -> &[usize];
    fn describe(&self) -> String;
}

pub trait Expectation {
    fn call_match(&self) -> &dyn CallMatch;
    fn is_satisfied(&self) -> bool;
    fn satisfy(&mut self, call: Call, mock_name: &str) -> Box<Satisfy>;
    fn describe(&self) -> String;
}

pub struct ExpectationNever<CM: CallMatch> {
    call_match: CM,
}
impl<CM: CallMatch> Expectation for ExpectationNever<CM> {
    fn call_match(&self) -> &dyn CallMatch {
        &self.call_match
    }
    fn is_satisfied(&self) -> bool {
        true
    }
    fn satisfy(&mut self, _call: Call, mock_name: &str) -> Box<Satisfy> {
        panic!(
            "{}.{} should never be called",
            mock_name,
            self.call_match().get_method_name()
        );
    }
    fn describe(&self) -> String {
        format!("{} should never be called", self.call_match.describe())
    }
}

pub trait Satisfy {
    fn satisfy(self: Box<Self>) -> *mut u8;
}

macro_rules! define_all {
    (
        ($call_match:ident, $reaction:ident,
          $action_clone:ident,
          $satisfy:ident, $satisfy_clone:ident,
          $expectation:ident, $expectation_times:ident)
        { $(($n:tt, $arg:ident, $Arg:ident)),* }
    ) => {
        struct $satisfy<$($Arg,)* Res, F: FnOnce($($Arg,)*) -> Res> {
            action: F,
            $($arg: $Arg,)*
        }

        impl<$($Arg,)* Res, F: FnOnce($($Arg,)*) -> Res> Satisfy for $satisfy<$($Arg,)* Res, F> {
            fn satisfy(self: Box<Self>) -> *mut u8 {
                let result = (self.action)($(self.$arg,)*);
                Box::into_raw(Box::new(result)) as *mut u8
            }
        }

        struct $satisfy_clone<$($Arg,)* Res> {
            action: $action_clone<$($Arg,)* Res>,
            $($arg: $Arg,)*
        }

        impl<$($Arg,)* Res> Satisfy for $satisfy_clone<$($Arg,)* Res> {
            fn satisfy(self: Box<Self>) -> *mut u8 {
                let result = self.action.borrow_mut().deref_mut()($(self.$arg,)*);
                Box::into_raw(Box::new(result)) as *mut u8
            }
        }

        #[must_use]
        pub struct $call_match<$($Arg,)* Res> {
            mock_id: usize,
            mock_type_id: usize,
            method_name: &'static str,
            type_param_ids: Vec<usize>,
            $($arg: Box<dyn MatchArg<$Arg>>,)*
            _phantom: PhantomData<Res>,
        }

        impl<$($Arg,)* Res> $call_match<$($Arg,)* Res> {
            pub fn new(
                mock_id: usize,
                mock_type_id: usize,
                method_name: &'static str,
                type_param_ids: Vec<usize>,
                $($arg: Box<dyn MatchArg<$Arg>>,)*
            ) -> Self {
                $call_match {
                    mock_id: mock_id,
                    mock_type_id: mock_type_id,
                    method_name: method_name,
                    type_param_ids: type_param_ids,
                    $($arg: $arg,)*
                    _phantom: PhantomData,
                }
            }

            fn get_args_ref(call: &Call) -> &($($Arg,)*) {
                unsafe { &mut *(call.args_ptr as *mut ($($Arg,)*)) }
            }

            fn get_args(mut call: Call) -> Box<($($Arg,)*)> {
                unsafe { Box::from_raw(call.take_args() as *mut ($($Arg,)*)) }
            }
        }

        #[must_use]
        pub struct $reaction<$($Arg,)* Res> {
            call_match: $call_match<$($Arg,)* Res>,
            action: $action_clone<$($Arg,)* Res>,
        }
        impl<$($Arg,)* Res> $reaction<$($Arg,)* Res> {
            pub fn times<C: Cardinality + 'static>(self, cardinality: C) -> $expectation_times<$($Arg,)* Res> {
                $expectation_times::new(self.call_match, self.action, Box::new(cardinality))
            }
        }

        #[must_use]
        pub struct $expectation_times<$($Arg,)* Res> {
            action: $action_clone<$($Arg,)* Res>,
            call_match: $call_match<$($Arg,)* Res>,
            cardinality: Box<dyn Cardinality>,
            count: u32,
        }

        impl<$($Arg,)* Res> $expectation_times<$($Arg,)* Res> {
            fn new(
                call_match: $call_match<$($Arg,)* Res>,
                action: $action_clone<$($Arg,)* Res>,
                cardinality: Box<dyn Cardinality>,
            ) -> Self {
                $expectation_times {
                    call_match: call_match,
                    action: action,
                    cardinality: cardinality,
                    count: 0,
                }
            }
        }

        impl<$($Arg: 'static,)* Res: 'static> Expectation for $expectation_times<$($Arg,)* Res> {
            fn call_match(&self) -> &dyn CallMatch {
                &self.call_match
            }
            fn is_satisfied(&self) -> bool {
                self.cardinality.check(self.count) == CardinalityCheckResult::Satisfied
            }
            fn satisfy(&mut self, call: Call, mock_name: &str) -> Box<Satisfy> {
                self.count += 1;
                if self.cardinality.check(self.count) == CardinalityCheckResult::Wrong {
                    panic!(
                        "{}.{} is called for the {} time, but expected to be {}",
                        mock_name,
                        self.call_match().get_method_name(),
                        format_ordinal(self.count),
                        self.cardinality.describe_upper_bound()
                    );
                }
                let ($($arg,)*) = *$call_match::<$($Arg,)* Res>::get_args(call);
                let action = self.action.clone();
                Box::new(
                    $satisfy_clone {
                        action,
                        $($arg,)*
                    }
                )
            }
            fn describe(&self) -> String {
                format!(
                    "{} must be {}, called {} times",
                    self.call_match.describe(),
                    self.cardinality.describe(),
                    self.count
                )
            }
        }

        #[must_use]
        pub struct $expectation<$($Arg,)* Res, F: FnOnce($($Arg,)*) -> Res> {
            call_match: $call_match<$($Arg,)* Res>,
            action: Option<F>,
        }

        impl<$($Arg: 'static,)* Res: 'static, F: FnOnce($($Arg,)*) -> Res + 'static> Expectation
        for $expectation<$($Arg,)* Res, F> {
            fn call_match(&self) -> &dyn CallMatch {
                &self.call_match
            }
            fn is_satisfied(&self) -> bool {
                self.action.is_none()
            }
            fn satisfy(&mut self, call: Call, mock_name: &str) -> Box<Satisfy> {
                match self.action.take() {
                    Some(action) => {
                        let ($($arg,)*) = *$call_match::<$($Arg,)* Res>::get_args(call);
                        Box::new(
                            $satisfy {
                                action,
                                $($arg,)*
                            }
                        )
                    }
                    None => {
                        panic!(
                            "{}.{} was already called earlier",
                            mock_name,
                            self.call_match().get_method_name()
                        );
                    }
                }
            }
            fn describe(&self) -> String {
                self.call_match.describe()
            }
        }

        impl<$($Arg: 'static,)* Res: 'static> $call_match<$($Arg,)* Res> {
            pub fn and_return(self, result: Res)
            -> $expectation<$($Arg,)* Res, impl FnOnce($($Arg,)*) -> Res> {
                #[allow(unused_variables)]
                $expectation {
                    call_match: self,
                    action: Some(move |$($arg,)*| result),
                }
            }

            pub fn and_panic(self, msg: String)
            -> $expectation<$($Arg,)* Res, impl FnOnce($($Arg,)*) -> Res> {
                #[allow(unused_variables)]
                $expectation {
                    call_match: self,
                    action: Some(move |$($arg,)*| panic!(msg)),
                }
            }

            pub fn and_call<F>(self, func: F)
            -> $expectation<$($Arg,)* Res, impl FnOnce($($Arg,)*) -> Res>
            where
                F: FnOnce($($Arg,)*) -> Res + 'static,
            {
                $expectation {
                    call_match: self,
                    action: Some(func),
                }
            }

            pub fn never(self) -> ExpectationNever<Self> {
                ExpectationNever { call_match: self }
            }
        }

        impl<$($Arg,)* Res: Clone + 'static> $call_match<$($Arg,)* Res> {
            pub fn and_return_clone(self, result: Res) -> $reaction<$($Arg,)* Res> {
                #[allow(unused_variables)]
                $reaction {
                    call_match: self,
                    action: Rc::new(RefCell::new(move |$($arg,)*| result.clone())),
                }
            }
        }

        impl<$($Arg,)* Res> $call_match<$($Arg,)* Res> {
            pub fn and_call_clone<F>(self, func: F) -> $reaction<$($Arg,)* Res>
            where
                F: FnMut($($Arg,)*) -> Res + 'static,
            {
                $reaction {
                    call_match: self,
                    action: Rc::new(RefCell::new(func)),
                }
            }
        }

        impl<$($Arg,)* Res: Default + 'static> $call_match<$($Arg,)* Res> {
            pub fn and_return_default(self) -> $reaction<$($Arg,)* Res> {
                #[allow(unused_variables)]
                $reaction {
                    call_match: self,
                    action: Rc::new(RefCell::new(|$($arg,)*| Res::default())),
                }
            }
        }

        impl<$($Arg,)* Res> CallMatch for $call_match<$($Arg,)* Res> {
            fn matches_args(&self, call: &Call) -> bool {
                assert!(
                    call.method_data.mock_type_id == self.mock_type_id
                        && call.method_data.method_name == self.method_name
                        && call.method_data.type_param_ids == self.type_param_ids
                );

                let __args = Self::get_args_ref(call);

                true $(&& self.$arg.matches(&__args.$n).is_ok())*
            }
            fn validate(&self, call: &Call) -> Vec<Result<(), String>> {
                let __args = Self::get_args_ref(call);
                vec![$(self.$arg.matches(&__args.$n)),*]
            }
            fn get_mock_id(&self) -> usize {
                self.mock_id
            }
            fn get_mock_type_id(&self) -> usize {
                self.mock_type_id
            }
            fn get_method_name(&self) -> &'static str {
                self.method_name
            }
            fn get_type_param_ids(&self) -> &[usize] {
                &self.type_param_ids
            }
            fn describe(&self) -> String {
                let args: &[&std::fmt::Display] = &[$(&self.$arg.describe()),*];
                format!("{}({})", self.get_method_name(), args.iter().format(", "))
            }
        }
    }
}

define_all!((CallMatch0, Reaction0, ActionClone0, Satisfy0, SatisfyClone0, Expectation0, ExpectationTimes0) {
});
define_all!((CallMatch1, Reaction1, ActionClone1, Satisfy1, SatisfyClone1, Expectation1, ExpectationTimes1) {
    (0, arg0, Arg0)
});
define_all!((CallMatch2, Reaction2, ActionClone2, Satisfy2, SatisfyClone2, Expectation2, ExpectationTimes2) {
    (0, arg0, Arg0), (1, arg1, Arg1)
});
define_all!((CallMatch3, Reaction3, ActionClone3, Satisfy3, SatisfyClone3, Expectation3, ExpectationTimes3) {
    (0, arg0, Arg0), (1, arg1, Arg1), (2, arg2, Arg2)
});
define_all!((CallMatch4, Reaction4, ActionClone4, Satisfy4, SatisfyClone4, Expectation4, ExpectationTimes4) {
    (0, arg0, Arg0), (1, arg1, Arg1), (2, arg2, Arg2), (3, arg3, Arg3)
});

/// Argument matcher
///
/// Basically it is predicate telling whether argument
/// value satisfies to some criteria. However, in case
/// of mismatch it explains what and why doesn't match.
pub trait MatchArg<T> {
    fn matches(&self, arg: &T) -> Result<(), String>;
    fn describe(&self) -> String;
}

/// Matches argument with value of same type using equality.
impl<T: Eq + std::fmt::Debug> MatchArg<T> for T {
    fn matches(&self, arg: &T) -> Result<(), String> {
        if self == arg {
            Ok(())
        } else {
            Err(format!("{:?} is not equal to {:?}", arg, self))
        }
    }

    fn describe(&self) -> String {
        format!("{:?}", self)
    }
}

#[derive(Default)]
pub struct Sequence {
    expectations: Vec<Box<dyn Expectation>>,
}
impl Sequence {
    pub fn new() -> Self {
        Sequence {
            expectations: Vec::new(),
        }
    }

    pub fn expect<E: Expectation + 'static>(&mut self, expectation: E) {
        assert!(!expectation.is_satisfied());
        self.expectations.push(Box::new(expectation));
    }
}
impl Expectation for Sequence {
    fn call_match(&self) -> &dyn CallMatch {
        self.expectations[0].call_match()
    }
    fn is_satisfied(&self) -> bool {
        self.expectations.is_empty()
    }
    fn satisfy(&mut self, call: Call, mock_name: &str) -> Box<Satisfy> {
        let (res, remove) = {
            let exp = &mut self.expectations[0];
            let res = exp.satisfy(call, mock_name);
            (res, exp.is_satisfied())
        };

        if remove {
            self.expectations.remove(0);
        }

        res
    }
    fn describe(&self) -> String {
        self.expectations[0].describe()
    }
}

pub trait Mock {
    fn new(id: usize, scenario_int: Rc<RefCell<ScenarioInternals>>) -> Self;
    fn mocked_class_name() -> &'static str;
}

pub trait Mocked {
    type MockImpl: Mock;
}

pub struct ScenarioInternals {
    expectations: Vec<Box<dyn Expectation>>,

    next_mock_id: usize,

    /// Mapping from mock ID to mock name.
    mock_names: HashMap<usize, Rc<String>>,
    /// Set of used mock names used to quicly check for conflicts.
    allocated_names: HashSet<Rc<String>>,
}

impl ScenarioInternals {
    fn get_next_mock_id(&mut self) -> usize {
        let id = self.next_mock_id;
        self.next_mock_id += 1;
        id
    }

    pub fn create_mock<T: Mock>(int: &Rc<RefCell<Self>>) -> T {
        let mut internals = int.borrow_mut();
        let mock_id = internals.get_next_mock_id();
        internals.generate_name_for_class(mock_id, T::mocked_class_name());
        T::new(mock_id, int.clone())
    }

    pub fn create_mock_with_id<T: Mock>(int: &Rc<RefCell<Self>>, mock_id: usize) -> T {
        T::new(mock_id, int.clone())
    }

    pub fn create_named_mock<T: Mock>(int: &Rc<RefCell<Self>>, name: String) -> T {
        let mut internals = int.borrow_mut();
        let mock_id = internals.get_next_mock_id();
        internals.register_name(mock_id, name);
        T::new(mock_id, int.clone())
    }

    pub fn create_mock_for<T: ?Sized>(int: &Rc<RefCell<Self>>) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        Self::create_mock::<<&'static T as Mocked>::MockImpl>(int)
    }

    pub fn create_named_mock_for<T: ?Sized>(
        int: &Rc<RefCell<Self>>,
        name: String,
    ) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        Self::create_named_mock::<<&'static T as Mocked>::MockImpl>(int, name)
    }

    pub fn generate_name_for_class(&mut self, mock_id: usize, class_name: &str) {
        for i in 0.. {
            let name = format!("{}#{}", class_name, i);
            if !self.allocated_names.contains(&name) {
                let name_rc = Rc::new(name);
                self.mock_names.insert(mock_id, name_rc.clone());
                self.allocated_names.insert(name_rc);
                break;
            }
        }
    }

    fn register_name(&mut self, mock_id: usize, name: String) {
        if self.allocated_names.contains(&name) {
            panic!("Mock name {} already used", name);
        }
        let name_rc = Rc::new(name);
        self.mock_names.insert(mock_id, name_rc.clone());
        self.allocated_names.insert(name_rc);
    }
}

pub struct Scenario {
    internals: Rc<RefCell<ScenarioInternals>>,
}

impl Scenario {
    pub fn new() -> Self {
        Scenario {
            internals: Rc::new(RefCell::new(ScenarioInternals {
                expectations: Vec::new(),
                next_mock_id: 0,

                mock_names: HashMap::new(),
                allocated_names: HashSet::new(),
            })),
        }
    }

    pub fn create_mock<T: Mock>(&self) -> T {
        ScenarioInternals::create_mock::<T>(&self.internals)
    }

    pub fn create_named_mock<T: Mock>(&self, name: String) -> T {
        ScenarioInternals::create_named_mock::<T>(&self.internals, name)
    }

    pub fn create_mock_for<T: ?Sized>(&self) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        ScenarioInternals::create_mock_for::<T>(&self.internals)
    }

    pub fn create_named_mock_for<T: ?Sized>(&self, name: String) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        ScenarioInternals::create_named_mock_for::<T>(&self.internals, name)
    }

    pub fn expect<C: Expectation + 'static>(&self, call: C) {
        self.internals
            .borrow_mut()
            .expectations
            .push(Box::new(call));
    }

    pub fn checkpoint(&self) {
        self.verify_expectations();
        self.internals.borrow_mut().expectations.clear();
    }

    pub fn handle(&self) -> ScenarioHandle {
        ScenarioHandle::new(Rc::downgrade(&self.internals))
    }

    fn verify_expectations(&self) {
        let int = self.internals.borrow();
        let expectations = &int.expectations;
        let mock_names = &int.mock_names;
        let mut active_expectations = expectations.iter().filter(|e| !e.is_satisfied()).peekable();
        if active_expectations.peek().is_some() {
            let mut s = String::from("Some expectations are not satisfied:\n");
            for expectation in active_expectations {
                let mock_name = mock_names
                    .get(&expectation.call_match().get_mock_id())
                    .unwrap();
                s.push_str(&format!("`{}.{}`\n", mock_name, expectation.describe()));
            }
            panic!(s);
        }
    }
}

impl Default for Scenario {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for Scenario {
    fn drop(&mut self) {
        // Test is already failed, so it isn't necessary to check remaining
        // expectations. And if we do, then panic-during-drop will cause
        // test to fail with uncomprehensive message like:
        // "(signal: 4, SIGILL: illegal instruction)"
        if std::thread::panicking() {
            return;
        }

        self.verify_expectations();
    }
}

pub struct ScenarioHandle {
    internals: Weak<RefCell<ScenarioInternals>>,
}

impl ScenarioHandle {
    pub fn new(scenario_int: Weak<RefCell<ScenarioInternals>>) -> Self {
        Self {
            internals: scenario_int,
        }
    }

    pub fn create_mock<T: Mock>(&self) -> T {
        ScenarioInternals::create_mock::<T>(&self.get_internals())
    }

    pub fn create_named_mock<T: Mock>(&self, name: String) -> T {
        ScenarioInternals::create_named_mock::<T>(&self.get_internals(), name)
    }

    pub fn create_mock_for<T: ?Sized>(&self) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        ScenarioInternals::create_mock_for::<T>(&self.get_internals())
    }

    pub fn create_named_mock_for<T: ?Sized>(&self, name: String) -> <&'static T as Mocked>::MockImpl
    where
        &'static T: Mocked,
    {
        ScenarioInternals::create_named_mock_for::<T>(&self.get_internals(), name)
    }

    pub fn expect<C: Expectation + 'static>(&self, call: C) {
        let ints = self.get_internals();
        ints.borrow_mut().expectations.push(Box::new(call));
    }

    fn get_internals(&self) -> Rc<RefCell<ScenarioInternals>> {
        self.internals.upgrade().expect("scenario is dead")
    }
}

pub struct Call {
    pub method_data: MethodData,
    pub args_ptr: *const u8,
    pub destroy: fn(*const u8),
    pub format_args: fn(*const u8) -> String,
}
impl Call {
    pub fn take_args(&mut self) -> *const u8 {
        std::mem::replace(&mut self.args_ptr, std::ptr::null())
    }
}
impl Drop for Call {
    fn drop(&mut self) {
        if !self.args_ptr.is_null() {
            (self.destroy)(self.args_ptr);
        }
    }
}

pub struct MethodData {
    /// Unique ID of mock object
    pub mock_id: usize,

    /// Unique ID of mock class
    pub mock_type_id: usize,

    /// Called method name
    pub method_name: &'static str,

    /// Type parameters of generic method
    pub type_param_ids: Vec<usize>,
}

macro_rules! define_verify {
    (
        $verify:ident { $(($n:tt, $arg:ident, $Arg:ident)),* }
    ) => {
        pub fn $verify<$($Arg: DebugOnStable,)* Res>(
            &mut self, method_data: MethodData$(, $arg: $Arg)*
        ) -> impl FnOnce() -> Res {
            let args = Box::new(($($arg,)*));
            let args_ptr: *const u8 = ::std::boxed::Box::into_raw(args) as *const u8;
            fn destroy<$($Arg,)*>(args_to_destroy: *const u8) {
                unsafe { Box::from_raw(args_to_destroy as *mut ($($Arg,)*)) };
            };
            fn format_args<$($Arg: DebugOnStable,)*>(args_ptr: *const u8) -> String {
                let __args_ref: &($($Arg,)*) = unsafe { ::std::mem::transmute(args_ptr) };
                let args_debug: &[&std::fmt::Debug] = &[$(&dbg(&__args_ref.$n)),*];
                format!("{:?}", args_debug.iter().format(", "))
            };
            let call = Call {
                method_data: method_data,
                args_ptr: args_ptr,
                destroy: destroy::<$($Arg,)*>,
                format_args: format_args::<$($Arg,)*>,
            };
            let action = self.verify(call);
            move || {
                let result_ptr: *mut u8 = action.satisfy();
                let result: Box<Res> = unsafe { Box::from_raw(result_ptr as *mut Res) };
                *result
            }
        }
    }
}

impl ScenarioInternals {
    define_verify!(verify0 { });
    define_verify!(verify1 { (0, arg0, Arg0) });
    define_verify!(verify2 { (0, arg0, Arg0), (1, arg1, Arg1) });
    define_verify!(verify3 { (0, arg0, Arg0), (1, arg1, Arg1), (2, arg2, Arg2) });
    define_verify!(verify4 { (0, arg0, Arg0), (1, arg1, Arg1), (2, arg2, Arg2), (3, arg3, Arg3) });

    /// Verify call performed on mock object
    /// Returns closure which returns result upon call.
    /// Closure returned instead of actual result, because expectation may
    /// use user-provided closure as action, and that closure may want to
    /// use scenario object to create mocks or establish expectations, so
    /// we need to release scenario borrow before calling expectation action.
    fn verify(&mut self, call: Call) -> Box<Satisfy> {
        for expectation in self.expectations.iter_mut().rev() {
            if expectation.call_match().matches(&call) {
                let mock_name = self
                    .mock_names
                    .get(&call.method_data.mock_id)
                    .unwrap()
                    .clone();
                return expectation.satisfy(call, &mock_name);
            }
        }

        // No expectations exactly matching call are found. However this may be
        // because of unexpected argument values. So check active expectations
        // with matching target (i.e. mock and method) and validate arguments.
        let mock_name = self.mock_names.get(&call.method_data.mock_id).unwrap();

        let mut msg = String::new();
        msg.write_str("\n\n").unwrap();
        write!(
            &mut msg,
            concat!(
                colored!(red: "error:"),
                " ",
                colored!(bold: "unexpected call to `{}.{}({})`\n\n")
            ),
            mock_name,
            call.method_data.method_name,
            (call.format_args)(call.args_ptr)
        )
        .unwrap();

        if self.expectations.is_empty() {
            msg.push_str("no call are expected");
            panic!(msg);
        }

        let mut target_first_match = true;
        for expectation in self.expectations.iter().rev() {
            if !expectation.is_satisfied() && expectation.call_match().matches_method(&call) {
                if target_first_match {
                    write!(
                        &mut msg,
                        concat!(
                            colored!(green: "note: "),
                            "here are active expectations for {}.{}\n"
                        ),
                        mock_name, call.method_data.method_name
                    )
                    .unwrap();
                    target_first_match = false;
                }

                write!(
                    &mut msg,
                    "\n  expectation `{}.{}`:\n",
                    mock_name,
                    expectation.describe()
                )
                .unwrap();
                for (index, res) in expectation.call_match().validate(&call).iter().enumerate() {
                    match *res {
                        Err(ref err) => write!(
                            &mut msg,
                            concat!("    arg #{}: ", colored!(bold: "{}"), "\n"),
                            index, err
                        )
                        .unwrap(),
                        Ok(()) => (),
                    }
                }
            }
        }

        if target_first_match {
            write!(
                &mut msg,
                concat!(
                    colored!(green: "note: "),
                    "there are no active expectations for {}.{}\n"
                ),
                mock_name, call.method_data.method_name
            )
            .unwrap();
        }

        let mut method_first_match = true;
        for expectation in self.expectations.iter().rev() {
            if !expectation.is_satisfied()
                && !expectation.call_match().matches_target(&call)
                && expectation.call_match().matches_method(&call)
                && expectation.call_match().matches_args(&call)
            {
                if method_first_match {
                    msg.push_str(concat!(
                        colored!(green: "note: "),
                        "there are matching expectations for another mock objects\n"
                    ));
                    method_first_match = false;
                }

                let other_mock_id = &expectation.call_match().get_mock_id();
                let other_mock_name = self.mock_names.get(other_mock_id).unwrap();
                write!(
                    &mut msg,
                    concat!("\n  expectation `", colored!(bold: "{}"), ".{}`\n"),
                    other_mock_name,
                    expectation.describe()
                )
                .unwrap();
            }
        }

        msg.push('\n');
        panic!(msg);
    }

    pub fn get_mock_name(&self, mock_id: usize) -> &str {
        self.mock_names.get(&mock_id).unwrap()
    }
}

fn format_ordinal(n: u32) -> String {
    match n % 10 {
        1 => format!("{}st", n),
        2 => format!("{}nd", n),
        3 => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}