I am not sure if this should be considered a bug, but I remember reading a
tip that you can pass the macro arguments to a function, to get better
behaviour
function my_macro_function(e::Expr)
# do your errors here
end
macro my_macro(e)
my_macro_function(e)
end
kl. 21:12:18 UTC+1 tirsdag 7. januar 2014 skrev Erik Engheim følgende:
>
>
>
> I got the code below. I am working on a slight change in how unittests
> report errors. When my tests fail, however I get the line number for where
> the error was thrown in the macro, but I would have wanted the line number
> for the expression being compared.
>
> module TLUnit
>
> export @test
>
> macro test(ex)
> if typeof(ex) != Expr || ex.head != :comparison
> error("Only comparison expressions may be evaluated with @test")
> end
> op = ex.args[2]
> ex1 = ex.args[1]
> ex2 = ex.args[3]
>
> quote
> oper = $(esc(op))
> expr1 = $(esc(ex1))
> expr2 = $(esc(ex2))
> if oper(expr1, expr2)
> nothing
> else
> error("test failed: ", $(string(ex1)), " ", oper, " ",
> $(string(ex2)), " evaluates to: ", expr1, " ", oper, " ", expr2)
> end
> end
>
> end
>
> end
>
> using TLUnit
>
> a = 2
>
> @test(a + 2 == 2*a + 1)
>
>
> When I run this code I get the exception thrown showing a problem on line
> number 20. But the test is on line 32. I have played around a bit with this
> and sometimes macros seem to return line numbers the way I want, but I
> don't understand how. So either this is a bug, or I just don't understand
> how this is supposed to work. Could anybody explain what the correct
> behavior should be?
>
> [julia] $ julia TLUnit.jl
>
> ERROR: test failed: :(+(a,2)) == :(+(*(2,a),1)) evaluates to: 4 == 5
>
> in error at error.jl:22
>
> in include at boot.jl:238
>
> in include_from_node1 at loading.jl:114
>
> in process_options at client.jl:303
>
> in _start at client.jl:389
>
> at /Users/erikengheim/Development/scripts/julia/TLUnit.jl:20
>