Skip to content

Refactor motor and encoder modules for unit testing#1

Draft
google-labs-jules[bot] wants to merge 2 commits intomainfrom
refactor-motors-encoders-tests-5729155304193828030
Draft

Refactor motor and encoder modules for unit testing#1
google-labs-jules[bot] wants to merge 2 commits intomainfrom
refactor-motors-encoders-tests-5729155304193828030

Conversation

@google-labs-jules
Copy link
Copy Markdown

This PR refactors src/hal/motors.rs and src/hal/encoders.rs to make them testable.
It introduces generic implementations for Motors and Encoders and defines traits for their hardware dependencies (PwmSlice and EncoderRx).
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

- 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.
@google-labs-jules
Copy link
Copy Markdown
Author

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants