From df875e29169016cf64f7002d3b656e81f076fdf8 Mon Sep 17 00:00:00 2001 From: Gaurav Arya Date: Fri, 12 Jan 2024 04:50:21 -0500 Subject: [PATCH] Add length check guards for delete methods (#31) * Add length check guards for delete methods * add test * Update test_ProtoStruct.jl * Update test_ProtoStruct.jl * Update test_ProtoStruct.jl * Update test_ProtoStruct.jl --------- Co-authored-by: Simon Christ Co-authored-by: Simon Christ --- src/ProtoStruct.jl | 16 ++++++++++++---- test/test_ProtoStruct.jl | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/ProtoStruct.jl b/src/ProtoStruct.jl index 8727e29..695bb02 100644 --- a/src/ProtoStruct.jl +++ b/src/ProtoStruct.jl @@ -116,8 +116,12 @@ macro proto(expr) error("The supertype of a proto struct is not redefinable. Please restart your julia session.") end the_methods = collect(methods($name)) - Base.delete_method(the_methods[1]) - Base.delete_method(the_methods[2]) + if length(the_methods) >= 1 + Base.delete_method(the_methods[1]) + end + if length(the_methods) >= 2 + Base.delete_method(the_methods[2]) + end end function $name($(fields...)) where {$(type_parameters...)} @@ -181,8 +185,12 @@ macro proto(expr) error("The supertype of a proto struct is not redefinable. Please restart your julia session.") end the_methods = collect(methods($name)) - Base.delete_method(the_methods[1]) - Base.delete_method(the_methods[2]) + if length(the_methods) >= 1 + Base.delete_method(the_methods[1]) + end + if length(the_methods) >= 2 + Base.delete_method(the_methods[2]) + end end function $name($(fields...)) where {$(type_parameters...)} diff --git a/test/test_ProtoStruct.jl b/test/test_ProtoStruct.jl index d70d301..7f918c2 100644 --- a/test/test_ProtoStruct.jl +++ b/test/test_ProtoStruct.jl @@ -168,6 +168,21 @@ end end end +@proto struct TestMethods end + +@testset "Constuctor updating I" begin + @test length(collect(methods(TestMethods))) == 1 +end + +@proto struct TestMethods + a + b +end + +@testset "Constuctor updating II" begin + @test length(collect(methods(TestMethods))) == 2 +end + """ This is a docstring. """