Skip to content

Commit

Permalink
Add more iteration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
contradict committed Sep 7, 2021
1 parent 0fdd8d0 commit 6289baa
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,33 @@ 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
""")
"""

@testset "multi_doc_bom" begin
iterable = YAML.load_all(multidoc_contents)
(val, state) = iterate(iterable)
@test equivalent(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
@test equivalent(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test equivalent(val, Dict("test" => 3))
@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 equivalent(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
Expand All @@ -288,6 +305,18 @@ test: 3
@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.HasEltype()
@test eltype(iterable) == Dict{Any, Any}
@test length(collect(iterable)) == 3
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

0 comments on commit 6289baa

Please sign in to comment.