Skip to content

Commit e5b7e44

Browse files
committed
added proper testing for PrefixContext of all demo models
1 parent 55e24e9 commit e5b7e44

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

test/contexts.jl

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,43 @@ end
138138
end
139139

140140
@testset "PrefixContext" begin
141-
ctx = @inferred PrefixContext{:f}(
142-
PrefixContext{:e}(
143-
PrefixContext{:d}(
144-
PrefixContext{:c}(
145-
PrefixContext{:b}(PrefixContext{:a}(DefaultContext()))
141+
@testset "prefixing" begin
142+
ctx = @inferred PrefixContext{:f}(
143+
PrefixContext{:e}(
144+
PrefixContext{:d}(
145+
PrefixContext{:c}(
146+
PrefixContext{:b}(PrefixContext{:a}(DefaultContext()))
147+
),
146148
),
147149
),
148-
),
149-
)
150-
vn = VarName{:x}()
151-
vn_prefixed = @inferred DynamicPPL.prefix(ctx, vn)
152-
@test DynamicPPL.getsym(vn_prefixed) == Symbol("a.b.c.d.e.f.x")
153-
@test getoptic(vn_prefixed) === getoptic(vn)
154-
155-
vn = VarName{:x}(((1,),))
156-
vn_prefixed = @inferred DynamicPPL.prefix(ctx, vn)
157-
@test DynamicPPL.getsym(vn_prefixed) == Symbol("a.b.c.d.e.f.x")
158-
@test getoptic(vn_prefixed) === getoptic(vn)
150+
)
151+
vn = VarName{:x}()
152+
vn_prefixed = @inferred DynamicPPL.prefix(ctx, vn)
153+
@test DynamicPPL.getsym(vn_prefixed) == Symbol("a.b.c.d.e.f.x")
154+
@test getoptic(vn_prefixed) === getoptic(vn)
155+
156+
vn = VarName{:x}(((1,),))
157+
vn_prefixed = @inferred DynamicPPL.prefix(ctx, vn)
158+
@test DynamicPPL.getsym(vn_prefixed) == Symbol("a.b.c.d.e.f.x")
159+
@test getoptic(vn_prefixed) === getoptic(vn)
160+
end
161+
162+
context = DynamicPPL.PrefixContext{:prefix}(SamplingContext())
163+
@testset "evaluation: $(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
164+
# Sample with the context.
165+
varinfo = DynamicPPL.VarInfo()
166+
DynamicPPL.evaluate!!(model, varinfo, context)
167+
# Extract the resulting symbols.
168+
vns_varinfo_syms = Set(map(DynamicPPL.getsym, keys(varinfo)))
169+
170+
# Extract the ground truth symbols.
171+
vns_syms = Set([
172+
Symbol("prefix", DynamicPPL.PREFIX_SEPARATOR, DynamicPPL.getsym(vn)) for vn in DynamicPPL.TestUtils.varnames(model)
173+
])
174+
175+
# Check that all variables are prefixed correctly.
176+
@test vns_syms == vns_varinfo_syms
177+
end
159178
end
160179

161180
@testset "SamplingContext" begin

0 commit comments

Comments
 (0)