From 6955ca6be29eee09e20ea74535f396fedd39bb18 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sat, 16 May 2020 01:49:55 +0900 Subject: [PATCH] add test for header processing --- test/runtests.jl | 4 +- test/test_header.jl | 110 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 168f671..8ad1a4d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,5 @@ using Weave, Test -using Weave: run_doc +using Weave: WeaveDoc, run_doc # TODO: add test for header processsing @@ -9,7 +9,7 @@ using Weave: run_doc function mock_doc(str, format = "markdown") f = tempname() write(f, str) - return Weave.WeaveDoc(f, format) + return WeaveDoc(f, format) end diff --git a/test/test_header.jl b/test/test_header.jl index 4b20206..f6b3cfa 100644 --- a/test/test_header.jl +++ b/test/test_header.jl @@ -1,26 +1,114 @@ -using Weave.YAML +using Weave: separate_header_text, parse_header, specific_options! -# TODO: -# - header stripping -# - header parsing from a Weave document +# TODO: add test for header restoring (strip) + +@testset "header separation" begin + +header_body = """ +options: + foo: bar +""" + +let + header_text = "---\n$header_body---" + f, l = separate_header_text("$header_text") + @test occursin(header_body, f) + @test isempty(l) +end + +let + doc_body = "hogehoge" + header_text = "---\n$header_body---\n$doc_body" + f, l = separate_header_text("$header_text") + @test occursin(header_body, f) + @test occursin(doc_body, l) +end + +let + slide_body = """ + --- + slide comes here ! + --- + """ + header_text = "---\n$header_body---\n$slide_body" + f, l = separate_header_text("$header_text") + @test occursin(header_body, f) + @test occursin(slide_body, l) +end + +end -header = YAML.load(""" +@testset "dynamic header specifications" begin + +let + d = mock_doc(""" + --- + title: No. `j 1` + --- + """) + run_doc(d) + @test d.header["title"] == "No. 1" +end + +let + m = Core.eval(Main, :(module $(gensym(:WeaveTest)) end)) + + # run in target module + @eval m n = 1 + d = mock_doc(""" + --- + title: No. `j n` + --- + """) + run_doc(d; mod = m) + @test d.header["title"] == "No. 1" + + # strip quotes by default + @eval m s = "1" + d = mock_doc(""" + --- + title: No. `j s` + --- + """) + run_doc(d; mod = m) + @test d.header["title"] == "No. 1" # otherwise `"No. "1""` +end + +end + + +@testset "doctype specific header configuration" begin + +header = parse_header(""" --- options: - out_path: reports + out_path: reports # should be overwrote md2html: out_path : html/ md2pdf: out_path : pdf/ github: out_path : md/ - fig_ext : .png + fig_ext : .png # should remain --- """) -let args = header[Weave.WEAVE_OPTION_NAME] - @test Weave.combine_args(args, "md2html") == Dict("fig_ext" => ".png", "out_path" => "html/") - @test Weave.combine_args(args, "github") == Dict("fig_ext" => ".png", "out_path" => "md/") - @test Weave.combine_args(args, "pandoc") == Dict("fig_ext" => ".png", "out_path" => "reports") +weave_options = header[Weave.WEAVE_OPTION_NAME] + +let md2html_options = copy(weave_options) + specific_options!(md2html_options, "md2html") + @test md2html_options == Dict("fig_ext" => ".png", "out_path" => "html/") +end + +let md2pdf_options = copy(weave_options) + specific_options!(md2pdf_options, "md2pdf") + @test md2pdf_options == Dict("fig_ext" => ".png", "out_path" => "pdf/") +end + +let github_options = copy(weave_options) + specific_options!(github_options, "github") + @test github_options == Dict("fig_ext" => ".png", "out_path" => "md/") +end + end