Skip to content

Commit 2d14cec

Browse files
committed
more tests
1 parent 6e0b7bc commit 2d14cec

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

test/basics.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ end
7777
@test_broken m3p.y.x == 1:3
7878
end
7979

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

134166
# The cache uses IDs from the first argument
@@ -138,6 +170,17 @@ end
138170
@test fmap(+, m3, n3) == (x = [2, 4, 6], y = [5, 7, 9], z = [2, 4, 6])
139171
z3 = fmap(+, m3, n3)
140172
@test z3.x === z3.z
173+
174+
# Pruning of duplicates:
175+
@test fmap(+, m3, n3; prune = nothing) == (x = [2,4,6], y = [5,7,9], z = nothing)
176+
177+
# More than two arguments:
178+
z4 = fmap(+, m3, n3, m3, n3)
179+
@test z4 == fmap(x -> 2x, z3)
180+
@test z4.x === z4.z
181+
182+
@test fmap(+, foo1, m1, n1) isa Foo
183+
@test fmap(.*, m1, foo1, n1) == (x = [4*7, 2*5*8], y = 3*6*9)
141184
end
142185

143186
@testset "old test update.jl" begin

0 commit comments

Comments
 (0)