Skip to content

Instantly share code, notes, and snippets.

@mauro3 mauro3/kwdef-test.jl Secret
Created Sep 24, 2018

Embed
What would you like to do?
kwdef-test.jl
using Test, Markdown, REPL
using Base: @kwdef
##########
# @kwdef
##########
"Test documentation"
@kwdef struct MT1
"Field r"
r::Int = 4
"A field"
c = "sdaf"
end
@test MT1().r==4
@test MT1().c=="sdaf"
@test "Test documentation\n" == Markdown.plain(@doc MT1)
# https://github.com/JuliaLang/julia/issues/27092 means this does not work:
# @test "A field Default: sdaf\n" == Markdown.plain(@doc MT1.c)
@test "Field r\n" == Markdown.plain(REPL.fielddoc(MT1, :r))
@test "A field\n" == Markdown.plain(REPL.fielddoc(MT1, :c))
# parameter-less
@kwdef struct MT2
r::Int
c
a::Float64
end
@test MT2(r=4, a=5., c=6) == MT2(4,6,5)
@test MT2(r=4, a=5, c="asdf") == MT2(4,"asdf",5)
@test_throws UndefKeywordError MT2(r=4)
@test_throws UndefKeywordError MT2()
@kwdef struct MT3
r::Int=5
a::Float64
end
@test MT3(r=4, a=5.) == MT3(4,5)
@test MT3(a=5) == MT3(5,5)
@test_throws UndefKeywordError MT3(r=4)
@test_throws UndefKeywordError MT3()
# parameter-less with supertype
abstract type MM1 end
@kwdef struct MT3_1 <: MM1
r::Int = 5
a::Float64
end
@test MT3_1(r=4, a=5.) == MT3_1(4,5)
@test MT3_1(a=5) == MT3_1(5,5)
@test_throws UndefKeywordError MT3_1(r=4)
@test_throws UndefKeywordError MT3_1()
abstract type MM2{T} end
@kwdef struct MT3_2 <: MM2{Int}
r::Int = 5
a::Float64
end
@test MT3_2(r=4, a=5.) == MT3_2(4,5)
@test MT3_2(a=5) == MT3_2(5,5)
@test_throws UndefKeywordError MT3_2(r=4)
@test_throws UndefKeywordError MT3_2()
# with type-parameters
@kwdef struct MT4{R,I} <: MM1
r::R=5
a::I
end
@test MT4(r=4, a=5.0) == MT4(4,5.0)
@test MT4{Float32, Int}(r=4, a=5.0) == MT4(4.0f0,5) == MT4(r=4.0f0,a=5)
@test MT4{Float32, Int}(a=5.) == MT4(5.0f0,5)
@test_throws UndefKeywordError MT4{Float32, Int}()
@test_throws InexactError MT4{Float32,Int}(a=5.5)
@test_throws InexactError MT4{Float32,Int}(5.5, 5.5)
@kwdef struct I10{T}
a::T
b::Int = 10
c::T="aaa"
end
@test_throws UndefKeywordError I10()
@test I10(1,2,3) == I10{Int}(1,2,3)
@test_throws MethodError I10(a=10) # typeof(a)!=typeof(c)
@test I10(a="asd") == I10{String}("asd",10,"aaa")
# field interdependence
@kwdef struct MT9{R<:Real}
a::R = 5
b::R
c::R = a+b
end
@test MT9{Float64}(b=1).c == 6
@test MT9{Float64}(b=1, c=1).c == 1
@kwdef struct MT10{R<:Real}
b::R = 6
c::R = a+b
a::R = 5
end
@test_throws UndefVarError MT10{Float64}() # defaults are evaluated in order
@test MT10{Float64}(b=1, c=1).c == 1
# Closures
a_MT11 = 7
b_MT11 = [1,2]
@kwdef struct MT11
a_MT11::Int = a_MT11 # a::Int=a is not possible as the outside a gets shadowed
b::Vector{Int}=b_MT11
end
m = MT11()
@test m.a_MT11 === a_MT11
@test m.b === b_MT11
# Inner constructors
@kwdef struct MT12
a = 1
MT12(a) = (@assert a>1; new(a))
end
@test_throws AssertionError MT12(0)
@test_throws AssertionError MT12(a=0)
@test MT12(a=2) == MT12(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.