Skip to content

Commit

Permalink
Consistent Face hashes
Browse files Browse the repository at this point in the history
Since we implement a custom equality function, it seems nice to have it
so that Face objects that are equal hash equally too.

I'm not aware of anybody using this, but it seems nice to have.
  • Loading branch information
tecosaur committed Aug 17, 2024
1 parent f6035eb commit 02cd20b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ Base.:(==)(a::Face, b::Face) =
getfield.(Ref(a), fieldnames(Face)) ==
getfield.(Ref(b), fieldnames(Face))

Base.hash(f::Face, h::UInt) =
mapfoldr(Base.Fix1(getfield, f), hash, fieldnames(Face), init=hash(Face, h))

Base.copy(f::Face) =
Face(f.font, f.height, f.weight, f.slant,
f.foreground, f.background, f.underline,
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ end
@test getface(:f).foreground.value == :blue
StyledStrings.resetfaces!()
end
# Equality/hashing equivalence
let testfaces = [Face(foreground=:blue),
Face(background=:blue),
Face(inherit=:something),
Face(inherit=:something)]
for f1 in testfaces, f2 in testfaces
@test (f1 == f2) == (hash(f1) == hash(f2))
end
end
# Pretty display
@test sprint(show, MIME("text/plain"), getface()) |> choppkg ==
"""
Expand Down

0 comments on commit 02cd20b

Please sign in to comment.