Skip to content

getfield-overloading tests #25164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1013,15 +1013,15 @@ let
@test_throws ErrorException("type LoadError is immutable") setfield!(strct, 4, "")
@test_throws ErrorException("type is immutable") setfield!(strct, :line, 0)
@test strct.file == "yofile"
@test strct.line == 0
@test strct.line === 0
@test strct.error == "bad"
@test getfield(strct, 1) == "yofile"
@test getfield(strct, 2) == 0
@test getfield(strct, 2) === 0
@test getfield(strct, 3) == "bad"

mstrct = TestMutable("melm", 1, nothing)
Base.setproperty!(mstrct, :line, 8.0)
@test mstrct.line == 8
@test mstrct.line === 8
@test_throws TypeError(:setfield!, "", Int, 8.0) setfield!(mstrct, :line, 8.0)
@test_throws TypeError(:setfield!, "", Int, 8.0) setfield!(mstrct, 2, 8.0)
setfield!(mstrct, 3, "hi")
Expand All @@ -1033,6 +1033,30 @@ let
@test_throws BoundsError(mstrct, 4) setfield!(mstrct, 4, "")
end

# test getfield-overloading
function Base.getproperty(mstrct::TestMutable, p::Symbol)
return (p, getfield(mstrct, :error))
end
function Base.setproperty!(mstrct::TestMutable, p::Symbol, v)
setfield!(mstrct, :error, (p, v))
end

let
mstrct = TestMutable("melm", 1, nothing)
@test mstrct.line === (:line, nothing)
@test mstrct.bar === (:bar, nothing)
@test getfield(mstrct, 1) == "melm"
@test getfield(mstrct, :file) == "melm"
@test_throws MethodError Base.getproperty(mstrct, 1)
mstrct.error = 8.0
@test mstrct.bar === (:bar, (:error, 8.0))
mstrct.line = 8.0
@test getfield(mstrct, :line) === 1
@test getfield(mstrct, :error) === (:line, 8.0)
@test mstrct.bar === (:bar, (:line, 8.0))
@test mstrct.error === (:error, (:line, 8.0))
end

# allow typevar in Union to match as long as the arguments contain
# sufficient information
# issue #814
Expand Down