######################################################################
# Voidables UTs
# -----
# Licensed under Apache License 2.0
module TestVoidables
using Test
using Pia.Voidables

negate(x) = !x

@testset "Voidables" begin
    @testset "fundamentals" begin
        let voidable = Voidable{Int}()
            @test voidable_param(voidable) === Int
            @test isvoided(voidable)
            @test_throws VoidError voidable[]
            
            @test (voidable[] = 42) == 42
            @test !isvoided(voidable) && voidable[] == 42
        end
        
        let voidable = Voidable(42)
            @test voidable_param(voidable) === Int
            @test !isvoided(voidable) && voidable[] == 42
            
            @test void!(voidable) === void
            @test isvoided(voidable)
            @test_throws VoidError voidable[]
        end
    end
    
    @testset "multi-ref" begin
        let base = Voidable{Int}(42), ref1 = base, ref2 = base, ref3 = ref1
            @test all(negate.(isvoided.((base, ref1, ref2, ref3))))
            @test void!(base) === void
            @test all(isvoided.((base, ref1, ref2, ref3)))
        end
    end
end

end # module TestVoidables
