From 7f5aaaed4823fb46eaffe08122a2a512f08cb4fd Mon Sep 17 00:00:00 2001 From: Jan Weidner <jw3126@gmail.com> Date: Thu, 16 Sep 2021 08:54:54 +0200 Subject: [PATCH] improve error messages --- src/StructHelpers.jl | 13 +++++++++++-- test/runtests.jl | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/StructHelpers.jl b/src/StructHelpers.jl index abd937e..6501e2f 100644 --- a/src/StructHelpers.jl +++ b/src/StructHelpers.jl @@ -39,6 +39,8 @@ const BATTERIES_DEFAULTS = ( getproperties=true, constructorof=true, ) +const ALLOWED_KW = keys(BATTERIES_DEFAULTS) + """ @batteries T [options] @@ -61,13 +63,20 @@ end """ macro batteries(T, kw...) nt = parse_all_macro_kw(kw) - for pname in propertynames(nt) + for (pname, val) in pairs(nt) if !(pname in propertynames(BATTERIES_DEFAULTS)) error(""" Unsupported keyword. Offending Keyword: $pname allowed: $ALLOWED_KW - Got: $kw + Got: $nt + """) + end + if !(val isa Bool) + error(""" + All options must be literally `true` or `false`. + Got: $nt + Offending Keyword: $pname """) end end diff --git a/test/runtests.jl b/test/runtests.jl index 509dd82..236c039 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -62,4 +62,9 @@ struct Empty2 end @test Empty1() !== Empty2() @test Empty1() != Empty2() @test hash(Empty1()) != hash(Empty2()) + + struct SErrors;a;b;c;end + @test_throws Exception @macroexpand @batteries SErrors kwconstructor="true" + @test_throws Exception @macroexpand @batteries SErrors nonsense=true + @macroexpand @batteries SErrors kwconstructor=true end