Skip to content

Commit 6b627a3

Browse files
committed
update to embedded-hal-mock = "0.10.0-rc.2"
also ensure that `done()` is called on all mocks (otherwise the tests now fail). this also unearthed 1-2 bugs where the expectations were wrong.
1 parent 565d946 commit 6b627a3

File tree

2 files changed

+76
-42
lines changed

2 files changed

+76
-42
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ embedded-hal = "0.2"
1616
defmt = { version = "0.3", optional = true }
1717

1818
[dev-dependencies]
19-
embedded-hal-mock = "0.9"
19+
embedded-hal-mock = "0.10.0-rc.2"

src/lib.rs

+75-41
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,22 @@ where
7979
///
8080
/// Usage example:
8181
/// ```
82-
/// # use embedded_hal_mock::pin::Mock as PinMock;
83-
/// # use embedded_hal_mock::pin::Transaction as PinTransaction;
82+
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
83+
/// # use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
8484
/// # let motor_a_in1 = PinMock::new([]);
85+
/// # let mut motor_a_in1_ = motor_a_in1.clone();
8586
/// # let motor_a_in2 = PinMock::new([]);
86-
/// # let motor_a_pwm_expectations = [PinTransaction::enable()];
87-
/// # let motor_a_pwm = PinMock::new(&motor_a_pwm_expectations);
87+
/// # let mut motor_a_in2_ = motor_a_in2.clone();
88+
/// # let motor_a_pwm = PinMock::new(&[PinTransaction::enable()]);
89+
/// # let mut motor_a_pwm_ = motor_a_pwm.clone();
8890
/// # let motor_b_in1 = PinMock::new([]);
91+
/// # let mut motor_b_in1_ = motor_b_in1.clone();
8992
/// # let motor_b_in2 = PinMock::new([]);
90-
/// # let motor_b_pwm_expectations = [PinTransaction::enable()];
91-
/// # let motor_b_pwm = PinMock::new(&motor_a_pwm_expectations);
93+
/// # let mut motor_b_in2_ = motor_b_in2.clone();
94+
/// # let motor_b_pwm = PinMock::new(&[PinTransaction::enable()]);
95+
/// # let mut motor_b_pwm_ = motor_b_pwm.clone();
9296
/// # let standby = PinMock::new([]);
97+
/// # let mut standby_ = standby.clone();
9398
/// use tb6612fng::Tb6612fng;
9499
///
95100
/// let controller = Tb6612fng::new(
@@ -101,6 +106,14 @@ where
101106
/// motor_b_pwm,
102107
/// standby,
103108
/// );
109+
///
110+
/// # motor_a_in1_.done();
111+
/// # motor_a_in2_.done();
112+
/// # motor_a_pwm_.done();
113+
/// # motor_b_in1_.done();
114+
/// # motor_b_in2_.done();
115+
/// # motor_b_pwm_.done();
116+
/// # standby_.done();
104117
/// ```
105118
pub fn new(
106119
motor_a_in1: MAIN1,
@@ -156,19 +169,25 @@ where
156169
///
157170
/// Usage example:
158171
/// ```
159-
/// # use embedded_hal_mock::pin::Mock as PinMock;
160-
/// # use embedded_hal_mock::pin::Transaction as PinTransaction;
172+
/// # use embedded_hal_mock::eh0::pin::Mock as PinMock;
173+
/// # use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
161174
/// # let motor_in1 = PinMock::new([]);
175+
/// # let mut motor_in1_ = motor_in1.clone();
162176
/// # let motor_in2 = PinMock::new([]);
163-
/// # let motor_pwm_expectations = [PinTransaction::enable()];
164-
/// # let motor_pwm = PinMock::new(&motor_pwm_expectations);
177+
/// # let mut motor_in2_ = motor_in2.clone();
178+
/// # let motor_pwm = PinMock::new(&[PinTransaction::enable()]);
179+
/// # let mut motor_pwm_ = motor_pwm.clone();
165180
/// use tb6612fng::Motor;
166181
///
167182
/// let motor = Motor::new(
168183
/// motor_in1,
169184
/// motor_in2,
170185
/// motor_pwm,
171186
/// );
187+
///
188+
/// # motor_in1_.done();
189+
/// # motor_in2_.done();
190+
/// # motor_pwm_.done();
172191
/// ```
173192
pub fn new(in1: IN1, in2: IN2, mut pwm: PWM) -> Motor<IN1, IN2, PWM> {
174193
pwm.enable();
@@ -275,9 +294,9 @@ where
275294
#[cfg(test)]
276295
mod tests {
277296
use crate::{DriveCommand, DriveError, Motor};
278-
use embedded_hal_mock::pin::State::{High, Low};
279-
use embedded_hal_mock::pin::Transaction as PinTransaction;
280-
use embedded_hal_mock::pin::{Mock as PinMock, PwmDuty};
297+
use embedded_hal_mock::eh0::pin::State::{High, Low};
298+
use embedded_hal_mock::eh0::pin::Transaction as PinTransaction;
299+
use embedded_hal_mock::eh0::pin::{Mock as PinMock, PwmDuty};
281300

282301
#[test]
283302
fn test_motor_stop() {
@@ -289,16 +308,20 @@ mod tests {
289308
PinTransaction::get_max_duty(max_duty),
290309
PinTransaction::set_duty(0),
291310
];
292-
let motor_in1 = PinMock::new(&motor_in1_expectations);
293-
let motor_in2 = PinMock::new(&motor_in2_expectations);
294-
let motor_pwm = PinMock::new(&motor_pwm_expectations);
311+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
312+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
313+
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
295314

296-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
315+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
297316

298317
motor.stop();
299318

300319
assert_eq!(*motor.current_drive_command(), DriveCommand::Stop);
301320
assert_eq!(motor.current_speed(), 0);
321+
322+
motor_in1.done();
323+
motor_in2.done();
324+
motor_pwm.done();
302325
}
303326

304327
#[test]
@@ -311,16 +334,20 @@ mod tests {
311334
PinTransaction::get_max_duty(max_duty),
312335
PinTransaction::set_duty(0),
313336
];
314-
let motor_in1 = PinMock::new(&motor_in1_expectations);
315-
let motor_in2 = PinMock::new(&motor_in2_expectations);
316-
let motor_pwm = PinMock::new(&motor_pwm_expectations);
337+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
338+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
339+
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
317340

318-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
341+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
319342

320343
motor.brake();
321344

322345
assert_eq!(*motor.current_drive_command(), DriveCommand::Brake);
323346
assert_eq!(motor.current_speed(), 0);
347+
348+
motor_in1.done();
349+
motor_in2.done();
350+
motor_pwm.done();
324351
}
325352

326353
#[test]
@@ -334,16 +361,20 @@ mod tests {
334361
PinTransaction::get_max_duty(max_duty),
335362
PinTransaction::set_duty(speed as PwmDuty),
336363
];
337-
let motor_in1 = PinMock::new(&motor_in1_expectations);
338-
let motor_in2 = PinMock::new(&motor_in2_expectations);
339-
let motor_pwm = PinMock::new(&motor_pwm_expectations);
364+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
365+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
366+
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
340367

341-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
368+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
342369

343370
motor.drive_forward(speed).expect("speed can be set");
344371

345372
assert_eq!(*motor.current_drive_command(), DriveCommand::Forward(100));
346373
assert_eq!(motor.current_speed(), speed as i8);
374+
375+
motor_in1.done();
376+
motor_in2.done();
377+
motor_pwm.done();
347378
}
348379

349380
#[test]
@@ -357,33 +388,32 @@ mod tests {
357388
PinTransaction::get_max_duty(max_duty),
358389
PinTransaction::set_duty(speed as PwmDuty),
359390
];
360-
let motor_in1 = PinMock::new(&motor_in1_expectations);
361-
let motor_in2 = PinMock::new(&motor_in2_expectations);
362-
let motor_pwm = PinMock::new(&motor_pwm_expectations);
391+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
392+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
393+
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
363394

364-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
395+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
365396

366397
motor.drive_backwards(speed).expect("speed can be set");
367398

368399
assert_eq!(*motor.current_drive_command(), DriveCommand::Backwards(100));
369400
assert_eq!(motor.current_speed(), -(speed as i8));
401+
402+
motor_in1.done();
403+
motor_in2.done();
404+
motor_pwm.done();
370405
}
371406

