Skip to content

Commit

Permalink
Dummy module now defines its own load/save instead of extending
Browse files Browse the repository at this point in the history
also adds tests for keyword arguments
  • Loading branch information
ssfrr committed Nov 21, 2019
1 parent 232a0d5 commit 7a23ff5
Showing 1 changed file with 27 additions and 9 deletions.
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

0 comments on commit 7a23ff5

Please sign in to comment.