diff --git a/src/read_MTG/read_MTG.jl b/src/read_MTG/read_MTG.jl index fbfb52a..cd25447 100644 --- a/src/read_MTG/read_MTG.jl +++ b/src/read_MTG/read_MTG.jl @@ -76,6 +76,10 @@ end function parse_mtg_file(f, mtg_type) line = [0] l = [""] + classes = nothing + description = nothing + features = nothing + mtg = nothing l[1] = next_line!(f, line) while !eof(f) @@ -95,7 +99,7 @@ function parse_mtg_file(f, mtg_type) # Parse the mtg CLASSES section, and then continue to next while loop iteration: if issection(l[1], "CLASSES") - global classes = parse_section!(f, ["SYMBOL", "SCALE", "DECOMPOSITION", "INDEXATION", "DEFINITION"], "CLASSES", line, l) + classes = parse_section!(f, ["SYMBOL", "SCALE", "DECOMPOSITION", "INDEXATION", "DEFINITION"], "CLASSES", line, l) classes.SCALE = parse.(Int, classes.SCALE) replace!(classes.SYMBOL, "\$" => "Scene") continue @@ -103,7 +107,7 @@ function parse_mtg_file(f, mtg_type) # Parse the mtg DESCRIPTION section: if issection(l[1], "DESCRIPTION") - global description = parse_section!(f, ["LEFT", "RIGHT", "RELTYPE", "MAX"], "DESCRIPTION", line, l, allow_empty=true) + description = parse_section!(f, ["LEFT", "RIGHT", "RELTYPE", "MAX"], "DESCRIPTION", line, l, allow_empty=true) if description !== nothing description.RIGHT = split.(description.RIGHT, ",") if !all([i in description.RELTYPE for i in ("+", "<")]) @@ -116,13 +120,13 @@ function parse_mtg_file(f, mtg_type) # Parse the mtg FEATURES section: if issection(l[1], "FEATURES") - global features = parse_section!(f, ["NAME", "TYPE"], "FEATURES", line, l, allow_empty=true) + features = parse_section!(f, ["NAME", "TYPE"], "FEATURES", line, l, allow_empty=true) continue end # Parse the mtg FEATURES section: if issection(l[1], "MTG") - global mtg = parse_mtg!(f, classes, features, line, l, mtg_type) + mtg = parse_mtg!(f, classes, features, line, l, mtg_type) continue end diff --git a/test/test-read_mtg.jl b/test/test-read_mtg.jl index 6ae1ee1..2640cf6 100644 --- a/test/test-read_mtg.jl +++ b/test/test-read_mtg.jl @@ -62,3 +62,24 @@ end !MultiScaleTreeGraph.isroot(x) && @test isempty(node_attributes(x)) end end + +@testset "read_mtg parser state is local" begin + parser_state_names = (:classes, :description, :features, :mtg) + @test all(name -> !isdefined(MultiScaleTreeGraph, name), parser_state_names) + + files = [ + "files/simple_plant.mtg", + "files/simple_plant-blanks.mtg", + "files/simple_plant-P1U1.mtg", + "files/palm.mtg", + ] + + for file in files + mtg = read_mtg(file, NodeMTG) + @test node_id(mtg) == 1 + @test mtg[:symbols] isa Vector{String} + @test mtg[:scales] isa Vector{Int} + end + + @test all(name -> !isdefined(MultiScaleTreeGraph, name), parser_state_names) +end