Skip to content

Commit 8d6126e

Browse files
committed
call done on all mocks
this ensures that all expected transactions have been done. this has also uncovered that the expectations in `test_motor_drive_invalid_speed` were wrong, which has now been fixed.
1 parent 36d8703 commit 8d6126e

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

src/lib.rs

+44-32
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,20 @@ mod tests {
346346
PwmTransaction::get_max_duty_cycle(max_duty),
347347
PwmTransaction::set_duty_cycle(0),
348348
];
349-
let motor_in1 = PinMock::new(&motor_in1_expectations);
350-
let motor_in2 = PinMock::new(&motor_in2_expectations);
351-
let motor_pwm = PwmMock::new(&motor_pwm_expectations);
349+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
350+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
351+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
352352

353-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
353+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
354354

355355
motor.stop();
356356

357357
assert_eq!(*motor.current_drive_command(), DriveCommand::Stop);
358358
assert_eq!(motor.current_speed(), 0);
359+
360+
motor_in1.done();
361+
motor_in2.done();
362+
motor_pwm.done();
359363
}
360364

361365
#[test]
@@ -374,16 +378,20 @@ mod tests {
374378
PwmTransaction::get_max_duty_cycle(max_duty),
375379
PwmTransaction::set_duty_cycle(0),
376380
];
377-
let motor_in1 = PinMock::new(&motor_in1_expectations);
378-
let motor_in2 = PinMock::new(&motor_in2_expectations);
379-
let motor_pwm = PwmMock::new(&motor_pwm_expectations);
381+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
382+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
383+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
380384

381-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
385+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
382386

383387
motor.brake();
384388

385389
assert_eq!(*motor.current_drive_command(), DriveCommand::Brake);
386390
assert_eq!(motor.current_speed(), 0);
391+
392+
motor_in1.done();
393+
motor_in2.done();
394+
motor_pwm.done();
387395
}
388396

389397
#[test]
@@ -403,16 +411,20 @@ mod tests {
403411
PwmTransaction::get_max_duty_cycle(max_duty),
404412
PwmTransaction::set_duty_cycle(speed as u16),
405413
];
406-
let motor_in1 = PinMock::new(&motor_in1_expectations);
407-
let motor_in2 = PinMock::new(&motor_in2_expectations);
408-
let motor_pwm = PwmMock::new(&motor_pwm_expectations);
414+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
415+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
416+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
409417

410-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
418+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
411419

412420
motor.drive_forward(speed).expect("speed can be set");
413421

414422
assert_eq!(*motor.current_drive_command(), DriveCommand::Forward(100));
415423
assert_eq!(motor.current_speed(), speed as i8);
424+
425+
motor_in1.done();
426+
motor_in2.done();
427+
motor_pwm.done();
416428
}
417429

418430
#[test]
@@ -432,39 +444,35 @@ mod tests {
432444
PwmTransaction::get_max_duty_cycle(max_duty),
433445
PwmTransaction::set_duty_cycle(speed as u16),
434446
];
435-
let motor_in1 = PinMock::new(&motor_in1_expectations);
436-
let motor_in2 = PinMock::new(&motor_in2_expectations);
437-
let motor_pwm = PwmMock::new(&motor_pwm_expectations);
447+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
448+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
449+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
438450

439-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
451+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
440452

441453
motor.drive_backwards(speed).expect("speed can be set");
442454

443455
assert_eq!(*motor.current_drive_command(), DriveCommand::Backwards(100));
444456
assert_eq!(motor.current_speed(), -(speed as i8));
457+
458+
motor_in1.done();
459+
motor_in2.done();
460+
motor_pwm.done();
445461
}
446462

447463
#[test]
448464
fn test_motor_drive_invalid_speed() {
449-
let max_duty = 100;
450-
let motor_in1_expectations = [PinTransaction::set(Low)];
451-
let motor_in2_expectations = [PinTransaction::set(High)];
465+
let motor_in1_expectations = [];
466+
let motor_in2_expectations = [];
452467
#[cfg(feature = "hal_v02")]
453-
let motor_pwm_expectations = [
454-
PinTransaction::enable(),
455-
PinTransaction::get_max_duty(max_duty),
456-
PinTransaction::set_duty(100),
457-
];
468+
let motor_pwm_expectations = [PinTransaction::enable()];
458469
#[cfg(feature = "hal_v1")]
459-
let motor_pwm_expectations = [
460-
PwmTransaction::get_max_duty_cycle(max_duty),
461-
PwmTransaction::set_duty_cycle(100),
462-
];
463-
let motor_in1 = PinMock::new(&motor_in1_expectations);
464-
let motor_in2 = PinMock::new(&motor_in2_expectations);
465-
let motor_pwm = PwmMock::new(&motor_pwm_expectations);
470+
let motor_pwm_expectations = [];
471+
let mut motor_in1 = PinMock::new(&motor_in1_expectations);
472+
let mut motor_in2 = PinMock::new(&motor_in2_expectations);
473+
let mut motor_pwm = PwmMock::new(&motor_pwm_expectations);
466474

467-
let mut motor = Motor::new(motor_in1, motor_in2, motor_pwm);
475+
let mut motor = Motor::new(motor_in1.clone(), motor_in2.clone(), motor_pwm.clone());
468476

469477
let current_drive_command = motor.current_drive_command().clone();
470478
let current_speed = motor.current_speed();
@@ -479,5 +487,9 @@ mod tests {
479487
// this should still be what was set before the invalid command
480488
assert_eq!(*motor.current_drive_command(), current_drive_command);
481489
assert_eq!(motor.current_speed(), current_speed);
490+
491+
motor_in1.done();
492+
motor_in2.done();
493+
motor_pwm.done();
482494
}
483495
}

0 commit comments

Comments
 (0)