Skip to content

Commit

Permalink
Improve error message in the absence of arguments to @b and add tes…
Browse files Browse the repository at this point in the history
…ts (#153)
  • Loading branch information
LilithHafner authored Dec 1, 2024
1 parent d35483b commit f4f6a6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/macro_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,25 @@ function process_args(exprs)
end
primary_index = length(args) ÷ 2 + 2
i = 3
while args[i] === :_ && i <= lastindex(args)
while i <= lastindex(args) && args[i] === :_
args[i] = nothing
i += 1
end
if i == primary_index && args[i] isa Expr && args[i].head === :tuple
map!(create_first_function, args[i].args, args[i].args)
else
args[i] = create_first_function(args[i])
end
i += 1
while i <= lastindex(args)
if i <= lastindex(args)
if i == primary_index && args[i] isa Expr && args[i].head === :tuple
map!(create_function, args[i].args, args[i].args)
map!(create_first_function, args[i].args, args[i].args)
else
args[i] = create_function(args[i])
args[i] = create_first_function(args[i])
end
i += 1
while i <= lastindex(args)
if i == primary_index && args[i] isa Expr && args[i].head === :tuple
map!(create_function, args[i].args, args[i].args)
else
args[i] = create_function(args[i])
end
i += 1
end
end
call = exprarray(:call, args)
esc(isempty(interpolations) ? call : Expr(:let, exprarray(:block, interpolations), Expr(:block, call)))
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ else
@test Chairmarks.only(b.samples).evals == 1
end

@testset "process_args" begin
@test Chairmarks.process_args(()) == esc(:($(Chairmarks.benchmark)(;)))
@test Chairmarks.process_args((:(k=v),)) == esc(:($(Chairmarks.benchmark)(; k=v)))
@test Chairmarks.process_args((:(k=v), :(k=v2))) == esc(:($(Chairmarks.benchmark)(; k=v, k=v2)))
@test Chairmarks.process_args((:f,:(k=v))) == esc(:($(Chairmarks.benchmark)(f; k=v)))
@test_throws ErrorException("Positional argument after keyword argument") Chairmarks.process_args((:(k=v),:f))
end

@testset "errors" begin
@test_throws UndefKeywordError Sample(allocs=1.5, bytes=1729) # needs `time`

Expand All @@ -89,6 +97,14 @@ else

t = @test_throws LoadError @eval(@b seconds=1 1+1)
@test t.value.error == ErrorException("Positional argument after keyword argument")

#149
t = @test_throws MethodError @b # no arguments
@test t.value.f === Chairmarks.benchmark
@test t.value.args === ()

t = @test_throws ErrorException @eval(@b seconds=1 seconds=2)
@test startswith(t.value.msg, "syntax: keyword argument \"seconds\" repeated in call to \"")
end

@testset "time_ns() close to typemax(UInt64)" begin
Expand Down

0 comments on commit f4f6a6c

Please # to comment.