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

Doctest output starting with # #1369

Merged
merged 8 commits into from
Aug 2, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Documenter.jl changelog

## Version `v0.25.2`

* ![Bugfix][badge-bugfix] REPL doctest output lines starting with `#` right after the input code part are now correctly treated as being part of the output (unless prepended with 7 spaces, in line with the standard heuristic). ([#1369][github-1369])

## Version `v0.25.1`

* ![Enhancement][badge-enhancement] When automatically determining the page list (i.e. `pages` is not passed to `makedocs`), Documenter now lists `index.md` before other pages. ([#1355][github-1355])
Expand Down Expand Up @@ -609,6 +613,7 @@
[github-1360]: https://github.com/JuliaDocs/Documenter.jl/pull/1360
[github-1365]: https://github.com/JuliaDocs/Documenter.jl/pull/1365
[github-1368]: https://github.com/JuliaDocs/Documenter.jl/pull/1368
[github-1369]: https://github.com/JuliaDocs/Documenter.jl/pull/1369

[documenterlatex]: https://github.com/JuliaDocs/DocumenterLaTeX.jl
[documentermarkdown]: https://github.com/JuliaDocs/DocumenterMarkdown.jl
Expand Down
12 changes: 5 additions & 7 deletions src/DocTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,18 @@ end

const PROMPT_REGEX = r"^julia> (.*)$"
const SOURCE_REGEX = r"^ (.*)$"
const ANON_FUNC_DECLARATION = r"#[0-9]+ \(generic function with [0-9]+ method(s)?\)"

function repl_splitter(code)
lines = split(string(code, "\n"), '\n')
input = String[]
output = String[]
buffer = IOBuffer()
buffer = IOBuffer() # temporary buffer for doctest inputs and outputs
found_first_prompt = false
while !isempty(lines)
line = popfirst!(lines)
# REPL code blocks may contain leading lines with comments. Drop them.
# TODO: handle multiline comments?
# ANON_FUNC_DECLARATION deals with `x->x` -> `#1 (generic function ....)` on 0.7
# TODO: Remove this special case and just disallow lines with comments?
startswith(line, '#') && !occursin(ANON_FUNC_DECLARATION, line) && continue
prompt = match(PROMPT_REGEX, line)
# We allow comments before the first julia> prompt
!found_first_prompt && startswith(line, '#') && continue
if prompt === nothing
source = match(SOURCE_REGEX, line)
if source === nothing
Expand All @@ -465,6 +462,7 @@ function repl_splitter(code)
println(buffer, source[1])
end
else
found_first_prompt = true
savebuffer!(output, buffer)
println(buffer, prompt[1])
end
Expand Down
49 changes: 49 additions & 0 deletions test/doctests/src/working.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,52 @@ This source file contains a working doctest:
julia> 2 + 2
4
```

Test comments or comment-like lines:

```jldoctest
julia> f(x) = println("# output from f\n$x");

julia> f(42)
# output from f
42

julia> f(42)
# comment line
# output from f
42
```

```jldoctest
julia> let x = 1
println("$x")
# comment
println("$x")
end
1
1

julia> println("xyz")
# comment
xyz
```

Original issue:

```jldoctest
julia> f()=0
f (generic function with 1 method)

julia> methods(f)
# 1 method for generic function "f":
[1] f() in Main at none:1
```

Comments at the start:

```jldoctest
# Initial comments before the first julia> prompt..
# .. should be ignored.
julia> 2 + 2
4
```