From 323094f418c7d3b056847debd5d7ebd240889111 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Mon, 20 Nov 2023 13:50:39 +0100 Subject: [PATCH] throw error in trixi_include when assignment is not found --- src/util.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/util.jl b/src/util.jl index df7d7378..1dc2168d 100644 --- a/src/util.jl +++ b/src/util.jl @@ -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 @@ -282,6 +292,7 @@ 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 @@ -289,12 +300,17 @@ function find_assignment(expr, destination) 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