/
test_samples.jl
64 lines (53 loc) · 1.44 KB
/
test_samples.jl
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
60
61
62
63
64
module TestSamples
using GeneratorsX
using Test
using Transducers: Map
@generator noone() = nothing
@generator oneone() = @yield 1
@generator function onetwothree()
@yield 1
@yield 2
@yield 3
end
@generator function iid(xs)
for x in xs
@yield x
end
end
@generator function iflatten(xs)
for y in xs
for x in y
@yield x
end
end
end
raw_testdata = """
noone() == []
oneone() == [1]
onetwothree() == [1, 2, 3]
iid(1:1) == [1]
iid(noone()) == []
iid(oneone()) == [1]
iid(onetwothree()) == [1, 2, 3]
iflatten([[1], [2, 3], [4]]) == [1, 2, 3, 4]
iflatten([[1], (2, 3), 4]) == [1, 2, 3, 4]
"""
args_and_kwargs(args...; kwargs...) = args, (; kwargs...)
# An array of `(label, (f, args, kwargs, comparison, desired))`
testdata = map(split(raw_testdata, "\n", keepempty = false)) do x
comp_ex = Meta.parse(x)
@assert comp_ex.head == :call
@assert length(comp_ex.args) == 3
comparison, ex, desired = comp_ex.args
f = ex.args[1]
ex.args[1] = args_and_kwargs
label = strip(x[1:prevind(x, first(findlast(String(comparison), x)))])
Meta.parse(label) # validation
@eval ($label, ($(Symbol(f)), $ex..., $comparison, $desired))
end
@testset "$label" for (label, (f, args, kwargs, comparison, desired)) in testdata
==′ = comparison
@test collect(f(args...; kwargs...)) ==′ desired
@test collect(Map(identity), f(args...; kwargs...)) ==′ desired
end
end # module