-
Notifications
You must be signed in to change notification settings - Fork 258
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
test(buffer): add assert_buffer_eq!
and Debug implementation
#161
Conversation
@a-kenji would love to hear your thoughts on this approach? |
It seems that you can't opt in to the I tried the following:
Apart from that I like the improvements to the macro. |
Good point - I wasn't sure about that - perhaps it might be best to just implement a nicer Debug implementation that's useful rather than cell by cell? |
Updated this so that there is a useful implementation of
|
I think this is ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - this is great.
Not blocking, but it would be nice to see an #[should_panic]
test too, to make sure the macro fails in expected ways
Done |
- The implementation of Debug is customized to make it easy to use the output (particularly the content) directly when writing tests (by surrounding it with `Buffer::with_lines(vec![])`). The styles part of the message shows the position of every style change, rather than the style of each cell, which reduces the verbosity of the detail, while still showing everything necessary to debug the buffer. ```rust Buffer { area: Rect { x: 0, y: 0, width: 12, height: 2 }, content: [ "Hello World!", "G'day World!", ], styles: [ x: 0, y: 0, fg: Reset, bg: Reset, modifier: (empty), x: 0, y: 1, fg: Green, bg: Yellow, modifier: BOLD, ] } ``` - The assert_buffer_eq! macro shows debug view and diff of the two buffers, which makes it easy to understand exactly where the difference is. - Also adds a unit test for buffer_set_string_multi_width_overwrite which was missing from the buffer tests
So the advantage of using this over manual |
The advantage is in the display when this fails.
When you're writing a test, being able to write a unit test that expects junk "xxxxxx" see that it fails, and then copy the value that the actual call spits out directly into your test makes writing tests super fast (assumes that you check that the actual result is what you intended as part of the testing process). In particularly The Debug format does most of the heavy lifting on that part, but initially this was just the assert_buffer_eq! macro. The macro adds the diff, which makes it super obvious where the difference is in cells (again, useful when writing tests for more complex widgets where you have small difficult to notice differences across a large area. Case in point is #167 which adds a tonne of characterization tests that fully cover the list implementation. Easy with this method, and without it, it would have been much more painful to write easily. Also see #159 (comment):
Having used this a little bit, I think it's missing an explanation parameter like assert_eq! has (e.g. |
assert_buffer_eq!
and Debug implementation
This makes it easier to write unit tests with fast feedback against buffers. The implementation is copied from TestBackend (with some refactoring). Also adds a unit test for buffer_set_string_multi_width_overwrite which was missing from the buffer tests
Output (from some intentionally failing tests):