BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / tests / table_dump_range.rs

table_dump_range.rs

table_dump_range.rs 2.6 KiB Download Raw
1use std::{cell::Cell, rc::Rc};
2
3use gpui::{App, Context, IntoElement, TestAppContext, Window, div};
4use gpui_component::table::{Column, TableDelegate, TableState};
5
6struct DumpDelegate {
7 rows: usize,
8 columns: usize,
9 cell_text_calls: Rc<Cell<usize>>,
10}
11
12impl TableDelegate for DumpDelegate {
13 fn columns_count(&self, _: &App) -> usize {
14 self.columns
15 }
16
17 fn rows_count(&self, _: &App) -> usize {
18 self.rows
19 }
20
21 fn column(&self, col_ix: usize, _: &App) -> Column {
22 let name = format!("column-{col_ix}");
23 Column::new(name.clone(), name)
24 }
25
26 fn render_td(
27 &mut self,
28 _: usize,
29 _: usize,
30 _: &mut Window,
31 _: &mut Context<TableState<Self>>,
32 ) -> impl IntoElement {
33 div()
34 }
35
36 fn cell_text(&self, row_ix: usize, col_ix: usize, _: &App) -> String {
37 self.cell_text_calls.set(self.cell_text_calls.get() + 1);
38 format!("row-{row_ix}-column-{col_ix}")
39 }
40}
41
42fn new_table(
43 cx: &mut TestAppContext,
44 rows: usize,
45 columns: usize,
46) -> (
47 gpui::WindowHandle<TableState<DumpDelegate>>,
48 Rc<Cell<usize>>,
49) {
50 cx.skip_drawing();
51 let cell_text_calls = Rc::new(Cell::new(0));
52 let delegate_calls = cell_text_calls.clone();
53 let window = cx.add_window(move |window, cx| {
54 TableState::new(
55 DumpDelegate {
56 rows,
57 columns,
58 cell_text_calls: delegate_calls,
59 },
60 window,
61 cx,
62 )
63 });
64 (window, cell_text_calls)
65}
66
67#[gpui::test]
68fn dump_range_clamps_and_only_materializes_requested_rows(cx: &mut TestAppContext) {
69 let (window, cell_text_calls) = new_table(cx, 100, 2);
70
71 let (headers, rows) = window
72 .update(cx, |table, _, cx| table.dump_range(98..103, cx))
73 .unwrap();
74
75 assert_eq!(headers, ["column-0", "column-1"]);
76 assert_eq!(
77 rows,
78 [
79 ["row-98-column-0", "row-98-column-1"],
80 ["row-99-column-0", "row-99-column-1"],
81 ]
82 );
83 assert_eq!(cell_text_calls.get(), 4);
84}
85
86#[gpui::test]
87fn dump_still_materializes_the_complete_table(cx: &mut TestAppContext) {
88 let (window, cell_text_calls) = new_table(cx, 3, 2);
89
90 let (headers, rows) = window.update(cx, |table, _, cx| table.dump(cx)).unwrap();
91
92 assert_eq!(headers, ["column-0", "column-1"]);
93 assert_eq!(
94 rows,
95 [
96 ["row-0-column-0", "row-0-column-1"],
97 ["row-1-column-0", "row-1-column-1"],
98 ["row-2-column-0", "row-2-column-1"],
99 ]
100 );
101 assert_eq!(cell_text_calls.get(), 6);
102}