Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round Trip Printing. update tests for repr and show methods #470

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests
anandijain committed Jul 17, 2021
commit 2a9717f2d28018df23bd99e0ceb42e7b51f4c32c
52 changes: 29 additions & 23 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1291,21 +1291,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}}"
anandijain marked this conversation as resolved.
Show resolved Hide resolved
@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}}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and some other tests) would need to be changed to match the old printing of type parameters (no spaces after commas), that’s why that @static if VERSION ≥ v"1.6.0-DEV.770" exists. But IMO we could just delete these tests since the printing isn’t customized anymore.

@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}()"
anandijain marked this conversation as resolved.
Show resolved Hide resolved
end
end

@@ -1315,31 +1315,37 @@ 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
@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("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)°"
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
anandijain marked this conversation as resolved.
Show resolved Hide resolved
end
end

@testset "DimensionError message" begin
@@ -1349,11 +1355,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."
anandijain marked this conversation as resolved.
Show resolved Hide resolved
@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
@@ -1673,12 +1679,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°"
anandijain marked this conversation as resolved.
Show resolved Hide resolved
end

@testset "> Thanks for signing up for Log Facts!" begin