Skip to content

Commit 057baf9

Browse files
committed
test: add tests to verify heirarchal kwargs and metadata and default values of different parameter definitions
1 parent 43034fe commit 057baf9

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

test/model_parsing.jl

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ModelingToolkit, Test
2-
using ModelingToolkit: get_gui_metadata
2+
using ModelingToolkit: get_gui_metadata, VariableDescription, getdefault
33
using URIs: URI
44

55
ENV["MTK_ICONS_DIR"] = "$(@__DIR__)/icons"
@@ -10,12 +10,12 @@ end
1010
@connector RealOutput begin
1111
u(t), [output = true]
1212
end
13-
@model Constant begin
13+
@model Constant(; k = 1) begin
1414
@components begin
1515
output = RealOutput()
1616
end
1717
@parameters begin
18-
k, [description = "Constant output value of block"]
18+
k = k, [description = "Constant output value of block"]
1919
end
2020
@equations begin
2121
output.u ~ k
@@ -61,10 +61,10 @@ end
6161
end
6262

6363
resistor_log = "$(@__DIR__)/logo/resistor.svg"
64-
@model Resistor begin
64+
@model Resistor(; R = 1) begin
6565
@extend v, i = oneport = OnePort()
6666
@parameters begin
67-
R = 1
67+
R = R
6868
end
6969
@icon begin
7070
"""<?xml version="1.0" encoding="UTF-8"?>
@@ -87,10 +87,10 @@ l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></pat
8787
end
8888
end
8989

90-
@model Capacitor begin
90+
@model Capacitor(; C = 1) begin
9191
@extend v, i = oneport = OnePort()
9292
@parameters begin
93-
C = 1
93+
C = C
9494
end
9595
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
9696
@equations begin
@@ -110,8 +110,8 @@ end
110110

111111
@model RC begin
112112
@components begin
113-
resistor = Resistor()
114-
capacitor = Capacitor()
113+
resistor = Resistor(; R)
114+
capacitor = Capacitor(; C = 10)
115115
source = Voltage()
116116
constant = Constant()
117117
ground = Ground()
@@ -123,7 +123,11 @@ end
123123
connect(capacitor.n, source.n, ground.g)
124124
end
125125
end
126-
@named rc = RC()
126+
127+
@named rc = RC(; resistor.R = 20)
128+
@test getdefault(rc.resistor.R) == 20
129+
@test getdefault(rc.capacitor.C) == 10
130+
@test getdefault(rc.constant.k) == 1
127131

128132
@test get_gui_metadata(rc.resistor).layout == Resistor.structure[:icon] ==
129133
read(joinpath(ENV["MTK_ICONS_DIR"], "resistor.svg"), String)
@@ -137,3 +141,34 @@ end
137141
URI("file:///" * abspath(ENV["MTK_ICONS_DIR"], "pin.png"))
138142

139143
@test length(equations(structural_simplify(rc))) == 1
144+
145+
@model MockModel(; cval, gval, jval = 6) begin
146+
@parameters begin
147+
a
148+
b(t)
149+
c(t) = cval
150+
d = 2
151+
e, [description = "e"]
152+
f = 3, [description = "f"]
153+
g = gval, [description = "g"]
154+
h(t), [description = "h(t)"]
155+
i(t) = 5, [description = "i(t)"]
156+
j(t) = jval, [description = "j(t)"]
157+
end
158+
end
159+
160+
@named model = MockModel(cval = 1, gval = 4)
161+
162+
@test hasmetadata(model.e, VariableDescription)
163+
@test hasmetadata(model.f, VariableDescription)
164+
@test hasmetadata(model.g, VariableDescription)
165+
@test hasmetadata(model.h, VariableDescription)
166+
@test hasmetadata(model.i, VariableDescription)
167+
@test hasmetadata(model.j, VariableDescription)
168+
169+
@test getdefault(model.c) == 1
170+
@test getdefault(model.d) == 2
171+
@test getdefault(model.f) == 3
172+
@test getdefault(model.g) == 4
173+
@test getdefault(model.i) == 5
174+
@test getdefault(model.j) == 6

0 commit comments

Comments
 (0)