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

Dummy module now defines its own load/save instead of extending #237

Merged
merged 1 commit into from
Nov 21, 2019
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
36 changes: 27 additions & 9 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,54 @@ savestreaming(s::Stream{format"DUMMY"}) = DummyWriter(s, false)
savestreaming(file::File{format"DUMMY"}) = DummyWriter(open(file, "w"), true)

# we could implement `load` and `save` in terms of their streaming versions
function FileIO.load(file::File{format"DUMMY"})
function load(file::File{format"DUMMY"}; extra=UInt8[])
open(file) do s
load(s)
load(s; extra=extra)
end
end

function FileIO.metadata(file::File{format"DUMMY"})
function metadata(file::File{format"DUMMY"})
s = open(file)
skipmagic(s)
n = read(s, Int64)
close(s)
return n
end

function FileIO.load(s::Stream{format"DUMMY"})
function load(s::Stream{format"DUMMY"}; extra=UInt8[])
skipmagic(s)
n = read(s, Int64)
out = Vector{UInt8}(undef, n)
read!(s, out)
# verify that the extradata is as expected. This is just to test that
# the keyword arguments are handled properly in loading and saving
extradata = read(s, length(extra))
if extradata != extra
throw(ErrorException("Got extra data $extradata instead of $extra"))
end
close(s)
out
end

function save(file::File{format"DUMMY"}, data)
function save(file::File{format"DUMMY"}, data; extra=UInt8[])
open(file, "w") do s
write(s, magic(format"DUMMY")) # Write the magic bytes
write(s, convert(Int64, length(data)))
udata = convert(Vector{UInt8}, data)
write(s, udata)
save(s, data; extra=extra)
end
end

function save(s::Stream{format"DUMMY"}, data; extra=UInt8[])
write(s, magic(format"DUMMY")) # Write the magic bytes
write(s, convert(Int64, length(data)))
udata = convert(Vector{UInt8}, data)
write(s, udata)
write(s, extra)
end

add_loader(format"DUMMY", :Dummy)
add_saver(format"DUMMY", :Dummy)

end # module Dummy

@testset "Save" begin
a = [0x01,0x02,0x03]
fn = string(tempname(), ".dmy")
Expand Down Expand Up @@ -268,6 +279,13 @@ add_saver(format"DUMMY", :Dummy)
@test load(fn) == a
rm(fn)

# test keyword arguments

a = [0x01,0x02,0x03]
fn = string(tempname(), ".dmy")
save(fn, a; extra=[0x42, 0x43])
# the loader verifies that the extra data was written properly
load(fn; extra=[0x42, 0x43])

@test_throws Exception save("missing.fmt",5)
end
Expand Down