Skip to content

Commit

Permalink
Add show method containing type info (#52)
Browse files Browse the repository at this point in the history
* Add `show` method containing type info

* Add tests
  • Loading branch information
nickrobinson251 committed Oct 21, 2022
1 parent 42be549 commit dcd643b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ end
Base.Vector{UInt8}(s::InlineString) = Vector{UInt8}(codeunits(s))
Base.Array{UInt8}(s::InlineString) = Vector{UInt8}(codeunits(s))

Base.show(io::IO, ::MIME"text/plain", s::InlineString) = Base.print_quoted(io, s)
function Base.show(io::IO, s::InlineString) # So `repr` shows how to recreate `s`
print(io, typeof(s), "(")
Base.print_quoted(io, s)
print(io, ")")
end

# add a codeunit to end of string method
function addcodeunit(x::T, b::UInt8) where {T <: InlineString}
if T === InlineString1
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,15 @@ end
@test typeof(inline127"a") == String127
@test typeof(inline255"a") == String255
end

@testset "print/show/repr" begin
s = InlineString7("abc")
# printing
@test "$(s)x" == "abcx"
@test sprint(print, s) == sprint(print, String(s)) == "abc"
# in the repl
@test sprint(show, MIME("text/plain"), s) == sprint(show, MIME("text/plain"), String(s)) == "\"abc\""
# repr
@test sprint(show, s) == "String7(\"abc\")"
@test eval(Meta.parse(repr(s))) === s
end

0 comments on commit dcd643b

Please sign in to comment.