Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions src/YAML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ function iterate(it::YAMLDocIterator, _ = nothing)
return doc, nothing
end

Base.IteratorSize(::Type{YAMLDocIterator}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.EltypeUnknown()

"""
load_all(x::Union{AbstractString, IO}) -> YAMLDocIterator

Expand Down Expand Up @@ -161,8 +164,8 @@ load_file(filename::AbstractString, args...; kwargs...) =
Parse the YAML file `filename`, and return corresponding YAML documents.
"""
load_all_file(filename::AbstractString, args...; kwargs...) =
open(filename, "r") do input
load_all(input, args...; kwargs...)
open(filename, "r") do f
io = IOBuffer(read(f))
load_all(io, args...; kwargs...)
end

end # module
40 changes: 37 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,59 @@ const encodings = [
@test YAML.load(IOBuffer(data)) == "test"
end

@testset "multi_doc_bom" begin
iterable = YAML.load_all("""
const multidoc_contents = """
\ufeff---\r
test: 1
\ufeff---
test: 2

\ufeff---
test: 3
""")
\ufeff---
42
"""

@testset "multi_doc_bom" begin
iterable = YAML.load_all(multidoc_contents)
(val, state) = iterate(iterable)
@test isequal(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test isequal(val, 42)
@test iterate(iterable, state) === nothing
end

@testset "multi_doc_file" begin
fname = tempname() # cleanup=true, file will be deleted on process exit
open(fname, "w") do f
write(f, multidoc_contents)
end
iterable = YAML.load_all_file(fname)
(val, state) = iterate(iterable)
@test isequal(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test isequal(val, 42)
@test iterate(iterable, state) === nothing
end

@testset "multi_doc_iteration_protocol" begin
fname = tempname() # cleanup=true, file will be deleted on process exit
open(fname, "w") do f
write(f, multidoc_contents)
end
iterable = YAML.load_all_file(fname)
@test Base.IteratorSize(YAML.YAMLDocIterator) == Base.SizeUnknown()
@test Base.IteratorEltype(YAML.YAMLDocIterator) == Base.EltypeUnknown()
@test length(collect(iterable)) == 4
end

# test that an OrderedDict is written in the correct order
using OrderedCollections, DataStructures
@test strip(YAML.yaml(OrderedDict(:c => 3, :b => 2, :a => 1))) == join(["c: 3", "b: 2", "a: 1"], "\n")
Expand Down