Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
70 lines
2.6 KiB
Rust
70 lines
2.6 KiB
Rust
/// Assert that two buffers are equal by comparing their areas and content.
|
|
///
|
|
/// # Panics
|
|
/// When the buffers differ this method panics and displays the differences similar to
|
|
/// `assert_eq!()`.
|
|
#[deprecated = "use assert_eq!(&actual, &expected)"]
|
|
#[macro_export]
|
|
macro_rules! assert_buffer_eq {
|
|
($actual_expr:expr, $expected_expr:expr) => {
|
|
match (&$actual_expr, &$expected_expr) {
|
|
(actual, expected) => {
|
|
assert!(
|
|
actual.area == expected.area,
|
|
"buffer areas not equal\nexpected: {expected:?}\nactual: {actual:?}",
|
|
);
|
|
let nice_diff = expected
|
|
.diff(actual)
|
|
.into_iter()
|
|
.enumerate()
|
|
.map(|(i, (x, y, cell))| {
|
|
let expected_cell = &expected[(x, y)];
|
|
format!("{i}: at ({x}, {y})\n expected: {expected_cell:?}\n actual: {cell:?}")
|
|
})
|
|
.collect::<Vec<String>>()
|
|
.join("\n");
|
|
assert!(
|
|
nice_diff.is_empty(),
|
|
"buffer contents not equal\nexpected: {expected:?}\nactual: {actual:?}\ndiff:\n{nice_diff}",
|
|
);
|
|
// shouldn't get here, but this guards against future behavior
|
|
// that changes equality but not area or content
|
|
assert_eq!(
|
|
actual, expected,
|
|
"buffers are not equal in an unexpected way. Please open an issue about this."
|
|
);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#[allow(deprecated)]
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::prelude::*;
|
|
|
|
#[test]
|
|
fn assert_buffer_eq_does_not_panic_on_equal_buffers() {
|
|
let buffer = Buffer::empty(Rect::new(0, 0, 5, 1));
|
|
let other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1));
|
|
assert_buffer_eq!(buffer, other_buffer);
|
|
}
|
|
|
|
#[should_panic = "buffer areas not equal"]
|
|
#[test]
|
|
fn assert_buffer_eq_panics_on_unequal_area() {
|
|
let buffer = Buffer::empty(Rect::new(0, 0, 5, 1));
|
|
let other_buffer = Buffer::empty(Rect::new(0, 0, 6, 1));
|
|
assert_buffer_eq!(buffer, other_buffer);
|
|
}
|
|
|
|
#[should_panic = "buffer contents not equal"]
|
|
#[test]
|
|
fn assert_buffer_eq_panics_on_unequal_style() {
|
|
let buffer = Buffer::empty(Rect::new(0, 0, 5, 1));
|
|
let mut other_buffer = Buffer::empty(Rect::new(0, 0, 5, 1));
|
|
other_buffer.set_string(0, 0, " ", Style::default().fg(Color::Red));
|
|
assert_buffer_eq!(buffer, other_buffer);
|
|
}
|
|
}
|