11//! IO mock implementations.
22//!
33//! This mock supports the specification and checking of expectations to allow
4- //! automated testing of IO based drivers. Mismatches between expected and
5- //! real IO transactions will cause runtime assertions to assist with locating
4+ //! automated testing of `embedded-io` based drivers. Mismatches between expected
5+ //! and real IO transactions will cause runtime assertions to assist with locating
66//! faults.
77//!
88//! ## Usage
2121//! let mut io = IoMock::new(&expectations);
2222//!
2323//! // Writing
24- //! let ret = io.write(&[10, 20]).unwrap();
25- //! assert_eq!(ret , 2);
24+ //! let bytes_written = io.write(&[10, 20]).unwrap();
25+ //! assert_eq!(bytes_written , 2);
2626//!
2727//! // Reading
2828//! let mut buffer = [0; 2];
29- //! let ret = io.read(&mut buffer).unwrap();
29+ //! let bytes_read = io.read(&mut buffer).unwrap();
3030//! assert_eq!(buffer, [20, 30]);
31- //! assert_eq!(ret , 2);
31+ //! assert_eq!(bytes_read , 2);
3232//!
3333//! // Flushing
3434//! io.flush().unwrap();
3535//!
3636//! // Finalizing expectations
3737//! io.done();
3838//!
39- //! // optional async
39+ //! // Async is supported with the optional `embedded-hal- async` feature.
4040//! #[cfg(feature = "embedded-hal-async")]
4141//! async {
4242//! use embedded_io_async;
4343//! let mut io = IoMock::new(&[IoTransaction::write(vec![10, 20])]);
44- //! let ret = embedded_io_async::Write::write(&mut io, &[10, 20]).await.unwrap();
45- //! assert_eq!(ret , 2);
44+ //! let bytes_written = embedded_io_async::Write::write(&mut io, &[10, 20]).await.unwrap();
45+ //! assert_eq!(bytes_written , 2);
4646//! io.done();
4747//! };
4848//!
@@ -92,10 +92,10 @@ pub struct Transaction {
9292
9393impl Transaction {
9494 /// Create a write transaction
95- pub fn write ( expected : Vec < u8 > ) -> Transaction {
95+ pub fn write ( expected_data : Vec < u8 > ) -> Transaction {
9696 Transaction {
9797 expected_mode : Mode :: Write ,
98- expected_data : expected ,
98+ expected_data,
9999 response : Vec :: new ( ) ,
100100 expected_err : None ,
101101 }
@@ -237,7 +237,7 @@ impl Read for Mock {
237237 panic ! ( "response longer than read buffer for io::read" ) ;
238238 }
239239
240- let len = std :: cmp :: min ( buffer . len ( ) , transaction. response . len ( ) ) ;
240+ let len = transaction. response . len ( ) ;
241241 buffer[ ..len] . copy_from_slice ( & transaction. response [ ..len] ) ;
242242
243243 match transaction. expected_err {
@@ -252,7 +252,7 @@ impl Seek for Mock {
252252 let transaction = self . next ( ) . expect ( "no expectation for io::seek call" ) ;
253253
254254 if let Mode :: Seek ( expected_pos) = transaction. expected_mode {
255- assert_eq ! ( expected_pos, pos, "io::seek unexpected mode " ) ;
255+ assert_eq ! ( expected_pos, pos, "io::seek unexpected pos " ) ;
256256
257257 let ret_offset: u64 = u64:: from_be_bytes ( transaction. response . try_into ( ) . unwrap ( ) ) ;
258258 match transaction. expected_err {
@@ -313,8 +313,7 @@ impl BufRead for Mock {
313313 "io::fill_buf unexpected mode"
314314 ) ;
315315
316- let response_vec = transaction. response ;
317- self . set_mock_data ( Some ( response_vec) ) ;
316+ self . set_mock_data ( Some ( transaction. response ) ) ;
318317
319318 match transaction. expected_err {
320319 Some ( err) => Err ( err) ,
0 commit comments