From bc7d076edf8e48b90a812ebe01d253c5c4d3572f Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 19 Jan 2024 00:30:10 +0000 Subject: [PATCH 1/2] Add `Base.float(::Type{Q})` --- Project.toml | 3 ++- src/utils.jl | 2 ++ test/runtests.jl | 3 +++ test/test_meshes.jl | 6 ++++++ test/unittests.jl | 6 ++++++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/test_meshes.jl diff --git a/Project.toml b/Project.toml index 0f83c637..72981019 100644 --- a/Project.toml +++ b/Project.toml @@ -34,6 +34,7 @@ julia = "1.6" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7" +Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa" Ratios = "c84ed2f1-dad5-54f0-aa8e-dbefe2724439" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" SaferIntegers = "88634af6-177f-5301-88b8-7819386cfa38" @@ -43,4 +44,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [targets] -test = ["Aqua", "LinearAlgebra", "Measurements", "Ratios", "SaferIntegers", "SafeTestsets", "ScientificTypes", "StaticArrays", "Test", "Unitful"] +test = ["Aqua", "LinearAlgebra", "Measurements", "Meshes", "Ratios", "SaferIntegers", "SafeTestsets", "ScientificTypes", "StaticArrays", "Test", "Unitful"] diff --git a/src/utils.jl b/src/utils.jl index 407b1552..552f19f0 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -286,6 +286,8 @@ Base.oneunit(::AbstractDimensions) = error("There is no such thing as a dimensio Base.oneunit(::Type{<:UnionAbstractQuantity}) = error("Cannot create a dimensionful 1 for a `UnionAbstractQuantity` type without knowing the dimensions. Please use `oneunit(::UnionAbstractQuantity)` instead.") Base.oneunit(::Type{<:AbstractDimensions}) = error("There is no such thing as a dimensionful 1 for a `AbstractDimensions` type, as + is only defined for `UnionAbstractQuantity`.") +Base.float(::Type{Q}) where {T,D,Q<:UnionAbstractQuantity{T,D}} = with_type_parameters(Q, Base.float(T), D) + Base.show(io::IO, d::AbstractDimensions) = let tmp_io = IOBuffer() for k in filter(k -> !iszero(d[k]), keys(d)) diff --git a/test/runtests.jl b/test/runtests.jl index 85c73883..70709e92 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,6 +19,9 @@ else @safetestset "Measurements.jl integration tests" begin include("test_measurements.jl") end + @safetestset "Meshes.jl integration tests" begin + include("test_meshes.jl") + end @safetestset "Unit tests" begin include("unittests.jl") end diff --git a/test/test_meshes.jl b/test/test_meshes.jl new file mode 100644 index 00000000..0bc42d2b --- /dev/null +++ b/test/test_meshes.jl @@ -0,0 +1,6 @@ +using DynamicQuantities +using Meshes: Meshes + +v = Meshes.Vec(1.0u"m/s", 2.0u"m/s", 3.0u"m/s") +@test typeof(v) <: Meshes.Vec{3, <:Quantity{Float64, <:Dimensions}} +@test v.coords == (1.0u"m/s", 2.0u"m/s", 3.0u"m/s") diff --git a/test/unittests.jl b/test/unittests.jl index 57074e1a..fd159f72 100644 --- a/test/unittests.jl +++ b/test/unittests.jl @@ -438,6 +438,12 @@ end @test typeof(d) == D @test typeof(ulength(d)) == R end + + # Can convert type with float(...): + x = Quantity{Int}(u"m/s") + @test typeof(x) == Quantity{Int64,DEFAULT_DIM_TYPE} + y = float(typeof(x)) + @test typeof(y) == Quantity{Float64,DEFAULT_DIM_TYPE} end @testset "Units" begin From c479ed3ca20c0b69ad2e2517c08d3c3f691a2736 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 19 Jan 2024 00:45:02 +0000 Subject: [PATCH 2/2] Fix `float(::Type)` test --- test/unittests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittests.jl b/test/unittests.jl index fd159f72..c7a9cf19 100644 --- a/test/unittests.jl +++ b/test/unittests.jl @@ -443,7 +443,7 @@ end x = Quantity{Int}(u"m/s") @test typeof(x) == Quantity{Int64,DEFAULT_DIM_TYPE} y = float(typeof(x)) - @test typeof(y) == Quantity{Float64,DEFAULT_DIM_TYPE} + @test y == Quantity{Float64,DEFAULT_DIM_TYPE} end @testset "Units" begin