Skip to content

improve image asserts#1694

Merged
asolntsev merged 2 commits intomainfrom
refactoring/image-asserts
Oct 10, 2025
Merged

improve image asserts#1694
asolntsev merged 2 commits intomainfrom
refactoring/image-asserts

Conversation

@asolntsev
Copy link
Copy Markdown
Collaborator

in addition to just "data:*base64" prefix, also

  1. check image size
  2. verify that the base64 part is a valid parseable image data
  3. validate XML format in case of SVG images

in addition to just "data:*base64" prefix, also

1. check image size
2. verify that the base64 part is a valid parseable  image data
3. validate XML format in case of SVG images
@asolntsev asolntsev self-assigned this Oct 9, 2025
@asolntsev asolntsev added this to the 2.5.2 milestone Oct 9, 2025
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 9, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (945f8f7) to head (4b764d2).
⚠️ Report is 1 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1694      +/-   ##
============================================
- Coverage     92.36%   92.24%   -0.13%     
+ Complexity     3418     3414       -4     
============================================
  Files           334      334              
  Lines          6760     6758       -2     
  Branches        671      670       -1     
============================================
- Hits           6244     6234      -10     
- Misses          352      356       +4     
- Partials        164      168       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asolntsev asolntsev requested review from bodiam and snuyanzin October 9, 2025 16:44
Comment on lines -5 to +6
import java.awt.Color;
import java.awt.*;
import java.awt.Color;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure why we have one specific import for colour and one for the entire package. Could you do away with the wildcard and use all specific imports?

byte[] bytes = Base64.getDecoder().decode(content);
try {
String xml = new String(bytes);
assertThat(xml).matches("<svg xmlns=\".+\" width=\"256\" height=\"256\">.*");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ends with a closing svg tag?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Or will the assert at 76 catch this if there isn't a closing tag?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

good point, added </svg> (though - yes, 76 line also checks the same thing).

@Test
void tiff() {
assertThat(faker.image().base64TIFF()).startsWith("data:image/tiff;base64,");
assertThatImage(faker.image().base64TIFF()).is(TIFF, 256, 256);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since these are all using 256x256 make a method that just takes the image type as a parameter?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks. Created method is(type) which by default assumes 256x256.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wouldn't it be better to use a custom assert here? Like:

assertThat(image).is(TIFF, 256, 256); 

See https://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, and this is the custom assert :)
Method assertThatImage returns the custom assert.

@asolntsev asolntsev requested a review from kingthorin October 9, 2025 21:37
Copy link
Copy Markdown
Collaborator

@kingthorin kingthorin left a comment

Choose a reason for hiding this comment

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

LGTM!

@asolntsev asolntsev merged commit b3309dd into main Oct 10, 2025
13 checks passed
@asolntsev asolntsev deleted the refactoring/image-asserts branch October 10, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants