Skip to content

Commit

Permalink
improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Sep 16, 2021
1 parent 750129c commit 7f5aaae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/StructHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const BATTERIES_DEFAULTS = (
getproperties=true, constructorof=true,
)

const ALLOWED_KW = keys(BATTERIES_DEFAULTS)

"""
@batteries T [options]
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7f5aaae

Please sign in to comment.