From 3adb13d9feb6785bd9774a1c0e9df490ff8b903f Mon Sep 17 00:00:00 2001 From: Michael Hatherly Date: Sun, 4 Sep 2016 22:49:54 +0200 Subject: [PATCH] Evaluate blocks as :toplevel Fixes issue on Julia 0.4 where some blocks containing `using` expressions would throw errors when evaluated. --- src/DocChecks.jl | 6 +++++- test/examples/src/index.md | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/DocChecks.jl b/src/DocChecks.jl index d997da3807..709c5fefc3 100644 --- a/src/DocChecks.jl +++ b/src/DocChecks.jl @@ -117,7 +117,11 @@ end function doctest(block::Markdown.Code, meta::Dict, doc::Documents.Document, page) if block.language == "jldoctest" code, sandbox = replace(block.code, "\r\n", "\n"), Module(:Main) - haskey(meta, :DocTestSetup) && eval(sandbox, meta[:DocTestSetup]) + if haskey(meta, :DocTestSetup) + expr = meta[:DocTestSetup] + Meta.isexpr(expr, :block) && (expr.head = :toplevel) + eval(sandbox, expr) + end if ismatch(r"^julia> "m, code) eval_repl(code, sandbox, meta, doc, page) block.language = "jlcon" diff --git a/test/examples/src/index.md b/test/examples/src/index.md index 38c9367bfb..6d19ca2dc1 100644 --- a/test/examples/src/index.md +++ b/test/examples/src/index.md @@ -32,6 +32,15 @@ zeros(5, 5) zeros(50, 50) ``` +```@meta +DocTestSetup = quote + using Base + x = -3:0.01:3 + y = -4:0.02:5 + z = [Float64((xi^2 + yi^2)) for xi in x, yi in y] +end +``` + ```jldoctest julia> [1.0, 2.0, 3.0] 3-element Array{Float64,1}: