diff --git a/src/rendering/common.jl b/src/rendering/common.jl index fd9767a..5db5fe9 100644 --- a/src/rendering/common.jl +++ b/src/rendering/common.jl @@ -6,7 +6,7 @@ set_rendering_options!(docformat::WeaveFormat; kwargs...) = return function restore_header!(doc) - (hasproperty(doc.format, :restore_header) && doc.format.restore_header) || return + (hasproperty(doc.format, :preserve_header) && doc.format.preserve_header) || return # only strips Weave headers delete!(doc.header, WEAVE_OPTION_NAME) diff --git a/test/formatter_test.jl b/test/formatter_test.jl index 1a2e1b3..11a03e8 100644 --- a/test/formatter_test.jl +++ b/test/formatter_test.jl @@ -103,13 +103,13 @@ str = """ α = 10 ``` """ -doc = mock_doc(str; doctype = "md2tex") +doc = mock_run(str; doctype = "md2tex") Weave.set_rendering_options!(doc.format) doc = Weave.render_doc(doc) @test occursin(Weave.uc2tex("α"), doc) @test !occursin("α", doc) -doc = mock_doc(str; doctype = "md2tex") +doc = mock_run(str; doctype = "md2tex") Weave.set_rendering_options!(doc.format; keep_unicode = true) doc = Weave.render_doc(doc) @test occursin("α", doc) diff --git a/test/runtests.jl b/test/runtests.jl index ba6a527..001f18a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,14 +5,26 @@ using Weave: WeaveDoc, run_doc # TODO: add test for header processsing # TODO: add test for `include_weave` -# constructs `WeaveDoc` from `String` and run it -function mock_doc(str; informat = "markdown", run = true, doctype = "md2html", kwargs...) +function mock_doc(str, informat = "markdown") f = tempname() write(f, str) - doc = WeaveDoc(f, informat) - return run ? run_doc(doc; doctype = doctype, kwargs...) : doc + return WeaveDoc(f, informat) +end +mock_run(str, informat = "markdown"; kwargs...) = run_doc(mock_doc(str, informat); kwargs...) + +function test_mock_weave(test_function, str; kwargs...) + f = tempname() + write(f, str) + f = weave(f; kwargs...) + try + weave_body = read(f, String) + test_function(weave_body) + catch + rethrow() + finally + rm(f) + end end -macro jmd_str(s) mock_doc(s) end @testset "Weave" begin diff --git a/test/test_display.jl b/test/test_display.jl index 653bf9e..ea1b8fd 100644 --- a/test/test_display.jl +++ b/test/test_display.jl @@ -3,23 +3,23 @@ @static VERSION ≥ v"1.4" && let # no limit -doc = jmd""" +doc = mock_run(""" ```julia using DataFrames DataFrame(rand(10,3)) ``` -""" +"""; doctype = "md2html") @test isdefined(doc.chunks[1], :rich_output) @test count("", doc.chunks[1].rich_output) == 12 # additonal 2 for name and type row # limit n = 100000 -doc = jmd""" +doc = mock_run(""" ```julia using DataFrames -DataFrame(rand(n,3)) +DataFrame(rand($n,3)) ``` -""" +"""; doctype = "md2html") @test isdefined(doc.chunks[1], :rich_output) @test count("", doc.chunks[1].rich_output) < n diff --git a/test/test_error_rendering.jl b/test/test_error_rendering.jl index aade8df..99130d6 100644 --- a/test/test_error_rendering.jl +++ b/test/test_error_rendering.jl @@ -35,7 +35,7 @@ err_str3_1 = get_err_str("plot(x)") err_str3_2 = get_err_str("f(y") -let doc = mock_doc(str; doctype = "github") +let doc = mock_run(str; doctype = "github") get_output(i) = doc.chunks[i].output @test occursin(err_str1, get_output(1)) @@ -44,6 +44,6 @@ let doc = mock_doc(str; doctype = "github") @test occursin(err_str3_2, get_output(3)) end -@test_throws ArgumentError mock_doc(str; doctype = "github", throw_errors = true) +@test_throws ArgumentError mock_run(str; doctype = "github", throw_errors = true) # TODO: test error rendering in `rich_output` diff --git a/test/test_header.jl b/test/test_header.jl index c8aba30..4510218 100644 --- a/test/test_header.jl +++ b/test/test_header.jl @@ -45,14 +45,14 @@ end weave_options: --- """ - @test (mock_doc(str; run = true); true) # no throw + @test (mock_run(str); true) # no throw end @testset "dynamic header specifications" begin let - d = mock_doc(""" + d = mock_run(""" --- title: No. `j 1` --- @@ -65,7 +65,7 @@ let # run in target module @eval m n = 1 - d = mock_doc(""" + d = mock_run(""" --- title: No. `j n` --- @@ -74,7 +74,7 @@ let # strip quotes by default @eval m s = "1" - d = mock_doc(""" + d = mock_run(""" --- title: No. `j s` --- @@ -119,3 +119,48 @@ let github_options = copy(weave_options) end end + + +@testset "end to end test" begin + +# preserve header +test_mock_weave(""" +--- +key: value +--- + +find_me +"""; informat = "markdown", doctype = "github") do body + @test occursin("key: \"value\"", body) + @test occursin("find_me", body) +end + +# only strips weave specific header +test_mock_weave(""" +--- +key: value +weave_options: + doctype: github +--- + +find_me +"""; informat = "markdown", doctype = "github") do body + @test occursin("key: \"value\"", body) + @test !occursin("weave_options", body) + @test occursin("find_me", body) +end + +# don't preserve header +test_mock_weave(""" +--- +weave_options: + doctype: md2html +--- + +find_me +"""; informat = "markdown", doctype = "md2html") do body + @test !occursin("weave_options", body) + @test occursin("find_me", body) +end + +end diff --git a/test/test_module_evaluation.jl b/test/test_module_evaluation.jl index 3e1fc38..bc2d3cb 100644 --- a/test/test_module_evaluation.jl +++ b/test/test_module_evaluation.jl @@ -1,6 +1,6 @@ @testset "evaluation module" begin function mock_output(str, mod = nothing) - result_doc = mock_doc(str; mod = mod) + result_doc = mock_run(str; mod = mod) return result_doc.chunks[1].output end