Skip to content

Commit a057d08

Browse files
committed
more tests
1 parent 6e0b7bc commit a057d08

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

test/basics.jl

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using Functors: functor
3+
24
struct Foo; x; y; end
35
@functor Foo
46

@@ -77,6 +79,38 @@ end
7779
@test_broken m3p.y.x == 1:3
7880
end
7981

82+
@testset "functor(typeof(x), y) from @functor" begin
83+
nt1, re1 = functor(Foo, (x=1, y=2, z=3))
84+
@test nt1 == (x = 1, y = 2)
85+
@test re1((x = 10, y = 20)) == Foo(10, 20)
86+
re1((y = 22, x = 11)) # gives Foo(22, 11), is that a bug?
87+
88+
nt2, re2 = functor(Foo, (z=33, x=1, y=2))
89+
@test nt2 == (x = 1, y = 2)
90+
@test re2((x = 10, y = 20)) == Foo(10, 20)
91+
92+
@test_throws Exception functor(Foo, (z=33, x=1)) # type NamedTuple has no field y
93+
94+
nt3, re3 = functor(OneChild3, (x=1, y=2, z=3))
95+
@test nt3 == (y = 2,)
96+
@test re3((y = 20,)) == OneChild3(1, 20, 3)
97+
re3(22) # gives OneChild3(1, 22, 3), is that a bug?
98+
end
99+
100+
@testset "functor(typeof(x), y) for Base types" begin
101+
nt11, re11 = functor(NamedTuple{(:x, :y)}, (x=1, y=2, z=3))
102+
@test nt11 == (x = 1, y = 2)
103+
@test re11((x = 10, y = 20)) == (x = 10, y = 20)
104+
re11((y = 22, x = 11))
105+
re11((11, 22)) # passes right through
106+
107+
nt12, re12 = functor(NamedTuple{(:x, :y)}, (z=33, x=1, y=2))
108+
@test nt12 == (x = 1, y = 2)
109+
@test re12((x = 10, y = 20)) == (x = 10, y = 20)
110+
111+
@test_throws Exception functor(NamedTuple{(:x, :y)}, (z=33, x=1))
112+
end
113+
80114
###
81115
### Extras
82116
###
@@ -128,7 +162,7 @@ end
128162
# Mismatched trees should be an error
129163
m2 = (x = [1,2], y = (a = [3,4], b = 5))
130164
n2 = (x = [6,7], y = 8)
131-
@test_throws Exception fmap(firsttuple, m2, n2)
165+
@test_throws Exception fmap(firsttuple, m2, n2) # ERROR: type Int64 has no field a
132166
@test_throws Exception fmap(firsttuple, m2, n2)
133167

134168
# The cache uses IDs from the first argument
@@ -138,6 +172,17 @@ end
138172
@test fmap(+, m3, n3) == (x = [2, 4, 6], y = [5, 7, 9], z = [2, 4, 6])
139173
z3 = fmap(+, m3, n3)
140174
@test z3.x === z3.z
175+
176+
# Pruning of duplicates:
177+
@test fmap(+, m3, n3; prune = nothing) == (x = [2,4,6], y = [5,7,9], z = nothing)
178+
179+
# More than two arguments:
180+
z4 = fmap(+, m3, n3, m3, n3)
181+
@test z4 == fmap(x -> 2x, z3)
182+
@test z4.x === z4.z
183+
184+
@test fmap(+, foo1, m1, n1) isa Foo
185+
@test fmap(.*, m1, foo1, n1) == (x = [4*7, 2*5*8], y = 3*6*9)
141186
end
142187

143188
@testset "old test update.jl" begin

0 commit comments

Comments
 (0)