You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -371,9 +371,57 @@ Script-style doctests are supported too:
371
371
4
372
372
```
373
373
374
+
### Setup code
375
+
376
+
You can have setup code for doctests that gets executed before the actual doctest.
377
+
For example, the following doctest needs to have the `Documenter` module to be present.
378
+
379
+
```jldoctest; setup=:(using Documenter)
380
+
julia> Documenter.splitexpr(:(Foo.Bar.baz))
381
+
(:(Foo.Bar), :(:baz))
382
+
```
383
+
384
+
This is achieved by the `setup` keyword to `jldoctest`.
385
+
386
+
````
387
+
```jldoctest; setup=:(using Documenter)
388
+
````
389
+
390
+
The alternative approach is to use the `DocTestSetup` keys in `@meta`-blocks, which will apply across multiple doctests.
391
+
392
+
````markdown
393
+
```@meta
394
+
DocTestSetup = quote
395
+
f(x) = x^2
396
+
end
397
+
```
398
+
````
399
+
```@meta
400
+
DocTestSetup = quote
401
+
f(x) = x^2
402
+
end
403
+
```
404
+
405
+
```jldoctest
406
+
julia> f(2)
407
+
4
408
+
```
409
+
410
+
The doctests and `@meta` blocks are evaluated sequentially on each page, so you can always unset the test code by setting it back to `nothing`.
411
+
412
+
````markdown
413
+
```@meta
414
+
DocTestSetup = nothing
415
+
```
416
+
````
417
+
```@meta
418
+
DocTestSetup = nothing
419
+
```
420
+
421
+
374
422
## Running interactive code
375
423
376
-
[`@example` block](@ref) run a code snippet and insert the output into the document.
424
+
[`@example` block](@ref reference-at-example) run a code snippet and insert the output into the document.
377
425
E.g. the following Markdown
378
426
379
427
````markdown
@@ -419,7 +467,7 @@ println("Hello World")
419
467
420
468
### Color output
421
469
422
-
Output from [`@repl` block](@ref)s and [`@example` block](@ref)s support colored output,
470
+
Output from [`@repl` block](@ref)s and [`@example` block](@ref reference-at-example)s support colored output,
423
471
transforming ANSI color codes to HTML.
424
472
425
473
!!! compat "Julia 1.6"
@@ -499,36 +547,94 @@ median(xs)
499
547
sum(xs)
500
548
```
501
549
502
-
##Doctest showcase
550
+
### Named blocks
503
551
504
-
Currently exists just so that there would be doctests to run in manual pages of Documenter's
505
-
manual. This page does not show up in navigation.
552
+
Generally, each blocks gets evaluate in a separate, clean context (i.e. no variables from previous blocks will be polluting the namespace etc).
553
+
However, you can also re-use a namespace by giving the blocks a name.
506
554
507
-
```jldoctest
508
-
julia> 2 + 2
509
-
4
555
+
````markdown
556
+
```@example block-name
557
+
x = 40
558
+
```
559
+
will show up like this:
560
+
````
561
+
```@example block-name
562
+
x = 40
510
563
```
511
564
512
-
The following doctests needs doctestsetup:
565
+
````markdown
566
+
```@example block-name
567
+
x + 1
568
+
```
569
+
will show up like this:
570
+
````
571
+
```@example block-name
572
+
x + 1
573
+
```
513
574
514
-
```jldoctest; setup=:(using Documenter)
515
-
julia> Documenter.splitexpr(:(Foo.Bar.baz))
516
-
(:(Foo.Bar), :(:baz))
575
+
When you need setup code that you do not wish to show in the generated documentation, you can use [an `@setup` block](@ref reference-at-setup):
576
+
577
+
````markdown
578
+
```@setup block-name
579
+
x = 42
580
+
```
581
+
````
582
+
```@setup block-name
583
+
x = 42
517
584
```
518
585
519
-
Let's also try `@meta` blocks:
586
+
The [`@setup` block](@ref reference-at-setup) essentially acts as a hidden [`@example` block](@ref reference-at-example).
587
+
Any state it sets up, you can access in subsequent blocks with the same name.
588
+
For example, the following `@example` block
520
589
521
-
```@meta
522
-
DocTestSetup = quote
523
-
f(x) = x^2
524
-
end
590
+
````markdown
591
+
```@example block-name
592
+
x
525
593
```
594
+
````
526
595
527
-
```jldoctest
528
-
julia> f(2)
529
-
4
596
+
will show up like this:
597
+
598
+
```@example block-name
599
+
x
530
600
```
531
601
532
-
```@meta
533
-
DocTestSetup = nothing
602
+
You also have continued blocks which do not evaluate immediately.
603
+
604
+
````markdown
605
+
```@example block-name; continued = true
606
+
y = 99
607
+
```
608
+
````
609
+
```@example block-name; continued = true
610
+
y = 99
611
+
```
612
+
613
+
The continued evaluation only applies to [`@example` blocks](@ref reference-at-example) and so if you put, for example, an `@repl` block in between, it will lead to an error, because the `y = 99` line of code has not run yet.
614
+
615
+
````markdown
616
+
```@repl block-name
617
+
x
618
+
y
619
+
```
620
+
````
621
+
622
+
```@repl block-name
623
+
x
624
+
y
625
+
```
626
+
627
+
Another [`@example` block](@ref reference-at-example) with the same name will, however, finish evaluating it.
0 commit comments