Browse Source

Support block syntax: at-enumx begin; ...; end.

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

12
src/EnumX.jl

@ -21,11 +21,17 @@ function enumx(_module_, name, args...)
throw(ArgumentError("invalid EnumX.@enumx type specification: $(name)")) throw(ArgumentError("invalid EnumX.@enumx type specification: $(name)"))
end end
name = modname name = modname
if length(args) == 1 && args[1] isa Expr && args[1].head === :block
syms = args[1].args
else
syms = args
end
namemap = Dict{baseT,Symbol}() namemap = Dict{baseT,Symbol}()
next = 0 next = 0
for arg in args for s in syms
@assert arg isa Symbol # TODO s isa LineNumberNode && continue
namemap[next] = arg s isa Symbol || throw(ArgumentError("invalid member expression: $(s)"))
namemap[next] = s
next += 1 next += 1
end end
module_block = quote module_block = quote

18
test/runtests.jl

@ -96,4 +96,22 @@ catch err
@test err.msg == "invalid EnumX.@enumx type specification: Fr + uit" @test err.msg == "invalid EnumX.@enumx type specification: Fr + uit"
end end
# Block syntax
@enumx FruitBlock begin
Apple
Banana
end
@test FruitBlock.Type <: EnumX.Enum{Int32} <: Base.Enum{Int32}
@test FruitBlock.Apple === FruitBlock.Type(0)
@test FruitBlock.Banana === FruitBlock.Type(1)
@enumx FruitBlock8::Int8 begin
Apple
Banana
end
@test FruitBlock8.Type <: EnumX.Enum{Int8} <: Base.Enum{Int8}
@test FruitBlock8.Apple === FruitBlock8.Type(0)
@test FruitBlock8.Banana === FruitBlock8.Type(1)
end # testset end # testset

Loading…
Cancel
Save