Skip to content

Commit

Permalink
Add tests for parser macro
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Sep 24, 2023
1 parent 0754294 commit 08e226a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 45 deletions.
14 changes: 4 additions & 10 deletions src/library/setup/parse.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function __setup_parse()
return _SamplerSpec()
end

function __setup_parse(args...)
setup_error("Macro takes 1 or 2 arguments, not '$(length(args))'")
end

function __setup_parse(expr)
if expr isa Symbol
if expr isa Symbol && Base.isidentifier(expr)
return _SamplerSpec(; id = __setup_parse_id(expr))
elseif (expr isa Expr && expr.head === :block)
return __setup_parse_block(expr)
Expand All @@ -31,9 +27,7 @@ function __setup_parse(id, block)
end

function __setup_parse_block(block; id = :Optimizer)
if !(block.head === :block)
setup_error("Sampler configuration must be provided within a `begin ... end` block")
end
@assert (block isa Expr && block.head === :block)

name = nothing
version = nothing
Expand All @@ -45,7 +39,7 @@ function __setup_parse_block(block; id = :Optimizer)
elseif item isa Expr && item.head === :(=)
key, value = item.args

if key isa Symbol
if key isa Symbol && Base.isidentifier(key)
if key === :name
if !isnothing(name)
setup_error("Duplicate entries for 'name'")
Expand Down Expand Up @@ -84,7 +78,7 @@ function __setup_parse_block(block; id = :Optimizer)
)
end
else
setup_error("Sampler configuration keys must be a valid identifiers")
setup_error("Sampler configuration keys must be valid identifiers, not '$key'")
end
else
setup_error("Sampler configuration must be provided by `key = value` pairs")
Expand Down
5 changes: 5 additions & 0 deletions test/assets/test_macro_throws.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
macro test_macro_throws(error, expr)
return quote
@test_throws $(esc(error)) eval(@macroexpand $(esc(expr)))
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using QUBODrivers: QUBOTools

const VI = MOI.VariableIndex

include("assets/test_macro_throws.jl")

include("setup/setup.jl")
include("drivers/sampler_bundle.jl")

Expand Down
147 changes: 112 additions & 35 deletions test/setup/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,121 @@ end

function test_setup_spec_parser()
@testset "▶ Parser" begin
spec = @setup_spec Optimizer begin
name = "Super Sampler"
version = v"1.2.3"
attributes = begin
SuperAttribute("super_attr")::Union{Integer,Nothing} = nothing
UltraAttribute["ultra_attr"]::Union{String,Nothing} = ""
MegaAttribute = (1, 2, 3)
"simple_attr"::Float64 = 1.2
@testset "→ Standard" begin
spec = @setup_spec Optimizer begin
name = "Super" * " " * "Sampler"
version = VersionNumber("1.2.3")
attributes = begin
SuperAttribute("super_attr")::Union{Integer,Nothing} = nothing
UltraAttribute["ultra_attr"]::Union{String,Nothing} = ""
MegaAttribute = (1, 2, 3)
"simple_attr"::Float64 = 1.2
NormalAttribute("normal_attr") = []
end
end

@test spec == QUBODrivers._SamplerSpec(;
id = :Optimizer,
name = :("Super" * " " * "Sampler"),
version = :(VersionNumber("1.2.3")),
attributes = [
QUBODrivers._AttrSpec(; #
opt_attr = :SuperAttribute,
raw_attr = "super_attr",
val_type = :(Union{Integer,Nothing}),
default = quote nothing end,
),
QUBODrivers._AttrSpec(; #
opt_attr = :UltraAttribute,
raw_attr = "ultra_attr",
val_type = :(Union{String,Nothing}),
default = "",
),
QUBODrivers._AttrSpec(; #
opt_attr = :MegaAttribute,
default = :((1, 2, 3))
),
QUBODrivers._AttrSpec(; #
raw_attr = "simple_attr",
val_type = :(Float64),
default = 1.2,
),
QUBODrivers._AttrSpec(; #
opt_attr = :NormalAttribute,
raw_attr = "normal_attr",
val_type = :Any,
default = quote [] end,
),
],
)
end

@test spec == QUBODrivers._SamplerSpec(;
id = :Optimizer,
name = "Super Sampler",
version = v"1.2.3",
attributes = [
QUBODrivers._AttrSpec(;
opt_attr = :SuperAttribute,
raw_attr = "super_attr",
val_type = :(Union{Integer,Nothing}),
default = quote
nothing
end,
),
QUBODrivers._AttrSpec(;
opt_attr = :UltraAttribute,
raw_attr = "ultra_attr",
val_type = :(Union{String,Nothing}),
default = "",
),
QUBODrivers._AttrSpec(; opt_attr = :MegaAttribute, default = :((1, 2, 3))),
QUBODrivers._AttrSpec(;
raw_attr = "simple_attr",
val_type = :(Float64),
default = 1.2,
),
],
)
@testset "→ Misuse" begin
# Empty macro call
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec()

# Too many arguments
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, 1, 2, 3)

# Invalid single argument
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(0)

# Invalid arguments
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, 0)
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(0, begin end)

# Invalid keys
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
key = "Optimizer"
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
! = "Optimizer"
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
"Optimizer"
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
0 => "Optimizer"
end)

# Duplicate entries
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
name = "Optimizer"
version = VersionNumber("1.2.3")
name = "Optimizer"
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
version = v"1.2.3"
name = "Optimizer"
version = VersionNumber("1.2.3")
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
attributes = begin end
name = "Optimizer"
version = v"1.2.3"
attributes = begin end
end)

# Invalid attribute block
@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
attributes = 0
name = "Optimizer"
version = v"1.2.3"
end)

@test_macro_throws QUBODrivers.DriverSetupError @setup_spec(Optimizer, begin
attributes = begin
! = 3
end
name = "Optimizer"
version = v"1.2.3"
end)
end
end

return nothing
Expand Down

0 comments on commit 08e226a

Please sign in to comment.