Skip to content

Commit

Permalink
Added scroller tests for directly using the inject function!
Browse files Browse the repository at this point in the history
  • Loading branch information
Enoumy committed Oct 15, 2024
1 parent 22ce035 commit 11e6bff
Showing 1 changed file with 286 additions and 1 deletion.
287 changes: 286 additions & 1 deletion lib/capytui/tui/scroller/test/test_capytui_scroller.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ module Result_spec = struct
type incoming =
| Set_dimensions of Dimensions.t
| Set_offset of Offset.t
| Inject of Capytui_scroller.action

let to_image t = t.view

let incoming
{ scroller = _; set_dimensions; set_offset; view = _ }
{ scroller = { inject; view = _; less_keybindings_handler = _ }
; set_dimensions
; set_offset
; view = _
}
(action : incoming)
=
match action with
| Set_dimensions dimensions -> set_dimensions dimensions
| Set_offset offset -> set_offset offset
| Inject action -> inject action
;;
end

Expand Down Expand Up @@ -386,3 +392,282 @@ let%expect_test "Testing the less keybindings" =
└────────────────────────────────────────┘
|}]
;;

let%expect_test "Testing the injection function" =
let handle =
Capytui_test.create_handle_generic
~initial_dimensions:{ height = 10; width = 40 }
~to_image:Result_spec.to_image
~handle_incoming:Result_spec.incoming
component
in
Capytui_test.do_actions
handle
[ Set_dimensions { height = 5; width = 40 }
; Set_offset { left = 0; top = 2 }
];
Handle.recompute_view handle;
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
0
1
2
3
4
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Down. *)
Capytui_test.do_actions handle [ Inject Down ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
1
2
3
4
5
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Up. *)
Capytui_test.do_actions handle [ Inject Up ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
0
1
2
3
4
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions handle [ Inject Up ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
0
1
2
3
4
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Half page down. *)
Capytui_test.do_actions handle [ Inject Down_half_screen ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
2
3
4
5
6
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions handle [ Inject Down_half_screen ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
4
5
6
7
8
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Half page up. *)
Capytui_test.do_actions handle [ Inject Up_half_screen ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
2
3
4
5
6
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions handle [ Inject Up_half_screen ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
0
1
2
3
4
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Bottom. *)
Capytui_test.do_actions handle [ Inject Bottom ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
999
│ │
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Top. *)
Fn.apply_n_times
~n:2
(fun () -> Capytui_test.do_actions handle [ Inject Top ])
();
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
0
1
2
3
4
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Showcasing scrolling behavior! *)
Capytui_test.do_actions
handle
[ Inject (Scroll_to { top = 100; bottom = 100 }) ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
96
97
98
99
100
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions
handle
[ Inject (Scroll_to { top = 100; bottom = 104 }) ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
100
101
102
103
104
│ │
│ │
└────────────────────────────────────────┘
|}];
(* Showing what happens when there is a non-sensical top+bottom... *)
Capytui_test.do_actions
handle
[ Inject (Scroll_to { top = 204; bottom = 200 }) ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
196
197
198
199
200
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions
handle
[ Inject (Scroll_to { top = 100; bottom = 300 }) ];
Handle.show handle;
(* NOTE: When the range is bigger than the viewport we go to the bottom.
Not totally sure if this is ideal in all situations though at least
there is a test showing this... *)
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
296
297
298
299
300
│ │
│ │
└────────────────────────────────────────┘
|}];
Capytui_test.do_actions
handle
[ Inject (Scroll_to { top = 100; bottom = 300 }) ];
Handle.show handle;
[%expect
{|
┌────────────────────────────────────────┐
│ │
│ │
296
297
298
299
300
│ │
│ │
└────────────────────────────────────────┘
|}]
;;

0 comments on commit 11e6bff

Please sign in to comment.