Skip to content

Commit

Permalink
Fix printing of StepRangeLen with affine units (#551)
Browse files Browse the repository at this point in the history
* Fix printing of StepRangeLen with affine units

* Use `absoluteunit()` for step units

* Add tests for ranges where `unit(eltype(r)) != unit(step(r))`

* Add tests for ranges where `unit(step(r)) != absoluteunit(unit(eltype(r)))`

* Use absolute unit for checking step unit is 1

* Take care of broken tests for `StepRangeLen` before Julia 1.5
  • Loading branch information
tomyun authored Sep 13, 2022
1 parent 9151c24 commit 634071e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,' ')
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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⁻¹ᐟ²"
Expand Down

0 comments on commit 634071e

Please sign in to comment.