2023-04-26 21:02:35 +00:00
|
|
|
#![cfg(feature = "widget-calendar")]
|
|
|
|
use ratatui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
|
|
|
style::Style,
|
|
|
|
widgets::{
|
|
|
|
calendar::{CalendarEventStore, Monthly},
|
|
|
|
Widget,
|
|
|
|
},
|
|
|
|
Terminal,
|
|
|
|
};
|
|
|
|
use time::{Date, Month};
|
|
|
|
|
2024-01-24 18:34:10 +00:00
|
|
|
#[track_caller]
|
2024-05-14 01:13:46 +00:00
|
|
|
fn test_render<W: Widget>(widget: W, width: u16, height: u16, expected: &Buffer) {
|
2024-03-02 09:06:53 +00:00
|
|
|
let backend = TestBackend::new(width, height);
|
2023-04-26 21:02:35 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2024-08-06 03:15:14 +00:00
|
|
|
.draw(|f| f.render_widget(widget, f.area()))
|
2023-04-26 21:02:35 +00:00
|
|
|
.unwrap();
|
2024-03-02 09:06:53 +00:00
|
|
|
terminal.backend().assert_buffer(expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn days_layout() {
|
|
|
|
let c = Monthly::new(
|
|
|
|
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
|
|
|
|
CalendarEventStore::default(),
|
|
|
|
);
|
2024-05-14 01:13:46 +00:00
|
|
|
let expected = Buffer::with_lines([
|
2023-04-26 21:02:35 +00:00
|
|
|
" 1 2 3 4 5 6 7",
|
|
|
|
" 8 9 10 11 12 13 14",
|
|
|
|
" 15 16 17 18 19 20 21",
|
|
|
|
" 22 23 24 25 26 27 28",
|
|
|
|
" 29 30 31",
|
|
|
|
]);
|
2024-05-14 01:13:46 +00:00
|
|
|
test_render(c, 21, 5, &expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn days_layout_show_surrounding() {
|
|
|
|
let c = Monthly::new(
|
|
|
|
Date::from_calendar_date(2023, Month::December, 1).unwrap(),
|
|
|
|
CalendarEventStore::default(),
|
|
|
|
)
|
|
|
|
.show_surrounding(Style::default());
|
2024-05-14 01:13:46 +00:00
|
|
|
let expected = Buffer::with_lines([
|
2023-04-26 21:02:35 +00:00
|
|
|
" 26 27 28 29 30 1 2",
|
|
|
|
" 3 4 5 6 7 8 9",
|
|
|
|
" 10 11 12 13 14 15 16",
|
|
|
|
" 17 18 19 20 21 22 23",
|
|
|
|
" 24 25 26 27 28 29 30",
|
|
|
|
" 31 1 2 3 4 5 6",
|
|
|
|
]);
|
2024-05-14 01:13:46 +00:00
|
|
|
test_render(c, 21, 6, &expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_month_header() {
|
|
|
|
let c = Monthly::new(
|
|
|
|
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
|
|
|
|
CalendarEventStore::default(),
|
|
|
|
)
|
|
|
|
.show_month_header(Style::default());
|
2024-05-14 01:13:46 +00:00
|
|
|
let expected = Buffer::with_lines([
|
2024-01-24 18:34:10 +00:00
|
|
|
" January 2023 ",
|
2023-04-26 21:02:35 +00:00
|
|
|
" 1 2 3 4 5 6 7",
|
|
|
|
" 8 9 10 11 12 13 14",
|
|
|
|
" 15 16 17 18 19 20 21",
|
|
|
|
" 22 23 24 25 26 27 28",
|
|
|
|
" 29 30 31",
|
|
|
|
]);
|
2024-05-14 01:13:46 +00:00
|
|
|
test_render(c, 21, 6, &expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_weekdays_header() {
|
|
|
|
let c = Monthly::new(
|
|
|
|
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
|
|
|
|
CalendarEventStore::default(),
|
|
|
|
)
|
|
|
|
.show_weekdays_header(Style::default());
|
2024-05-14 01:13:46 +00:00
|
|
|
let expected = Buffer::with_lines([
|
2023-04-26 21:02:35 +00:00
|
|
|
" Su Mo Tu We Th Fr Sa",
|
|
|
|
" 1 2 3 4 5 6 7",
|
|
|
|
" 8 9 10 11 12 13 14",
|
|
|
|
" 15 16 17 18 19 20 21",
|
|
|
|
" 22 23 24 25 26 27 28",
|
|
|
|
" 29 30 31",
|
|
|
|
]);
|
2024-05-14 01:13:46 +00:00
|
|
|
test_render(c, 21, 6, &expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_combo() {
|
|
|
|
let c = Monthly::new(
|
|
|
|
Date::from_calendar_date(2023, Month::January, 1).unwrap(),
|
|
|
|
CalendarEventStore::default(),
|
|
|
|
)
|
|
|
|
.show_weekdays_header(Style::default())
|
|
|
|
.show_month_header(Style::default())
|
|
|
|
.show_surrounding(Style::default());
|
2024-05-14 01:13:46 +00:00
|
|
|
let expected = Buffer::with_lines([
|
2024-01-24 18:34:10 +00:00
|
|
|
" January 2023 ",
|
2023-04-26 21:02:35 +00:00
|
|
|
" Su Mo Tu We Th Fr Sa",
|
|
|
|
" 1 2 3 4 5 6 7",
|
|
|
|
" 8 9 10 11 12 13 14",
|
|
|
|
" 15 16 17 18 19 20 21",
|
|
|
|
" 22 23 24 25 26 27 28",
|
|
|
|
" 29 30 31 1 2 3 4",
|
|
|
|
]);
|
2024-05-14 01:13:46 +00:00
|
|
|
test_render(c, 21, 7, &expected);
|
2023-04-26 21:02:35 +00:00
|
|
|
}
|