Skip to content

Commit

Permalink
Migrate tests from run to alcotest
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Jun 27, 2023
1 parent 92a2424 commit 4f07d37
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 51 deletions.
13 changes: 5 additions & 8 deletions packages/belt/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@

(subdir
test
(executable
(test
(name test)
(modules test)
(modules :standard)
(flags :standard -w -32-27-34)
(libraries server-reason-react.belt server-reason-react.js))
(rule
(alias runtest)
(deps test.ml)
(action
(run ./test.exe))))
(libraries alcotest fmt server-reason-react.belt server-reason-react.js)
(preprocess
(pps server-reason-react.ppx))))
128 changes: 85 additions & 43 deletions packages/belt/test/test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
let case title fn = Alcotest.test_case title `Quick fn

let assert_string left right =
Alcotest.check Alcotest.string "should be equal" right left

let assert_int left right =
Alcotest.check Alcotest.int "should be equal" right left

let assert_option ty left right =
Alcotest.check (Alcotest.option ty) "should be equal" right left

let assert_array ty left right =
Alcotest.check (Alcotest.array ty) "should be equal" right left

let assert_list ty left right =
Alcotest.check (Alcotest.list ty) "should be equal" right left

type ('a, 'id) eq = ('a, 'id) Belt.Id.eq
type ('a, 'id) hash = ('a, 'id) Belt.Id.hash
type ('a, 'id) id = ('a, 'id) Belt.Id.hashable
Expand All @@ -20,23 +37,32 @@ module Example = struct
end)
end

let tony = Example.foo ~name:"Tony" ~age:27
let _ = print_endline (Example.name tony)
let aa : int Js.undefined array = Belt.Array.makeUninitialized 1
let _ = print_endline @@ "size: " ^ string_of_int (Belt.Array.length aa)
let eq () =
let tony = Example.foo ~name:"Tony" ~age:27 in
print_endline (Example.name tony)

let length () =
let arr : int Js.undefined array = Belt.Array.makeUninitialized 1 in
assert_int 1 (Belt.Array.length arr)

let mapU () =
let array : int Js.undefined array = Belt.Array.makeUninitialized 1 in
let result = Belt.Array.mapU array Js.Undefined.toOption in
assert_array (Alcotest.option Alcotest.int) array result

let _ =
Belt.Array.forEachU aa (fun x ->
match Js.Undefined.toOption x with
| None -> print_endline "YUP"
| Some x -> assert false)
let mapWithIndex () =
let result = Belt.Array.mapWithIndex [| "a"; "b"; "c" |] (fun i _ -> i) in
assert_array Alcotest.int [| 0; 1; 2 |] result

let aa = Belt.Array.mapWithIndex aa (fun i _ -> i)
let aaa = Belt.List.concat [ 1.0; 2.0 ] [ 3.0; 4.0 ]
let concat () =
let result = Belt.List.concat [ 1; 2 ] [ 3; 4 ] in
assert_list Alcotest.int [ 1; 2; 3; 4 ] result

let _ =
Belt.List.forEach aaa (fun x ->
print_endline @@ "Number: " ^ string_of_float x)
let map () =
let result =
Belt.List.map [ 3.0; 4.0 ] (fun x -> "Number: " ^ string_of_float x)
in
assert_list Alcotest.string [ "Number: 3."; "Number: 4." ] result

module TestingMore = struct
include (
Expand All @@ -60,44 +86,60 @@ end

let aaaaa = TestingMore.t ~age2:10 ()

let () =
let ten = Belt.Int.fromString "10" in
match ten with
| Some t -> print_endline (Belt.Int.toString t)
| None -> print_endline "waaaa"

let () =
let keep_1 () =
let (some10 : int option) = Belt.Option.keep (Some 10) (fun x -> x > 5) in
(match some10 with
| Some t -> print_endline (string_of_int t)
| None -> print_endline "error");
assert_option Alcotest.int (Some 10) some10

let keep_2 () =
let (none : int option) = Belt.Option.keep (Some 4) (fun x -> x > 5) in
(match none with
| Some _ -> print_endline "error"
| None -> print_endline "green");
assert_option Alcotest.int None none

let keep_3 () =
let (none : int option) = Belt.Option.keep None (fun x -> x > 5) in
match none with
| Some _ -> print_endline "error"
| None -> print_endline "green"
assert_option Alcotest.int None none

let () =
let fromString () =
let ten = Belt.Int.fromString "10" in
match ten with
| Some t -> print_endline (Belt.Int.toString t)
| None -> print_endline "waaaa"
| Some t -> assert_string "10" (Belt.Int.toString t)
| None -> Alcotest.fail "fromString returned None"

let print_array arr =
print_string "[";
print_string (Belt.Array.getUnsafe arr 0);
Belt.Array.forEach (Belt.Array.sliceToEnd arr 1) (fun x ->
print_string (", " ^ x));
print_string "]\n"
let toString () =
let ten = Belt.Int.toString 10 in
assert_string "10" ten

let () =
print_endline "\nBelt.Array";
let truncateToLengthUnsafe () =
let arr = Belt.Array.makeUninitializedUnsafe 5 "lola" in
print_array arr;
let newa = Belt.Array.truncateToLengthUnsafe arr 3 in
print_array newa
assert_string (Belt.Array.getUnsafe newa 0) "lola"

let makeUninitializedUnsafe () =
let arr = Belt.Array.makeUninitializedUnsafe 5 "lola" in
assert_string (Belt.Array.getUnsafe arr 0) "lola";
assert_string (Belt.Array.getUnsafe arr 1) "lola";
assert_string (Belt.Array.getUnsafe arr 2) "lola";
assert_string (Belt.Array.getUnsafe arr 3) "lola";
assert_string (Belt.Array.getUnsafe arr 4) "lola"

let () =
Alcotest.run "Belt"
[
("Records", [ case "eq" eq ]);
( "Array",
[
case "truncateToLengthUnsafe" truncateToLengthUnsafe;
case "makeUninitializedUnsafe" makeUninitializedUnsafe;
case "length" length;
] );
( "List",
[
case "eq" eq;
case "map" map;
case "mapU" mapU;
case "mapWithIndex" mapWithIndex;
case "concat" concat;
] );
("Int", [ case "fromString" fromString; case "toString" toString ]);
( "Option",
[ case "keep_1" keep_1; case "keep_2" keep_2; case "keep_3" keep_3 ] );
]

0 comments on commit 4f07d37

Please sign in to comment.