Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Dec 7, 2022
1 parent 84f5253 commit 6a2f79c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Functors.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Functors

using ConstructionBase: constructorof
export @functor, @flexiblefunctor, fmap, fmapstructure, fcollect
export @functor, @flexiblefunctor, fmap, fmapstructure, fcollect, @leaf

include("functor.jl")
include("walks.jl")
Expand Down
35 changes: 16 additions & 19 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ struct Bar{T}; x::T; end
struct OneChild3; x; y; z; end
@functor OneChild3 (y,)

struct NoChildren2; x; y; end
@functor NoChildren2 ()
struct NoChild2; x; y; end
@functor NoChild2 ()

struct NoChild{T}; x::T; end
@functor NoChild ()
struct NoChild1{T}; x::T; end
@functor NoChild1 ()

struct LeafType{T}; x::T; end
@leaf LeafType

###
### Basic functionality
###

@testset "Children and Leaves" begin
no_children = NoChildren2(1, 2)
@testset "NoChild is not a leaf" begin
no_children = NoChild2(1, 2)
has_children = Foo(1, 2)
@test Functors.isleaf(no_children)
@test !Functors.isleaf(no_children)
@test !Functors.isleaf(has_children)
@test Functors.children(no_children) === Functors.NoChildren()
@test Functors.children(no_children) === (;)
@test Functors.children(has_children) == (x=1, y=2)
end

Expand Down Expand Up @@ -97,8 +100,8 @@ end
# Leaf types:
@test usecache(d, [1,2])
@test !usecache(d, 4.0)
@test usecache(d, NoChild([1,2]))
@test !usecache(d, NoChild((3,4)))
@test usecache(d, LeafType([1,2]))
@test !usecache(d, LeafType((3,4)))

# Not leaf:
@test usecache(d, Ref(3)) # mutable container
Expand Down Expand Up @@ -181,7 +184,7 @@ end

m1 = [1, 2, 3]
m2 = Bar(m1)
m0 = NoChildren2(:a, :b)
m0 = NoChild2(:a, :b)
m3 = Foo(m2, m0)
m4 = Bar(m3)
@test all(fcollect(m4) .=== [m4, m3, m2, m1, m0])
Expand Down Expand Up @@ -238,9 +241,7 @@ end
@test z4.x === z4.z

@test fmap(+, foo1, m1, n1) isa Foo
@static if VERSION >= v"1.6" # fails on Julia 1.0
@test fmap(.*, m1, foo1, n1) == (x = [4*7, 2*5*8], y = 3*6*9)
end
@test fmap(.*, m1, foo1, n1) == (x = [4*7, 2*5*8], y = 3*6*9)
end

@testset "old test update.jl" begin
Expand Down Expand Up @@ -303,11 +304,7 @@ end
end

@testset "@leaf" begin
struct A; x; end
@functor A
a = A(1)
@test Functors.children(a) === (x = 1,)
Functors.@leaf A
a = LeafType(1)
children, re = Functors.functor(a)
@test children == Functors.NoChildren()
@test re(children) === a
Expand Down
2 changes: 1 addition & 1 deletion test/flexiblefunctors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct FFoo
@test all(fcollect(m4, exclude = x -> x isa Array) .=== [m4, m3])
@test all(fcollect(m4, exclude = x -> x isa FFoo) .=== [m4])

m0 = NoChildren2(:a, :b)
m0 = NoChild2(:a, :b)
m1 = [1, 2, 3]
m2 = FBar(m1, ())
m3 = FFoo(m2, m0, (:x, :y,))
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using StaticArrays
include("base.jl")
include("flexiblefunctors.jl")

# if VERSION < v"1.6" # || VERSION > v"1.7-"
# if VERSION >= v"1.7"
# @warn "skipping doctests, on Julia $VERSION"
# else
# using Documenter
Expand Down

0 comments on commit 6a2f79c

Please sign in to comment.