Skip to content

Commit 7bad5db

Browse files
author
Mal Miller
committed
Update macro docstrings, error message and add tests
1 parent 7fda0ae commit 7bad5db

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/StructTypes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module StructTypes
22

33
using UUIDs, Dates
44

5-
include("macros.jl")
6-
75
"Abstract super type of various `StructType`s; see `StructTypes.DataType`, `StructTypes.CustomType`, `StructTypes.InterfaceType`, and `StructTypes.AbstractType` for more specific kinds of `StructType`s"
86
abstract type StructType end
97

@@ -1068,4 +1066,6 @@ function constructfrom(::AbstractType, ::Type{T}, obj::S) where {T, S}
10681066
return constructfrom(TT, obj)
10691067
end
10701068

1069+
include("macros.jl")
1070+
10711071
end # module

src/macros.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
isolate_name(name) = split(string(name), '.')[end]
2+
13
for struct_type in (:Struct, :Mutable, :CustomStruct, :OrderedStruct, :AbstractType, :DictType, :ArrayType, :StringType, :NumberType, :BoolType, :NullType)
24
@eval begin
3-
@doc """
4-
@$($struct_type)(expr::Expr)
5-
@$($struct_type)(expr::Symbol)
5+
"""
6+
@$(isolate_name($struct_type))(expr::Expr)
7+
@$(isolate_name($struct_type))(expr::Symbol)
68
79
If `expr` is a struct definition, sets the `StructType` of the defined struct to
8-
$($struct_type)(). If `expr` is the name of a `Type`, sets the `StructType` of that
9-
type to $($struct_type)().
10+
`$(isolate_name($struct_type))()`. If `expr` is the name of a `Type`, sets the `StructType` of that
11+
type to `$(isolate_name($struct_type))()`.
1012
1113
# Examples
1214
```julia
13-
@$($struct_type) MyStruct
15+
@$(isolate_name($struct_type)) MyStruct
1416
```
1517
is equivalent to
1618
```julia
1719
StructTypes.StructType(::Type{MyStruct}) = StructType.Struct()
1820
```
1921
and
2022
```julia
21-
@$($struct_type) struct MyStruct
23+
@$(isolate_name($struct_type)) struct MyStruct
2224
val::Int
2325
end
2426
```
@@ -42,15 +44,15 @@ function macro_constructor(expr::Expr, structtype)
4244
StructTypes.StructType(::Type{$(expr.args[2])}) = $structtype()
4345
end)
4446
else
45-
return :(throw(ArgumentError("Macro constructors can only be used with a struct definition or a Type")))
47+
return :(throw(ArgumentError("StructType macros can only be used with a struct definition or a Type")))
4648
end
4749
end
4850
function macro_constructor(expr::Symbol, structtype)
4951
return esc(quote
5052
if $(expr) isa Type
5153
StructTypes.StructType(::Type{$(expr)}) = $structtype()
5254
else
53-
throw(ArgumentError("Macro constructors can only be used with a struct definition or a Type"))
55+
throw(ArgumentError("StructType macros can only be used with a struct definition or a Type"))
5456
end
5557
end)
5658
end

test/runtests.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,32 @@ StructTypes.StructType(::Type{C2}) = StructTypes.Mutable()
562562
end
563563
end
564564
end
565+
566+
@testset "Macros" begin
567+
struct MyStruct1
568+
val::Int
569+
end
570+
StructTypes.@Struct MyStruct1
571+
@test StructTypes.StructType(MyStruct1) isa StructTypes.Struct
572+
573+
StructTypes.@NullType struct MyStruct2
574+
val::Int
575+
end
576+
@test StructTypes.StructType(MyStruct2) isa StructTypes.NullType
577+
578+
StructTypes.@Mutable mutable struct MyStruct3
579+
val::Int
580+
end
581+
@test StructTypes.StructType(MyStruct3) isa StructTypes.Mutable
582+
583+
# Test an expression that is not a struct def
584+
expr = StructTypes.macro_constructor(Expr(:a), StructTypes.Struct)
585+
@test_throws ArgumentError eval(expr)
586+
587+
# Test a symbol that is not a type
588+
expr = StructTypes.macro_constructor(:var, StructTypes.Struct)
589+
eval(quote
590+
var = 1
591+
end)
592+
@test_throws ArgumentError eval(expr.args[1]) # Extract body from within escape
593+
end

0 commit comments

Comments
 (0)