Skip to content

Commit 7f5aaae

Browse files
committed
improve error messages
1 parent 750129c commit 7f5aaae

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/StructHelpers.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const BATTERIES_DEFAULTS = (
3939
getproperties=true, constructorof=true,
4040
)
4141

42+
const ALLOWED_KW = keys(BATTERIES_DEFAULTS)
43+
4244
"""
4345
4446
@batteries T [options]
@@ -61,13 +63,20 @@ end
6163
"""
6264
macro batteries(T, kw...)
6365
nt = parse_all_macro_kw(kw)
64-
for pname in propertynames(nt)
66+
for (pname, val) in pairs(nt)
6567
if !(pname in propertynames(BATTERIES_DEFAULTS))
6668
error("""
6769
Unsupported keyword.
6870
Offending Keyword: $pname
6971
allowed: $ALLOWED_KW
70-
Got: $kw
72+
Got: $nt
73+
""")
74+
end
75+
if !(val isa Bool)
76+
error("""
77+
All options must be literally `true` or `false`.
78+
Got: $nt
79+
Offending Keyword: $pname
7180
""")
7281
end
7382
end

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ struct Empty2 end
6262
@test Empty1() !== Empty2()
6363
@test Empty1() != Empty2()
6464
@test hash(Empty1()) != hash(Empty2())
65+
66+
struct SErrors;a;b;c;end
67+
@test_throws Exception @macroexpand @batteries SErrors kwconstructor="true"
68+
@test_throws Exception @macroexpand @batteries SErrors nonsense=true
69+
@macroexpand @batteries SErrors kwconstructor=true
6570
end

0 commit comments

Comments
 (0)