Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message in the absence of arguments to @b and add tests #153

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading