Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
better mime handling, tests, and deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
pfitzseb committed Jul 25, 2018
1 parent 1336cc8 commit bf09f07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/TreeViews.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__precompile__(true)

module TreeViews

"""
Expand All @@ -21,16 +23,20 @@ numberofnodes(x::T) where {T} = fieldcount(T)
Prints `x`'s tree header to `io`.
"""
treelabel(io::IO, x::T, mime::MIME"text/plain" = MIME"text/plain"()) where {T} = show(io, mime, T)
treelabel(io::IO, x::T, mime::MIME"text/plain") where {T} = show(io, mime, T)
treelabel(io::IO, x::T, mime::AbstractString) where {T} = treelabel(io, x, MIME(mime))
treelabel(io::IO, x::T) where {T} = treelabel(io, x, MIME"text/plain"())

"""
nodelabel(io::IO, x::T, i::Integer, mime::MIME"text/plain" = MIME"text/plain"())
Prints the label of `x`'s `i`-th child to `io`.
"""
function nodelabel(io::IO, x::T, i::Integer, mime::MIME"text/plain" = MIME"text/plain"()) where {T}
function nodelabel(io::IO, x::T, i::Integer, mime::MIME"text/plain") where {T}
show(io, mime, Text(String(fieldname(T, i))))
end
nodelabel(io::IO, x::T, i::Integer, mime::AbstractString) where {T} = nodelabel(io, x, i, MIME(mime))
nodelabel(io::IO, x::T, i::Integer) where {T} = nodelabel(io, x, i, MIME"text/plain"())

"""
treenode(x::T, i::Integer)
Expand All @@ -40,7 +46,8 @@ the corresponding `treelabel`.
"""
treenode(x::T, i::Integer) where {T} = getfield(x, fieldname(T, i))

@deprecate(treelabel(io::IO, x::T, i::Integer, mime::MIME"text/plain" = MIME"text/plain"()) where {T},
@deprecate(treelabel(io::IO, x::T, i::Integer, mime) where {T},
nodelabel(io, x, i, mime))
@deprecate(treelabel(io::IO, x::T, i::Integer) where {T}, nodelabel(io, x, i))

end # module
16 changes: 13 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,31 @@ teststruct = TVT_customized(1, "asd", UInt8(2), 22.22)
hastreeview(::TVT_customized) = true

numberofnodes(x::TVT_customized) = 2
treelabel(io::IO, x::TVT_customized) = print(io, "customized")
function nodelabel(io::IO, x::TVT_customized, i::Integer)
treelabel(io::IO, x::TVT_customized, ::MIME"text/plain") = print(io, "customized")
treelabel(io::IO, x::TVT_customized, ::MIME"text/html") = print(io, "HTML")

function nodelabel(io::IO, x::TVT_customized, i::Integer, ::MIME"text/plain")
i <= 2 || throw(BoundsError(x, i))
print(io, "customized$i")
end
function nodelabel(io::IO, x::TVT_customized, i::Integer, ::MIME"text/html")
i <= 2 || throw(BoundsError(x, i))
print(io, "HTML$i")
end

function treenode(x::TVT_customized, i::Integer)
i == 1 && return x.a
i == 2 && return TVT_default(x.b, x.c, x.c, x.d)
end

@test numberofnodes(teststruct) == 2
@test sprint(io -> treelabel(io, teststruct)) == "customized"
@test sprint(io -> treelabel(io, teststruct, "text/html")) == "HTML"
@test sprint(io -> treelabel(io, teststruct, MIME"text/html"())) == "HTML"
@test_throws BoundsError sprint(io -> nodelabel(io, teststruct, 3))
@test sprint(io -> nodelabel(io, teststruct, 2)) == "customized2"
@test treenode(teststruct, 1) == teststruct.a
@test treenode(teststruct, 2) == TVT_default(teststruct.b, teststruct.c, teststruct.c, teststruct.d)

@test_deprecated treelabel(IOBuffer(), teststruct, 1)
@test_deprecated(@test sprint(io -> treelabel(io, teststruct, 1)) == "customized1")
@test_deprecated(@test sprint(io -> treelabel(io, teststruct, 1, "text/html")) == "HTML1")

0 comments on commit bf09f07

Please sign in to comment.