Skip to content

Commit 1cb6ea7

Browse files
committed
Add assertions in NodeMTG and MutableNodeMTG + tests
1 parent 947ec97 commit 1cb6ea7

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

src/types/AbstractNodeMTG.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,31 @@ struct NodeMTG <: AbstractNodeMTG
3434
symbol::String
3535
index::Int
3636
scale::Int
37+
38+
function NodeMTG(link, symbol, index, scale)
39+
@assert scale >= 0 "The scale should be greater than or equal to 0."
40+
@assert link in ["/", "<", "+"] "The link should be one of '/', '<', '+'"
41+
return new(link, symbol, index, scale)
42+
end
3743
end
3844

39-
NodeMTG(link, symbol, index::Nothing, scale) = NodeMTG(link, symbol, -9999, scale)
45+
function NodeMTG(link, symbol, index::Nothing, scale)
46+
return NodeMTG(link, symbol, -9999, scale)
47+
end
4048

4149
mutable struct MutableNodeMTG <: AbstractNodeMTG
4250
link::String
4351
symbol::String
4452
index::Int
4553
scale::Int
54+
55+
function MutableNodeMTG(link, symbol, index, scale)
56+
@assert scale >= 0 "The scale should be greater than or equal to 0."
57+
@assert link in ["/", "<", "+"] "The link should be one of '/', '<', '+'"
58+
new(link, symbol, index, scale)
59+
end
4660
end
4761

48-
MutableNodeMTG(link, symbol, index::Nothing, scale) = MutableNodeMTG(link, symbol, -9999, scale)
62+
function MutableNodeMTG(link, symbol, index::Nothing, scale)
63+
MutableNodeMTG(link, symbol, -9999, scale)
64+
end

test/test-nodes.jl

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
21
# Create a node:
32

3+
@testset "NodeMTG and MutableNodeMTG" begin
4+
# Test NodeMTG constructor with all arguments
5+
node_mtg = MultiScaleTreeGraph.NodeMTG("/", "Plant", 1, 2)
6+
@test node_mtg.link == "/"
7+
@test node_mtg.symbol == "Plant"
8+
@test node_mtg.index == 1
9+
@test node_mtg.scale == 2
10+
11+
# Test NodeMTG constructor with nothing as index
12+
node_mtg_nothing = MultiScaleTreeGraph.NodeMTG("/", "Internode", nothing, 3)
13+
@test node_mtg_nothing.link == "/"
14+
@test node_mtg_nothing.symbol == "Internode"
15+
@test node_mtg_nothing.index == -9999
16+
@test node_mtg_nothing.scale == 3
17+
18+
# Test MutableNodeMTG constructor with all arguments
19+
mutable_node_mtg = MultiScaleTreeGraph.MutableNodeMTG("+", "Leaf", 2, 4)
20+
@test mutable_node_mtg.link == "+"
21+
@test mutable_node_mtg.symbol == "Leaf"
22+
@test mutable_node_mtg.index == 2
23+
@test mutable_node_mtg.scale == 4
24+
25+
# Test MutableNodeMTG constructor with nothing as index
26+
mutable_node_mtg_nothing = MultiScaleTreeGraph.MutableNodeMTG("<", "Apex", nothing, 5)
27+
@test mutable_node_mtg_nothing.link == "<"
28+
@test mutable_node_mtg_nothing.symbol == "Apex"
29+
@test mutable_node_mtg_nothing.index == -9999
30+
@test mutable_node_mtg_nothing.scale == 5
31+
32+
# Test mutability of MutableNodeMTG
33+
mutable_node_mtg.link = "<"
34+
mutable_node_mtg.symbol = "Flower"
35+
mutable_node_mtg.index = 3
36+
mutable_node_mtg.scale = 6
37+
@test mutable_node_mtg.link == "<"
38+
@test mutable_node_mtg.symbol == "Flower"
39+
@test mutable_node_mtg.index == 3
40+
@test mutable_node_mtg.scale == 6
41+
42+
# Test assertions
43+
@test_throws AssertionError MultiScaleTreeGraph.NodeMTG("/", "Plant", 1, -1) # scale < 0
44+
@test_throws AssertionError MultiScaleTreeGraph.NodeMTG("invalid", "Plant", 1, 1) # invalid link
45+
@test_throws AssertionError MultiScaleTreeGraph.MutableNodeMTG("/", "Plant", 1, -1) # scale < 0
46+
@test_throws AssertionError MultiScaleTreeGraph.MutableNodeMTG("invalid", "Plant", 1, 1) # invalid link
47+
end
48+
449
@testset "Create node" begin
550
mtg_code = MultiScaleTreeGraph.NodeMTG("/", "Plant", 1, 1)
651
mtg = MultiScaleTreeGraph.Node(mtg_code)

0 commit comments

Comments
 (0)