Browse Source

Use show instead of print for displaying the values.

pull/8/head v1.0.2
Fredrik Ekre 4 years ago
parent
commit
92560b30df
  1. 2
      Project.toml
  2. 5
      src/EnumX.jl
  3. 14
      test/runtests.jl

2
Project.toml

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

5
src/EnumX.jl

@ -134,7 +134,7 @@ function Base.show(io::IO, ::MIME"text/plain", x::E) where E <: Enum
print(iob, "$(nameof(parentmodule(E))).$(k) = ") print(iob, "$(nameof(parentmodule(E))).$(k) = ")
end end
end end
print(iob, "$(Integer(x))") show(iob, ix)
write(io, seekstart(iob)) write(io, seekstart(iob))
return nothing return nothing
end end
@ -155,7 +155,8 @@ function Base.show(io::IO, ::MIME"text/plain", ::Type{E}) where E <: Enum
"Enum{$(Base.Enums.basetype(E))} with $(n) instance$(n == 1 ? "" : "s")$(n>0 ? ":" : "")" "Enum{$(Base.Enums.basetype(E))} with $(n) instance$(n == 1 ? "" : "s")$(n>0 ? ":" : "")"
) )
for (k, v) in stringmap for (k, v) in stringmap
print(iob, "\n ", rpad(k, mx), " = $(v)") print(iob, "\n ", rpad(k, mx), " = ")
show(iob, v)
end end
write(io, seekstart(iob)) write(io, seekstart(iob))
return nothing return nothing

14
test/runtests.jl

@ -86,10 +86,22 @@ end
@test Base.Enums.basetype(Fruit8.T) === Int8 @test Base.Enums.basetype(Fruit8.T) === Int8
@test Integer(Fruit8.Apple) === Int8(0) @test Integer(Fruit8.Apple) === Int8(0)
@enumx FruitU8::UInt8 Apple # no overflow even if first is typemin(T) @enumx FruitU8::UInt8 Apple Banana # no overflow even if first is typemin(T)
@test Base.Enums.basetype(FruitU8.T) === UInt8 @test Base.Enums.basetype(FruitU8.T) === UInt8
@test FruitU8.Apple === FruitU8.T(0) @test FruitU8.Apple === FruitU8.T(0)
let io = IOBuffer()
show(io, "text/plain", FruitU8.T)
str = String(take!(io))
@test str == "Enum type FruitU8.T <: Enum{UInt8} with 2 instances:\n FruitU8.Apple = 0x00\n FruitU8.Banana = 0x01"
show(io, "text/plain", FruitU8.Apple)
str = String(take!(io))
@test str == "FruitU8.Apple = 0x00"
show(io, "text/plain", FruitU8.Banana)
str = String(take!(io))
@test str == "FruitU8.Banana = 0x01"
end
@enumx Fruit16::T16 Apple @enumx Fruit16::T16 Apple
@test Fruit16.T <: EnumX.Enum{Int16} <: Base.Enum{Int16} @test Fruit16.T <: EnumX.Enum{Int16} <: Base.Enum{Int16}
@test Base.Enums.basetype(Fruit16.T) === Int16 @test Base.Enums.basetype(Fruit16.T) === Int16

Loading…
Cancel
Save