Skip to content

Commit

Permalink
fix some errors and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
disberd committed Oct 7, 2023
1 parent fd82d7e commit 48552a9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/combine_htl/PlutoCombineHTL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module HelperFunctions
_ex_names = (
:shouldskip, :haslisteners, :hasreturn, :returned_element,
:script_id, :add_pluto_compat, :hasinvalidation, :plutodefault,
:displaylocation, :children, :inner_node, :getfirst,
:displaylocation, :children, :inner_node,
)
for n in _ex_names
eval(:(import ..PlutoCombineHTL: $n))
Expand Down
6 changes: 0 additions & 6 deletions src/combine_htl/helpers.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
function getfirst(f, iterable)
@inbounds for iter in iterable
f(iter) && return iter
end
end

# Basics
plutodefault(::Union{InsidePluto, InsideAndOutsidePluto}) = true
plutodefault(::OutsidePluto) = false
Expand Down
6 changes: 3 additions & 3 deletions src/combine_htl/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function _iterate_scriptcontents(io::IO, iter, location::SingleDisplayLocation,
# We cycle through the DualScript iterable
pluto = plutodefault(location)
for pts in iter
kwargs = if _eltype(pts) isa Script
kwargs = if _eltype(pts) <: Script
(;
pluto,
kind,
Expand Down Expand Up @@ -131,12 +131,12 @@ function print_html(io::IO, n::NonScript{L}; pluto = plutodefault(n)) where L <:
return
end
print_html(io::IO, dn::DualNode; pluto = plutodefault(dn)) = print_html(io, inner_node(dn, displaylocation(pluto)); pluto)
function print_html(io::IO, cn::CombinedNodes; pluto = plutodefault(io))
function print_html(io::IO, cn::CombinedNodes; pluto = is_inside_pluto(io))
for n in children(cn)
print_html(io, n; pluto)
end
end
print_html(io::IO, swph::ShowWithPrintHTML; pluto = plutodefault(io)) = print_html(io, swph.el; pluto)
print_html(io::IO, swph::ShowWithPrintHTML; pluto = plutodefault(io, swph)) = print_html(io, swph.el; pluto)
# Catchall method reverting to show text/javascript
print_html(io::IO, x; kwargs...) = (@nospecialize; show(io, MIME"text/html"(), x))

Expand Down
2 changes: 1 addition & 1 deletion src/combine_htl/typedef.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ struct CombinedScripts <: Script{InsideAndOutsidePluto}
returned_element === missing && return new(v)
final_v = if return_idx === 0
# We have to add a DualScript that just returns the element
new_v = vcat(v, DualScript(""; returned_element))
new_v = vcat(v, DualScript(""; returned_element) |> PrintToScript)
else
new_v = copy(v)
dn = new_v[return_idx].el
Expand Down
94 changes: 93 additions & 1 deletion test/combinehtl_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,20 @@ load_notebook, Configuration
@test returned_element(ds3, InsidePluto()) === "asd"
@test returned_element(ds3, OutsidePluto()) === "boh"

cs = make_script([
PrintToScript(3)
]; returned_element = "asd")
@test last(children(cs)).el isa DualScript

@test make_script(:outside, ds2) === ds2.outside_pluto
@test make_script(:Inside, ds2) === ds2.inside_pluto

pts = PrintToScript(3)
@test make_script(pts) === pts
@test make_node(pts) isa CombinedScripts

@test PrintToScript(pts) === pts
@test_throws "You can't wrap" PrintToScript(PlutoNode("asd"))
end

@testset "make_node" begin
Expand All @@ -135,9 +147,14 @@ end
"lol"
])
@test cn isa CombinedNodes
@test CombinedNodes(cn) === cn
@test length(children(cn)) === 2 # We skipped the second empty element
@test make_node(cn) === cn

cn = CombinedNodes(dn)
dn_test = cn.children[1].el
@test dn_test == dn

@test PlutoNode(dn.inside_pluto) === dn.inside_pluto
@test PlutoNode(@htl("")).empty === true

Expand All @@ -156,6 +173,7 @@ end
dn = DualNode("asd", "lol")
@test inner_node(dn, InsidePluto()) == pn
@test inner_node(dn, OutsidePluto()) == nn
@test DualNode(dn) === dn

function compare_content(n1, n2; pluto = missing)
io1 = IOBuffer()
Expand Down Expand Up @@ -267,15 +285,52 @@ end
swp2 = ShowWithPrintHTML(make_node("asd"); display_type = :both) # :both is the default
@test plutodefault(io, swp1) === plutodefault(swp1)
@test plutodefault(io, swp2) === is_inside_pluto(io) !== plutodefault(swp1)

