script.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use crate::grid::grid_editor::GridEditorTest;
  2. use flowy_grid::entities::{GroupPB, MoveRowParams, RowPB};
  3. pub enum GroupScript {
  4. AssertGroup {
  5. group_index: usize,
  6. row_count: usize,
  7. },
  8. AssertGroupCount(usize),
  9. AssertGroupRow {
  10. group_index: usize,
  11. row_index: usize,
  12. row: RowPB,
  13. },
  14. MoveRow {
  15. from_group_index: usize,
  16. from_row_index: usize,
  17. to_group_index: usize,
  18. to_row_index: usize,
  19. },
  20. }
  21. pub struct GridGroupTest {
  22. inner: GridEditorTest,
  23. }
  24. impl GridGroupTest {
  25. pub async fn new() -> Self {
  26. let editor_test = GridEditorTest::new_board().await;
  27. Self { inner: editor_test }
  28. }
  29. pub async fn run_scripts(&mut self, scripts: Vec<GroupScript>) {
  30. for script in scripts {
  31. self.run_script(script).await;
  32. }
  33. }
  34. pub async fn run_script(&mut self, script: GroupScript) {
  35. match script {
  36. GroupScript::AssertGroup { group_index, row_count } => {
  37. assert_eq!(row_count, self.group_at_index(group_index).await.rows.len());
  38. }
  39. GroupScript::AssertGroupCount(count) => {
  40. let groups = self.editor.load_groups().await.unwrap();
  41. assert_eq!(count, groups.len());
  42. }
  43. GroupScript::MoveRow {
  44. from_group_index,
  45. from_row_index,
  46. to_group_index,
  47. to_row_index,
  48. } => {
  49. let groups: Vec<GroupPB> = self.editor.load_groups().await.unwrap().items;
  50. let from_row = groups.get(from_group_index).unwrap().rows.get(from_row_index).unwrap();
  51. let to_row = groups.get(to_group_index).unwrap().rows.get(to_row_index).unwrap();
  52. let params = MoveRowParams {
  53. view_id: self.inner.grid_id.clone(),
  54. from_row_id: from_row.id.clone(),
  55. to_row_id: to_row.id.clone(),
  56. };
  57. self.editor.move_row(params).await.unwrap();
  58. }
  59. GroupScript::AssertGroupRow {
  60. group_index,
  61. row_index,
  62. row,
  63. } => {
  64. //
  65. let group = self.group_at_index(group_index).await;
  66. let compare_row = group.rows.get(row_index).unwrap().clone();
  67. assert_eq!(row.id, compare_row.id);
  68. }
  69. }
  70. }
  71. pub async fn group_at_index(&self, index: usize) -> GroupPB {
  72. let groups = self.editor.load_groups().await.unwrap().items;
  73. groups.get(index).unwrap().clone()
  74. }
  75. }
  76. impl std::ops::Deref for GridGroupTest {
  77. type Target = GridEditorTest;
  78. fn deref(&self) -> &Self::Target {
  79. &self.inner
  80. }
  81. }
  82. impl std::ops::DerefMut for GridGroupTest {
  83. fn deref_mut(&mut self) -> &mut Self::Target {
  84. &mut self.inner
  85. }
  86. }