diff --git a/test/runtests.jl b/test/runtests.jl index b0e2374..86f8ded 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,261 +2,7 @@ using Test using MixedStructTypes -@sum_struct_type @kwdef A{X,Y} begin - mutable struct B{X} - a::Tuple{X, X} - b::Tuple{Float64, Float64} - const c::Symbol - end - mutable struct C - a::Tuple{Int, Int} - d::Int32 - e::Bool - const c::Symbol - end - struct D{Y} - a::Tuple{Int, Int} - f::Y - g::Tuple{Complex, Complex} - c::Symbol - end +@testset "MixedStructTypes.jl Tests" begin + include("test_sum_struct_type_macro.jl") + include("test_compact_struct_type_macro.jl") end - -@sum_struct_type @kwdef Animal{T,N,J} begin - mutable struct Wolf{T,N} - energy::T = 0.5 - ground_speed::N - const fur_color::Symbol - end - mutable struct Hawk{T,N,J} - energy::T = 0.1 - ground_speed::N - flight_speed::J - end -end - -abstract type AbstractSimple end -@sum_struct_type Simple <: AbstractSimple begin - struct SimpleA - x - z::Int - end - struct SimpleB - y - q::String - end -end - -@sum_struct_type TestOrder1 begin - struct TestOrder11 - x::String - y::Float64 - end - struct TestOrder12 - y::Float64 - z::Vector{Int} - x::String - end -end - -@testset "@sum_struct_type" begin - - b = B((1,1), (1.0, 1.0), :s) - c1 = C((1,1), 1, 1, :c) - c2 = C(; a = (1,1), d = 1, e = 1, c = :c) - - @test b.a == (1,1) - @test b.b == (1.0, 1.0) - @test b.c == :s - @test c1.d === c2.d === Int32(1) - @test c1.e === c2.e === true - - b.a = (3, 3) - @test b.a == (3, 3) - - @test kindof(b) == :B - @test MixedStructTypes.constructor(b) == B - @test propertynames(b) == (:a, :b, :c) - - hawk_1 = Hawk(1.0, 2.0, 3) - hawk_2 = Hawk(; ground_speed = 2.3, flight_speed = 2) - wolf_1 = Wolf(2.0, 3.0, :black) - wolf_2 = Wolf(; ground_speed = 2.0, fur_color = :white) - wolf_3 = Wolf{Int, Float64}(2.0, 3.0, :black) - wolf_4 = Wolf{Float64, Float64}(; ground_speed = 2.0, fur_color = :white) - - @test hawk_1.energy == 1.0 - @test hawk_2.energy == 0.1 - @test wolf_1.energy == 2.0 - @test wolf_2.energy == 0.5 - @test wolf_3.energy === 2 && wolf_4.energy === 0.5 - @test hawk_1.flight_speed == 3 - @test hawk_2.flight_speed == 2 - @test wolf_1.fur_color == :black - @test wolf_2.fur_color == :white - @test_throws "" hawk_1.fur_color - @test_throws "" wolf_1.flight_speed - @test kindof(hawk_1) == kindof(hawk_2) == :Hawk - @test kindof(wolf_1) == kindof(wolf_2) == :Wolf - - b = SimpleA(1, 3) - c = SimpleB(2, "a") - - @test b.x == 1 && b.z == 3 - @test c.y == 2 && c.q == "a" - @test_throws "" b.y - @test_throws "" b.q - @test_throws "" c.x - @test_throws "" c.z - @test kindof(b) == :SimpleA - @test kindof(c) == :SimpleB - @test Simple <: AbstractSimple - @test b isa Simple && c isa Simple - - o1 = TestOrder11("a", 2.0) - o2 = TestOrder12(3.0, [1], "b") - - @test propertynames(o1) == (:x, :y) - @test propertynames(o2) == (:y, :z, :x) - @test o1.x == "a" && o2.x == "b" - @test o1.y == 2.0 && o2.y == 3.0 - @test o2.z == [1] - @test_throws "" o1.z -end - -@static if VERSION >= v"1.10" - @testset "copy tests @struct_sum_type" begin - b = B((1,1), (1.0, 1.0), :s) - copy_b = copy(b) - @test copy_b.a == b.a - @test kindof(copy_b) == kindof(b) - end -end - -@compact_struct_type @kwdef E{X,Y} begin - mutable struct F{X} - a::Tuple{X, X} - b::Tuple{Float64, Float64} - const c::Symbol - end - mutable struct G{X} - a::Tuple{X, X} - d::Int32 - e::Bool - const c::Symbol - end - mutable struct H{X,Y} - a::Tuple{X, X} - f::Y - g::Tuple{Complex, Complex} - const c::Symbol - end -end - -@compact_struct_type @kwdef Animal2{T,N,J} begin - mutable struct Wolf2{T,N} - energy::T = 0.5 - ground_speed::N - const fur_color::Symbol - end - mutable struct Hawk2{T,N,J} - energy::T = 0.1 - ground_speed::N - flight_speed::J - end -end - -abstract type AbstractSimple2 end -@compact_struct_type Simple2 <: AbstractSimple2 begin - struct SimpleA2 - x - z::Int - end - struct SimpleB2 - y - q::String - end -end - -@compact_struct_type TestOrder2 begin - struct TestOrder21 - x::String - y::Float64 - end - struct TestOrder22 - y::Float64 - z::Vector{Int} - x::String - end -end - -@testset "@compact_struct_type" begin - - f = F((1,1), (1.0, 1.0), :s) - g1 = G((1,1), 1, 1, :c) - g2 = G(; a = (1,1), d = 1, e = 1, c = :c) - - @test f.a == (1,1) - @test f.b == (1.0, 1.0) - @test f.c == :s - @test g1.d === g2.d === Int32(1) - @test g1.e === g2.e === true - - f.a = (3, 3) - @test f.a == (3, 3) - - @test kindof(f) == :F - @test MixedStructTypes.constructor(f) == F - @test propertynames(f) == (:a, :b, :c) - - copy_f = copy(f) - @test copy_f.a == f.a - @test kindof(copy_f) == kindof(f) - - hawk_1 = Hawk2(1.0, 2.0, 3) - hawk_2 = Hawk2(; ground_speed = 2.3, flight_speed = 2) - wolf_1 = Wolf2(2.0, 3.0, :black) - wolf_2 = Wolf2(; ground_speed = 2.0, fur_color = :white) - wolf_3 = Wolf2{Int, Float64}(2.0, 3.0, :black) - wolf_4 = Wolf2{Float64, Float64}(; ground_speed = 2.0, fur_color = :white) - - @test hawk_1.energy == 1.0 - @test hawk_2.energy == 0.1 - @test wolf_1.energy == 2.0 - @test wolf_2.energy == 0.5 - @test wolf_3.energy === 2 && wolf_4.energy === 0.5 - @test hawk_1.flight_speed == 3 - @test hawk_2.flight_speed == 2 - @test wolf_1.fur_color == :black - @test wolf_2.fur_color == :white - @test_throws "" hawk_1.fur_color - @test_throws "" wolf_1.flight_speed - @test kindof(hawk_1) == kindof(hawk_2) == :Hawk2 - @test kindof(wolf_1) == kindof(wolf_2) == :Wolf2 - - - b = SimpleA2(1, 3) - c = SimpleB2(2, "a") - - @test b.x == 1 && b.z == 3 - @test c.y == 2 && c.q == "a" - @test_throws "" b.y - @test_throws "" b.q - @test_throws "" c.x - @test_throws "" c.z - @test kindof(b) == :SimpleA2 - @test kindof(c) == :SimpleB2 - @test Simple2 <: AbstractSimple2 - @test b isa Simple2 && c isa Simple2 - - o1 = TestOrder21("a", 2.0) - o2 = TestOrder22(3.0, [1], "b") - - @test propertynames(o1) == (:x, :y) - @test propertynames(o2) == (:y, :z, :x) - @test o1.x == "a" && o2.x == "b" - @test o1.y == 2.0 && o2.y == 3.0 - @test o2.z == [1] - @test_throws "" o1.z -end - diff --git a/test/test_compact_struct_type_macro.jl b/test/test_compact_struct_type_macro.jl new file mode 100644 index 0000000..f27d450 --- /dev/null +++ b/test/test_compact_struct_type_macro.jl @@ -0,0 +1,126 @@ +@compact_struct_type @kwdef E{X,Y} begin + mutable struct F{X} + a::Tuple{X, X} + b::Tuple{Float64, Float64} + const c::Symbol + end + mutable struct G{X} + a::Tuple{X, X} + d::Int32 + e::Bool + const c::Symbol + end + mutable struct H{X,Y} + a::Tuple{X, X} + f::Y + g::Tuple{Complex, Complex} + const c::Symbol + end +end + +@compact_struct_type @kwdef Animal2{T,N,J} begin + mutable struct Wolf2{T,N} + energy::T = 0.5 + ground_speed::N + const fur_color::Symbol + end + mutable struct Hawk2{T,N,J} + energy::T = 0.1 + ground_speed::N + flight_speed::J + end +end + +abstract type AbstractSimple2 end +@compact_struct_type Simple2 <: AbstractSimple2 begin + struct SimpleA2 + x + z::Int + end + struct SimpleB2 + y + q::String + end +end + +@compact_struct_type TestOrder2 begin + struct TestOrder21 + x::String + y::Float64 + end + struct TestOrder22 + y::Float64 + z::Vector{Int} + x::String + end +end + +@testset "@compact_struct_type" begin + + f = F((1,1), (1.0, 1.0), :s) + g1 = G((1,1), 1, 1, :c) + g2 = G(; a = (1,1), d = 1, e = 1, c = :c) + + @test f.a == (1,1) + @test f.b == (1.0, 1.0) + @test f.c == :s + @test g1.d === g2.d === Int32(1) + @test g1.e === g2.e === true + + f.a = (3, 3) + @test f.a == (3, 3) + + @test kindof(f) == :F + @test MixedStructTypes.constructor(f) == F + @test propertynames(f) == (:a, :b, :c) + + copy_f = copy(f) + @test copy_f.a == f.a + @test kindof(copy_f) == kindof(f) + + hawk_1 = Hawk2(1.0, 2.0, 3) + hawk_2 = Hawk2(; ground_speed = 2.3, flight_speed = 2) + wolf_1 = Wolf2(2.0, 3.0, :black) + wolf_2 = Wolf2(; ground_speed = 2.0, fur_color = :white) + wolf_3 = Wolf2{Int, Float64}(2.0, 3.0, :black) + wolf_4 = Wolf2{Float64, Float64}(; ground_speed = 2.0, fur_color = :white) + + @test hawk_1.energy == 1.0 + @test hawk_2.energy == 0.1 + @test wolf_1.energy == 2.0 + @test wolf_2.energy == 0.5 + @test wolf_3.energy === 2 && wolf_4.energy === 0.5 + @test hawk_1.flight_speed == 3 + @test hawk_2.flight_speed == 2 + @test wolf_1.fur_color == :black + @test wolf_2.fur_color == :white + @test_throws "" hawk_1.fur_color + @test_throws "" wolf_1.flight_speed + @test kindof(hawk_1) == kindof(hawk_2) == :Hawk2 + @test kindof(wolf_1) == kindof(wolf_2) == :Wolf2 + + + b = SimpleA2(1, 3) + c = SimpleB2(2, "a") + + @test b.x == 1 && b.z == 3 + @test c.y == 2 && c.q == "a" + @test_throws "" b.y + @test_throws "" b.q + @test_throws "" c.x + @test_throws "" c.z + @test kindof(b) == :SimpleA2 + @test kindof(c) == :SimpleB2 + @test Simple2 <: AbstractSimple2 + @test b isa Simple2 && c isa Simple2 + + o1 = TestOrder21("a", 2.0) + o2 = TestOrder22(3.0, [1], "b") + + @test propertynames(o1) == (:x, :y) + @test propertynames(o2) == (:y, :z, :x) + @test o1.x == "a" && o2.x == "b" + @test o1.y == 2.0 && o2.y == 3.0 + @test o2.z == [1] + @test_throws "" o1.z +end diff --git a/test/test_sum_struct_type_macro.jl b/test/test_sum_struct_type_macro.jl new file mode 100644 index 0000000..a4bf514 --- /dev/null +++ b/test/test_sum_struct_type_macro.jl @@ -0,0 +1,130 @@ +@sum_struct_type @kwdef A{X,Y} begin + mutable struct B{X} + a::Tuple{X, X} + b::Tuple{Float64, Float64} + const c::Symbol + end + mutable struct C + a::Tuple{Int, Int} + d::Int32 + e::Bool + const c::Symbol + end + struct D{Y} + a::Tuple{Int, Int} + f::Y + g::Tuple{Complex, Complex} + c::Symbol + end +end + +@sum_struct_type @kwdef Animal{T,N,J} begin + mutable struct Wolf{T,N} + energy::T = 0.5 + ground_speed::N + const fur_color::Symbol + end + mutable struct Hawk{T,N,J} + energy::T = 0.1 + ground_speed::N + flight_speed::J + end +end + +abstract type AbstractSimple end +@sum_struct_type Simple <: AbstractSimple begin + struct SimpleA + x + z::Int + end + struct SimpleB + y + q::String + end +end + +@sum_struct_type TestOrder1 begin + struct TestOrder11 + x::String + y::Float64 + end + struct TestOrder12 + y::Float64 + z::Vector{Int} + x::String + end +end + +@testset "@sum_struct_type" begin + + b = B((1,1), (1.0, 1.0), :s) + c1 = C((1,1), 1, 1, :c) + c2 = C(; a = (1,1), d = 1, e = 1, c = :c) + + @test b.a == (1,1) + @test b.b == (1.0, 1.0) + @test b.c == :s + @test c1.d === c2.d === Int32(1) + @test c1.e === c2.e === true + + b.a = (3, 3) + @test b.a == (3, 3) + + @test kindof(b) == :B + @test MixedStructTypes.constructor(b) == B + @test propertynames(b) == (:a, :b, :c) + + hawk_1 = Hawk(1.0, 2.0, 3) + hawk_2 = Hawk(; ground_speed = 2.3, flight_speed = 2) + wolf_1 = Wolf(2.0, 3.0, :black) + wolf_2 = Wolf(; ground_speed = 2.0, fur_color = :white) + wolf_3 = Wolf{Int, Float64}(2.0, 3.0, :black) + wolf_4 = Wolf{Float64, Float64}(; ground_speed = 2.0, fur_color = :white) + + @test hawk_1.energy == 1.0 + @test hawk_2.energy == 0.1 + @test wolf_1.energy == 2.0 + @test wolf_2.energy == 0.5 + @test wolf_3.energy === 2 && wolf_4.energy === 0.5 + @test hawk_1.flight_speed == 3 + @test hawk_2.flight_speed == 2 + @test wolf_1.fur_color == :black + @test wolf_2.fur_color == :white + @test_throws "" hawk_1.fur_color + @test_throws "" wolf_1.flight_speed + @test kindof(hawk_1) == kindof(hawk_2) == :Hawk + @test kindof(wolf_1) == kindof(wolf_2) == :Wolf + + b = SimpleA(1, 3) + c = SimpleB(2, "a") + + @test b.x == 1 && b.z == 3 + @test c.y == 2 && c.q == "a" + @test_throws "" b.y + @test_throws "" b.q + @test_throws "" c.x + @test_throws "" c.z + @test kindof(b) == :SimpleA + @test kindof(c) == :SimpleB + @test Simple <: AbstractSimple + @test b isa Simple && c isa Simple + + o1 = TestOrder11("a", 2.0) + o2 = TestOrder12(3.0, [1], "b") + + @test propertynames(o1) == (:x, :y) + @test propertynames(o2) == (:y, :z, :x) + @test o1.x == "a" && o2.x == "b" + @test o1.y == 2.0 && o2.y == 3.0 + @test o2.z == [1] + @test_throws "" o1.z +end + +@static if VERSION >= v"1.10" + @testset "copy tests @struct_sum_type" begin + b = B((1,1), (1.0, 1.0), :s) + copy_b = copy(b) + @test copy_b.a == b.a + @test kindof(copy_b) == kindof(b) + end +end