Skip to content

Commit 2680c14

Browse files
committed
Expand uconvert tests
1 parent 5559886 commit 2680c14

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/symbolic_dimensions.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ expand_units(q::QuantityArray) = expand_units.(q)
113113
Convert a quantity `q` with base SI units to the symbolic units of `qout`, for `q` and `qout` with compatible units.
114114
Mathematically, the result has value `q / expand_units(qout)` and units `dimension(qout)`.
115115
"""
116-
function uconvert(qout::AbstractQuantity{<:Any, <:SymbolicDimensions}, q::AbstractQuantity{<:Any, <:Dimensions})
116+
function uconvert(qout::AbstractQuantity{T, <:SymbolicDimensions}, q::AbstractQuantity{<:Any, <:Dimensions}) where {T}
117+
isone(ustrip(qout)) || error("You passed a quantity with a non-unit value to uconvert.")
117118
qout_expanded = expand_units(qout)
118119
dimension(q) == dimension(qout_expanded) || throw(DimensionError(q, qout_expanded))
119120
return new_quantity(typeof(qout), ustrip(q) / ustrip(qout_expanded), dimension(qout))

test/unittests.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,6 @@ end
569569
@test us"Constants.h" != us"h"
570570
@test expand_units(us"Constants.h") == u"Constants.h"
571571

572-
@test uconvert(us"nm", 5e-9u"m") (5e-9u"m" |> uconvert(us"nm")) 5us"nm"
573-
@test_throws DimensionError uconvert(us"nm * J", 5e-9u"m")
574-
575572
# Actually expands to:
576573
@test dimension(us"Constants.h")[:m] == 2
577574
@test dimension(us"Constants.h")[:s] == -1
@@ -594,6 +591,22 @@ end
594591
@test_throws "rad is not available as a symbol" sym5.rad
595592
end
596593

594+
@testset "uconvert" begin
595+
@test uconvert(us"nm", 5e-9u"m") (5e-9u"m" |> uconvert(us"nm")) 5us"nm"
596+
@test_throws DimensionError uconvert(us"nm * J", 5e-9u"m")
597+
598+
q = 1.5u"Constants.M_sun"
599+
qs = uconvert(us"Constants.M_sun", 5.0 * q)
600+
@test qs 7.5us"Constants.M_sun"
601+
@test dimension(qs)[:kg] == 0
602+
@test dimension(qs)[:g] == 0
603+
@test dimension(qs)[:M_sun] == 1
604+
@test expand_units(qs) 5.0 * q
605+
606+
# Refuses to convert to non-unit quantities:
607+
@test_throws ErrorException uconvert(1.2us"m", 1.0u"m")
608+
end
609+
597610
@testset "Test ambiguities" begin
598611
R = DEFAULT_DIM_BASE_TYPE
599612
x = convert(R, 10)

0 commit comments

Comments
 (0)