Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Nov 24, 2023
1 parent 939beb2 commit ee68aa7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Dimensions/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ end

format(val::AbstractArray, D::Type, axis::AbstractRange) = format(AutoLookup(), D, val, axis)
format(m::LookupArray, D::Type, axis::AbstractRange) = format(m, D, parent(m), axis)
format(v::AutoVal, D::Type, axis::AbstractRange) = _valformaterror(val(v), D)
format(v, D::Type, axis::AbstractRange) = _valformaterror(v, D)

# Format LookupArrays
# No more identification required for NoLookup
Expand Down Expand Up @@ -131,3 +133,7 @@ checkaxis(lookup, axis) = first(axes(lookup)) == axis || _checkaxiserror(lookup,
throw(DimensionMismatch(
"axes of $(basetypeof(lookup)) of $(first(axes(lookup))) do not match array axis of $axis"
))
@noinline _valformaterror(v, D::Type) =
throw(ArgumentError(
"Lookup value of `$v` for dimension $D cannot be converted to a `LookupArray`. Did you mean to pass a range or array?"
))
7 changes: 7 additions & 0 deletions test/dimension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ end
@test sortdims(dimz, (X, X)) === dimz
end

@testset "constructing with keywords" begin
@test X(1; foo=:bar) == X(DimensionalData.AutoVal(1, (; foo=:bar)))
@test X(1:10; foo=:bar) == X(DimensionalData.AutoLookup(1:10, (; foo=:bar)))
@test Dim{:x}(1; foo=:bar) == Dim{:x}(DimensionalData.AutoVal(1, (; foo=:bar)))
@test Dim{:x}(1:10; foo=:bar) == Dim{:x}(DimensionalData.AutoLookup(1:10, (; foo=:bar)))
end

@testset "applying function on a dimension" begin
d = X(0:0.01:2π)
a = DimArray(cos, d)
Expand Down
5 changes: 5 additions & 0 deletions test/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,9 @@ end
@test format(l, X, Base.OneTo(2)) === Sampled(l, ForwardOrdered(), Regular(0.0), Points(), NoMetadata())
end

@testset "LookupArray conversion errors" begin
@test_throws ArgumentError DimArray(rand(5, 4), (X(1), Y(1:4)))
@test_throws ArgumentError DimArray(rand(5), X(1; foo=:bar))
end

end

0 comments on commit ee68aa7

Please sign in to comment.