From 48552a9397baafd106896870b584edf24bf3026e Mon Sep 17 00:00:00 2001 From: Alberto Mengali Date: Sat, 7 Oct 2023 21:37:58 +0200 Subject: [PATCH] fix some errors and tests --- src/combine_htl/PlutoCombineHTL.jl | 2 +- src/combine_htl/helpers.jl | 6 -- src/combine_htl/show.jl | 6 +- src/combine_htl/typedef.jl | 2 +- test/combinehtl_module.jl | 94 +++++++++++++++++++++++++++++- 5 files changed, 98 insertions(+), 12 deletions(-) diff --git a/src/combine_htl/PlutoCombineHTL.jl b/src/combine_htl/PlutoCombineHTL.jl index 7d443b3..56e46d8 100644 --- a/src/combine_htl/PlutoCombineHTL.jl +++ b/src/combine_htl/PlutoCombineHTL.jl @@ -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)) diff --git a/src/combine_htl/helpers.jl b/src/combine_htl/helpers.jl index b5c173b..ab1881c 100644 --- a/src/combine_htl/helpers.jl +++ b/src/combine_htl/helpers.jl @@ -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 diff --git a/src/combine_htl/show.jl b/src/combine_htl/show.jl index 5612640..cc0782d 100644 --- a/src/combine_htl/show.jl +++ b/src/combine_htl/show.jl @@ -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, @@ -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)) diff --git a/src/combine_htl/typedef.jl b/src/combine_htl/typedef.jl index bb47340..ed19bec 100644 --- a/src/combine_htl/typedef.jl +++ b/src/combine_htl/typedef.jl @@ -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 diff --git a/test/combinehtl_module.jl b/test/combinehtl_module.jl index 71ae721..3a9f0a6 100644 --- a/test/combinehtl_module.jl +++ b/test/combinehtl_module.jl @@ -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 @@ -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 @@ -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() @@ -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"") + s = to_string(r, MIME"text/html"()) + @test contains(s, "