Skip to content

Commit

Permalink
Merge branch 'main' into gd/literate
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Oct 1, 2023
2 parents d4676ee + e793622 commit 0b46f2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/implements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Without specifying `Options`, the return value specifies that at least
all the mandatory components of the interace are implemented.
"""
function implements end
implements(::Type{<:Interface}, obj) = false
implements(T::Type{<:Interface}, obj) = implements(T, typeof(obj))
implements(::Type{<:Interface}, obj::Type) = false

"""
@implements(interface, objtype)
Expand Down Expand Up @@ -67,3 +68,23 @@ end

_all_in(items::Tuple, collection) = all(map(in(collection), items))
_all_in(item::Symbol, collection) = in(item, collection)

struct Implemented{T<:Interface} end
struct NotImplemented{T<:Interface} end

"""
implemented_trait(T::Type{<:Interface}, obj)
implemented_trait(T::Type{<:Interface{Option}}, obj)
Provides a single type for using interface implementation
as a trait.
Returns `Implemented{T}()` or `NotImplemented{T}()`.
"""
function implemented_trait(::Type{T}, obj) where T<:Interface
if implements(T, obj)
Implemented{T}()
else
NotImplemented{T}()
end
end
15 changes: 9 additions & 6 deletions src/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ If no interface type is passed, Interfaces.jl will find all the
interfaces available and test them.
"""
function test(T::Type{<:Interface{Keys}}, O::Type, test_objects; kw...) where Keys
# Allow passing the keys in the abstract type
# But get them out and put them in the `keys` keyword
T1 = _get_type(T).name.wrapper
objs = TestObjectWrapper(test_objects)
return test(T1, O, objs; keys=Keys, kw...)
# And run the tests on the parameterless type
return _test(T1, O, objs; keys=Keys, kw...)
end
function test(T::Type{<:Interface}, O::Type, test_objects; kw...)
objs = TestObjectWrapper(test_objects)
return test(T, O, objs; kw...)
return _test(T, O, objs; kw...)
end
function test(T::Type{<:Interface}, O::Type, objs::TestObjectWrapper;
# Convenience method for users to test a single object
test(T::Type{<:Interface}, obj; kw...) = test(T, typeof(obj), (obj,); kw...)

function _test(T::Type{<:Interface}, O::Type, objs::TestObjectWrapper;
show=true, keys=nothing
)
if show
Expand All @@ -52,9 +58,6 @@ function test(T::Type{<:Interface}, O::Type, objs::TestObjectWrapper;
return all(_bool(results))
end
end
# Convenience method for users to test a single object
test(T::Type{<:Interface}, obj; kw...) =
test(T, typeof(obj), TestObjectWrapper((obj,)); kw...)

function _test(tests::NamedTuple{K}, objs::TestObjectWrapper) where K
map(keys(tests), values(tests)) do k, v
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Aqua
using Documenter
using Interfaces
using Test
using Aqua

@testset verbose = true "Interfaces.jl" begin
doctest(Interfaces)
Expand Down

0 comments on commit 0b46f2a

Please sign in to comment.