Skip to content

Commit

Permalink
Throw error in trixi_include when assignment is not found (#68)
Browse files Browse the repository at this point in the history
* throw error in trixi_include when assignment is not found

* remove lake_at_rest from kwarg list in test_trixi_include
  • Loading branch information
JoshuaLampert authored Nov 20, 2023
1 parent a7dcc23 commit 5e1f36e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit 5e1f36e

Please sign in to comment.