Skip to content

Commit 4bec18a

Browse files
authored
Merge pull request #237 from JuliaIO/noextend2
Dummy module now defines its own load/save instead of extending
2 parents 232a0d5 + 7a23ff5 commit 4bec18a

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

test/loadsave.jl

+27-9
Original file line numberDiff line numberDiff line change
@@ -126,43 +126,54 @@ savestreaming(s::Stream{format"DUMMY"}) = DummyWriter(s, false)
126126
savestreaming(file::File{format"DUMMY"}) = DummyWriter(open(file, "w"), true)
127127

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

135-
function FileIO.metadata(file::File{format"DUMMY"})
135+
function metadata(file::File{format"DUMMY"})
136136
s = open(file)
137137
skipmagic(s)
138138
n = read(s, Int64)
139139
close(s)
140140
return n
141141
end
142142

143-
function FileIO.load(s::Stream{format"DUMMY"})
143+
function load(s::Stream{format"DUMMY"}; extra=UInt8[])
144144
skipmagic(s)
145145
n = read(s, Int64)
146146
out = Vector{UInt8}(undef, n)
147147
read!(s, out)
148+
# verify that the extradata is as expected. This is just to test that
149+
# the keyword arguments are handled properly in loading and saving
150+
extradata = read(s, length(extra))
151+
if extradata != extra
152+
throw(ErrorException("Got extra data $extradata instead of $extra"))
153+
end
148154
close(s)
149155
out
150156
end
151157

152-
function save(file::File{format"DUMMY"}, data)
158+
function save(file::File{format"DUMMY"}, data; extra=UInt8[])
153159
open(file, "w") do s
154-
write(s, magic(format"DUMMY")) # Write the magic bytes
155-
write(s, convert(Int64, length(data)))
156-
udata = convert(Vector{UInt8}, data)
157-
write(s, udata)
160+
save(s, data; extra=extra)
158161
end
159162
end
160163

164+
function save(s::Stream{format"DUMMY"}, data; extra=UInt8[])
165+
write(s, magic(format"DUMMY")) # Write the magic bytes
166+
write(s, convert(Int64, length(data)))
167+
udata = convert(Vector{UInt8}, data)
168+
write(s, udata)
169+
write(s, extra)
161170
end
162171

163172
add_loader(format"DUMMY", :Dummy)
164173
add_saver(format"DUMMY", :Dummy)
165174

175+
end # module Dummy
176+
166177
@testset "Save" begin
167178
a = [0x01,0x02,0x03]
168179
fn = string(tempname(), ".dmy")
@@ -268,6 +279,13 @@ add_saver(format"DUMMY", :Dummy)
268279
@test load(fn) == a
269280
rm(fn)
270281

282+
# test keyword arguments
283+
284+
a = [0x01,0x02,0x03]
285+
fn = string(tempname(), ".dmy")
286+
save(fn, a; extra=[0x42, 0x43])
287+
# the loader verifies that the extra data was written properly
288+
load(fn; extra=[0x42, 0x43])
271289

272290
@test_throws Exception save("missing.fmt",5)
273291
end

0 commit comments

Comments
 (0)