Skip to content

Commit

Permalink
Test macro syntax.
Browse files Browse the repository at this point in the history
In Julia 0.7+ exceptions thrown from macros get wrapped in a LoadError,
so we need a macro to unwrap the actual exception.
  • Loading branch information
traktofon committed Aug 27, 2018
1 parent 06bdc9f commit f910519
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/fixedlengthrecords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ function unsafe_read( rec::FixedLengthRecord, p::Ptr{UInt8}, n::UInt )
end

function unsafe_write( rec::FixedLengthRecord, p::Ptr{UInt8}, n::UInt )
if (n > rec.nleft)
fthrow("attempting to write beyond record end") #TODO
end
if (n > rec.nleft); fthrow("attempting to write beyond record end"); end
nwritten = unsafe_write( rec.io, p, n )
rec.nleft -= n
return nwritten
Expand Down
6 changes: 3 additions & 3 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ macro fread(fortranFile, args...)
recnum = esc(arg.args[2])
haverecnum = true
else
throw(ArgumentError("unknown keyword argument '$(arg.args[1])'")) #TODO
throw(ArgumentError("unknown keyword argument '$(arg.args[1])'"))
end
elseif arg.head == :(::)
var = esc(arg.args[1])
typ = esc(arg.args[2])
push!(specs, (var,typ))
else
throw(ArgumentError("unsupported specification for read: '$(arg)'")) #TODO
throw(ArgumentError("unsupported specification for read: '$(arg)'"))
end
elseif isa(arg, Symbol)
var = esc(arg)
push!(specs, var)
else
throw(ArgumentError("unsupported specification for read: '$(arg)'")) #TODO
throw(ArgumentError("unsupported specification for read: '$(arg)'"))
end
end

Expand Down
12 changes: 4 additions & 8 deletions src/withoutsubrecords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,29 @@ end

function unsafe_read( rec::RecordWithoutSubrecords, p::Ptr{UInt8}, n::UInt )
if (n > rec.nleft)
fthrow("attempting to read beyond record end") #TODO
fthrow("attempting to read beyond record end")
end
unsafe_read( rec.io, p, n )
rec.nleft -= n
nothing
end

function unsafe_write( rec::RecordWithoutSubrecords, p::Ptr{UInt8}, n::UInt )
if (n > rec.nleft)
fthrow("attempting to write beyond record end") #TODO
end
if (n > rec.nleft); fthrow("attempting to write beyond record end"); end
nwritten = unsafe_write( rec.io, p, n )
rec.nleft -= n
return nwritten
end

function close( rec::RecordWithoutSubrecords{T} ) where {T}
if rec.writable
if rec.nleft != 0
fthrow("record has not yet been completely written") #TODO
end
@assert rec.nleft == 0
write(rec.io, rec.convert.onwrite( convert(T, rec.reclen)) ) # write trailing record marker
else
skip(rec.io, rec.nleft)
reclen = rec.convert.onread( read(rec.io, T) ) # read trailing record marker
if reclen != rec.reclen
fthrow("trailing record marker doesn't match") #TODO
fthrow("trailing record marker doesn't match")
end
end
nothing
Expand Down
6 changes: 2 additions & 4 deletions src/withsubrecords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end
function advance!( rec::RecordWithSubrecords )
subreclen, sign = rdmarker(rec) # read trailing record marker
if subreclen != rec.subreclen
fthrow("trailing subrecord marker doesn't match") #TODO
fthrow("trailing subrecord marker doesn't match")
end
if rec.more
subreclen, more = rdmarker(rec) # read next leading subrecord marker
Expand Down Expand Up @@ -79,9 +79,7 @@ end
function unsafe_write( rec::RecordWithSubrecords, p::Ptr{UInt8}, n::UInt )
nwritten = 0
while (n>0)
if rec.totleft==0
fthrow("attempting to write beyond record end") #TODO
end
if rec.totleft==0; fthrow("attempting to write beyond record end"); end
towrite = min(n, rec.subleft)
nwritten += unsafe_write(rec.io, p, towrite)
p += towrite
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using FortranFiles
using Compat.Test
using Base.Iterators: product

include("test_macro_throws.jl")

import FortranFiles: RecordMarkerType, AccessMode, SequentialAccess, DirectAccess

# To test long records being split into subrecords, without needing
Expand Down Expand Up @@ -269,6 +271,12 @@ end
end
end
end

@testset "Macro syntax" begin
@test_macro_throws ArgumentError eval(:(@fread f foo=1))
@test_macro_throws ArgumentError eval(:(@fread f 10))
@test_macro_throws ArgumentError eval(:(@fread f (Int,Int)))
end
end

@testset "Strings" begin
Expand Down
23 changes: 23 additions & 0 deletions test/test_macro_throws.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Like @test_throws, but for macros.
# Julia-0.7 and later always wrap the exception in a LoadError.

@static if VERSION >= v"0.7.0-alpha.0"
macro test_macro_throws(typ, expr)
quote
@test_throws $typ begin
try
$expr
catch e
rethrow(e.error)
end
end
end
end
else
macro test_macro_throws(typ, expr)
quote
@test_throws $typ $expr
end
end
end

0 comments on commit f910519

Please sign in to comment.