Skip to content

Commit

Permalink
Define 3-arg Base.show for text/plain MIME only (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano authored Jul 5, 2024
1 parent 225f253 commit 5867fd3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/PhysicalConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function _constant_end(qname, ename, qsym, descr, val, reference, eunit)
Unitful.unit(::PhysicalConstant{_name($ename),T,D,U}) where {T,D,U} = $eunit
Unitful.dimension(::PhysicalConstant{_name($ename),T,D,U}) where {T,D,U} = D

function Base.show(io::IO, x::PhysicalConstant{_name($ename),T,D,U}) where {T,D,U}
function Base.show(io::IO, ::MIME"text/plain", x::PhysicalConstant{_name($ename),T,D,U}) where {T,D,U}
unc = uncertainty(ustrip(measurement($ename)))
println(io, $descr, " (", $qsym, ")")
println(io, "Value = ", float($ename))
Expand Down
13 changes: 9 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,29 @@ end
end

@testset "Show" begin
function plain_repr(x)
io = IOBuffer()
show(io, "text/plain", x)
return String(take!(io))
end
@test occursin(r"Speed of light in vacuum \(c_0\)
Value = 2.99792458e8 m .*
Standard uncertainty = \(exact\)
Relative standard uncertainty = \(exact\)
Reference = CODATA 2014", repr(c_0))
@test repr(α) ==
Reference = CODATA 2014", plain_repr(c_0))
@test plain_repr(α) ==
"Fine-structure constant (α)
Value = 0.0072973525664
Standard uncertainty = 1.7e-12
Relative standard uncertainty = 2.3e-10
Reference = CODATA 2014"
@test repr(e) ==
@test plain_repr(e) ==
"Elementary charge (e)
Value = 1.6021766208e-19 C
Standard uncertainty = 9.8e-28 C
Relative standard uncertainty = 6.1e-9
Reference = CODATA 2014"
@test repr(ħ) ==
@test plain_repr(ħ) ==
"Planck constant over 2pi (ħ)
Value = 1.0545718001391127e-34 J s
Standard uncertainty = 1.2891550390443523e-42 J s
Expand Down

0 comments on commit 5867fd3

Please sign in to comment.