Skip to content

Commit c97fe97

Browse files
committed
Add custom macro example to docs
1 parent 6f9c229 commit c97fe97

File tree

8 files changed

+33
-14
lines changed

8 files changed

+33
-14
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
34
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

docs/make.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
using Setfield, Documenter
1+
using Setfield, Documenter, Literate
2+
3+
inputdir = joinpath(@__DIR__, "..", "examples")
4+
outputdir = joinpath(@__DIR__, "src", "examples")
5+
mkpath(outputdir)
6+
for filename in readdir(inputdir)
7+
inpath = joinpath(inputdir, filename)
8+
Literate.markdown(inpath, outputdir; documenter=true)
9+
end
210

311
makedocs(
412
modules = [Setfield],
513
sitename = "Setfield.jl",
614
pages = [
715
"Introduction" => "intro.md",
816
"Docstrings" => "index.md",
17+
"Custom Macros" => "examples/custom_macros.md",
918
],
1019
strict = true, # to exit with non-zero code on error
1120
)

docs/src/examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.md

docs/src/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ SpaceShip(Person(:JULIA, 2009), [0.0, 0.0, 0.0], [0.0, 0.0, 0.0])
4040
julia> s = @set s.velocity[1] += 999999
4141
SpaceShip(Person(:JULIA, 2009), [999999.0, 0.0, 0.0], [0.0, 0.0, 0.0])
4242
43-
julia> s = @set s.velocity[1] += 999999
43+
julia> s = @set s.velocity[1] += 1000001
4444
SpaceShip(Person(:JULIA, 2009), [2.0e6, 0.0, 0.0], [0.0, 0.0, 0.0])
4545
4646
julia> @set s.position[2] = 20

test/test_custom_macros.jl renamed to examples/custom_macros.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
module CustomMacros
21
# # Extending `@set` and `@lens`
32
# This code demonstrates how to extend the `@set` and `@lens` mechanism with custom
43
# lenses.
@@ -74,5 +73,3 @@ set(o, l, 100)
7473
@test o == (foo=[100,2,3], bar=:bar)
7574

7675
# Everything works, we can do arbitrary nesting and also use `+=` syntax etc.
77-
78-
end#module

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module TestSetfield
2+
3+
include("test_examples.jl")
24
include("test_setmacro.jl")
3-
include("test_custom_macros.jl")
45
include("test_core.jl")
56
include("test_functionlenses.jl")
67
include("test_settable.jl")

test/test_examples.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TestExamples
2+
using Test
3+
dir = joinpath("..", "examples")
4+
@testset "example $filename" for filename in readdir(dir)
5+
path = joinpath(dir, filename)
6+
include(path)
7+
end
8+
end#module

test/test_setmacro.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,27 @@ using StaticArrays: @SMatrix
2222
@testset "setmacro, lensmacro isolation" begin
2323

2424
# test that no symbols like `IndexLens` are needed:
25-
index = 1
26-
@test Clone.@lens( _) isa Setfield.Lens
27-
@test Clone.@lens( _.a) isa Setfield.Lens
28-
@test Clone.@lens( _[end]) isa Setfield.Lens
29-
@test Clone.@lens( _[$index]) isa Setfield.Lens
30-
@test Clone.@lens( _.a[1][end, end-2].b[$index, $index]) isa Setfield.Lens
25+
@test Clone.@lens(_ ) isa Setfield.Lens
26+
@test Clone.@lens(_.a ) isa Setfield.Lens
27+
@test Clone.@lens(_[1] ) isa Setfield.Lens
28+
@test Clone.@lens(first(_) ) isa Setfield.Lens
29+
@test Clone.@lens(_[end] ) isa Setfield.Lens
30+
@test Clone.@lens(_[$1] ) isa Setfield.Lens
31+
@test Clone.@lens(_.a[1][end, end-2].b[$1, $1]) isa Setfield.Lens
3132

3233
@test Setfield.@lens(_.a) === Clone.@lens(_.a)
3334
@test Setfield.@lens(_.a.b) === Clone.@lens(_.a.b)
3435
@test Setfield.@lens(_.a.b[1,2]) === Clone.@lens(_.a.b[1,2])
35-
36+
3637
o = (a=1, b=2)
37-
3838
@test Clone.@set(o.a = 2) === Setfield.@set(o.a = 2)
3939
@test Clone.@set(o.a += 2) === Setfield.@set(o.a += 2)
4040

4141
m = @SMatrix [0 0; 0 0]
4242
m2 = Clone.@set m[end-1, end] = 1
4343
@test m2 === @SMatrix [0 1; 0 0]
44+
m3 = Clone.@set(first(m) = 1)
45+
@test m3 === @SMatrix[1 0; 0 0]
4446
end
4547

4648
end#module

0 commit comments

Comments
 (0)