Skip to content

Commit

Permalink
Expand type of a property with union and add tests on it
Browse files Browse the repository at this point in the history
  • Loading branch information
AleMorales committed Dec 16, 2024
1 parent da44d81 commit bd0b200
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Mesh/Mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,21 @@ julia> add_property!(r, :absorbed_PAR, [0.0, 0.0]);
function add_property!(m::Mesh, prop::Symbol, data, nt = ntriangles(m))
# Check if the data is an array and if not convert it to an array with length nt
vecdata = data isa AbstractVector ? data : fill(data, nt)
# Create new property if the one being added does not exist
if !haskey(properties(m), prop)
properties(m)[prop] = vecdata
# Otherwise append the data to the existing property creating an union type if necessary
# when the type of `data` does not inherit from the type of the existing property
else
append!(properties(m)[prop], vecdata)
etype1 = eltype(properties(m)[prop])
etype2 = eltype(vecdata)
if etype2 isa etype1
append!(properties(m)[prop], vecdata)
else
etype3 = Union{etype1, etype2}
properties(m)[prop] = convert(Vector{etype3}, properties(m)[prop])
append!(properties(m)[prop], vecdata)
end
end
return m
end
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ end
@testset "mesh_io" begin
include("test_meshio.jl")
end

# Different cases where properties are added
@testset "properties" begin
include("test_properties.jl")
end
28 changes: 28 additions & 0 deletions test/test_properties.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import PlantGeomPrimitives as PGP
using Test


module dummy
struct bar end

struct foo end

struct oomp end
end

let

import .dummy as D

r = PGP.Rectangle()
e = PGP.Ellipse()
t = PGP.Triangle()

PGP.add_property!(r, :prop, D.bar())
eltype(PGP.properties(r)[:prop]) == D.bar
PGP.add!(r, e, prop = D.foo())
eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo}
PGP.add!(r, t, prop = D.oomp())
eltype(PGP.properties(r)[:prop]) == Union{D.bar, D.foo, D.oomp}

end

0 comments on commit bd0b200

Please sign in to comment.