|
1 | 1 |
|
| 2 | +using Functors: functor |
| 3 | + |
2 | 4 | struct Foo; x; y; end
|
3 | 5 | @functor Foo
|
4 | 6 |
|
|
77 | 79 | @test_broken m3p.y.x == 1:3
|
78 | 80 | end
|
79 | 81 |
|
| 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 | + |
80 | 114 | ###
|
81 | 115 | ### Extras
|
82 | 116 | ###
|
|
128 | 162 | # Mismatched trees should be an error
|
129 | 163 | m2 = (x = [1,2], y = (a = [3,4], b = 5))
|
130 | 164 | n2 = (x = [6,7], y = 8)
|
131 |
| - @test_throws Exception fmap(first∘tuple, m2, n2) |
| 165 | + @test_throws Exception fmap(first∘tuple, m2, n2) # ERROR: type Int64 has no field a |
132 | 166 | @test_throws Exception fmap(first∘tuple, m2, n2)
|
133 | 167 |
|
134 | 168 | # The cache uses IDs from the first argument
|
|
138 | 172 | @test fmap(+, m3, n3) == (x = [2, 4, 6], y = [5, 7, 9], z = [2, 4, 6])
|
139 | 173 | z3 = fmap(+, m3, n3)
|
140 | 174 | @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) |
141 | 186 | end
|
142 | 187 |
|
143 | 188 | @testset "old test update.jl" begin
|
|
0 commit comments