Skip to content

Commit

Permalink
Correct IteratorEltype
Browse files Browse the repository at this point in the history
A document can be a single scalar, so eltype can't be known without
iterating. Add an example test to be sure this works.
  • Loading branch information
contradict committed Jun 7, 2024
1 parent da5ee57 commit e4a1ab3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/YAML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ iterate(it::YAMLDocIterator) = next(it, start(it))
iterate(it::YAMLDocIterator, s) = done(it, s) ? nothing : next(it, s)

Base.IteratorSize(::Type{YAMLDocIterator}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.HasEltype()
Base.eltype(it::YAMLDocIterator) = Dict{Any, Any}
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.EltypeUnknown()

load_all(input::IO, args...; kwargs...) =
YAMLDocIterator(input, args...; kwargs...)
Expand Down
11 changes: 8 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ test: 2
\ufeff---
test: 3
\ufeff---
42
"""

@testset "multi_doc_bom" begin
Expand All @@ -289,6 +291,8 @@ test: 3
@test equivalent(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test equivalent(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test equivalent(val, 42)
@test iterate(iterable, state) === nothing
end

Expand All @@ -304,6 +308,8 @@ end
@test equivalent(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test equivalent(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test equivalent(val, 42)
@test iterate(iterable, state) === nothing
end

Expand All @@ -314,9 +320,8 @@ end
end
iterable = YAML.load_all_file(fname)
@test Base.IteratorSize(YAML.YAMLDocIterator) == Base.SizeUnknown()
@test Base.IteratorEltype(YAML.YAMLDocIterator) == Base.HasEltype()
@test eltype(iterable) == Dict{Any, Any}
@test length(collect(iterable)) == 3
@test Base.IteratorEltype(YAML.YAMLDocIterator) == Base.EltypeUnknown()
@test length(collect(iterable)) == 4
end

# test that an OrderedDict is written in the correct order
Expand Down

0 comments on commit e4a1ab3

Please sign in to comment.