Skip to content

Commit

Permalink
Add doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Dec 14, 2023
1 parent 1472609 commit 82b2faf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 31 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CairoMakieExt = "CairoMakie"
[compat]
Aqua = "0.8"
CairoMakie = "0.9, 0.10, 0.11"
Documenter = "1"
JuliaFormatter = "1"
NCDatasets = "0.12, 0.13"
OrderedCollections = "1"
Expand All @@ -28,9 +29,10 @@ julia = "1.9"
[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "CairoMakie", "JuliaFormatter", "SafeTestsets", "Test"]
test = ["Aqua", "CairoMakie", "Documenter", "JuliaFormatter", "SafeTestsets", "Test"]
29 changes: 20 additions & 9 deletions src/OutputVar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import OrderedCollections: OrderedDict

import Statistics: mean

export OutputVar,
read_var,
average_lat,
average_lon,
average_time,
slice_time,
slice_x,
slice_x,
slice_y,
slice_lon,
slice_lat

struct OutputVar{
T <: AbstractArray,
A <: AbstractArray,
Expand Down Expand Up @@ -105,15 +117,14 @@ Example
Average over latitudes
```jldoctest
julia> import Statistics: mean
julia> long = 0.:180. |> collect
julia> lat = 0.:90. |> collect
julia> data = reshape(1.:91*181., (181, 91))
julia> dims = Dict(["lat" => lat, "long" => long])
julia> var = OutputVar(dims, data)
julia> reduce_over(mean, "lat", var)
```julia
import Statistics: mean
long = 0.:180. |> collect
lat = 0.:90. |> collect
data = reshape(1.:91*181., (181, 91))
dims = Dict(["lat" => lat, "long" => long])
var = OutputVar(dims, data)
_reduce_over(mean, "lat", var)
```
"""
function _reduce_over(
Expand Down
42 changes: 21 additions & 21 deletions src/Utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Utils

export match_nc_filename, squeeze, nearest_index, kwargs

"""
match_nc_filename(filename::String)
Expand All @@ -11,32 +13,23 @@ The convention is: `shortname_(period)_reduction.nc`, with `period` being option
Examples
=========
```julia-repl
```jldoctest
julia> match_nc_filename("bob")
```
```julia-repl
```jldoctest
julia> match_nc_filename("ta_1d_average.nc")
3-element Vector{Union{Nothing, SubString{String}}}:
"ta"
"1d"
"average"
("ta", "1d", "average")
```
```julia-repl
```jldoctest
julia> match_nc_filename("pfull_6.0min_max.nc")
3-element Vector{Union{Nothing, SubString{String}}}:
"pfull"
"6.0min"
"max"
("pfull", "6.0min", "max")
```
```julia-repl
```jldoctest
julia> match_nc_filename("hu_inst.nc")
3-element Vector{Union{Nothing, SubString{String}}}:
"hu"
nothing
"inst"
("hu", nothing, "inst")
```
"""
function match_nc_filename(filename::String)
Expand Down Expand Up @@ -78,13 +71,18 @@ Examples
=========
```jldoctest
julia> A = [[1 2] [3 4]]
julia> A = [[1 2] [3 4]];
julia> size(A)
(1, 4)
julia> A_squeezed = squeeze(A)
julia> A_squeezed = squeeze(A);
julia> size(A_squeezed)
(4, )
julia> A_not_squeezed = squeeze(A; dims = (2, ))
(4,)
julia> A_not_squeezed = squeeze(A; dims = (2, ));
julia> size(A_not_squeezed)
(1, 4)
```
Expand Down Expand Up @@ -116,9 +114,11 @@ Examples
=========
```jldoctest
julia> A = [-1, 0, 1, 2, 3, 4, 5]
julia> A = [-1, 0, 1, 2, 3, 4, 5];
julia> nearest_index(A, 3)
5
julia> nearest_index(A, 0.1)
2
```
Expand Down
14 changes: 14 additions & 0 deletions test/doctest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Documenter
import ClimaAnalysis

@testset "Test docstrings" begin

DocMeta.setdocmeta!(
ClimaAnalysis,
:DocTestSetup,
:(using ClimaAnalysis.Utils);
recursive = true,
)

doctest(ClimaAnalysis; manual = false)
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ using Test

#! format: off
@safetestset "Aqua" begin @time include("aqua.jl") end
@safetestset "Docstrings" begin @time include("doctest.jl") end
@safetestset "Format" begin @time include("format.jl") end

@safetestset "Utils" begin @time include("Utils.jl") end
@safetestset "SimDir" begin @time include("SimDir.jl") end
@safetestset "OutputVar" begin @time include("OutputVar.jl") end
Expand Down

0 comments on commit 82b2faf

Please sign in to comment.