Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix header regression, add test, update test #363

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rendering/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/formatter_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 17 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/test_display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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("<tr>", 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("<tr>", doc.chunks[1].rich_output) < n

Expand Down
4 changes: 2 additions & 2 deletions test/test_error_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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`
53 changes: 49 additions & 4 deletions test/test_header.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
---
Expand All @@ -65,7 +65,7 @@ let

# run in target module
@eval m n = 1
d = mock_doc("""
d = mock_run("""
---
title: No. `j n`
---
Expand All @@ -74,7 +74,7 @@ let

# strip quotes by default
@eval m s = "1"
d = mock_doc("""
d = mock_run("""
---
title: No. `j s`
---
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/test_module_evaluation.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down