372407
#[test]
373408
fn test_motor_drive_invalid_speed() {
374-
let max_duty = 100;
375-
let motor_in1_expectations = [PinTransaction::set(Low)];
376-
let motor_in2_expectations = [PinTransaction::set(High)];
377-
let motor_pwm_expectations = [
378-
PinTransaction::enable(),
379-
PinTransaction::get_max_duty(max_duty),
380-
PinTransaction::set_duty(100),
381-
];
382-
let motor_in1 = PinMock::new(&motor_in1_expectations);
383-
let motor_in2 = PinMock::new(&motor_in2_expectations);
384-
let motor_pwm = PinMock::new(&motor_pwm_expectations);
409+
let motor_in1_expectations = [];
410+
let motor_in2_expectations = [];
411+
let motor_pwm_expectations = [PinTransaction::enable()];
412+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
413+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
414+
let mut motor_pwm = PinMock::new(&motor_pwm_expectations);
385415

386-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
416+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
387417

388418
let current_drive_command = motor.current_drive_command().clone();
389419
let current_speed = motor.current_speed();
@@ -398,5 +428,9 @@ mod tests {
398428
// this should still be what was set before the invalid command
399429
assert_eq!(*motor.current_drive_command(), current_drive_command);
400430
assert_eq!(motor.current_speed(), current_speed);
431+
432+
motor_in1.done();
433+
motor_in2.done();
434+
motor_pwm.done();
401435
}
402436
}

0 commit comments

Comments
 (0)