diff --git a/src/Dimensions/format.jl b/src/Dimensions/format.jl index 9bc2e9ef2..ff31f4af2 100644 --- a/src/Dimensions/format.jl +++ b/src/Dimensions/format.jl @@ -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 @@ -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?" + )) diff --git a/test/dimension.jl b/test/dimension.jl index 4ef8df203..81eaa7fde 100644 --- a/test/dimension.jl +++ b/test/dimension.jl @@ -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) diff --git a/test/format.jl b/test/format.jl index 93ff2b62b..36ff80f0d 100644 --- a/test/format.jl +++ b/test/format.jl @@ -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