Skip to content

Commit e0ae7a9

Browse files
test: test new namespacing functionality
1 parent f9bbdc5 commit e0ae7a9

File tree

3 files changed

+194
-2
lines changed

3 files changed

+194
-2
lines changed

test/analysis_points.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ end
4747
@named sys = ODESystem(Equation[], t; systems = [sys_ap])
4848
ap3 = @test_nowarn sys.hej.plant_input
4949
@test nameof(ap3) == Symbol(join(["sys", "hej", "plant_input"], NAMESPACE_SEPARATOR))
50-
sys = complete(sys)
51-
ap4 = sys.hej.plant_input
50+
csys = complete(sys)
51+
ap4 = csys.hej.plant_input
52+
@test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR))
53+
nsys = toggle_namespacing(sys, false)
54+
ap5 = nsys.hej.plant_input
5255
@test nameof(ap4) == Symbol(join(["hej", "plant_input"], NAMESPACE_SEPARATOR))
5356
end
5457

@@ -61,6 +64,7 @@ ap = AnalysisPoint(:plant_input)
6164
eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)]
6265
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
6366
@named nested_sys = ODESystem(Equation[], t; systems = [sys])
67+
nonamespace_sys = toggle_namespacing(nested_sys, false)
6468

6569
@testset "simplifies and solves" begin
6670
ssys = structural_simplify(sys)
@@ -73,6 +77,7 @@ end
7377
test_cases = [
7478
("inner", sys, sys.plant_input),
7579
("nested", nested_sys, nested_sys.hej.plant_input),
80+
("nonamespace", nonamespace_sys, nonamespace_sys.hej.plant_input),
7681
("inner - Symbol", sys, :plant_input),
7782
("nested - Symbol", nested_sys, nameof(sys.plant_input))
7883
]

