Skip to content

Commit

Permalink
Add tests for MetaArray (#7)
Browse files Browse the repository at this point in the history
* Add tests for MetaArray

* Fix+Test private property methods
  • Loading branch information
Tokazama authored Aug 11, 2021
1 parent 50cc0c5 commit 767fb4c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Metadata"
uuid = "4fb893c1-3164-4f58-823a-cb4c64eabb4f"
authors = ["Zachary P. Christensen <[email protected]>"]
version = "0.2.7"
version = "0.2.8"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
27 changes: 4 additions & 23 deletions src/metaarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,10 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
parent::A
metadata::M

function MetaArray{T,N,M,A}(a::AbstractArray, meta) where {T,N,M,A}
if a isa A
if meta isa M
return new{T,N,M,A}(a, meta)
else
return new{T,N,M,A}(a, M(meta))
end
else
if meta isa M
return new{T,N,M,A}(A(a), meta)
else
return new{T,N,M,A}(A(a), M(meta))
end
end
end

MetaArray{T,N,M,A}(a::A, m::M) where {T,N,M,A} = new{T,N,M,A}(a, m)
MetaArray{T,N,M,A}(a::A, m) where {T,N,M,A} = new{T,N,M,A}(a, M(m))
MetaArray{T,N,M,A}(a, m::M) where {T,N,M,A} = MetaArray{T,N,M,A}(A(a), m)
MetaArray{T,N,M,A}(a, m) where {T,N,M,A} = MetaArray{T,N,M,A}(A(a), M(m))
function MetaArray{T,N,M,A}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N,M,A}
return MetaArray{T,N,M,A}(a, _construct_meta(metadata, values(kwargs)))
end
Expand All @@ -64,11 +52,9 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
### MetArray{T,N}
###
MetaArray{T,N}(a::AbstractArray, m::M) where {T,N,M} = MetaArray{T,N,M}(a, m)

function MetaArray{T,N}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N}
return MetaArray{T,N}(a, _construct_meta(metadata, values(kwargs)))
end

function MetaArray{T,N}(args...; metadata=Dict{Symbol,Any}(), kwargs...) where {T,N}
return MetaArray{T,N}(Array{T,N}(args...); metadata=metadata, kwargs...)
end
Expand All @@ -79,9 +65,7 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
function MetaArray{T}(args...; metadata=Dict{Symbol,Any}(), kwargs...) where {T}
return MetaArray{T}(Array{T}(args...); metadata=metadata, kwargs...)
end

MetaArray{T}(a::AbstractArray, m::M) where {T,M} = MetaArray{T,ndims(a)}(a, m)

function MetaArray{T}(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...) where {T}
return MetaArray{T,ndims(a)}(a; metadata=metadata, kwargs...)
end
Expand All @@ -90,7 +74,6 @@ struct MetaArray{T, N, M, A<:AbstractArray} <: ArrayInterface.AbstractArray2{T,
### MetaArray
###
MetaArray(v::AbstractArray{T,N}, m::M) where {T,N,M} = new{T,N,M,typeof(v)}(v, m)

function MetaArray(a::AbstractArray; metadata=Dict{Symbol,Any}(), kwargs...)
return MetaArray{eltype(a)}(a; metadata=metadata, kwargs...)
end
Expand Down Expand Up @@ -134,8 +117,6 @@ function Base.show(io::IO, ::MIME"text/plain", X::MetaArray)
Base.print_array(recur_io, parent(X))
end

Base.print_array(io::IO, A::MetaArray) = Base.print_array(io, parent(A))

function Base.similar(x::MetaArray, ::Type{T}, dims::NTuple{N,Int}) where {T,N}
return Metadata.share_metadata(x, similar(parent(x), T, dims))
end
Expand Down
4 changes: 0 additions & 4 deletions src/metastruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ struct MetaStruct{P,M}
metadata::M
end

#Base.parent(m::MetaStruct) = @inbounds(getindex(metadata(parent_module(x)), Base.objectid(x)))

Base.parent(m::MetaStruct) = getfield(m, :parent)
ArrayInterface.parent_type(::Type{MetaStruct{P,M}}) where {P,M} = P



Base.eltype(::Type{T}) where {T<:MetaStruct} = eltype(parent_type(T))

function Base.show(io::IO, ::MIME"text/plain", x::MetaStruct)
Expand Down
2 changes: 2 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ end

macro defproperties(T)
esc(quote
Base.getproperty(x::$T, k::String) = Metadata.metadata(x, k)
@inline function Base.getproperty(x::$T, k::Symbol)
if hasproperty(parent(x), k)
return getproperty(parent(x), k)
Expand All @@ -29,6 +30,7 @@ macro defproperties(T)
end
end

Base.setproperty!(x::$T, k::String, v) = Metadata.metadata!(x, k, v)
@inline function Base.setproperty!(x::$T, k::Symbol, val)
if hasproperty(parent(x), k)
return setproperty!(parent(x), k, val)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ end
@test @inferred(has_metadata(y, :m1))
@test @inferred(Metadata.metadata_keys(y)) === keys(m)
@test Metadata.metadata_keys(1 => 2) === propertynames(1 => 2)

mutable struct MutableType
field::Int
end
x = Metadata.MetaStruct(MutableType(1), Dict{String,Any}())
x."m1" = 1
@test getproperty(x, "m1") == 1
x.field = 2
@test getproperty(x, :field) == 2
end

@testset "MetaArray" begin
Expand All @@ -58,6 +67,9 @@ end
@test @inferred(parent_type(mx)) <: typeof(x)
@test @inferred(parent_type(mxview)) <: typeof(xview)
@test @inferred(typeof(mx)(xview, meta)) isa typeof(mx)
@test mxview.indices === xview.indices

@test ArrayInterface.defines_strides(typeof(mx))

@test isempty(metadata(Metadata.MetaArray(ones(2,2))))

Expand Down

0 comments on commit 767fb4c

Please sign in to comment.