Skip to content

Commit 2f5d167

Browse files
committed
Add test folder and fix doctests
1 parent 801ad40 commit 2f5d167

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/varname.jl

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ x[Colon(),1][2]
2222
julia> vn.indexing
2323
((Colon(), 1), (2,))
2424
25-
julia> VarName(DynamicPPL.@vsym(x[:, 1][1+1]), DynamicPPL.@vinds(x[:, 1][1+1]))
25+
julia> VarName(AbstractPPL.@vsym(x[:, 1][1+1]), AbstractPPL.@vinds(x[:, 1][1+1]))
2626
x[Colon(),1][2]
2727
```
2828
"""
@@ -253,7 +253,7 @@ varname(expr::Symbol) = VarName(expr)
253253
function varname(expr::Expr)
254254
if Meta.isexpr(expr, :ref)
255255
sym, inds = vsym(expr), vinds(expr)
256-
return :($(DynamicPPL.VarName)($(QuoteNode(sym)), $inds))
256+
return :($(AbstractPPL.VarName)($(QuoteNode(sym)), $inds))
257257
else
258258
throw("Malformed variable name $(expr)!")
259259
end
@@ -266,15 +266,30 @@ end
266266
A macro that returns the variable symbol given the input variable expression `expr`.
267267
For example, `@vsym x[1]` returns `:x`.
268268
269+
## Examples
270+
269271
```jldoctest
270-
julia> AbstractPPL.@vsym x[1]
272+
julia> AbstractPPL.@vsym x
273+
:x
274+
275+
julia> AbstractPPL.@vsym x[1,1][2,3]
276+
:x
277+
278+
julia> AbstractPPL.@vsym x[end]
271279
:x
272280
```
273281
"""
274282
macro vsym(expr::Union{Expr, Symbol})
275283
return QuoteNode(vsym(expr))
276284
end
277285

286+
"""
287+
vsym(expr)
288+
289+
Return name part of the [`@varname`](@ref)-compatible expression `expr` as a symbol for input of the
290+
[`VarName`](@ref) constructor."""
291+
function vsym end
292+
278293
vsym(expr::Symbol) = expr
279294
function vsym(expr::Expr)
280295
if Meta.isexpr(expr, :ref)
@@ -289,9 +304,23 @@ end
289304
290305
Returns a tuple of tuples of the indices in `expr`.
291306
307+
## Examples
308+
292309
```jldoctest
293-
julia> AbstractPPL.@vinds x[1, :][2]
294-
((1, Colon()), (2,))
310+
julia> AbstractPPL.@vinds x
311+
()
312+
313+
julia> AbstractPPL.@vinds x[1,1][2,3]
314+
((1, 1), (2, 3))
315+
316+
julia> AbstractPPL.@vinds x[:,1][2,:]
317+
((Colon(), 1), (2, Colon()))
318+
319+
julia> AbstractPPL.@vinds x[2:3,1][2,1:2]
320+
((2:3, 1), (2, 1:2))
321+
322+
julia> AbstractPPL.@vinds x[2:3,2:3][[1,2],[1,2]]
323+
((2:3, 2:3), ([1, 2], [1, 2]))
295324
```
296325
297326
!!! compat "Julia 1.5"
@@ -302,6 +331,25 @@ macro vinds(expr::Union{Expr, Symbol})
302331
return esc(vinds(expr))
303332
end
304333

334+
335+
"""
336+
vinds(expr)
337+
338+
Return the indexing part of the [`@varname`](@ref)-compatible expression `expr` as an expression
339+
suitable for input of the [`VarName`](@ref) constructor.
340+
341+
## Examples
342+
343+
```jldoctest
344+
julia> AbstractPPL.vinds(:(x[end]))
345+
:((((lastindex)(x),),))
346+
347+
julia> AbstractPPL.vinds(:(x[1, end]))
348+
:(((1, (lastindex)(x, 2)),))
349+
```
350+
"""
351+
function vinds end
352+
305353
vinds(expr::Symbol) = Expr(:tuple)
306354
function vinds(expr::Expr)
307355
if Meta.isexpr(expr, :ref)

test/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using AbstractPPL
2+
using Documenter
3+
using Test
4+
5+
@testset "AbstractPPL.jl" begin
6+
@testset "doctests" begin
7+
DocMeta.setdocmeta!(
8+
AbstractPPL,
9+
:DocTestSetup,
10+
quote
11+
using AbstractPPL
12+
end;
13+
recursive=true,
14+
)
15+
doctest(AbstractPPL; manual=false)
16+
end
17+
end
18+

0 commit comments

Comments
 (0)