@@ -52,6 +52,7 @@ pub struct Handle {
52
52
pub struct Builder {
53
53
// Sequence of actions for the Mock to take
54
54
actions : VecDeque < Action > ,
55
+ name : String ,
55
56
}
56
57
57
58
#[ derive( Debug , Clone ) ]
@@ -71,6 +72,7 @@ struct Inner {
71
72
sleep : Option < Pin < Box < Sleep > > > ,
72
73
read_wait : Option < Waker > ,
73
74
rx : UnboundedReceiverStream < Action > ,
75
+ name : String ,
74
76
}
75
77
76
78
impl Builder {
@@ -127,6 +129,12 @@ impl Builder {
127
129
self
128
130
}
129
131
132
+ /// Set name of the mock IO object to include in panic messages and debug output
133
+ pub fn name ( & mut self , name : impl Into < String > ) -> & mut Self {
134
+ self . name = name. into ( ) ;
135
+ self
136
+ }
137
+
130
138
/// Build a `Mock` value according to the defined script.
131
139
pub fn build ( & mut self ) -> Mock {
132
140
let ( mock, _) = self . build_with_handle ( ) ;
@@ -135,7 +143,7 @@ impl Builder {
135
143
136
144
/// Build a `Mock` value paired with a handle
137
145
pub fn build_with_handle ( & mut self ) -> ( Mock , Handle ) {
138
- let ( inner, handle) = Inner :: new ( self . actions . clone ( ) ) ;
146
+ let ( inner, handle) = Inner :: new ( self . actions . clone ( ) , self . name . clone ( ) ) ;
139
147
140
148
let mock = Mock { inner } ;
141
149
@@ -184,7 +192,7 @@ impl Handle {
184
192
}
185
193
186
194
impl Inner {
187
- fn new ( actions : VecDeque < Action > ) -> ( Inner , Handle ) {
195
+ fn new ( actions : VecDeque < Action > , name : String ) -> ( Inner , Handle ) {
188
196
let ( tx, rx) = mpsc:: unbounded_channel ( ) ;
189
197
190
198
let rx = UnboundedReceiverStream :: new ( rx) ;
@@ -195,6 +203,7 @@ impl Inner {
195
203
read_wait : None ,
196
204
rx,
197
205
waiting : None ,
206
+ name,
198
207
} ;
199
208
200
209
let handle = Handle { tx } ;
@@ -256,7 +265,7 @@ impl Inner {
256
265
Action :: Write ( ref mut expect) => {
257
266
let n = cmp:: min ( src. len ( ) , expect. len ( ) ) ;
258
267
259
- assert_eq ! ( & src[ ..n] , & expect[ ..n] ) ;
268
+ assert_eq ! ( & src[ ..n] , & expect[ ..n] , "name={} i={}" , self . name , i ) ;
260
269
261
270
// Drop data that was matched
262
271
expect. drain ( ..n) ;
@@ -418,7 +427,7 @@ impl AsyncWrite for Mock {
418
427
self . inner . actions . push_back ( action) ;
419
428
}
420
429
Poll :: Ready ( None ) => {
421
- panic ! ( "unexpected write" ) ;
430
+ panic ! ( "unexpected write {}" , self . pmsg ( ) ) ;
422
431
}
423
432
}
424
433
}
@@ -429,7 +438,7 @@ impl AsyncWrite for Mock {
429
438
let until = Instant :: now ( ) + rem;
430
439
self . inner . sleep = Some ( Box :: pin ( time:: sleep_until ( until) ) ) ;
431
440
} else {
432
- panic ! ( "unexpected WouldBlock" ) ;
441
+ panic ! ( "unexpected WouldBlock {}" , self . pmsg ( ) ) ;
433
442
}
434
443
}
435
444
Ok ( 0 ) => {
@@ -445,7 +454,7 @@ impl AsyncWrite for Mock {
445
454
continue ;
446
455
}
447
456
None => {
448
- panic ! ( "unexpected write" ) ;
457
+ panic ! ( "unexpected write {}" , self . pmsg ( ) ) ;
449
458
}
450
459
}
451
460
}
@@ -475,8 +484,16 @@ impl Drop for Mock {
475
484
}
476
485
477
486
self . inner . actions . iter ( ) . for_each ( |a| match a {
478
- Action :: Read ( data) => assert ! ( data. is_empty( ) , "There is still data left to read." ) ,
479
- Action :: Write ( data) => assert ! ( data. is_empty( ) , "There is still data left to write." ) ,
487
+ Action :: Read ( data) => assert ! (
488
+ data. is_empty( ) ,
489
+ "There is still data left to read. {}" ,
490
+ self . pmsg( )
491
+ ) ,
492
+ Action :: Write ( data) => assert ! (
493
+ data. is_empty( ) ,
494
+ "There is still data left to write. {}" ,
495
+ self . pmsg( )
496
+ ) ,
480
497
_ => ( ) ,
481
498
} ) ;
482
499
}
@@ -505,6 +522,33 @@ fn is_task_ctx() -> bool {
505
522
506
523
impl fmt:: Debug for Inner {
507
524
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
508
- write ! ( f, "Inner {{...}}" )
525
+ if self . name . is_empty ( ) {
526
+ write ! ( f, "Inner {{...}}" )
527
+ } else {
528
+ write ! ( f, "Inner {{name={}, ...}}" , self . name)
529
+ }
530
+ }
531
+ }
532
+
533
+ struct PanicMsgSnippet < ' a > ( & ' a Inner ) ;
534
+
535
+ impl < ' a > fmt:: Display for PanicMsgSnippet < ' a > {
536
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
537
+ if self . 0 . name . is_empty ( ) {
538
+ write ! ( f, "({} actions remain)" , self . 0 . actions. len( ) )
539
+ } else {
540
+ write ! (
541
+ f,
542
+ "(name {}, {} actions remain)" ,
543
+ self . 0 . name,
544
+ self . 0 . actions. len( )
545
+ )
546
+ }
547
+ }
548
+ }
549
+
550
+ impl Mock {
551
+ fn pmsg ( & self ) -> PanicMsgSnippet < ' _ > {
552
+ PanicMsgSnippet ( & self . inner )
509
553
}
510
554
}
0 commit comments