test/namespacing.jl

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
using ModelingToolkit
2+
using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing
3+
4+
@testset "ODESystem" begin
5+
@variables x(t)
6+
@parameters p
7+
sys = ODESystem(D(x) ~ p * x, t; name = :inner)
8+
@test !iscomplete(sys)
9+
@test does_namespacing(sys)
10+
11+
csys = complete(sys)
12+
@test iscomplete(csys)
13+
@test !does_namespacing(csys)
14+
15+
nsys = toggle_namespacing(sys, false)
16+
@test !iscomplete(nsys)
17+
@test !does_namespacing(nsys)
18+
19+
@test isequal(x, csys.x)
20+
@test isequal(x, nsys.x)
21+
@test !isequal(x, sys.x)
22+
@test isequal(p, csys.p)
23+
@test isequal(p, nsys.p)
24+
@test !isequal(p, sys.p)
25+
26+
@test_throws ["namespacing", "inner"] ODESystem(
27+
Equation[], t; systems = [nsys], name = :a)
28+
end
29+
30+
@testset "SDESystem" begin
31+
@variables x(t)
32+
@parameters p
33+
sys = SDESystem([D(x) ~ p * x], [x], t, [x], [p]; name = :inner)
34+
@test !iscomplete(sys)
35+
@test does_namespacing(sys)
36+
37+
csys = complete(sys)
38+
@test iscomplete(csys)
39+
@test !does_namespacing(csys)
40+
41+
nsys = toggle_namespacing(sys, false)
42+
@test !iscomplete(nsys)
43+
@test !does_namespacing(nsys)
44+
45+
@test isequal(x, csys.x)
46+
@test isequal(x, nsys.x)
47+
@test !isequal(x, sys.x)
48+
@test isequal(p, csys.p)
49+
@test isequal(p, nsys.p)
50+
@test !isequal(p, sys.p)
51+
52+
@test_throws ["namespacing", "inner"] SDESystem(
53+
Equation[], [], t; systems = [nsys], name = :a)
54+
end
55+
56+
@testset "DiscreteSystem" begin
57+
@variables x(t)
58+
@parameters p
59+
k = ShiftIndex(t)
60+
sys = DiscreteSystem([x(k) ~ p * x(k - 1)], t; name = :inner)
61+
@test !iscomplete(sys)
62+
@test does_namespacing(sys)
63+
64+
csys = complete(sys)
65+
@test iscomplete(csys)
66+
@test !does_namespacing(csys)
67+
68+
nsys = toggle_namespacing(sys, false)
69+
@test !iscomplete(nsys)
70+
@test !does_namespacing(nsys)
71+
72+
@test isequal(x, csys.x)
73+
@test isequal(x, nsys.x)
74+
@test !isequal(x, sys.x)
75+
@test isequal(p, csys.p)
76+
@test isequal(p, nsys.p)
77+
@test !isequal(p, sys.p)
78+
79+
@test_throws ["namespacing", "inner"] DiscreteSystem(
80+
Equation[], t; systems = [nsys], name = :a)
81+
end
82+
83+
@testset "ImplicitDiscreteSystem" begin
84+
@variables x(t)
85+
@parameters p
86+
k = ShiftIndex(t)
87+
sys = ImplicitDiscreteSystem([x(k) ~ p + x(k - 1) * x(k)], t; name = :inner)
88+
@test !iscomplete(sys)
89+
@test does_namespacing(sys)
90+
91+
csys = complete(sys)
92+
@test iscomplete(csys)
93+
@test !does_namespacing(csys)
94+
95+
nsys = toggle_namespacing(sys, false)
96+
@test !iscomplete(nsys)
97+
@test !does_namespacing(nsys)
98+
99+
@test isequal(x, csys.x)
100+
@test isequal(x, nsys.x)
101+
@test !isequal(x, sys.x)
102+
@test isequal(p, csys.p)
103+
@test isequal(p, nsys.p)
104+
@test !isequal(p, sys.p)
105+
106+
@test_throws ["namespacing", "inner"] ImplicitDiscreteSystem(
107+
Equation[], t; systems = [nsys], name = :a)
108+
end
109+
110+
@testset "NonlinearSystem" begin
111+
@variables x
112+
@parameters p
113+
sys = NonlinearSystem([x ~ p * x^2 + 1]; name = :inner)
114+
@test !iscomplete(sys)
115+
@test does_namespacing(sys)
116+
117+
csys = complete(sys)
118+
@test iscomplete(csys)
119+
@test !does_namespacing(csys)
120+
121+
nsys = toggle_namespacing(sys, false)
122+
@test !iscomplete(nsys)
123+
@test !does_namespacing(nsys)
124+
125+
@test isequal(x, csys.x)
126+
@test isequal(x, nsys.x)
127+
@test !isequal(x, sys.x)
128+
@test isequal(p, csys.p)
129+
@test isequal(p, nsys.p)
130+
@test !isequal(p, sys.p)
131+
132+
@test_throws ["namespacing", "inner"] NonlinearSystem(
133+
Equation[]; systems = [nsys], name = :a)
134+
end
135+
136+
@testset "OptimizationSystem" begin
137+
@variables x
138+
@parameters p
139+
sys = OptimizationSystem(p * x^2 + 1; name = :inner)
140+
@test !iscomplete(sys)
141+
@test does_namespacing(sys)
142+
143+
csys = complete(sys)
144+
@test iscomplete(csys)
145+
@test !does_namespacing(csys)
146+
147+
nsys = toggle_namespacing(sys, false)
148+
@test !iscomplete(nsys)
149+
@test !does_namespacing(nsys)
150+
151+
@test isequal(x, csys.x)
152+
@test isequal(x, nsys.x)
153+
@test !isequal(x, sys.x)
154+
@test isequal(p, csys.p)
155+
@test isequal(p, nsys.p)
156+
@test !isequal(p, sys.p)
157+
158+
@test_throws ["namespacing", "inner"] OptimizationSystem(
159+
[]; systems = [nsys], name = :a)
160+
end
161+
162+
@testset "ConstraintsSystem" begin
163+
@variables x
164+
@parameters p
165+
sys = ConstraintsSystem([x^2 + p ~ 0], [x], [p]; name = :inner)
166+
@test !iscomplete(sys)
167+
@test does_namespacing(sys)
168+
169+
csys = complete(sys)
170+
@test iscomplete(csys)
171+
@test !does_namespacing(csys)
172+
173+
nsys = toggle_namespacing(sys, false)
174+
@test !iscomplete(nsys)
175+
@test !does_namespacing(nsys)
176+
177+
@test isequal(x, csys.x)
178+
@test isequal(x, nsys.x)
179+
@test !isequal(x, sys.x)
180+
@test isequal(p, csys.p)
181+
@test isequal(p, nsys.p)
182+
@test !isequal(p, sys.p)
183+
184+
@test_throws ["namespacing", "inner"] ConstraintsSystem(
185+
[], [], []; systems = [nsys], name = :a)
186+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ end
9797
@safetestset "Analysis Points Test" include("analysis_points.jl")
9898
@safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl")
9999
@safetestset "Debugging Test" include("debugging.jl")
100+
@safetestset "Namespacing test" include("namespacing.jl")
100101
end
101102
end
102103

0 commit comments

Comments
 (0)