Skip to content

Commit efe3b63

Browse files
mlechuc42f
andcommitted
Hook tests
Co-authored-by: Claire Foster <[email protected]>
1 parent c03287e commit efe3b63

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/hooks.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ function core_lowering_hook(@nospecialize(code), mod::Module,
1010
# e.g. LineNumberNode, integer...
1111
return Core.svec(code)
1212
end
13-
st0 = code isa Expr ? expr_to_syntaxtree(code) : code
13+
14+
# TODO: fix in base
15+
file = file isa Ptr{UInt8} ? unsafe_string(file) : file
16+
line = !(line isa Int64) ? Int64(line) : line
17+
18+
st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code
1419
try
1520
ctx1, st1 = expand_forms_1( mod, st0)
1621
ctx2, st2 = expand_forms_2( ctx1, st1)

test/hooks.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const JL = JuliaLowering
2+
3+
@testset "hooks" begin
4+
test_mod = Module()
5+
6+
@testset "`core_lowering_hook`" begin
7+
# Non-AST types are often sent through lowering
8+
stuff = Any[LineNumberNode(1), 123, 123.123, true, "foo", test_mod]
9+
for s in stuff
10+
@test JL.core_lowering_hook(s, test_mod) == Core.svec(s)
11+
end
12+
13+
for ast_type in (Expr, JL.SyntaxTree)
14+
ex = parsestmt(ast_type, "[1,2,3] .+= 1")
15+
out = JL.core_lowering_hook(ex, test_mod)
16+
@test out isa Core.SimpleVector && out[1] isa Expr
17+
val = Core.eval(test_mod, out[1])
18+
@test val == [2,3,4]
19+
end
20+
end
21+
22+
@testset "integration: `JuliaLowering.activate!`" begin
23+
prog = parseall(Expr, "global asdf = 1")
24+
JL.activate!()
25+
out = Core.eval(test_mod, prog)
26+
JL.activate!(false)
27+
@test out === 1
28+
@test isdefined(test_mod, :asdf)
29+
30+
prog = parseall(Expr, "module M; x = 1; end")
31+
JL.activate!()
32+
out = Core.eval(test_mod, prog)
33+
JL.activate!(false)
34+
@test out isa Module
35+
@test isdefined(test_mod, :M)
36+
@test isdefined(test_mod.M, :x)
37+
end
38+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ include("utils.jl")
2626
include("scopes.jl")
2727
include("typedefs.jl")
2828
include("compat.jl")
29+
include("hooks.jl")
2930
end

0 commit comments

Comments
 (0)