Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Mar 24, 2023
1 parent adc057a commit 8fb120c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions test/test_Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ function empty_func(::Bool) end
anykwargs(a; kwargs...) = println(a, " keyword args: ", kwargs...)
hasdefaultargs(a, b=2) = a + b

function first_specialization(m::Method)
specs = m.specializations
if specs isa Core.MethodInstance
return specs
elseif specs isa Core.SimpleVector
return specs[1]::Core.MethodInstance
else
throw((m, specs))
end
end

@testset "Callsites" begin
callsites = find_callsites_by_ftt(testf_simple)
@test length(callsites) >= 4
Expand Down Expand Up @@ -53,7 +64,7 @@ hasdefaultargs(a, b=2) = a + b

# Callsite handling in source-view mode: for kwarg functions, strip the body, and use "typed" callsites
for m in (@which(anykwargs("animals")), @which(anykwargs("animals"; cat=1, dog=2)))
mi = m.specializations[1]
mi = first_specialization(m)
src, rt = Cthulhu.TypedSyntax.getsrc(mi)
tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false)
str = sprint(printstyled, tsn)
Expand All @@ -62,15 +73,15 @@ hasdefaultargs(a, b=2) = a + b
end
# Likewise for methods that fill in default positional arguments
m = @which hasdefaultargs(1)
mi = m.specializations[1]
mi = first_specialization(m)
src, rt = Cthulhu.TypedSyntax.getsrc(mi)
tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false)
str = sprint(printstyled, tsn)
@test occursin("hasdefaultargs(a, b=2)", str)
@test !occursin("a + b", str)
@test isempty(mappings)
m = @which hasdefaultargs(1, 5)
mi = m.specializations[1]
mi = first_specialization(m)
src, rt = Cthulhu.TypedSyntax.getsrc(mi)
tsn, mappings = Cthulhu.get_typed_sourcetext(mi, src, rt; warn=false)
str = sprint(printstyled, tsn)
Expand Down Expand Up @@ -699,8 +710,7 @@ fst5(x) = fst4(x)
@testset "backedges and treelist" begin
@test fbackedge2(0.2) == 1
@test fbackedge2(-0.2) == -1
mspec = @which(fbackedge1()).specializations
mi = isa(mspec, Core.SimpleVector) ? mspec[1] : mspec.func
mi = first_specialization(@which(fbackedge1()))
root = Cthulhu.treelist(mi)
@test Cthulhu.count_open_leaves(root) == 2
@test root.data.callstr == "fbackedge1()"
Expand All @@ -709,7 +719,7 @@ fst5(x) = fst4(x)
# issue #114
unspecva(@nospecialize(i::Int...)) = 1
@test unspecva(1, 2) == 1
mi = something(first(methods(unspecva)).specializations...)
mi = first_specialization(only(methods(unspecva)))
root = Cthulhu.treelist(mi)
@test occursin("Vararg", root.data.callstr)

Expand Down

0 comments on commit 8fb120c

Please sign in to comment.