Skip to content

Commit 4246a7c

Browse files
committed
test: conditional statements inside @components and @equations and conditional default values to parameters and variables
1 parent 07f7f4d commit 4246a7c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/model_parsing.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,60 @@ end
313313
@test A.structure[:kwargs] == Dict(:p => nothing, :v => nothing)
314314
@test A.structure[:components] == [[:cc, :C]]
315315
end
316+
317+
@testset "Conditional components, equations and parameters" begin
318+
@mtkmodel C begin
319+
@parameters begin
320+
val
321+
end
322+
end
323+
324+
# Conditional statements inside @components, @equations
325+
# Conditional default value
326+
@mtkmodel A begin
327+
@parameters begin
328+
eq
329+
end
330+
@structural_parameters begin
331+
eq_flag = true
332+
c_flag = 1
333+
end
334+
@parameters begin
335+
eq = eq_flag ? 1 : 2
336+
end
337+
@components begin
338+
c0 = C(; val = 0)
339+
if c_flag == 1
340+
c = C(; val = 1)
341+
elseif c_flag == 2
342+
c = C(; val = 2)
343+
else
344+
c = C(; val = 3)
345+
end
346+
end
347+
@equations begin
348+
eq ~ 0
349+
if eq_flag isa Int
350+
eq ~ 1
351+
elseif eq_flag
352+
eq ~ 2
353+
else
354+
eq ~ 3
355+
end
356+
end
357+
end
358+
359+
@mtkbuild a1 = A(eq_flag = 1)
360+
@test getdefault(a1.c.val) == 1
361+
@test all([a1.eq ~ 0, a1.eq ~ 1] .∈ [equations(a1)])
362+
363+
@mtkbuild a2 = A(c_flag = 2)
364+
@test getdefault(a2.c.val) == 2
365+
@test all([a2.eq ~ 0, a2.eq ~ 2] .∈ [equations(a2)])
366+
367+
@test all(:comp0 .∈ [nameof.(a1.systems), nameof.(a2.systems)])
368+
369+
@mtkbuild a3 = A(eq_flag = false, c_flag = 3)
370+
@test getdefault(a3.c.val) == 3
371+
@test all([a3.eq ~ 0, a3.eq ~ 3] .∈ [equations(a3)])
372+
end

0 commit comments

Comments
 (0)