Refactor motor and encoder modules for unit testing#1
Refactor motor and encoder modules for unit testing#1google-labs-jules[bot] wants to merge 2 commits intomainfrom
Conversation
- Refactored `Motors` and `Encoders` structs to be generic (`GenericMotors`, `GenericEncoders`) to allow mocking hardware dependencies. - Introduced `PwmSlice` and `EncoderRx` traits to abstract hardware interactions. - Added mocks and unit tests for both modules. - Preserved backward compatibility using type aliases. - Enabled `std` for testing in `lib.rs` to run tests on host.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
- Refactored `src/hal/motors.rs` and `src/hal/encoders.rs` to use `#[cfg(test)]` to substitute hardware types with mocks during testing. - Introduced `PwmSlice` and `EncoderRx` traits to abstract hardware interactions, implementing them for both real hardware and mocks. - Added comprehensive unit tests for `set_speed` (motors) and `get_counts` (encoders) logic. - Configured `src/lib.rs` to enable `std` during tests (`#[cfg_attr(not(test), no_std)]`) to support `Rc`, `RefCell`, and `VecDeque` in mocks. - Avoided using generic structs for `Motors` and `Encoders` as requested, keeping the public API simple.
This PR refactors
src/hal/motors.rsandsrc/hal/encoders.rsto make them testable.It introduces generic implementations for
MotorsandEncodersand defines traits for their hardware dependencies (PwmSliceandEncoderRx).Mock implementations of these traits are used to write unit tests ensuring the logic is correct without needing actual hardware.
The original struct names are preserved as type aliases to minimize impact on existing code.
Unit tests are verified to pass on the host machine.
PR created automatically by Jules for task 5729155304193828030 started by @gundralaa