Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and test info field styling #1214

Merged
merged 2 commits into from
Nov 16, 2023
Merged

Conversation

spenserblack
Copy link
Collaborator

@spenserblack spenserblack commented Nov 13, 2023

This PR is to illustrate where we might want to switch to snapshot tests. With the change to alignment in #1207, for example, a snapshot test makes it much easier for a review to confirm proper alignment. "assert contains" isn't really an ideal test -- who knows how many leading or trailing characters there are from the test alone?

I'm leaving this as a draft right now because, as you can see, these snapshots look kind of weird. That's because they're testing .value(), which assumes that it will be preceded by a title. Thus, the TODO comment.

Edit: After working on this PR a bit to get more useful snapshot tests, I realized there was an opportunity to refactor a bit. This PR now moves the logic that styles info fields onto the InfoField trait, and then uses styled snapshot tests (you can view the styled output with cat path/to/snapshot) to test and visualize the outputs.

This moves the bulk of the logic for writing styled info into the
`InfoField` trait as a provided set of methods. This makes each type
that implements `InfoField` more testable.
@spenserblack spenserblack marked this pull request as ready for review November 14, 2023 14:52
@spenserblack spenserblack changed the title Convert author tests to snapshot tests Refactor and test info field styling Nov 14, 2023
Copy link
Owner

@o2sh o2sh left a comment

Choose a reason for hiding this comment

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

You're absolutely right; it makes more sense to have the formatting logic of info fields inside info_field trait 😅. Additionally, get_style and format_number could also be moved out of info/mod.rs into info/utils/mod.rs.

Thanks as well for implementing the snapshot tests; as you mentioned, they are better suited for visual outputs. Only downside being the multiple snapshots folders.

@spenserblack
Copy link
Collaborator Author

Only downside being the multiple snapshots folders.

We could wait and see where mitsuhiko/insta#416 goes 🙂

@o2sh o2sh added the chore label Nov 16, 2023
@o2sh
Copy link
Owner

o2sh commented Nov 16, 2023

This is minor IMO, no need to block the PR. We'll reconsider as we add more snapshot tests and await the upstream issue resolution.

@o2sh o2sh merged commit 3186493 into o2sh:main Nov 16, 2023
9 checks passed
@spenserblack spenserblack deleted the snapshot-tests branch November 17, 2023 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants