Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StructHelpers"
uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f"
authors = ["Jan Weidner <jw3126@gmail.com> and contributors"]
version = "0.1.4"
version = "0.1.5"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
11 changes: 10 additions & 1 deletion src/StructHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import ConstructionBase: getproperties, constructorof, setproperties
@inline function structural_eq(o1, o2)
getproperties(o1) == getproperties(o2)
end
@inline function structural_isequal(o1, o2)
isequal(getproperties(o1), getproperties(o2))
end

start_hash(o, h, typesalt::Nothing) = Base.hash(typeof(o), h)
start_hash(o, h, typesalt) = Base.hash(typesalt, h)
Expand Down Expand Up @@ -41,7 +44,8 @@ function def_kwconstructor(T, propertynames)
end

const BATTERIES_DEFAULTS = (
eq = true ,
eq = true,
isequal = true,
hash = true ,
kwconstructor = false,
selfconstructor = true,
Expand All @@ -53,6 +57,7 @@ const BATTERIES_DEFAULTS = (

const BATTERIES_DOCSTRINGS = (
eq = "Define `Base.(==)` structurally.",
isequal = "Define `Base.isequal` structurally.",
hash = "Define `Base.hash` structurally.",
kwconstructor = "Add a keyword constructor.",
selfconstructor = "Add a constructor of the for `T(self::T) = self`",
Expand Down Expand Up @@ -150,6 +155,10 @@ macro batteries(T, kw...)
def = :(Base.:(==)(o1::$T, o2::$T) = $(structural_eq)(o1, o2))
push!(ret.args, def)
end
if nt.isequal
def = :(Base.isequal(o1::$T, o2::$T) = $(structural_isequal)(o1, o2))
push!(ret.args, def)
end
if nt.kwshow
def = :(Base.show(io::IO, o::$T) = $(kwshow)(io, o))
push!(ret.args, def)
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ struct WithSelfCtor; a; end
@batteries NoSelfCtor selfconstructor=false
@batteries WithSelfCtor selfconstructor=true

struct SNoIsEqual; a; end
@batteries SNoIsEqual isequal=false

@testset "@batteries" begin
@test SBatteries(1,2) == SBatteries(1,2)
@test SBatteries(1,[]) == SBatteries(1,[])
@test Skw(1,[]) == Skw(1,[])
@test SNoHash(1,[]) == SNoHash(1,[])
@test SVanilla(1,[]) != SVanilla(1,[])

@test isequal(SBatteries(NaN, 1), SBatteries(NaN, 1))
@test !isequal(SBatteries(1, 1), SBatteries(NaN, 1))
@test !isequal(SBatteries(NaN, 1), SBatteries(NaN, 2))
@test SBatteries(NaN, 1) != SBatteries(NaN, 1)
@test !isequal(SNoIsEqual(NaN), SNoIsEqual(NaN))
@test isequal(SNoIsEqual(1), SNoIsEqual(1))
@test SBatteries(2,1) != SBatteries(1,2)
@test Skw(2,1) != Skw(1,2)
@test SNoHash(2,1) != SNoHash(1,2)
Expand Down