Repository: tlienart/JuDoc.jl
Pull request #120 (2019-04-11 05:34:07)

Tests


Patch:
diff --git a/README.md b/README.md
index 3be8a3ae..e9453fbf 100644
--- a/README.md
+++ b/README.md
@@ -82,7 +82,7 @@ The list of goals was then to build something
 * that allows latex-like commands,
 * in Julia.
 
-JuDoc is an attempt at meeting these criterion, help to make this better is always welcome!
+JuDoc is an attempt at meeting these criterion, help to make it better is always welcome!
 
 ## Installation
 
diff --git a/src/converter/html_hblocks.jl b/src/converter/html_hblocks.jl
index 47b91788..7efe271b 100644
--- a/src/converter/html_hblocks.jl
+++ b/src/converter/html_hblocks.jl
@@ -29,7 +29,7 @@ end
 """
     convert_hblock(β, allvars, fpath)
 
-Helper function to process an individual block when the block is a `HIfDef` such as `{{ ifdef
+Helper function to process an individual block when the block is a `HIsDef` such as `{{ ifdef
 author }} {{ fill author }} {{ end }}`. Which checks if a variable exists and if it does, applies
 something.
 """
diff --git a/src/converter/html_hfuns.jl b/src/converter/html_hfuns.jl
index 6df4a618..54c1e712 100644
--- a/src/converter/html_hfuns.jl
+++ b/src/converter/html_hfuns.jl
@@ -26,7 +26,7 @@ function convert_hblock(β::HFun, allvars::Dict, ::AbstractString="")
 
     # if here, then the function name is unknown, warn and ignore
     @warn "I found a function block '{{$fn ...}}' but I don't recognise this function name. Ignoring."
-    return β.ss
+    return ""
 end
 
 
