Skip to content

Commit c01a406

Browse files
committed
Better tests for errors in amplitude construction
1 parent 3b95e72 commit c01a406

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

test/test_amplitudes.jl

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Test
2+
using IOCapture: IOCapture
23
using QuantumPropagators.Interfaces: check_amplitude, check_control
34
using QuantumPropagators.Shapes: flattop
45
using QuantumPropagators.Amplitudes: LockedAmplitude, ShapedAmplitude
@@ -25,7 +26,11 @@ using QuantumPropagators.Controls: discretize_on_midpoints
2526
@test length(get_controls(ampl)) == 0
2627
t = t_mid(tlist, 20)
2728
@test evaluate(ampl, tlist, 20) S(t)
28-
@test_throws Exception evaluate(ampl, t)
29+
captured = IOCapture.capture(rethrow = Union{}) do
30+
evaluate(ampl, t)
31+
end
32+
@test captured.error
33+
@test contains(captured.value.msg, "can only be evaluated with (tlist, n)")
2934

3035
end
3136

@@ -49,7 +54,6 @@ end
4954
t = t_mid(tlist, 20)
5055
@test evaluate(ampl, tlist, 20) ϵ(t) * S(t)
5156

52-
5357
ampl = ShapedAmplitude(ϵ, tlist; shape = S)
5458
@test startswith("$ampl", "ShapedAmplitude(")
5559
controls = get_controls(ampl)
@@ -58,7 +62,14 @@ end
5862
@test check_amplitude(ampl; tlist)
5963
t = t_mid(tlist, 20)
6064
@test evaluate(ampl, tlist, 20) ϵ(t) * S(t)
61-
@test_throws Exception evaluate(ampl, t)
65+
captured = IOCapture.capture(rethrow = Union{}) do
66+
evaluate(ampl, t)
67+
end
68+
@test captured.error
69+
@test contains(
70+
captured.value.msg,
71+
"vector control and shape can only be evaluated with (tlist, n)"
72+
)
6273

6374
end
6475

@@ -116,7 +127,11 @@ end
116127
@test check_amplitude(ampl; tlist)
117128
t = t_mid(tlist, 20)
118129
@test evaluate(ampl, tlist, 20) ϵ(t) * S(t)
119-
@test_throws Exception evaluate(ampl, t)
130+
captured = IOCapture.capture(rethrow = Union{}) do
131+
evaluate(ampl, t)
132+
end
133+
@test captured.error
134+
@test contains(captured.value.msg, "vector shape can only be evaluated with (tlist, n)")
120135

121136
# vector control, callable shape
122137
ampl = ShapedAmplitude(ϵ_vec; shape = S)
@@ -126,7 +141,14 @@ end
126141
@test check_amplitude(ampl; tlist)
127142
t = t_mid(tlist, 20)
128143
@test evaluate(ampl, tlist, 20) ϵ(t) * S(t)
129-
@test_throws Exception evaluate(ampl, t)
144+
captured = IOCapture.capture(rethrow = Union{}) do
145+
evaluate(ampl, t)
146+
end
147+
@test captured.error
148+
@test contains(
149+
captured.value.msg,
150+
"vector control can only be evaluated with (tlist, n)"
151+
)
130152

131153
end
132154

@@ -144,6 +166,13 @@ end
144166
@test check_amplitude(ampl2; tlist)
145167
t = t_mid(tlist, 20)
146168
@test evaluate(ampl2, tlist, 20) ϵ(t) * S(t)
147-
@test_throws Exception evaluate(ampl2, t)
169+
captured = IOCapture.capture(rethrow = Union{}) do
170+
evaluate(ampl2, t)
171+
end
172+
@test captured.error
173+
@test contains(
174+
captured.value.msg,
175+
"vector control can only be evaluated with (tlist, n)"
176+
)
148177

149178
end

0 commit comments

Comments
 (0)