Skip to content

Commit

Permalink
Extend test_ambiguity tests by case with kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 28, 2023
1 parent 591a27e commit 299cb35
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
16 changes: 16 additions & 0 deletions test/pkgs/PkgWithAmbiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ module PkgWithAmbiguities
# 1 ambiguity
f(::Any, ::Int) = 1
f(::Int, ::Any) = 2
const num_ambs_f = 1

# 2 ambiguities:
# 1 for g
# 1 for Core.kwfunc(g) if VERSION >= 1.4
# 2 for Core.kwfunc(g) if VERSION < 1.4
g(::Any, ::Int; kw) = 1
g(::Int, ::Any; kw) = 2
const num_ambs_g = VERSION >= v"1.4-" ? 2 : 3

abstract type AbstractType end
struct SingletonType <: AbstractType end
Expand All @@ -20,6 +29,8 @@ SingletonType(::Int, ::Any, ::Any) = 3
(::SingletonType)(::Any, ::Float64) = 1
(::SingletonType)(::Float64, ::Any) = 2

const num_ambs_SingletonType = 3

# 3 ambiguities
ConcreteType(::Any, ::Any, ::Int) = 1
ConcreteType(::Any, ::Int, ::Any) = 2
Expand All @@ -29,12 +40,17 @@ ConcreteType(::Int, ::Any, ::Any) = 3
(::ConcreteType)(::Any, ::Float64) = 1
(::ConcreteType)(::Float64, ::Any) = 2

const num_ambs_ConcreteType = 4


@static if VERSION >= v"1.3-"
# 1 ambiguity if VERSION >= 1.3
abstract type AbstractParameterizedType{T} end
struct ConcreteParameterizedType{T} <: AbstractParameterizedType{T} end
(::AbstractParameterizedType{T})(::Tuple{Tuple{Int}}) where {T} = 1
(::ConcreteParameterizedType)(::Tuple) = 2

const num_ambs_ParameterizedType = 1
end

end # module
56 changes: 41 additions & 15 deletions test/test_ambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@ module TestAmbiguities

include("preamble.jl")

using PkgWithAmbiguities

@testset begin
using PkgWithAmbiguities

@static if VERSION >= v"1.3-"
using PkgWithAmbiguities:
num_ambs_f,
num_ambs_g,
num_ambs_SingletonType,
num_ambs_ConcreteType,
num_ambs_ParameterizedType
total =
num_ambs_f +
num_ambs_g +
num_ambs_SingletonType +
num_ambs_ConcreteType +
num_ambs_ParameterizedType
else
using PkgWithAmbiguities:
num_ambs_f, num_ambs_g, num_ambs_SingletonType, num_ambs_ConcreteType
total = num_ambs_f + num_ambs_g + num_ambs_SingletonType + num_ambs_ConcreteType
end

function check_testcase(exclude, num_ambiguities::Int; broken::Bool = false)
pkgids = Aqua.aspkgids([PkgWithAmbiguities, Core]) # include Core to find constructor ambiguities
num_ambiguities_, strout, strerr = Aqua._find_ambiguities(pkgids; exclude = exclude)
Expand All @@ -16,51 +35,58 @@ using PkgWithAmbiguities
@test isempty(strerr)
end

@static if VERSION >= v"1.3-"
total = 9
else
total = 8
end

check_testcase([], total)

# exclude just anything irrelevant, see #49
check_testcase([convert], total)

# exclude function
check_testcase([PkgWithAmbiguities.f], total - 1)
check_testcase([PkgWithAmbiguities.f], total - num_ambs_f)

# exclude function and kwsorter
check_testcase([PkgWithAmbiguities.g], total - num_ambs_g; broken = true)

# exclude callables and constructors
check_testcase([PkgWithAmbiguities.SingletonType], total - 2 - 1)
check_testcase([PkgWithAmbiguities.ConcreteType], total - 3 - 1)
check_testcase([PkgWithAmbiguities.SingletonType], total - num_ambs_SingletonType)
check_testcase([PkgWithAmbiguities.ConcreteType], total - num_ambs_ConcreteType)

# exclude abstract supertype without callables and constructors
check_testcase([PkgWithAmbiguities.AbstractType], total)

@static if VERSION >= v"1.3-"
# for ambiguities between abstract and concrete type callables, only one needs to be excluded
check_testcase([PkgWithAmbiguities.AbstractParameterizedType], total - 1)
check_testcase([PkgWithAmbiguities.ConcreteParameterizedType], total - 1)
check_testcase(
[PkgWithAmbiguities.AbstractParameterizedType],
total - num_ambs_ParameterizedType,
)
check_testcase(
[PkgWithAmbiguities.ConcreteParameterizedType],
total - num_ambs_ParameterizedType,
)

# exclude everything
check_testcase(
[
PkgWithAmbiguities.f,
PkgWithAmbiguities.g,
PkgWithAmbiguities.SingletonType,
PkgWithAmbiguities.ConcreteType,
PkgWithAmbiguities.ConcreteParameterizedType,
],
0,
0;
broken = true,
)
else
# exclude everything
check_testcase(
[
PkgWithAmbiguities.f,
PkgWithAmbiguities.g,
PkgWithAmbiguities.SingletonType,
PkgWithAmbiguities.ConcreteType,
],
0,
0;
broken = true,
)
end

Expand Down

0 comments on commit 299cb35

Please sign in to comment.