From 507b524c98786fdaebad0ac513017814e7805e05 Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Fri, 16 Jul 2021 21:23:42 -0400 Subject: [PATCH 01/13] Round-trip printing --- src/display.jl | 43 +++++++++---------------------------------- src/logarithm.jl | 8 ++++---- src/types.jl | 2 +- 3 files changed, 14 insertions(+), 39 deletions(-) diff --git a/src/display.jl b/src/display.jl index c85af809..b66f263a 100644 --- a/src/display.jl +++ b/src/display.jl @@ -44,10 +44,6 @@ function prefix(x::Unit) end end -function show(io::IO, x::Unit{N,D}) where {N,D} - show(io, FreeUnits{(x,), D, nothing}()) -end - abstract type BracketStyle end struct NoBrackets <: BracketStyle end @@ -89,7 +85,7 @@ brackets are not printed. """ function showval(io::IO, x::Number, brackets::Bool=true) brackets && print_opening_bracket(io, x) - show(io, x) + show(io, MIME"text/plain"(), x) brackets && print_closing_bracket(io, x) end @@ -107,20 +103,10 @@ has_unit_spacing(u) = true has_unit_spacing(u::Units{(Unit{:Degree, NoDims}(0, 1//1),), NoDims}) = false """ - show(io::IO, x::Quantity) + show(io::IO, mime::MIME"text/plain", x::Quantity) Show a unitful quantity by calling [`showval`](@ref) on the numeric value, appending a space, and then calling `show` on a units object `U()`. """ -function show(io::IO, x::Quantity) - if isunitless(unit(x)) - showval(io, x.val, false) - else - showval(io, x.val, true) - has_unit_spacing(unit(x)) && print(io, ' ') - show(io, unit(x)) - end -end - function show(io::IO, mime::MIME"text/plain", x::Quantity) if isunitless(unit(x)) showval(io, mime, x.val, false) @@ -131,42 +117,31 @@ function show(io::IO, mime::MIME"text/plain", x::Quantity) end end -function show(io::IO, r::StepRange{T}) where T<:Quantity +function show(io::IO, mime::MIME"text/plain", r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity a,s,b = first(r), step(r), last(r) U = unit(a) V = absoluteunit(U) print(io, '(') if ustrip(V, s) == 1 - show(io, ustrip(U, a):ustrip(U, b)) + show(io, mime, ustrip(U, a):ustrip(U, b)) else - show(io, ustrip(U, a):ustrip(V, s):ustrip(U, b)) + show(io, mime, ustrip(U, a):ustrip(V, s):ustrip(U, b)) end print(io, ')') has_unit_spacing(U) && print(io,' ') - show(io, U) -end - -function show(io::IO, r::StepRangeLen{T}) where T<:Quantity - a,s,b = first(r), step(r), last(r) - U = unit(a) - V = absoluteunit(U) - print(io, '(') - show(io, StepRangeLen(ustrip(U, a), ustrip(V, s), length(r))) - print(io, ')') - has_unit_spacing(U) && print(io,' ') - show(io, U) + show(io, mime, U) end -function show(io::IO, x::typeof(NoDims)) +function show(io::IO, ::MIME"text/plain", x::typeof(NoDims)) print(io, "NoDims") end """ - show(io::IO, x::Unitlike) + show(io::IO, ::MIME"text/plain", x::Unitlike) Call [`Unitful.showrep`](@ref) on each object in the tuple that is the type variable of a [`Unitful.Units`](@ref) or [`Unitful.Dimensions`](@ref) object. """ -function show(io::IO, x::Unitlike) +function show(io::IO, ::MIME"text/plain", x::Unitlike) showoperators = get(io, :showoperators, false) first = "" sep = showoperators ? "*" : " " diff --git a/src/logarithm.jl b/src/logarithm.jl index 58d76beb..a9d9daa4 100644 --- a/src/logarithm.jl +++ b/src/logarithm.jl @@ -84,11 +84,11 @@ tolog(L,x) = (1+isrootpower(L)) * prefactor(L()) * (logfn(L()))(x) fromlog(L,S,x) = unwrap(S) * expfn(L())( x / ((1+isrootpower(S))*prefactor(L())) ) fromlog(L,x) = expfn(L())( x / ((1+isrootpower(L))*prefactor(L())) ) -function Base.show(io::IO, x::MixedUnits{T,U}) where {T,U} +function Base.show(io::IO, mime::MIME"text/plain", x::MixedUnits{T,U}) where {T,U} print(io, abbr(x)) if x.units != NoUnits print(io, " ") - show(io, x.units) + show(io, mime, x.units) end end @@ -312,11 +312,11 @@ function Base.promote_rule(::Type{G}, ::Type{N}) where {L,S,T1, G<:Gain{L,S,T1}, end Base.promote_rule(A::Type{G}, B::Type{L}) where {G<:Gain, L2, L<:Level{L2}} = LogScaled{L2} -function Base.show(io::IO, x::Gain) +function Base.show(io::IO, mime::MIME"text/plain", x::Gain) print(io, x.val, " ", abbr(x)) nothing end -function Base.show(io::IO, x::Level) +function Base.show(io::IO, mime::MIME"text/plain", x::Level) print(io, ustrip(x), " ", abbr(x)) nothing end diff --git a/src/types.jl b/src/types.jl index a50dbeb4..58e03b99 100644 --- a/src/types.jl +++ b/src/types.jl @@ -286,7 +286,7 @@ struct IsRootPowerRatio{S,T} val::T end IsRootPowerRatio{S}(x) where {S} = IsRootPowerRatio{S, typeof(x)}(x) -Base.show(io::IO, x::IsRootPowerRatio{S}) where {S} = +Base.show(io::IO, ::MIME"text/plain", x::IsRootPowerRatio{S}) where {S} = print(io, ifelse(S, "root-power ratio", "power ratio"), " with reference ", x.val) const PowerRatio{T} = IsRootPowerRatio{false,T} const RootPowerRatio{T} = IsRootPowerRatio{true,T} From 1bfd46d9f4a675baf8f7f2f1bbcff8b2d568c1b2 Mon Sep 17 00:00:00 2001 From: anand jain Date: Sat, 17 Jul 2021 14:52:22 -0400 Subject: [PATCH 02/13] update tests --- test/runtests.jl | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 8a707747..e85a2cb5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1479,21 +1479,21 @@ end withenv("UNITFUL_FANCY_EXPONENTS" => false) do @static if VERSION ≥ v"1.6.0-DEV.770" @test string(typeof(1.0m/s)) == - "Quantity{Float64, 𝐋 𝐓^-1, FreeUnits{(m, s^-1), 𝐋 𝐓^-1, nothing}}" + "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" @test string(typeof(m/s)) == - "FreeUnits{(m, s^-1), 𝐋 𝐓^-1, nothing}" + "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" else @test string(typeof(1.0m/s)) == - "Quantity{Float64,𝐋 𝐓^-1,FreeUnits{(m, s^-1),𝐋 𝐓^-1,nothing}}" + "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" @test string(typeof(m/s)) == - "FreeUnits{(m, s^-1),𝐋 𝐓^-1,nothing}" + "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" end - @test string(dimension(1u"m/s")) == "𝐋 𝐓^-1" - @test string(NoDims) == "NoDims" + @test string(dimension(1u"m/s")) == "Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}()" + @test string(NoDims) == "Unitful.Dimensions{()}()" end @testset ":fancy_exponent IOContext property" begin - @test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "m s⁻¹" - @test sprint(io -> show(IOContext(io, :fancy_exponent => false), u"m/s")) == "m s^-1" + @test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}()" + @test sprint(io -> show(IOContext(io, :fancy_exponent => false), u"m/s")) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}()" end end @@ -1503,17 +1503,17 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @testset "Show quantities" begin withenv("UNITFUL_FANCY_EXPONENTS" => false) do - @test repr(1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1" + @test repr(1.0 * u"m * s * kg^-1") == "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1.0)" @test repr("text/plain", 1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1" - @test repr(Foo() * u"m * s * kg^-1") == "1 m s kg^-1" + @test repr(Foo() * u"m * s * kg^-1") == "Quantity{Foo, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1)" @test repr("text/plain", Foo() * u"m * s * kg^-1") == "42.0 m s kg^-1" # Complex quantities - @test repr((1+2im) * u"m/s") == "(1 + 2im) m s^-1" + @test repr((1+2im) * u"m/s") == "Quantity{Complex{Int64}, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}(1 + 2im)" @test repr("text/plain", (1+2im) * u"m/s") == "(1 + 2im) m s^-1" # Angular degree printing #253 - @test sprint(show, 1.0°) == "1.0°" + @test sprint(show, 1.0°) == "Quantity{Float64, Unitful.Dimensions{()}(), FreeUnits{(Unitful.Unit{:Degree, Unitful.Dimensions{()}()}(0, 1//1),), Unitful.Dimensions{()}(), nothing}}(1.0)" @test repr("text/plain", 1.0°) == "1.0°" # Concise printing of ranges @@ -1541,12 +1541,18 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") end end withenv("UNITFUL_FANCY_EXPONENTS" => true) do - @test repr(1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²" + @test repr("text/plain", 1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²" end withenv("UNITFUL_FANCY_EXPONENTS" => nothing) do - @test repr(1.0 * u"m * s * kg^(-1//2)") == + @test repr("text/plain", 1.0 * u"m * s * kg^(-1//2)") == (Sys.isapple() ? "1.0 m s kg⁻¹ᐟ²" : "1.0 m s kg^-1/2") end + + @testset "roundtripping show" begin + u = u"m/s" + u2 = eval(Meta.parse(repr(u))) + @test u == u2 + end end @testset "DimensionError message" begin @@ -1556,11 +1562,11 @@ end String(take!(b)) end @test errorstr(DimensionError(1u"m",2)) == - "DimensionError: 1 m and 2 are not dimensionally compatible." + "DimensionError: Quantity{Int64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}}(1) and 2 are not dimensionally compatible." @test errorstr(DimensionError(1u"m",NoDims)) == - "DimensionError: 1 m and NoDims are not dimensionally compatible." + "DimensionError: Quantity{Int64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}}(1) and Unitful.Dimensions{()}() are not dimensionally compatible." @test errorstr(DimensionError(u"m",2)) == - "DimensionError: m and 2 are not dimensionally compatible." + "DimensionError: FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}() and 2 are not dimensionally compatible." end @testset "Logarithmic quantities" begin @@ -1880,12 +1886,12 @@ end @testset "> Display" begin withenv("UNITFUL_FANCY_EXPONENTS" => false) do - @test repr(3u"dB/Hz") == "[3 dB] Hz^-1" + @test repr(3u"dB/Hz") == "Quantity{Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), FreeUnits{(Unitful.Unit{:Hertz, Unitful.Dimensions{(Unitful.Dimension{:Time}(-1//1),)}()}(0, -1//1),), Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), nothing}}(Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}(3))" @test repr("text/plain", 3u"dB/Hz") == "[3 dB] Hz^-1" end @test Unitful.abbr(3u"dBm") == "dBm" - @test Unitful.abbr(@dB 3V/1.241V) == "dB (1.241 V)" - @test string(360°) == "360°" + @test Unitful.abbr(@dB 3V/1.241V) == "dB (Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}(), FreeUnits{(Unitful.Unit{:Volt, Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}(), nothing}}(1.241))" + @test repr("text/plain", 360°) == "360°" end @testset "> Thanks for signing up for Log Facts!" begin From 96a980badbb16a3eac7fdd5602c4d0a48887cd1a Mon Sep 17 00:00:00 2001 From: anand jain Date: Sun, 25 Jul 2021 13:43:43 -0400 Subject: [PATCH 03/13] fix dimerror display, overload print, fix test --- src/display.jl | 9 +++++++++ src/utils.jl | 4 ++-- test/runtests.jl | 22 +++++++++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/display.jl b/src/display.jl index b66f263a..39b0c938 100644 --- a/src/display.jl +++ b/src/display.jl @@ -227,3 +227,12 @@ superscript(i::Integer) = map(repr(i)) do c c == '0' ? '\u2070' : error("unexpected character") end + +Base.print(io::IO, x::Quantity) = show(io, "text/plain", x) +Base.print(io::IO, x::Dimension) = show(io, "text/plain", x) +Base.print(io::IO, x::Unit) = show(io, "text/plain", x) +Base.print(io::IO, x::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity = show(io, "text/plain", x) +Base.print(io::IO, x::typeof(NoDims)) = show(io, "text/plain", x) +Base.print(io::IO, x::Unitlike) = show(io, "text/plain", x) +Base.print(io::IO, x::MixedUnits{T,U}) where {T,U} = show(io, "text/plain", x) +Base.print(io::IO, x::FreeUnits{N,D,A}) where {N,D,A} = show(io, "text/plain", x) diff --git a/src/utils.jl b/src/utils.jl index c0975ce5..13ccb20a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -235,7 +235,7 @@ struct DimensionError <: Exception end Base.showerror(io::IO, e::DimensionError) = - print(io, "DimensionError: $(e.x) and $(e.y) are not dimensionally compatible."); + print(io, "DimensionError: $(string(e.x)) and $(string(e.y)) are not dimensionally compatible."); """ struct AffineError <: Exception @@ -245,4 +245,4 @@ struct AffineError <: Exception x end -Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: $(e.x)") +Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: $(string(e.x))") diff --git a/test/runtests.jl b/test/runtests.jl index e85a2cb5..224fd33b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1479,7 +1479,7 @@ end withenv("UNITFUL_FANCY_EXPONENTS" => false) do @static if VERSION ≥ v"1.6.0-DEV.770" @test string(typeof(1.0m/s)) == - "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" + "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" @test string(typeof(m/s)) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" else @@ -1488,8 +1488,8 @@ end @test string(typeof(m/s)) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" end - @test string(dimension(1u"m/s")) == "Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}()" - @test string(NoDims) == "Unitful.Dimensions{()}()" + @test string(dimension(1u"m/s")) == "𝐋 𝐓^-1" + @test string(NoDims) == "NoDims" end @testset ":fancy_exponent IOContext property" begin @test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}()" @@ -1552,6 +1552,14 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") u = u"m/s" u2 = eval(Meta.parse(repr(u))) @test u == u2 + + q = Quantity(5, u"m") + q2 = eval(Meta.parse(repr(q))) + @test q == q2 + + d = u"𝐌*𝐋/𝐓^2" + d2 = eval(Meta.parse(repr(d))) + @test d == d2 end end @@ -1562,11 +1570,11 @@ end String(take!(b)) end @test errorstr(DimensionError(1u"m",2)) == - "DimensionError: Quantity{Int64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}}(1) and 2 are not dimensionally compatible." + "DimensionError: 1 m and 2 are not dimensionally compatible." @test errorstr(DimensionError(1u"m",NoDims)) == - "DimensionError: Quantity{Int64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}}(1) and Unitful.Dimensions{()}() are not dimensionally compatible." + "DimensionError: 1 m and NoDims are not dimensionally compatible." @test errorstr(DimensionError(u"m",2)) == - "DimensionError: FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}(), nothing}() and 2 are not dimensionally compatible." + "DimensionError: m and 2 are not dimensionally compatible." end @testset "Logarithmic quantities" begin @@ -1890,7 +1898,7 @@ end @test repr("text/plain", 3u"dB/Hz") == "[3 dB] Hz^-1" end @test Unitful.abbr(3u"dBm") == "dBm" - @test Unitful.abbr(@dB 3V/1.241V) == "dB (Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}(), FreeUnits{(Unitful.Unit{:Volt, Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}()}(0, 1//1),), Unitful.Dimensions{(Unitful.Dimension{:Current}(-1//1), Unitful.Dimension{:Length}(2//1), Unitful.Dimension{:Mass}(1//1), Unitful.Dimension{:Time}(-3//1))}(), nothing}}(1.241))" + @test Unitful.abbr(@dB 3V/1.241V) == "dB (1.241 V)" @test repr("text/plain", 360°) == "360°" end From 71861c20d8b9b3682d9b0d381da3f077c1a7121c Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:50:41 -0400 Subject: [PATCH 04/13] Update src/display.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/display.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 39b0c938..1adf7c47 100644 --- a/src/display.jl +++ b/src/display.jl @@ -228,7 +228,7 @@ superscript(i::Integer) = map(repr(i)) do c error("unexpected character") end -Base.print(io::IO, x::Quantity) = show(io, "text/plain", x) +Base.print(io::IO, x::AbstractQuantity) = show(io, "text/plain", x) Base.print(io::IO, x::Dimension) = show(io, "text/plain", x) Base.print(io::IO, x::Unit) = show(io, "text/plain", x) Base.print(io::IO, x::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity = show(io, "text/plain", x) From 50762e4e3f23de3b2ae06b2771bce71caa879cb8 Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:50:47 -0400 Subject: [PATCH 05/13] Update src/display.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/display.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/display.jl b/src/display.jl index 1adf7c47..1f3d3534 100644 --- a/src/display.jl +++ b/src/display.jl @@ -232,7 +232,7 @@ Base.print(io::IO, x::AbstractQuantity) = show(io, "text/plain", x) Base.print(io::IO, x::Dimension) = show(io, "text/plain", x) Base.print(io::IO, x::Unit) = show(io, "text/plain", x) Base.print(io::IO, x::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity = show(io, "text/plain", x) -Base.print(io::IO, x::typeof(NoDims)) = show(io, "text/plain", x) Base.print(io::IO, x::Unitlike) = show(io, "text/plain", x) -Base.print(io::IO, x::MixedUnits{T,U}) where {T,U} = show(io, "text/plain", x) -Base.print(io::IO, x::FreeUnits{N,D,A}) where {N,D,A} = show(io, "text/plain", x) +Base.print(io::IO, x::MixedUnits) = show(io, "text/plain", x) +Base.print(io::IO, x::LogScaled) = show(io, "text/plain", x) +Base.print(io::IO, x::IsRootPowerRatio) = show(io, "text/plain", x) From b3e6e1cd0041031b23e80adf1afd42c6b8d621e3 Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:51:54 -0400 Subject: [PATCH 06/13] Update test/runtests.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 224fd33b..9d7c07ee 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1492,8 +1492,8 @@ end @test string(NoDims) == "NoDims" end @testset ":fancy_exponent IOContext property" begin - @test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}()" - @test sprint(io -> show(IOContext(io, :fancy_exponent => false), u"m/s")) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}()" + @test sprint(print, u"m/s", context = :fancy_exponent => true) == "m s⁻¹" + @test sprint(print, u"m/s", context = :fancy_exponent => false) == "m s^-1" end end From efc7cbe582ea048c621684f259443042aecb9740 Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:52:00 -0400 Subject: [PATCH 07/13] Update test/runtests.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 9d7c07ee..ae7fa46d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1899,7 +1899,7 @@ end end @test Unitful.abbr(3u"dBm") == "dBm" @test Unitful.abbr(@dB 3V/1.241V) == "dB (1.241 V)" - @test repr("text/plain", 360°) == "360°" + @test string(360°) == "360°" end @testset "> Thanks for signing up for Log Facts!" begin From f243a685fc1f7030fab6fbac4f53af32e3a67b0a Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:52:56 -0400 Subject: [PATCH 08/13] Update src/utils.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 13ccb20a..93f6338a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -235,7 +235,7 @@ struct DimensionError <: Exception end Base.showerror(io::IO, e::DimensionError) = - print(io, "DimensionError: $(string(e.x)) and $(string(e.y)) are not dimensionally compatible."); + print(io, "DimensionError: ", e.x, " and ", e.y, " are not dimensionally compatible."); """ struct AffineError <: Exception From c229837d4cebae8b65e28b8c0c9346dd7485c7b0 Mon Sep 17 00:00:00 2001 From: anand jain <33790004+anandijain@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:53:01 -0400 Subject: [PATCH 09/13] Update src/utils.jl Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 93f6338a..5c8df828 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -245,4 +245,4 @@ struct AffineError <: Exception x end -Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: $(string(e.x))") +Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: ", e.x) From a30242f508fb7c10f6dcebd6dc55753b4ccf70f4 Mon Sep 17 00:00:00 2001 From: contradict Date: Wed, 8 Mar 2023 08:46:37 -0800 Subject: [PATCH 10/13] Request correct mime type in tests. --- test/runtests.jl | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index ae7fa46d..63ed8b03 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1517,27 +1517,27 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @test repr("text/plain", 1.0°) == "1.0°" # Concise printing of ranges - @test repr((1:10)*u"kg/m^3") == "(1:10) kg m^-3" - @test repr((1.0:0.1:10.0)*u"kg/m^3") == "(1.0:0.1:10.0) kg m^-3" - @test repr((1:10)*°) == "(1:10)°" - @test repr(range(1.0+2.0im, length=5)*u"m") == "(1.0 + 2.0im:1.0 + 0.0im:5.0 + 2.0im) m" - @test repr(range(1+2im, step=1+1im, length=5)*u"m") == "(1 + 2im:1 + 1im:5 + 6im) m" - @test repr(StepRange((1//1)u"m", 1u"cm", (2//1)u"m")) == "(1//1:1//100:2//1) m" - @test repr(StepRangeLen(1.0u"m", 1.0u"cm", 101)) == "(1.0:0.01:2.0) m" + @test repr("text/plain", (1:10)*u"kg/m^3") == "(1:10) kg m^-3" + @test repr("text/plain", (1.0:0.1:10.0)*u"kg/m^3") == "(1.0:0.1:10.0) kg m^-3" + @test repr("text/plain", (1:10)*°) == "(1:10)°" + @test repr("text/plain", range(1.0+2.0im, length=5)*u"m") == "(1.0 + 2.0im:1.0 + 0.0im:5.0 + 2.0im) m" + @test repr("text/plain", range(1+2im, step=1+1im, length=5)*u"m") == "(1 + 2im:1 + 1im:5 + 6im) m" + @test repr("text/plain", StepRange((1//1)u"m", 1u"cm", (2//1)u"m")) == "(1//1:1//100:2//1) m" + @test repr("text/plain", StepRangeLen(1.0u"m", 1.0u"cm", 101)) == "(1.0:0.01:2.0) m" # Concise printing of affine ranges with mixed step unit - @test repr(StepRange(1u"°C", 1u"K", 3u"°C")) == "(1:3) °C" - @test repr(StepRange(1u"°C", 1.0u"K", 3u"°C")) == "(1:3) °C" - @test repr(StepRange(1.0u"°C", 1u"K", 3.0u"°C")) == "(1.0:1.0:3.0) °C" - @test repr(StepRange(1.0u"°C", 1.0u"K", 3.0u"°C")) == "(1.0:1.0:3.0) °C" - @test repr(StepRange((0//1)u"°F", 1u"K", (9//1)u"°F")) == "(0//1:9//5:9//1) °F" - @test repr(StepRangeLen{typeof(1.0u"°C"),typeof(1.0u"°C"),typeof(1u"K")}(1.0u"°C", 1u"K", 3, 1)) == "(1.0:1.0:3.0) °C" + @test repr("text/plain", StepRange(1u"°C", 1u"K", 3u"°C")) == "(1:3) °C" + @test repr("text/plain", StepRange(1u"°C", 1.0u"K", 3u"°C")) == "(1:3) °C" + @test repr("text/plain", StepRange(1.0u"°C", 1u"K", 3.0u"°C")) == "(1.0:1.0:3.0) °C" + @test repr("text/plain", StepRange(1.0u"°C", 1.0u"K", 3.0u"°C")) == "(1.0:1.0:3.0) °C" + @test repr("text/plain", StepRange((0//1)u"°F", 1u"K", (9//1)u"°F")) == "(0//1:9//5:9//1) °F" + @test repr("text/plain", StepRangeLen{typeof(1.0u"°C"),typeof(1.0u"°C"),typeof(1u"K")}(1.0u"°C", 1u"K", 3, 1)) == "(1.0:1.0:3.0) °C" @static if VERSION < v"1.5" @test_broken repr(StepRangeLen{typeof(1u"°C"),typeof(1u"°C"),typeof(1u"K")}(1u"°C", 1u"K", 3, 1)) == "(1:1:3) °C" @test_broken repr(StepRangeLen{typeof(1.0u"°F"),typeof(1.0u"°F"),typeof(1u"K")}(0.0u"°F", 1u"K", 6)) == "(0.0:1.8:9.0) °F" else - @test repr(StepRangeLen{typeof(1u"°C"),typeof(1u"°C"),typeof(1u"K")}(1u"°C", 1u"K", 3, 1)) == "(1:1:3) °C" - @test repr(StepRangeLen{typeof(1.0u"°F"),typeof(1.0u"°F"),typeof(1u"K")}(0.0u"°F", 1u"K", 6)) == "(0.0:1.8:9.0) °F" + @test repr("text/plain", StepRangeLen{typeof(1u"°C"),typeof(1u"°C"),typeof(1u"K")}(1u"°C", 1u"K", 3, 1)) == "(1:1:3) °C" + @test repr("text/plain", StepRangeLen{typeof(1.0u"°F"),typeof(1.0u"°F"),typeof(1u"K")}(0.0u"°F", 1u"K", 6)) == "(0.0:1.8:9.0) °F" end end withenv("UNITFUL_FANCY_EXPONENTS" => true) do From e2a504d9a9ac6d59de5deda73020df1a96fc8923 Mon Sep 17 00:00:00 2001 From: contradict Date: Wed, 8 Mar 2023 08:46:56 -0800 Subject: [PATCH 11/13] Correct printing for affine types. --- src/user.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user.jl b/src/user.jl index 8df784c9..875fe8d9 100644 --- a/src/user.jl +++ b/src/user.jl @@ -291,7 +291,7 @@ macro affineunit(symb, abbr, offset) s = Symbol(symb) return esc(quote Base.@__doc__ const global $s = $affineunit($offset) - $Base.show(io::$IO, ::$genericunit($s)) = $print(io, $abbr) + $Base.show(io::$IO, mime::$(MIME"text/plain"), ::$genericunit($s)) = $print(io, $abbr) end) end From d91f45aac9e354df6233abb9a3985b68828255f1 Mon Sep 17 00:00:00 2001 From: contradict Date: Wed, 8 Mar 2023 08:51:50 -0800 Subject: [PATCH 12/13] Fix complex value ranges and affine StepRangeLen. --- src/display.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 1f3d3534..074059aa 100644 --- a/src/display.jl +++ b/src/display.jl @@ -117,7 +117,7 @@ function show(io::IO, mime::MIME"text/plain", x::Quantity) end end -function show(io::IO, mime::MIME"text/plain", r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity +function show(io::IO, mime::MIME"text/plain", r::StepRange{T}) where T<:Quantity a,s,b = first(r), step(r), last(r) U = unit(a) V = absoluteunit(U) @@ -132,6 +132,17 @@ function show(io::IO, mime::MIME"text/plain", r::Union{StepRange{T},StepRangeLen show(io, mime, U) end +function show(io::IO, mime::MIME"text/plain", r::StepRangeLen{T}) where T<:Quantity + a,s,b = first(r), step(r), last(r) + U = unit(a) + V = absoluteunit(U) + print(io, '(') + show(io, mime, StepRangeLen(ustrip(U, a), ustrip(V, s), length(r))) + print(io, ')') + has_unit_spacing(U) && print(io,' ') + show(io, mime, U) +end + function show(io::IO, ::MIME"text/plain", x::typeof(NoDims)) print(io, "NoDims") end From cac626f855dd72222de5ba70c6f1ab64cca556c9 Mon Sep 17 00:00:00 2001 From: contradict Date: Wed, 8 Mar 2023 09:26:21 -0800 Subject: [PATCH 13/13] Guard or delete tests broken on old version due to ',' vs ', ' As suggested in previous PR discussion. --- test/runtests.jl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 63ed8b03..c1fda723 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1482,11 +1482,6 @@ end "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" @test string(typeof(m/s)) == "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" - else - @test string(typeof(1.0m/s)) == - "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}" - @test string(typeof(m/s)) == - "FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}" end @test string(dimension(1u"m/s")) == "𝐋 𝐓^-1" @test string(NoDims) == "NoDims" @@ -1503,17 +1498,19 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @testset "Show quantities" begin withenv("UNITFUL_FANCY_EXPONENTS" => false) do - @test repr(1.0 * u"m * s * kg^-1") == "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1.0)" + @static if VERSION ≥ v"1.6.0-DEV.770" + @test repr(1.0 * u"m * s * kg^-1") == "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1.0)" + @test repr(Foo() * u"m * s * kg^-1") == "Quantity{Foo, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1)" + @test repr((1+2im) * u"m/s") == "Quantity{Complex{Int64}, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}(1 + 2im)" + @test sprint(show, 1.0°) == "Quantity{Float64, Unitful.Dimensions{()}(), FreeUnits{(Unitful.Unit{:Degree, Unitful.Dimensions{()}()}(0, 1//1),), Unitful.Dimensions{()}(), nothing}}(1.0)" + end @test repr("text/plain", 1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1" - @test repr(Foo() * u"m * s * kg^-1") == "Quantity{Foo, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1)" @test repr("text/plain", Foo() * u"m * s * kg^-1") == "42.0 m s kg^-1" # Complex quantities - @test repr((1+2im) * u"m/s") == "Quantity{Complex{Int64}, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}(1 + 2im)" @test repr("text/plain", (1+2im) * u"m/s") == "(1 + 2im) m s^-1" # Angular degree printing #253 - @test sprint(show, 1.0°) == "Quantity{Float64, Unitful.Dimensions{()}(), FreeUnits{(Unitful.Unit{:Degree, Unitful.Dimensions{()}()}(0, 1//1),), Unitful.Dimensions{()}(), nothing}}(1.0)" @test repr("text/plain", 1.0°) == "1.0°" # Concise printing of ranges @@ -1894,7 +1891,9 @@ end @testset "> Display" begin withenv("UNITFUL_FANCY_EXPONENTS" => false) do - @test repr(3u"dB/Hz") == "Quantity{Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), FreeUnits{(Unitful.Unit{:Hertz, Unitful.Dimensions{(Unitful.Dimension{:Time}(-1//1),)}()}(0, -1//1),), Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), nothing}}(Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}(3))" + @static if VERSION ≥ v"1.6.0-DEV.770" + @test repr(3u"dB/Hz") == "Quantity{Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), FreeUnits{(Unitful.Unit{:Hertz, Unitful.Dimensions{(Unitful.Dimension{:Time}(-1//1),)}()}(0, -1//1),), Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), nothing}}(Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}(3))" + end @test repr("text/plain", 3u"dB/Hz") == "[3 dB] Hz^-1" end @test Unitful.abbr(3u"dBm") == "dBm"