Terminal styling and layout widgets
0

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-console / test / test_spinner.ml
2.3 kB 60 lines
1(*--------------------------------------------------------------------------- 2 Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. 3 SPDX-License-Identifier: ISC 4 ---------------------------------------------------------------------------*) 5 6open Console 7 8let frames = [| "a"; "b"; "c" |] 9 10let test_v_empty () = 11 Alcotest.check_raises "empty frames rejected" 12 (Invalid_argument "Console.Spinner.v: no frames") (fun () -> 13 ignore (Spinner.v [||])) 14 15let test_frames_roundtrip () = 16 Alcotest.(check (array string)) 17 "frames returns what v was given" frames 18 (Spinner.frames (Spinner.v frames)) 19 20(* [frame] indexes into the cycle, wrapping in both directions. *) 21let test_frame_cycles () = 22 let s = Spinner.v frames in 23 let c = Alcotest.(check string) in 24 c "0" "a" (Spinner.frame s 0); 25 c "1" "b" (Spinner.frame s 1); 26 c "2" "c" (Spinner.frame s 2); 27 c "wraps at 3" "a" (Spinner.frame s 3); 28 c "wraps at 7" "b" (Spinner.frame s 7); 29 c "negative -1" "c" (Spinner.frame s (-1)); 30 c "negative -3" "a" (Spinner.frame s (-3)) 31 32(* [at] picks the frame for [floor (elapsed *. fps)]; fps 2 keeps the products 33 exact so the boundaries are unambiguous. *) 34let test_at_advances_with_fps () = 35 let s = Spinner.v frames in 36 let c = Alcotest.(check string) in 37 c "elapsed 0" "a" (Spinner.at ~fps:2. s ~elapsed:0.); 38 c "negative elapsed shows first" "a" (Spinner.at ~fps:2. s ~elapsed:(-1.)); 39 c "0.5s -> 1" "b" (Spinner.at ~fps:2. s ~elapsed:0.5); 40 c "1.0s -> 2" "c" (Spinner.at ~fps:2. s ~elapsed:1.0); 41 c "1.5s -> 3 wraps" "a" (Spinner.at ~fps:2. s ~elapsed:1.5); 42 c "default fps at 0" "a" (Spinner.at s ~elapsed:0.) 43 44let test_presets () = 45 Alcotest.(check int) 46 "braille has ten frames" 10 47 (Array.length (Spinner.frames Spinner.braille)); 48 Alcotest.(check int) 49 "ascii has four frames" 4 50 (Array.length (Spinner.frames Spinner.ascii)) 51 52let suite = 53 ( "spinner", 54 [ 55 Alcotest.test_case "v rejects empty" `Quick test_v_empty; 56 Alcotest.test_case "frames roundtrip" `Quick test_frames_roundtrip; 57 Alcotest.test_case "frame cycles" `Quick test_frame_cycles; 58 Alcotest.test_case "at advances with fps" `Quick test_at_advances_with_fps; 59 Alcotest.test_case "presets" `Quick test_presets; 60 ] )