Skip to content

Commit 752ed1b

Browse files
feat: adds test.for and it.for bindings
These have the test context passed as an argument, and preserves the array's tuple to be passed in as the first argument. This removes the need for the -N suffix bindings and allows use of the non-BuiltIn bindings
1 parent e78beeb commit 752ed1b

2 files changed

Lines changed: 172 additions & 0 deletions

File tree

src/Vitest.res

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,71 @@ module Each: EachType = {
845845
)
846846
}
847847

848+
module type ForType = {
849+
let test: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
850+
let testAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
851+
852+
let it: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
853+
let itAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
854+
}
855+
856+
module For: ForType = {
857+
module Ext = {
858+
type test
859+
type it
860+
861+
@module("vitest") @val
862+
external test: test = "test"
863+
864+
@module("vitest") @val
865+
external it: it = "it"
866+
867+
@send
868+
external testObj: (
869+
~test: test,
870+
~cases: array<'a>,
871+
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
872+
"for"
873+
874+
@send
875+
external testObjAsync: (
876+
~test: test,
877+
~cases: array<'a>,
878+
) => (~name: string, ~f: @uncurry ('a, testCtx) => promise<unit>, ~timeout: Js.undefined<int>) => unit =
879+
"for"
880+
881+
@send
882+
external itObj: (
883+
~it: it,
884+
~cases: array<'a>,
885+
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
886+
"for"
887+
888+
@send
889+
external itObjAsync: (
890+
~it: it,
891+
~cases: array<'a>,
892+
) => (~name: string, ~f: @uncurry ('a, testCtx) => promise<unit>, ~timeout: Js.undefined<int>) => unit =
893+
"for"
894+
}
895+
896+
@inline
897+
let test = (cases, name, ~timeout=?, f) =>
898+
Ext.testObj(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
899+
900+
@inline
901+
let testAsync = (cases, name, ~timeout=?, f) =>
902+
Ext.testObjAsync(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
903+
904+
@inline
905+
let it = (cases, name, ~timeout=?, f) =>
906+
Ext.itObj(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
907+
908+
@inline
909+
let itAsync = (cases, name, ~timeout=?, f) =>
910+
Ext.itObjAsync(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
911+
}
912+
848913
module Todo = {
849914
type todo_describe
850915
type todo_test

tests/for.test.res

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
open Vitest
2+
3+
let sumObj = [{"a": 3, "b": 5, "sum": 8}, {"a": 6, "b": 2, "sum": 8}]
4+
let sum2 = [(1, "1"), (6, "6")]
5+
let sum3 = [(1, 3, "4"), (6, 3, "9")]
6+
let sum4 = [(1, 2, 8, "11"), (6, 3, 8, "17"), (5, 9, 2, "16")]
7+
let sum5 = [(1, 3, 8, 6, "18"), (6, 3, 2, 4, "15"), (5, 9, 2, 3, "19")]
8+
9+
For.test(sumObj, "test: sum $a+$b=$sum", (i, t) => {
10+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
11+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
12+
})
13+
14+
For.test(sum2, "test: %i=%s", ((a, b), t) => {
15+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
16+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
17+
})
18+
19+
For.test(sum3, "test: sum %i+%i=%s", ((a, b, sum), t) => {
20+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
21+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
22+
})
23+
24+
For.test(sum4, "test: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
25+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
26+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
27+
})
28+
29+
For.test(sum5, "test: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
30+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
31+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
32+
})
33+
34+
For.testAsync(sumObj, "testAsync: sum $a+$b=$sum", async (i, t) => {
35+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
36+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
37+
})
38+
39+
For.testAsync(sum2, "testAsync: %i=%s", async ((a, b), t) => {
40+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
41+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
42+
})
43+
44+
For.testAsync(sum3, "testAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
45+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
46+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
47+
})
48+
49+
For.testAsync(sum4, "testAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
50+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
51+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
52+
})
53+
54+
For.testAsync(sum5, "testAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
55+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
56+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
57+
})
58+
59+
For.it(sumObj, "it: sum $a+$b=$sum", (i, t) => {
60+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
61+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
62+
})
63+
64+
For.it(sum2, "it: %i=%s", ((a, b), t) => {
65+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
66+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
67+
})
68+
69+
For.it(sum3, "it: sum %i+%i=%s", ((a, b, sum), t) => {
70+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
71+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
72+
})
73+
74+
For.it(sum4, "it: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
75+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
76+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
77+
})
78+
79+
For.it(sum5, "it: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
80+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
81+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
82+
})
83+
84+
For.itAsync(sumObj, "itAsync: sum $a+$b=$sum", async (i, t) => {
85+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
86+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
87+
})
88+
89+
For.itAsync(sum2, "itAsync: %i=%s", async ((a, b), t) => {
90+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
91+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
92+
})
93+
94+
For.itAsync(sum3, "itAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
95+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
96+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
97+
})
98+
99+
For.itAsync(sum4, "itAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
100+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
101+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
102+
})
103+
104+
For.itAsync(sum5, "itAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
105+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
106+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
107+
})

0 commit comments

Comments
 (0)