Skip to content

Commit 87464f7

Browse files
committed
Add tests for various erroneous declarations
1 parent 37ea1d9 commit 87464f7

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

src/dsl.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function make_reaction_system(ex::Expr, name)
330330
end
331331
forbidden_symbol_check(union(species, parameters))
332332
forbidden_variable_check(variables)
333-
unique_symbol_check(union(species, parameters, variables, ivs))
333+
unique_symbol_check(vcat(species, parameters, variables, ivs))
334334

335335
# Creates expressions corresponding to actual code from the internal DSL representation.
336336
psexpr_init = get_pexpr(parameters_extracted, options)
@@ -783,6 +783,7 @@ function read_observed_options(options, species_n_vars_declared, ivs_sorted)
783783
error("A forbidden symbol ($(obs_eq.args[2])) was used as an observable name.")
784784
end
785785
if (obs_name in species_n_vars_declared) && is_escaped_expr(obs_eq.args[2])
786+
println("HERE")
786787
error("An interpolated observable have been used, which has also been explicitly declared within the system using either @species or @variables. This is not permitted.")
787788
end
788789
if ((obs_name in species_n_vars_declared) || is_escaped_expr(obs_eq.args[2])) &&

test/dsl/dsl_advanced_model_construction.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ let
267267
@test isequal(rn1,rn2)
268268
end
269269

270+
# Tests that erroneous metadata declarations yields errors.
271+
let
272+
# Malformed metadata/value separator.
273+
@test_throws Exception @eval @reaction_network begin
274+
d, X --> 0, [misc=>"Metadata should use `=`, not `=>`."]
275+
end
276+
277+
# Malformed lhs
278+
@test_throws Exception @eval @reaction_network begin
279+
d, X --> 0, [misc,description=>"Metadata lhs should be a single symbol."]
280+
end
281+
282+
# Malformed metadata separator.
283+
@test_throws Exception @eval @reaction_network begin
284+
d, X --> 0, [misc=>:misc; description="description"]
285+
end
286+
end
287+
270288
### Other Tests ###
271289

272290
# Test floating point stoichiometry work.
@@ -344,4 +362,4 @@ let
344362
rx = Reaction(k[1]*a+k[2], [X[1], X[2]], [Y, C], [1, V[1]], [V[2] * W, B])
345363
@named arrtest = ReactionSystem([rx], t)
346364
arrtest == rn
347-
end
365+
end

test/dsl/dsl_basic_model_construction.jl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ let
463463
@test rn1 == rn2
464464
end
465465

466-
# Tests arrow variants in `@reaction`` macro .
466+
# Tests arrow variants in `@reaction`` macro.
467467
let
468468
@test isequal((@reaction k, 0 --> X), (@reaction k, X <-- 0))
469469
@test isequal((@reaction k, 0 --> X), (@reaction k, X 0))
@@ -496,6 +496,41 @@ let
496496
@test_throws LoadError @eval @reaction nothing, 0 --> B
497497
@test_throws LoadError @eval @reaction k, 0 --> im
498498
@test_throws LoadError @eval @reaction k, 0 --> nothing
499+
500+
# Checks that non-supported arrow type usage yields error.
501+
@test_throws Exception @eval @reaction_network begin
502+
d, X 0
503+
end
499504
end
500505

506+
### Error Test ###
507+
508+
# Erroneous `@reaction` usage.
509+
let
510+
# Bi-directional reaction using the `@reaction` macro.
511+
@test_throws Exception @eval @reaction (k1,k2), X1 <--> X2
501512

513+
# Bundles reactions.
514+
@test_throws Exception @eval @reaction k, (X1,X2) --> 0
515+
end
516+
517+
# Tests that malformed reactions yields errors.
518+
let
519+
# Checks that malformed combinations of entries yields errors.
520+
@test_throws Exception @eval @reaction_network begin
521+
d, X --> 0, Y --> 0
522+
end
523+
@test_throws Exception @eval @reaction_network begin
524+
d, X --> 0, [misc="Ok metadata"], [description="Metadata in (erroneously) extra []."]
525+
end
526+
527+
# Checks that incorrect bundling yields error.
528+
@test_throws Exception @eval @reaction_network begin
529+
(k1,k2,k3), (X1,X2) --> 0
530+
end
531+
532+
# Checks that incorrect stoichiometric expression yields an error.
533+
@test_throws Exception @eval @reaction_network begin
534+
k, X^Y --> XY
535+
end
536+
end

test/dsl/dsl_options.jl

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,44 @@ let
414414
spcs = (A, B1, B2, C)
415415
@test issetequal(unknowns(rn), sts)
416416
@test issetequal(species(rn), spcs)
417+
end
417418

418-
@test_throws ArgumentError begin
419-
rn = @reaction_network begin
420-
@variables K
421-
k, K*A --> B
422-
end
419+
# Tests errors in `@variables` declarations.
420+
let
421+
# Variable used as species in reaction.
422+
@test_throws Exception @eval rn = @reaction_network begin
423+
@variables K
424+
k, K*A --> B
425+
end
426+
427+
# Tests error when disallowed name is used for variable.
428+
@test_throws Exception @eval @reaction_network begin
429+
@variables π(t)
430+
end
431+
end
432+
433+
434+
# Tests that duplicate iv/parameter/species/variable names cannot be provided.
435+
let
436+
@test_throws Exception @eval @reaction_network begin
437+
@spatial_ivs X
438+
@species X(t)
439+
end
440+
@test_throws Exception @eval @reaction_network begin
441+
@parameters X
442+
@species X(t)
443+
end
444+
@test_throws Exception @eval @reaction_network begin
445+
@species X(t)
446+
@variables X(t)
447+
end
448+
@test_throws Exception @eval @reaction_network begin
449+
@parameters X
450+
@variables X(t)
423451
end
424452
end
425453

454+
426455
### Test Independent Variable Designations ###
427456

428457
# Test ivs in DSL.
@@ -739,6 +768,14 @@ let
739768
@observables $X ~ X1 + X2
740769
(k1,k2), X1 <--> X2
741770
end
771+
772+
# Observable metadata provided twice.
773+
@test_throws Exception @eval @reaction_network begin
774+
@species X2 [description="Twice the amount of X"]
775+
@observables (X2, [description="X times two."]) ~ 2X
776+
d, X --> 0
777+
end
778+
742779
end
743780

744781

@@ -916,8 +953,15 @@ let
916953
@equations X ~ p - S
917954
(P,D), 0 <--> S
918955
end
956+
957+
# Differential equation using a forbidden variable (in the DSL).
958+
@test_throws Exception @eval @reaction_network begin
959+
@equations D(π) ~ -1
960+
end
919961
end
920962

963+
### Other DSL Option Tests ###
964+
921965
# test combinatoric_ratelaws DSL option
922966
let
923967
rn = @reaction_network begin
@@ -951,3 +995,11 @@ let
951995
@unpack k1, A = rn3
952996
@test isequal(rl, k1*A^2)
953997
end
998+
999+
# Erroneous `@default_noise_scaling` declaration (other noise scaling tests are mostly in the SDE file).
1000+
let
1001+
# Default noise scaling with multiple entries.
1002+
@test_throws Exception @eval @reaction_network begin
1003+
@default_noise_scaling η1 η2
1004+
end
1005+
end

0 commit comments

Comments
 (0)