Skip to content
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

Change pop! for LittleDict behavior #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions src/little_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function Base.pop!(dd::UnfrozenLittleDict)
return pop!(dd.vals)
end

function Base.pop!(dd::UnfrozenLittleDict, key)
function _pop!(dd::UnfrozenLittleDict, key, default=NotFoundSentinel())
@assert length(dd.keys) == length(dd.vals)

for ii in 1:length(dd.keys)
Expand All @@ -253,10 +253,17 @@ function Base.pop!(dd::UnfrozenLittleDict, key)
return val
end
end

return default
end

function Base.pop!(dd::UnfrozenLittleDict, key, default=NotFoundSentinel())
val = _pop!(dd, key, default)
return val === NotFoundSentinel() ? throw(KeyError(key)) : val
end

function Base.delete!(dd::UnfrozenLittleDict, key)
pop!(dd, key)
_pop!(dd, key)
return dd
end

Expand Down
42 changes: 42 additions & 0 deletions test/test_little_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,48 @@ using OrderedCollections: FrozenLittleDict, UnfrozenLittleDict
@test od60[14] == 15
end

@testset "pop!" begin
@testset "key" begin
expected = "bar"
ld = LittleDict("foo"=>expected)
ret = pop!(ld, "foo")

@test ret == expected
end

@testset "key dne" begin
@test_throws KeyError pop!(LittleDict(), "foo")
end

@testset "key -- default" begin
expected = "bar"
ld = LittleDict("foo"=>expected)
ret = pop!(ld, "foo", "baz")

@test ret == expected
end

@testset "key dne -- default" begin
expected = "baz"
ret = pop!(LittleDict(), "foo", expected)

@test ret == expected
end
end

@testset "delete!" begin
@testset "key" begin
ld = LittleDict("foo"=>"bar")
ret = delete!(ld, "foo")

@test ret == LittleDict()
end

@testset "key dne" begin
@test delete!(LittleDict(), "foo") == LittleDict()
end
end


##############################
# Copied and modified from Base/test/dict.jl
Expand Down
Loading