Skip to content

Commit

Permalink
tests for line number and docstring preservation
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Aug 8, 2023
1 parent fc4a0dc commit 5f87113
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.jl.mem
examples/.ipynb_checkpoints
docs/build
Manifest.toml
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREA
@doset "main"
@doset "yieldfrom"
@doset "typeparams"
@doset "coverage_preservation"
VERSION >= v"1.8" && @doset "doctests"
VERSION >= v"1.8" && @doset "aqua"
get(ENV,"JET_TEST","")=="true" && @doset "jet"
43 changes: 43 additions & 0 deletions test/test_coverage_preservation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ResumableFunctions
using Test
using MacroTools: postwalk

before = :(function f(arg, arg2::Int)
@yield arg
for i in 1:10
let i=i
@yield arg2
arg2 = arg2 + i
end
end
while true
try
@yield arg2
arg2 = arg2 + 1
catch e
@yield e
arg2 = arg2 + 1
end
break
end
arg2+arg
@yield arg2
end
)

after = eval(quote @macroexpand @resumable $before end)

function get_all_linenodes(expr)
nodes = Set()
postwalk(expr) do x
if x isa LineNumberNode
push!(nodes, x)
end
x
end
return nodes
end

@testset "all line numbers are preserved" begin
@test get_all_linenodes(before) get_all_linenodes(after)
end
75 changes: 75 additions & 0 deletions test/test_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,67 @@ end
@test collect(test_let()) == [[1,3],[2,4],[5,7],[6,8]]
end

if VERSION >= v"1.8"

q_test_let2 = quote
@resumable function test_let2()
for u in [[(1,2),(3,4)], [(5,6),(7,8)]]
for i in 1:2
let i=i, j=i
val = [(a[i],a[j]) for a in u]
end
@yield val
end
end
end
end

@testset "test_let2" begin
#@test collect(test_let2()) == ...
@test_throws "@resumable currently supports only single" eval(q_test_let2)
@test_broken false # the test above throws an error when it should not -- it happens because we do not support variables without assignments in let blocks -- see issues #69 and #70
end

q_test_let_noassignment = quote
@resumable function test_let_noassignment()
for u in [[(1,2),(3,4)], [(5,6),(7,8)]]
for i in 1:2
let i
val = [a[1] for a in u]
end
@yield val
end
end
end
end

@testset "test_let_noassignment" begin
#@test collect(test_let_noassignment()) == ...
@test_throws "@resumable currently supports only single" eval(q_test_let_noassignment)
@test_broken false # the test above throws an error when it should not -- it happens because we do not support variables without assignments in let blocks -- see issues #69 and #70
end

q_test_let_multipleargs = quote
@resumable function test_let_multipleargs()
for u in [[(1,2),(3,4)], [(5,6),(7,8)]]
for i in 1:2
let i=i, j
val = [a[i] for a in u]
end
@yield val
end
end
end
end

@testset "test_let_multipleargs" begin
#@test collect(test_let_multipleargs()) == ...
@test_throws "@resumable currently supports only single" eval(q_test_let_noassignment)
@test_broken false # the test above throws an error when it should not -- it happens because we do not support variables without assignments in let blocks -- see issues #69 and #70
end

end # VERSION >= v"1.8"

@resumable function test_nosave()
for i in 65:74
@nosave tmp = Char(i)
Expand Down Expand Up @@ -141,3 +202,17 @@ end
@test collect(test_continue()) == [1, 3, 4, 5, 6, 7, 8, 9, 10]
@test collect(test_continue_double()) == [1, 3, 1, 3]
end

"""docstring"""
@resumable function fwithdoc()
@yield 1
end

"""docstring"""
function gwithdoc()
return 1
end

@testset "test_docstring" begin
@test (@doc fwithdoc) == (@doc gwithdoc)
end

0 comments on commit 5f87113

Please sign in to comment.