Skip to content

Commit f04950d

Browse files
committed
Add tests of expression manipulation
1 parent d78d089 commit f04950d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using .Turing
66
turnprogress(false)
77

88
@testset "DynamicPPL.jl" begin
9+
include("utils.jl")
910
include("compiler.jl")
1011
include("varinfo.jl")
1112
include("prob_macro.jl")

test/utils.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using DynamicPPL
2+
using DynamicPPL: apply_dotted, getargs_dottilde, getargs_tilde
3+
4+
using Test
5+
6+
@testset "apply_dotted" begin
7+
# Some things that are not expressions.
8+
@test apply_dotted(:x) === :x
9+
@test apply_dotted(1.0) === 1.0
10+
@test apply_dotted([1.0, 2.0, 4.0]) == [1.0, 2.0, 4.0]
11+
12+
# Some expressions.
13+
@test apply_dotted(:(x ~ Normal(μ, σ))) == :(x ~ Normal(μ, σ))
14+
@test apply_dotted(:((.~)(x, Normal(μ, σ)))) == :((.~)(x, Normal(μ, σ)))
15+
@test apply_dotted(:((~).(x, Normal(μ, σ)))) == :((~).(x, Normal(μ, σ)))
16+
@test apply_dotted(:(@. x ~ Normal(μ, σ))) == :((~).(x, Normal.(μ, σ)))
17+
@test apply_dotted(:(@. x ~ Normal(μ, $(Expr(:$, :(sqrt(v))))))) ==
18+
:((~).(x, Normal.(μ, sqrt(v))))
19+
@test apply_dotted(:(@~ Normal.(μ, σ))) == :(@~ Normal.(μ, σ))
20+
end
21+
22+
@testset "getargs_dottilde" begin
23+
# Some things that are not expressions.
24+
@test getargs_dottilde(:x) === nothing
25+
@test getargs_dottilde(1.0) === nothing
26+
@test getargs_dottilde([1.0, 2.0, 4.0]) === nothing
27+
28+
# Some expressions.
29+
@test getargs_dottilde(:(x ~ Normal(μ, σ))) === nothing
30+
@test getargs_dottilde(:((.~)(x, Normal(μ, σ)))) == (:x, :(Normal(μ, σ)))
31+
@test getargs_dottilde(:((~).(x, Normal(μ, σ)))) == (:x, :(Normal(μ, σ)))
32+
@test getargs_dottilde(:(@. x ~ Normal(μ, σ))) === nothing
33+
@test getargs_dottilde(:(@. x ~ Normal(μ, $(Expr(:$, :(sqrt(v))))))) === nothing
34+
@test getargs_dottilde(:(@~ Normal.(μ, σ))) === nothing
35+
end
36+
37+
@testset "getargs_tilde" begin
38+
# Some things that are not expressions.
39+
@test getargs_tilde(:x) === nothing
40+
@test getargs_tilde(1.0) === nothing
41+
@test getargs_tilde([1.0, 2.0, 4.0]) === nothing
42+
43+
# Some expressions.
44+
@test getargs_tilde(:(x ~ Normal(μ, σ))) == (:x, :(Normal(μ, σ)))
45+
@test getargs_tilde(:((.~)(x, Normal(μ, σ)))) === nothing
46+
@test getargs_tilde(:((~).(x, Normal(μ, σ)))) === nothing
47+
@test getargs_tilde(:(@. x ~ Normal(μ, σ))) === nothing
48+
@test getargs_tilde(:(@. x ~ Normal(μ, $(Expr(:$, :(sqrt(v))))))) === nothing
49+
@test getargs_tilde(:(@~ Normal.(μ, σ))) === nothing
50+
end

0 commit comments

Comments
 (0)