Skip to content

Commit 8b616e4

Browse files
abhroMilesCranmer
andauthored
docs: add doctests (#175)
* Add compat bounds for Documenter.jl * Add self dep to docs/Project.toml * Update repo option for makedocs * Turn off second doctest in makedocs * Make code blocks into `@example`/`@repl` blocks * Add doctests * Add cross-reference links * Fix backticks * Fix code blocks in markdown list * Use semicolon to suppress docs example block output * Run doctests as part of makedocs() * Ignore all Manifest.toml files * Ignore auto-generated index.md * Put `deploydocs` in CI conditional in make.jl * Fix typo in parent commit * docs: build previews * docs: fix incorrect [sources] --------- Co-authored-by: MilesCranmer <[email protected]>
1 parent 725e48d commit 8b616e4

File tree

10 files changed

+71
-61
lines changed

10 files changed

+71
-61
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
permissions:
4747
contents: write
4848
statuses: write
49+
pull-requests: read
4950
steps:
5051
- uses: actions/checkout@v4
5152
- uses: julia-actions/setup-julia@v2

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.jl.*.cov
22
*.jl.cov
33
*.jl.mem
4-
/Manifest.toml
5-
/docs/Manifest.toml
4+
Manifest.toml
5+
# index.md is auto-generated by docs build process
6+
/docs/src/index.md
67
/docs/build/

docs/make.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ using DynamicQuantities
22
using DynamicQuantities.Units
33
using DynamicQuantities: constructorof, with_type_parameters, dimension_names
44
using Documenter
5+
using Documenter.Remotes: GitHub
56

67
DocMeta.setdocmeta!(DynamicQuantities, :DocTestSetup, :(using DynamicQuantities); recursive=true)
7-
doctest(DynamicQuantities)
88

99
readme = open(dirname(@__FILE__) * "/../README.md") do io
1010
read(io, String)
@@ -31,7 +31,7 @@ end
3131
makedocs(;
3232
modules=[DynamicQuantities, DynamicQuantities.Units],
3333
authors="MilesCranmer <[email protected]> and contributors",
34-
repo="https://github.com/JuliaPhysics/DynamicQuantities.jl/blob/{commit}{path}#{line}",
34+
repo=GitHub("JuliaPhysics/DynamicQuantities.jl"),
3535
sitename="DynamicQuantities.jl",
3636
format=Documenter.HTML(;
3737
prettyurls=get(ENV, "CI", "false") == "true",
@@ -51,15 +51,20 @@ makedocs(;
5151
warnonly = [:missing_docs]
5252
)
5353

54-
deploydocs(;
55-
repo="github.com/JuliaPhysics/DynamicQuantities.jl",
56-
devbranch="main"
57-
)
54+
in_CI_env = get(ENV, "CI", "false") == "true"
55+
if in_CI_env
56+
deploydocs(;
57+
repo="github.com/JuliaPhysics/DynamicQuantities.jl",
58+
devbranch="main",
59+
push_preview=true
60+
)
5861

59-
# Mirror to DAMTP:
60-
ENV["DOCUMENTER_KEY"] = ENV["DOCUMENTER_KEY_CAM"]
61-
ENV["GITHUB_REPOSITORY"] = "ai-damtp-cam-ac-uk/dynamicquantities.git"
62-
deploydocs(;
63-
repo="github.com/ai-damtp-cam-ac-uk/dynamicquantities.git",
64-
devbranch="main"
65-
)
62+
# Mirror to DAMTP:
63+
ENV["DOCUMENTER_KEY"] = ENV["DOCUMENTER_KEY_CAM"]
64+
ENV["GITHUB_REPOSITORY"] = "ai-damtp-cam-ac-uk/dynamicquantities.git"
65+
deploydocs(;
66+
repo="github.com/ai-damtp-cam-ac-uk/dynamicquantities.git",
67+
devbranch="main",
68+
push_preview=true
69+
)
70+
end

docs/src/examples.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Say that we wish to track angles as a unit, rather than assume
112112
the SI convention that `1 rad = 1`. We can do this by creating
113113
a new struct to track dimensions:
114114

115-
```julia
115+
```@example dimensional-angle
116116
using DynamicQuantities
117117
118118
struct AngleDimensions{R} <: AbstractDimensions{R}
@@ -130,15 +130,14 @@ end
130130
Simply by inheriting from `AbstractDimensions`, we get all the
131131
constructors and operations as defined on `Dimensions`:
132132

133-
```julia
134-
julia> x = Quantity(1.0, AngleDimensions(length=1, angle=-1))
135-
1.0 m angle⁻¹
133+
```@repl dimensional-angle
134+
x = Quantity(1.0, AngleDimensions(length=1, angle=-1))
136135
```
137136

138137
However, perhaps we want to set the default `angle` dimension as
139138
`rad`. We can do this by defining a method for `dimension_name`:
140139

141-
```julia
140+
```@example dimensional-angle
142141
import DynamicQuantities: DynamicQuantities as DQ
143142
144143
function DQ.dimension_name(::AngleDimensions, k::Symbol)
@@ -153,14 +152,13 @@ function DQ.dimension_name(::AngleDimensions, k::Symbol)
153152
angle = "rad",
154153
)
155154
return get(default_dimensions, k, string(k))
156-
end
155+
end;
157156
```
158157

159158
This gives us the following behavior:
160159

161-
```julia
162-
julia> x = Quantity(1.0, AngleDimensions(length=1, angle=-1))
163-
1.0 m rad⁻¹
160+
```@repl dimensional-angle
161+
x = Quantity(1.0, AngleDimensions(length=1, angle=-1))
164162
```
165163

166164
Next, say that we are working with existing quantities defined using
@@ -169,7 +167,7 @@ standard `Dimensions`. We want to promote these to our new `AngleDimensions` typ
169167
For this, we define two functions: `promote_rule` and a constructor for `AngleDimensions`
170168
from regular `Dimensions`:
171169

172-
```julia
170+
```@example dimensional-angle
173171
function Base.promote_rule(::Type{AngleDimensions{R1}}, ::Type{Dimensions{R2}}) where {R1,R2}
174172
return AngleDimensions{promote_type(R1, R2)}
175173
end
@@ -182,7 +180,7 @@ function Base.convert(::Type{Quantity{T,AngleDimensions{R}}}, q::Quantity{<:Any,
182180
d.length, d.mass, d.time, d.current, d.temperature, d.luminosity, d.amount, angle=zero(R)
183181
)
184182
)
185-
end
183+
end;
186184
```
187185

188186
This means that whenever a `Quantity{<:Any,<:Dimensions}`
@@ -194,22 +192,16 @@ tracked.)
194192

195193
Let's define a constant for `rad`:
196194

197-
```julia
198-
julia> const rad = Quantity(1.0, AngleDimensions(angle = 1))
199-
1.0 rad
195+
```@repl dimensional-angle
196+
const rad = Quantity(1.0, AngleDimensions(angle = 1))
200197
```
201198

202199
and use it in a calculation:
203200

204-
```julia
205-
julia> x = 2rad
206-
2.0 rad
207-
208-
julia> y = 10u"min"
209-
600.0 s
210-
211-
julia> angular_velocity = x / y
212-
0.0033333333333333335 s⁻¹ rad
201+
```@repl dimensional-angle
202+
x = 2rad
203+
y = 10u"min"
204+
angular_velocity = x / y
213205
```
214206

215207
which as we can see, automatically promotes to

src/affine_dimensions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ for op in [:*, :/, :+, :-], (first, second) in [(:AffineUnit, :Number), (:Number
3838

3939
# Skip the already defined value * unit case
4040
op == :* && first == :Number && second == :AffineUnit && continue
41-
41+
4242
@eval function Base.$op(a::$first, b::$second)
4343
throw(ArgumentError("Affine units only support scalar multiplication in the form 'number * unit', e.g., 22 * ua\"degC\", which will immediately convert it to a regular `Quantity{Float64,Dimensions{R}}`. Other operations are not supported."))
4444
end
@@ -122,7 +122,7 @@ Convert a quantity `q` to the numerical value in terms of affine units specified
122122
then strip the units. This allows getting the numerical value in terms of degrees Celsius or Fahrenheit.
123123
124124
# Examples
125-
```julia
125+
```jldoctest
126126
julia> ustrip(ua"degC", 27ua"degC")
127127
27.0
128128

src/arrays.jl

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,41 @@ and so can be used in most places where a normal array would be used, including
1616
1717
- `QuantityArray(v::AbstractArray, d::AbstractDimensions)`: Create a `QuantityArray` with value `v` and dimensions `d`,
1818
using `Quantity` if the eltype of `v` is numeric, and `GenericQuantity` otherwise.
19+
1920
- `QuantityArray(v::AbstractArray{<:Number}, q::AbstractQuantity)`: Create a `QuantityArray` with value `v` and dimensions inferred
20-
with `dimension(q)`. This is so that you can easily create an array with the units module, like so:
21-
```julia
22-
julia> A = QuantityArray(randn(32), 1u"m")
23-
```
21+
with `dimension(q)`. This is so that you can easily create an array with the units module, like so:
22+
23+
```julia
24+
A = QuantityArray(randn(32), 1u"m")
25+
```
26+
2427
- `QuantityArray(v::AbstractArray{<:Any}, q::AbstractGenericQuantity)`: Create a `QuantityArray` with
25-
value `v` and dimensions inferred with `dimension(q)`.
26-
This is so that you can easily create quantity arrays of non-numeric eltypes, like so:
27-
```julia
28-
julia> A = QuantityArray([[1.0], [2.0, 3.0]], GenericQuantity(1u"m"))
29-
```
28+
value `v` and dimensions inferred with `dimension(q)`.
29+
This is so that you can easily create quantity arrays of non-numeric eltypes, like so:
30+
31+
```julia
32+
A = QuantityArray([[1.0], [2.0, 3.0]], GenericQuantity(1u"m"))
33+
```
34+
3035
- `QuantityArray(v::AbstractArray{<:UnionAbstractQuantity})`: Create a `QuantityArray` from an array of quantities. This means the following
3136
syntax works:
37+
3238
```julia
33-
julia> A = QuantityArray(randn(32) .* 1u"km/s")
39+
A = QuantityArray(randn(32) .* 1u"km/s")
3440
```
41+
3542
- `QuantityArray(v::AbstractArray; kws...)`: Create a `QuantityArray` with dimensions inferred from the keyword arguments. For example:
43+
3644
```julia
37-
julia> A = QuantityArray(randn(32); length=1)
45+
A = QuantityArray(randn(32); length=1)
3846
```
47+
3948
is equivalent to
49+
4050
```julia
41-
julia> A = QuantityArray(randn(32), u"m")
51+
A = QuantityArray(randn(32), u"m")
4252
```
53+
4354
The keyword arguments are passed to `DEFAULT_DIM_TYPE`.
4455
"""
4556
struct QuantityArray{T,N,D<:AbstractDimensions,Q<:UnionAbstractQuantity{T,D},V<:AbstractArray{T,N}} <: AbstractArray{Q,N}
@@ -480,7 +491,7 @@ end
480491
for dim1 in ((3,), (3, 3)), dim2 in ((3,), (3, 3))
481492
A = QuantityArray(rand(T, dim1...), Q{T}(u"m"))
482493
B = QuantityArray(rand(T, dim2...), Q{T}(u"s^2"))
483-
494+
484495
if dim1 == (3,) && dim2 == (3,)
485496
@test ustrip(A / B) ustrip(A) / ustrip(B)
486497
@test dimension(A / B) == dimension(A) / dimension(B)

src/register_units.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ a particular value.
2323
2424
# Example
2525
26-
```julia
26+
```jldoctest register-unit
2727
julia> @register_unit MyVolt 1.5u"V"
2828
```
2929
3030
This will register a new unit `MyVolt` with a value of `1.5u"V"`.
3131
You can then use this unit in your calculations:
3232
33-
```julia
33+
```jldoctest register-unit
3434
julia> x = 20us"MyVolt^2"
3535
20.0 MyVolt²
3636

src/symbolic_dimensions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Convert a quantity `q` with base SI units to the symbolic units of `qout`, for `
173173
Mathematically, the result has value `q / uexpand(qout)` and units `dimension(qout)`.
174174
175175
You can also use `|>` as a shorthand for `uconvert`:
176-
```julia
176+
```jldoctest
177177
julia> q = 1u"m/s^2" |> us"km/h^2"
178178
12960.0 km h⁻²
179179
```

src/units.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ end
147147
C,
148148
)
149149
@doc(
150-
"Voltage in Volts. Available variants: `pV`, `nV`, `μV` (/`uV`), `mV`, kV`, `MV`, `GV`.",
150+
"Voltage in Volts. Available variants: `pV`, `nV`, `μV` (/`uV`), `mV`, `kV`, `MV`, `GV`.",
151151
V,
152152
)
153153
@doc(

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For example, `promote_quantity_on_value(Quantity, Float64)` would return `Quanti
4646
`promote_quantity_on_value(RealQuantity, String)` would return `GenericQuantity`.
4747
The user should overload this function to define a custom type hierarchy.
4848
49-
Also see `promote_quantity_on_quantity`.
49+
Also see [`promote_quantity_on_quantity`](@ref).
5050
"""
5151
@unstable @inline promote_quantity_on_value(::Type{<:Union{GenericQuantity,Quantity,RealQuantity}}, ::Type{<:Any}) = GenericQuantity
5252
@unstable @inline promote_quantity_on_value(::Type{<:Union{Quantity,RealQuantity}}, ::Type{<:Number}) = Quantity
@@ -63,7 +63,7 @@ as it can store both `Quantity` and `GenericQuantity` values.
6363
Similarly, `promote_quantity_on_quantity(RealQuantity, RealQuantity)` would return `RealQuantity`,
6464
as that is the most specific type.
6565
66-
Also see `promote_quantity_on_value`.
66+
Also see [`promote_quantity_on_value`](@ref).
6767
"""
6868
@unstable @inline promote_quantity_on_quantity(::Type{<:Union{GenericQuantity,Quantity,RealQuantity}}, ::Type{<:Union{GenericQuantity,Quantity,RealQuantity}}) = GenericQuantity
6969
@unstable @inline promote_quantity_on_quantity(::Type{<:Union{Quantity,RealQuantity}}, ::Type{<:Union{Quantity,RealQuantity}}) = Quantity
@@ -395,14 +395,14 @@ Convert quantity `q` to the units specified by `unit`, then strip the units.
395395
This is equivalent to `ustrip(q / unit)`, but also verifies the dimensions are compatible.
396396
397397
# Examples
398-
```julia
398+
```jldoctest
399399
julia> ustrip(u"km", 1000u"m")
400400
1.0
401401
402402
julia> ustrip(u"s", 1u"minute")
403403
60.0
404404
405-
julia> ustrip(u"km", [1000u"m", 2000u"m"])
405+
julia> ustrip.(u"km", [1000u"m", 2000u"m"])
406406
2-element Vector{Float64}:
407407
1.0
408408
2.0

0 commit comments

Comments
 (0)