diff --git a/src/converter/lx.jl b/src/converter/lx.jl
index 1c20ce9d..3e322e85 100644
--- a/src/converter/lx.jl
+++ b/src/converter/lx.jl
@@ -164,7 +164,7 @@ function resolve_input_hlcode(fname::AbstractString, lang::AbstractString; use_h
         highlight(io_out, MIME("text/html"), String(take!(io_in)), lexer)
         return String(take!(io_out))
     end
-    return "<pre><code $lang>$(String(take!(io_in)))</code></pre>"
+    return "<pre><code class=\"language-$lang\">$(String(take!(io_in)))</code></pre>"
 end
 
 """
@@ -175,7 +175,7 @@ also [`resolve_input`](@ref).
 """
 function resolve_input_othercode(fname::AbstractString, lang::AbstractString)
     fp = check_input_fname(fname)
-    return "<pre><code $lang>$(read(fp, String))</code></pre>"
+    return "<pre><code class=\"language-$lang\">$(read(fp, String))</code></pre>"
 end
 
 """
@@ -185,7 +185,8 @@ Internal function to read the raw output of the execution of a file and display
 See also [`resolve_input`](@ref).
 """
 function resolve_input_plainoutput(fname::AbstractString)
-    fp = check_input_fname(fname)
+    # will throw an error if fname doesn't exist as a script
+    check_input_fname(fname)
     # find a file in output that has the same root name
     d, fn = splitdir(fname)
     fn, _ = splitext(fn)
diff --git a/src/misc_utils.jl b/src/misc_utils.jl
index 8fb539af..80a2febb 100644
--- a/src/misc_utils.jl
+++ b/src/misc_utils.jl
@@ -57,6 +57,7 @@ function time_it_took(start)
                             "$(round(comp_time*1000; digits=1))ms"
     mess = "[done $mess]"
     println(mess)
+    return nothing
 end
 
 


Test patch:
diff --git a/test/converter/html.jl b/test/converter/html.jl
index d098f479..241ff37f 100644
--- a/test/converter/html.jl
+++ b/test/converter/html.jl
@@ -16,28 +16,60 @@
         final text
         """
 
-    tokens = JuDoc.find_tokens(hs, JuDoc.HTML_TOKENS, JuDoc.HTML_1C_TOKENS)
-    hblocks, tokens = JuDoc.find_all_ocblocks(tokens, J.HTML_OCB)
-    qblocks = JuDoc.qualify_html_hblocks(hblocks)
-    cblocks, qblocks = JuDoc.find_html_cblocks(qblocks)
-    cdblocks, qblocks = JuDoc.find_html_cdblocks(qblocks)
-    hblocks = JuDoc.merge_blocks(qblocks, cblocks, cdblocks)
-    convhbs = [JuDoc.convert_hblock(hb, allvars) for hb ∈ hblocks]
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
+    cblocks, qblocks = J.find_html_cblocks(qblocks)
+    cdblocks, qblocks = J.find_html_cdblocks(qblocks)
+    hblocks = J.merge_blocks(qblocks, cblocks, cdblocks)
+    convhbs = [J.convert_hblock(hb, allvars) for hb ∈ hblocks]
     @test convhbs[1] == "INPUT1"
     @test convhbs[2] == "\nother stuff\n"
-    @test JuDoc.convert_html(hs, allvars) == "Some text then INPUT1 and\n\nother stuff\n\nfinal text\n"
+    @test J.convert_html(hs, allvars) == "Some text then INPUT1 and\n\nother stuff\n\nfinal text\n"
+
+    hs = raw"""
+        abc {{isdef no}}yada{{end}} def
+        """
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
+    cblocks, qblocks = J.find_html_cblocks(qblocks)
+    cdblocks, qblocks = J.find_html_cdblocks(qblocks)
+    hblocks = J.merge_blocks(qblocks, cblocks, cdblocks)
+    @test J.convert_hblock(hblocks[1], allvars) == ""
+
+    hs = raw"""abc {{ fill nope }} ... """
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
+    @test (@test_logs (:warn, "I found a '{{fill nope}}' but I do not know the variable 'nope'. Ignoring.") J.convert_hblock(qblocks[1], allvars)) == ""
+
+    hs = raw"""
+        unknown function {{ unknown fun }} and see.
+        """
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
+    @test qblocks[1] isa J.HFun
+    @test (@test_logs (:warn, "I found a function block '{{unknown ...}}' but I don't recognise this function name. Ignoring.") J.convert_hblock(qblocks[1], allvars)) == ""
 end
 
 
 @testset "h-insert" begin
     # Julia 0.7 complains if there's no global here.
-    global temp_rnd = joinpath(JuDoc.JD_PATHS[:in_html], "temp.rnd")
+    global temp_rnd = joinpath(J.JD_PATHS[:in_html], "temp.rnd")
     write(temp_rnd, "some random text to insert")
     hs = raw"""
         Trying to insert: {{ insert temp.rnd }} and see.
         """
     allvars = Dict{String, Pair{Any, Tuple}}()
-    @test JuDoc.convert_html(hs, allvars) == "Trying to insert: some random text to insert and see.\n"
+    @test J.convert_html(hs, allvars) == "Trying to insert: some random text to insert and see.\n"
+
+    hs = raw"""Trying to insert: {{ insert nope.rnd }} and see."""
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
+    @test (@test_logs (:warn, "I found an {{insert ...}} block and tried to insert '$(joinpath(J.JD_PATHS[:in_html], "nope.rnd"))' but I couldn't find the file. Ignoring.") J.convert_hblock(qblocks[1], allvars)) == ""
 end
 
 
@@ -47,7 +79,7 @@ end
         "date_format" => "U dd, yyyy" => (String,),
         "isnotes" => true => (Bool,))
     hs = "foot {{if isnotes}} {{fill author}}{{end}}"
-    rhs = JuDoc.convert_html(hs, allvars)
+    rhs = J.convert_html(hs, allvars)
     @test rhs == "foot  Stefan Zweig"
 end
 
@@ -58,10 +90,10 @@ end
         "date_format" => "U dd, yyyy" => (String,),
         "isnotes" => true => (Bool,))
     hs = "foot {{isdef author}} {{fill author}}{{end}}"
-    rhs = JuDoc.convert_html(hs, allvars)
+    rhs = J.convert_html(hs, allvars)
     @test rhs == "foot  Stefan Zweig"
     hs2 = "foot {{isnotdef blogname}}hello{{end}}"
-    rhs = JuDoc.convert_html(hs2, allvars)
+    rhs = J.convert_html(hs2, allvars)
     @test rhs == "foot hello"
 end
 
@@ -71,7 +103,7 @@ end
         "date_format" => "U dd, yyyy" => (String,),
         "isnotes" => true => (Bool,))
     hs = "foot <!-- {{ fill blahblah }} {{ if v1 }} --> {{isdef author}} {{fill author}}{{end}}"
-    rhs = JuDoc.convert_html(hs, allvars)
+    rhs = J.convert_html(hs, allvars)
     @test rhs == "foot <!-- {{ fill blahblah }} {{ if v1 }} -->  Stefan Zweig"
 end
 
@@ -81,7 +113,7 @@ end
         "b1" => false => (Bool,),
         "b2" => true => (Bool,))
 
-    jdc = x->JuDoc.convert_html(x, allvars)
+    jdc = x->J.convert_html(x, allvars)
 
     # flag b1 is false
     @test "{{if b1}} blah {{ else }} blih {{ end }}" |> jdc == " blih " # else
@@ -108,9 +140,9 @@ end
         {{isnotpage /src/blah.html /src/ya/xx}} blih {{end}} done.
         """
 
-    tokens = JuDoc.find_tokens(hs, JuDoc.HTML_TOKENS, JuDoc.HTML_1C_TOKENS)
-    hblocks, tokens = JuDoc.find_all_ocblocks(tokens, J.HTML_OCB)
-    qblocks = JuDoc.qualify_html_hblocks(hblocks)
+    tokens = J.find_tokens(hs, J.HTML_TOKENS, J.HTML_1C_TOKENS)
+    hblocks, tokens = J.find_all_ocblocks(tokens, J.HTML_OCB)
+    qblocks = J.qualify_html_hblocks(hblocks)
 
     @test qblocks[1] isa J.HIsPage
     @test qblocks[1].pages[1] == "/index.html"
@@ -119,11 +151,11 @@ end
     @test qblocks[3].pages[1] == "/src/blah.html"
     @test qblocks[3].pages[2] == "/src/ya/xx"
 
-    cblocks, qblocks = JuDoc.find_html_cblocks(qblocks)
+    cblocks, qblocks = J.find_html_cblocks(qblocks)
     @test isempty(cblocks)
-    cdblocks, qblocks = JuDoc.find_html_cdblocks(qblocks)
+    cdblocks, qblocks = J.find_html_cdblocks(qblocks)
     @test isempty(cblocks)
-    cpblocks, qblocks = JuDoc.find_html_cpblocks(qblocks)
+    cpblocks, qblocks = J.find_html_cpblocks(qblocks)
 
     @test isempty(qblocks)
     @test cpblocks[1].checkispage == true
@@ -132,15 +164,20 @@ end
     @test cpblocks[2].checkispage == false
     @test cpblocks[2].pages[1] == "/src/blah.html"
 
-    hblocks = JuDoc.merge_blocks(qblocks, cblocks, cdblocks, cpblocks)
+    hblocks = J.merge_blocks(qblocks, cblocks, cdblocks, cpblocks)
 
     @test hblocks[1] isa J.HCondPage
     @test hblocks[1].pages == cpblocks[1].pages
 
-    convhbs = [JuDoc.convert_hblock(hb, allvars, "/index.md") for hb ∈ hblocks]
+    @test J.convert_hblock(hblocks[1], allvars, "/index.md") == " blah "
+    @test J.convert_hblock(hblocks[1], allvars, "/indosdfx.md") == ""
+    @test J.convert_hblock(hblocks[2], allvars, "/index.md") == " blih "
+    @test J.convert_hblock(hblocks[2], allvars, "/src/blah.html") == ""
+
+    convhbs = [J.convert_hblock(hb, allvars, "/index.md") for hb ∈ hblocks]
 
     @test convhbs[1] == " blah " # condition is met
     @test convhbs[2] == " blih " # condition is met
 
-    @test JuDoc.convert_html(hs, allvars, joinpath(J.JD_PATHS[:in], "index.md")) == "Some text then  blah  but\n blih  done.\n"
+    @test J.convert_html(hs, allvars, joinpath(J.JD_PATHS[:in], "index.md")) == "Some text then  blah  but\n blih  done.\n"
 end
diff --git a/test/converter/hyperref.jl b/test/converter/hyperref.jl
index 0463ada2..e03353f7 100644
--- a/test/converter/hyperref.jl
+++ b/test/converter/hyperref.jl
@@ -64,3 +64,31 @@ end
 
     m == "<p>Then something like  \$\$\\begin{array}{c}  \\mathbb E\\left[ f(X)\\right] \\in \\mathbb R &\\text{if}& f:\\mathbb R\\maptso\\mathbb R\\end{array}\$\$ and then  <a id=\"$h1\"></a>\$\$\\begin{array}{c}  1+1 &=&2 \\end{array}\$\$ but further  <a id=\"$h2\"></a>\$\$\\begin{array}{c}  1 &=& 1 \\end{array}\$\$ and finally a  <span class=\"eqref)\">({{href EQR $h1}})</span> and maybe  <span class=\"eqref)\">({{href EQR $h2}})</span>.</p>\n"
 end
+
+
+@testset "Input" begin
+    #
+    # check_input_fname
+    #
+    script1 = joinpath(J.JD_PATHS[:scripts], "script1.jl")
+    write(script1, "1+1")
+    @test J.check_input_fname("script1.jl") == script1
+    @test_throws ArgumentError J.check_input_fname("script2.jl")
+
+    #
+    # resolve_input_hlcode
+    #
+    r = J.resolve_input_hlcode("script1.jl", "julia", use_hl=false)
+    r2 = J.resolve_input_othercode("script1.jl", "julia")
+    @test r == "<pre><code class=\"language-julia\">1+1\n</code></pre>"
+    @test r2 == "<pre><code class=\"language-julia\">1+1</code></pre>"
+
+    #
+    # resolve_input_plainoutput
+    #
+    plain1 = joinpath(J.JD_PATHS[:scripts], "output", "script1.zog")
+    write(plain1, "2")
+
+    r = J.resolve_input_plainoutput("script1.jl")
+    @test r == "<pre id=\"judoc-out-plain\"><code>2</code></pre>"
+end
diff --git a/test/jd_paths_vars.jl b/test/jd_paths_vars.jl
index bea5eca6..c4f361fd 100644
--- a/test/jd_paths_vars.jl
+++ b/test/jd_paths_vars.jl
@@ -7,13 +7,15 @@ J.def_GLOB_LXDEFS()
 @testset "Paths" begin
     P = J.set_paths!()
 
-    @test J.JD_PATHS[:f] == td
-    @test J.JD_PATHS[:in] == joinpath(td, "src")
-    @test J.JD_PATHS[:in_css] == joinpath(td, "src", "_css")
+    @test J.JD_PATHS[:f]       == td
+    @test J.JD_PATHS[:in]      == joinpath(td, "src")
+    @test J.JD_PATHS[:in_css]  == joinpath(td, "src", "_css")
     @test J.JD_PATHS[:in_html] == joinpath(td, "src", "_html_parts")
-    @test J.JD_PATHS[:libs] == joinpath(td, "libs")
-    @test J.JD_PATHS[:out] == joinpath(td, "pub")
+    @test J.JD_PATHS[:libs]    == joinpath(td, "libs")
+    @test J.JD_PATHS[:out]     == joinpath(td, "pub")
     @test J.JD_PATHS[:out_css] == joinpath(td, "css")
+    @test J.JD_PATHS[:scripts] == joinpath(td, "assets", "scripts")
+
     @test P == J.JD_PATHS
 
     mkdir(J.JD_PATHS[:in])
@@ -21,6 +23,8 @@ J.def_GLOB_LXDEFS()
     mkdir(J.JD_PATHS[:libs])
     mkdir(J.JD_PATHS[:in_css])
     mkdir(J.JD_PATHS[:in_html])
+    mkpath(J.JD_PATHS[:scripts])
+    mkpath(joinpath(J.JD_PATHS[:scripts], "output"))
 end
 
 @testset "Set vars" begin