-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_tree.gleam
59 lines (53 loc) · 1.34 KB
/
test_tree.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gleam/dynamic.{type DecodeError, type Dynamic} as dyn
import gleam/list
import gleam/string
import startest/test_case.{type Test, decode_test}
pub type TestTree {
Suite(name: String, suite: List(TestTree))
Test(Test)
}
pub fn all_tests(tree: TestTree) -> List(#(String, Test)) {
collect_all_tests(tree, [], [])
}
fn collect_all_tests(
tree: TestTree,
path: List(String),
acc: List(#(String, Test)),
) -> List(#(String, Test)) {
case tree {
Suite(name, suite) ->
suite
|> list.flat_map(collect_all_tests(_, [name, ..path], acc))
Test(test_case) -> {
let test_path =
[test_case.name, ..path]
|> list.reverse
|> string.join(" ❯ ")
[#(test_path, test_case), ..acc]
}
}
}
@target(erlang)
pub fn decode_test_tree(value: Dynamic) -> Result(TestTree, List(DecodeError)) {
value
|> dyn.any([
dyn.decode2(
Suite,
dyn.element(1, dyn.string),
dyn.element(2, dyn.list(decode_test_tree)),
),
dyn.decode1(Test, dyn.element(1, decode_test)),
])
}
@target(javascript)
pub fn decode_test_tree(value: Dynamic) -> Result(TestTree, List(DecodeError)) {
value
|> dyn.any([
dyn.decode2(
Suite,
dyn.field("name", dyn.string),
dyn.field("suite", dyn.list(decode_test_tree)),
),
dyn.decode1(Test, dyn.field("0", decode_test)),
])
}