@test displaylocation(PrintToScript(3)) === InsideAndOutsidePluto()
@test displaylocation(PrintToScript(PlutoScript("asd"))) === InsidePluto()

swph = ShowWithPrintHTML(3)
@test PlutoCombineHTL._eltype(swph) === Int
@test shouldskip(swph, InsidePluto()) === false

swph = ShowWithPrintHTML(3; display_type = :outside)
@test shouldskip(swph, InsidePluto()) === true

@test add_pluto_compat(3) === false
@test hasinvalidation(3) === false

@test haslisteners(PrintToScript(3)) === false
@test hasreturn(PrintToScript(3)) === false

@test script_id(PlutoScript("asd"), OutsidePluto()) === missing
pts = PrintToScript(3)
@test script_id(pts) === missing
end

@testset "Show methods" begin
ps = PlutoScript("asd")
ps = PlutoScript("asd", "lol")
s = to_string(ps, MIME"text/javascript"())
hs = to_string(ps, MIME"text/html"())
@test contains(s, r"JS Listeners .* PlutoDevMacros") === false # No listeners helpers should be added
@test contains(s, "invalidation.then(() => {\nlol") === true # Test that the invaliation script is printed
@test contains(hs, r"<script id='\w+'") === true

struct ASD end
function PlutoCombineHTL.print_javascript(io::IO, ::ASD; pluto)
if pluto
println(io, "ASD_PLUTO")
else
println(io, "ASD_NONPLUTO")
end
end

cs = make_script([ASD() |> PrintToScript])
s_in = to_string(cs, MIME"text/javascript"(); pluto = true)
s_out = to_string(cs, MIME"text/javascript"(); pluto = false)
@test contains(s_in, "ASD_PLUTO")
@test contains(s_out, "ASD_NONPLUTO")


ds = DualScript("addScriptEventListeners('lol')", "magic"; id = "custom_id")
s_in = to_string(ds, MIME"text/javascript"(); pluto = true)
s_out = to_string(ds, MIME"text/javascript"(); pluto = false)
Expand All @@ -290,7 +345,31 @@ end
@test_throws "Interpolation of `Script` subtypes is not allowed" HypertextLiteral.print_script(IOBuffer(), ds)
# Test error with show javascript ShowWithPrintHTML
@test_throws "not supposed to be shown with mime 'text/javascript'" show(IOBuffer(), MIME"text/javascript"(), make_html("asd"))
# Test error when trying to print invalidation of a NormalScript
pts = PrintToScript(NormalScript("lol"))
@test_throws "can't print invalidation" print_javascript(IOBuffer(), pts; pluto = false, kind = :invalidation)
# Test that print_javascript goes to Base.show with mime text/javascript by default
@test_throws r"matching show\(.*::Missing" print_javascript(IOBuffer(), missing)
# Test that print_javascript goes to Base.show with mime text/javascript by default
@test_throws r"matching show\(.*::Missing" print_html(IOBuffer(), missing)
# Test that to_string only supports text/html and text/javascript
@test_throws "Unsupported mime" to_string(1, MIME"text/plain"())

n = NormalNode("asd")
# Test that interpolation of nodes in @htl works
r = @htl("$n")
s = to_string(r, MIME"text/html"())
@test contains(s, "asd")

cn = make_node([n])
s = to_string(cn, MIME"text/html"(); pluto = false)
@test contains(s, "asd")

# Test that interpolation of ScriptContent inside @htl <script> tags work
sc = ScriptContent("asd")
r = @htl("<script>$([sc])</script>")
s = to_string(r, MIME"text/html"())
@test contains(s, "<script>asd")

# Show outside Pluto
# PlutoScript should be empty when shown out of Pluto
Expand Down Expand Up @@ -351,6 +430,19 @@ end
@test formatted_code(ds) == formatted_code(ds, MIME"text/html"())
sc = ScriptContent("asd")
@test formatted_code(sc) == formatted_code(sc, MIME"text/javascript"())
s = let
io = IOBuffer()
show(io, MIME"text/html"(), sc)
String(take!(io))
end
@test contains(s, "<div class=\"markdown\"")
s = let
io = IOBuffer()
n = NormalNode("lol")
show(io, MIME"text/html"(), n; pluto = false)
String(take!(io))
end
@test contains(s, "lol")
end

function noerror(cell; verbose=true)
Expand Down

0 comments on commit 48552a9

Please sign in to comment.