Skip to content

Commit

Permalink
Fixed bug in dictextension code and tested it
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Jan 28, 2020
1 parent 9177cdf commit 9c0c96e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/dictextension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ end
# be enforced early in call (in the `_setproperty!` methods)
function set_dictextension_property!(x, p, val)
if has_dictextension(x)
return not_property, setindex!(dictextension(x), val, _propname(p))
setindex!(dictextension(x), val, _propname(p))
return true
else
return not_property, not_property
return false
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/dictextension_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using FieldProperties: set_dictextension_property!

@testset "dictextension" begin
@test set_dictextension_property!(m, :testprop, 1)
@test set_dictextension_property!(MultiNest(Nested1(1,2),Nested2(3,4)), :testprop, 1) == false
end
19 changes: 18 additions & 1 deletion test/macro_composability.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

using FieldProperties: get_optional_properties_expr!, to_field_name, to_property_name, to_property, to_field_name
using FieldProperties: get_optional_properties_expr!,
to_field_name,
to_property_name,
to_property,
to_field_name,
is_dictextension,
has_optional_properties_expr

ex = :(:field => prop1)

Expand All @@ -20,3 +26,14 @@ end
@test to_field_name(:field) == to_field_name(ex)
@test to_field_name(QuoteNode(:field)) == to_field_name(ex)
end

@testset "is_dictextension" begin
@test is_dictextension(:dictextension)
@test is_dictextension(:(:field => dictextension))
@test is_dictextension(:(:field => dictextension(prop1, prop2)))
end

@testset "has_optional_properties" begin
@test has_optional_properties_expr(:dictextension) == false
@test has_optional_properties_expr(:(:field => dictextension(prop1, prop2)))
end
54 changes: 17 additions & 37 deletions test/nested_tests.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@

@testset "nest properties" begin
mutable struct Nested1
field1
field2
mn = MultiNest(Nested1(1,2), Nested2(3,4))

@test @inferred(FieldProperties.has_dictextension(mn)) == false
@test @inferred(FieldProperties.has_nested_fields(mn)) == true
@test @inferred(FieldProperties.nested_fields(mn)) == (:field1, :field2)
@test @inferred(FieldProperties.assigned_fields(mn)) == ()
# we can't infer nested property names unless they are inferreable (which for some reason
# they aren't for Nested1 and 2
@test propertynames(mn) == (:field1, :field2, :field3, :field4)

@test mn.field1 == 1
@test mn.field2 == 2
@test mn.field3 == 3
@test mn.field4 == 4

mn.field1 = 2
@test mn.field1 == 2
end

mutable struct Nested2
field3
field4
end

struct MultiNest{F1,F2}
field1::F1
field2::F2
end

@assignprops(
MultiNest,
:field1 => nested,
:field2 => nested
)

mn = MultiNest(Nested1(1,2), Nested2(3,4))

@test @inferred(FieldProperties.has_dictextension(mn)) == false
@test @inferred(FieldProperties.has_nested_fields(mn)) == true
@test @inferred(FieldProperties.nested_fields(mn)) == (:field1, :field2)
@test @inferred(FieldProperties.assigned_fields(mn)) == ()
# we can't infer nested property names unless they are inferreable (which for some reason
# they aren't for Nested1 and 2
@test propertynames(mn) == (:field1, :field2, :field3, :field4)

@test mn.field1 == 1
@test mn.field2 == 2
@test mn.field3 == 3
@test mn.field4 == 4

mn.field1 = 2
@test mn.field1 == 2
end
22 changes: 22 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ end

m = MyProperties("", Dict{Symbol,Any}())

mutable struct Nested1
field1
field2
end

mutable struct Nested2
field3
field4
end

struct MultiNest{F1,F2}
field1::F1
field2::F2
end

@assignprops(
MultiNest,
:field1 => nested,
:field2 => nested
)

@testset "@property Description{:description}::String" begin
encapsulated_getproperty(x) = getproperty(x, :description)

Expand Down Expand Up @@ -96,3 +117,4 @@ end


include("nested_tests.jl")
include("dictextension_tests.jl")

0 comments on commit 9c0c96e

Please sign in to comment.