Skip to content

Add truth extensions for Measure, and add isNotWithin#151

Merged
cuttestkittensrule merged 21 commits into
mainfrom
kyle/truth-units-assert
Jun 11, 2026
Merged

Add truth extensions for Measure, and add isNotWithin#151
cuttestkittensrule merged 21 commits into
mainfrom
kyle/truth-units-assert

Conversation

@cuttestkittensrule

Copy link
Copy Markdown
Collaborator
  • Add MeasureSubject
    • Operates on any WPILib Measure class, and can check if a value is what is expected, within a given degree of tolerance. This degree of tolerance is supplied as a Measure so it is clear what unit it is in.
    • When printing error messages, it shows the values in the units it was created in. This does mean that the different prints can all be different types.
  • Add isNotWithin
    • Uses DoubleSubject's isNotWithin to assert that a value is not within a given tolerance of an expected value.
    • For non-measure subjects, they still exhibit the same behavior, interpreting the tolerance as the default unit (meters for distance, and radians for rotation).

@kcooney kcooney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, but would be better with tests.

I sent out #152 which adds tests to the existing subjects

@kcooney

kcooney commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

I started adding tests for Pose2dSubject.isNotWithin() and, unfortunately, it doesn't work.

For example:

      @Override
      public void of(Pose2d expected) {
        translation().isNotWithin(tolerance).of(expected.getTranslation());
        rotation().isNotWithin(tolerance).of(expected.getRotation());
      }

If both the rotation and the translation are not within tolerance than this will not throw, but if only one of them is not within tolerance it will throw.

@kcooney kcooney force-pushed the kyle/truth-units-assert branch from db80c86 to 787d19c Compare March 28, 2026 18:07
@kcooney

kcooney commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

@cuttestkittensrule I removed the broken isNotWithin() methods and added some tests for MeasureSubject.

Currently all of the tests use expected and actual values that have the same units. We should add tests for different units (ex: volts vs minivolts).

…0 in the tolerence

Should just use the helper method once #156 gets merged into main fully
@cuttestkittensrule

cuttestkittensrule commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

@kcooney Thanks for adding tests! I removed some uneccesary absolute value calculations, and added another edge case to the tolerance check. I'll mege in a bit (assuming the tests pass) (though I might add more tests first)

@cuttestkittensrule

Copy link
Copy Markdown
Collaborator Author

Looks like there was a bug in both isWithin and isNotWithin... It is now fixed; isWithin is now more accurate than isNear in the WPILib Measure class :)

Originally, the tolerance was just converted to the base unit, and that
is used as the tolerance. This is usually accurate, but it isn't for
units that have an offset, like temperature. So, the tolerance is now
the given tolerance (in the base unit) minus 0 units of the tolerance's
unit in the base unit. This makes it work as expected for units with an
offset (like Fahrenheit and Celsius).
@cuttestkittensrule cuttestkittensrule force-pushed the kyle/truth-units-assert branch from 7d2e6cb to 1679846 Compare June 10, 2026 19:28
cuttestkittensrule and others added 8 commits June 10, 2026 17:17
Now there are tests for both units with offsets, and those without offsets
This will make it easier to copy new Javadoc to the website for future
releases.
**New Builder Methods**
  - Add `withPoseFilter()`
    - Poses that are not within the field are filtered out
  - Add `withMultiTagFallbackStrategy()`

**Logging Support**
- Add an overload for `processAllUnreadResults()` that allows the caller
  to be notified of rejected poses
- Use `instanceof` pattern matching
- Make classes static that can be static
- Fix Javadoc errors
@cuttestkittensrule cuttestkittensrule force-pushed the kyle/truth-units-assert branch from b181988 to 1c5dde3 Compare June 11, 2026 00:54
@cuttestkittensrule cuttestkittensrule merged commit c47f353 into main Jun 11, 2026
1 check passed
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.

2 participants