Browse Source

Fix show(...) of invalid instances.

pull/8/head v1.0.4
Fredrik Ekre 3 years ago
parent
commit
6120a51ecd
  1. 2
      Project.toml
  2. 5
      src/EnumX.jl
  3. 10
      test/runtests.jl

2
Project.toml

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
name = "EnumX"
uuid = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
version = "1.0.3"
version = "1.0.4"
[compat]
julia = "1.6"

5
src/EnumX.jl

@ -151,11 +151,16 @@ end @@ -151,11 +151,16 @@ end
function Base.show(io::IO, ::MIME"text/plain", x::E) where E <: Enum
iob = IOBuffer()
ix = Integer(x)
found = false
for (k, v) in symbol_map(E)
if v == ix
print(iob, "$(nameof(parentmodule(E))).$(k) = ")
found = true
end
end
if !found
print(iob, "$(nameof(parentmodule(E))).#invalid# = ")
end
show(iob, ix)
write(io, seekstart(iob))
return nothing

10
test/runtests.jl

@ -266,6 +266,16 @@ end @@ -266,6 +266,16 @@ end
@test instances(FruitEmptyT.Typ) == ()
# Showing invalid instances
@enumx Invalid A
let io = IOBuffer()
invalid = Base.bitcast(Invalid.T, Int32(1))
show(io, "text/plain", invalid)
str = String(take!(io))
@test str == "Invalid.#invalid# = 1"
end
# Documented type (module) and instances
begin
"""

Loading…
Cancel
Save