Browse Source

Allow initialization of values from preview enum instances.

pull/8/head
Fredrik Ekre 4 years ago
parent
commit
d623eb6805
  1. 8
      src/EnumX.jl
  2. 8
      test/runtests.jl

8
src/EnumX.jl

@ -43,7 +43,13 @@ function enumx(_module_, name, args...) @@ -43,7 +43,13 @@ function enumx(_module_, name, args...)
end
sym = s
elseif s isa Expr && s.head === :(=) && s.args[1] isa Symbol && length(s.args) == 2
nx = Core.eval(_module_, s.args[2])
if s.args[2] isa Symbol &&
(i = findfirst(x -> x.first === s.args[2], name_value_map); i !== nothing)
@assert name_value_map[i].first === s.args[2]
nx = name_value_map[i].second
else
nx = Core.eval(_module_, s.args[2])
end
if !(nx isa Integer && typemin(baseT) <= nx <= typemax(baseT))
panic(
"invalid value for Enum $(modname){$(baseT)}: " *

8
test/runtests.jl

@ -4,6 +4,8 @@ using EnumX, Test @@ -4,6 +4,8 @@ using EnumX, Test
const T16 = Int16
getInt64() = Int64
const Elppa = -1
const Ananab = -1
@testset "EnumX" begin
@ -192,4 +194,10 @@ let io = IOBuffer() @@ -192,4 +194,10 @@ let io = IOBuffer()
@test str == "FruitDup.Apple = FruitDup.Banana = 0"
end
# Initialize with previous instance name
@enumx FruitPrev Elppa Banana=Elppa Orange=Ananab
@test FruitPrev.Elppa === FruitPrev.Banana === FruitPrev.Type(0)
@test FruitPrev.Orange === FruitPrev.Type(-1)
end # testset

Loading…
Cancel
Save