Skip to content

Commit 02cd20b

Browse files
committed
Consistent Face hashes
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.
1 parent f6035eb commit 02cd20b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/faces.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ Base.:(==)(a::Face, b::Face) =
174174
getfield.(Ref(a), fieldnames(Face)) ==
175175
getfield.(Ref(b), fieldnames(Face))
176176

177+
Base.hash(f::Face, h::UInt) =
178+
mapfoldr(Base.Fix1(getfield, f), hash, fieldnames(Face), init=hash(Face, h))
179+
177180
Base.copy(f::Face) =
178181
Face(f.font, f.height, f.weight, f.slant,
179182
f.foreground, f.background, f.underline,

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ end
257257
@test getface(:f).foreground.value == :blue
258258
StyledStrings.resetfaces!()
259259
end
260+
# Equality/hashing equivalence
261+
let testfaces = [Face(foreground=:blue),
262+
Face(background=:blue),
263+
Face(inherit=:something),
264+
Face(inherit=:something)]
265+
for f1 in testfaces, f2 in testfaces
266+
@test (f1 == f2) == (hash(f1) == hash(f2))
267+
end
268+
end
260269
# Pretty display
261270
@test sprint(show, MIME("text/plain"), getface()) |> choppkg ==
262271
"""

0 commit comments

Comments
 (0)