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

Throw error in trixi_include when assignment is not found #68

Merged
merged 2 commits into from
Nov 20, 2023
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
16 changes: 16 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ julia> redirect_stdout(devnull) do
```
"""
function trixi_include(mod::Module, example::AbstractString; kwargs...)
# Check that all kwargs exist as assignments
code = read(example, String)
expr = Meta.parse("begin \n$code \nend")
expr = insert_maxiters(expr)

for (key, val) in kwargs
# This will throw an error when `key` is not found
find_assignment(expr, key)
end

Base.include(ex -> replace_assignments(insert_maxiters(ex); kwargs...), mod, example)
end

Expand Down Expand Up @@ -282,19 +292,25 @@ end
function find_assignment(expr, destination)
# declare result to be able to assign to it in the closure
local result
found = false

# find explicit and keyword assignments
walkexpr(expr) do x
if x isa Expr
if (x.head === Symbol("=") || x.head === :kw) &&
x.args[1] === Symbol(destination)
result = x.args[2]
found = true
# dump(x)
end
end
return x
end

if !found
throw(ArgumentError("assignment `$destination` not found in expression"))
end

result
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ macro test_trixi_include(example, args...)
if (arg.head == :(=) &&
!(arg.args[1] in (:l2, :linf, :cons_error, :change_waterheight,
:change_velocity, :change_momentum, :change_entropy,
:change_entropy_modified, :atol, :rtol, :atol_ints,
:rtol_ints)))
:change_entropy_modified, :lake_at_rest,
:atol, :rtol, :atol_ints, :rtol_ints)))
push!(kwargs, Pair(arg.args...))
end
end
Expand Down