diff --git a/src/display.jl b/src/display.jl index 274f555e..97d82d6a 100644 --- a/src/display.jl +++ b/src/display.jl @@ -134,11 +134,12 @@ end function show(io::IO, r::StepRange{T}) where T<:Quantity a,s,b = first(r), step(r), last(r) U = unit(a) + V = absoluteunit(U) print(io, '(') - if ustrip(U, s) == 1 + if ustrip(V, s) == 1 show(io, ustrip(U, a):ustrip(U, b)) else - show(io, ustrip(U, a):ustrip(U, s):ustrip(U, b)) + show(io, ustrip(U, a):ustrip(V, s):ustrip(U, b)) end print(io, ')') has_unit_spacing(U) && print(io,' ') @@ -148,8 +149,9 @@ 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(U, s), length(r))) + show(io, StepRangeLen(ustrip(U, a), ustrip(V, s), length(r))) print(io, ')') has_unit_spacing(U) && print(io,' ') show(io, U) diff --git a/test/runtests.jl b/test/runtests.jl index 89c42243..7eed7b51 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1480,6 +1480,23 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @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" + + # 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" + @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" + end end withenv("UNITFUL_FANCY_EXPONENTS" => true) do @test repr(1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²"