@@ -8,27 +8,29 @@ returning `true` or `false`.
88If no interface type is passed, Interfaces.jl will find all the
99interfaces available and test them.
1010"""
11- function test (T:: Type{<:Interface{Keys}} , O:: Type ; kw... ) where Keys
11+ function test (T:: Type{<:Interface{Keys}} , O:: Type ; kw... ) where Keys
1212 T1 = _get_type (T). name. wrapper
13- obj = test_object (T1, O)
14- return test (T1, obj ; keys= Keys, _O = O , kw... )
13+ objs = test_objects (T1, O)
14+ return test (T1, O, objs ; keys= Keys, kw... )
1515end
1616function test (T:: Type{<:Interface} , O:: Type ; kw... )
17- obj = test_object (T, O)
18- return test (T, obj; _O = O, kw... )
17+ objs = test_objects (T, O)
18+ return test (T, O, objs; kw... )
1919end
20- function test (T:: Type{<:Interface} , obj; show= true , keys= nothing , _O= typeof (obj))
21- if show
20+ function test (T:: Type{<:Interface} , O:: Type , objs:: TestObjectWrapper ;
21+ show= true , keys= nothing
22+ )
23+ if show
2224 print (" Testing " )
2325 printstyled (_get_type (T). name. name; color= :blue )
2426 print (" is implemented for " )
25- printstyled (_O , " \n " ; color= :blue )
27+ printstyled (O , " \n " ; color= :blue )
2628 end
2729 if isnothing (keys)
28- optional = NamedTuple {optional_keys(T, obj )} (components (T). optional)
29- mandatory_results = _test (components (T). mandatory, obj)
30- optional_results = _test (optional, obj )
31- if show
30+ optional = NamedTuple {optional_keys(T, O )} (components (T). optional)
31+ mandatory_results = _test (components (T). mandatory, objs)
32+ optional_results = _test (optional, objs )
33+ if show
3234 _showresults (mandatory_results, " Mandatory components" )
3335 _showresults (optional_results, " Optional components" )
3436 end
@@ -37,16 +39,50 @@ function test(T::Type{<:Interface}, obj; show=true, keys=nothing, _O=typeof(obj)
3739 else
3840 allcomponents = merge (components (T)... )
3941 optional = NamedTuple {_as_tuple(keys)} (allcomponents)
40- results = _test (optional, obj )
42+ results = _test (optional, objs )
4143 show && _showresults (results, " Specified components" )
4244 println ()
4345 return all (_bool (results))
4446 end
4547end
48+ # Convenience method for users to test a single object
49+ test (T:: Type{<:Interface} , obj; kw... ) =
50+ test (T, typeof (obj), TestObjectWrapper ((obj,)); kw... )
4651
47- _test (tests:: NamedTuple , obj) = map (t -> _test (t, obj), tests)
48- _test (condition:: Tuple , obj) = map (c -> _test (c, obj), condition)
49- _test (condition, obj) = condition (obj)
52+ function _test (tests:: NamedTuple{K} , objs:: TestObjectWrapper ) where K
53+ map (keys (tests), values (tests)) do k, v
54+ _test (k, v, objs)
55+ end |> NamedTuple{K}
56+ end
57+ function _test (name:: Symbol , condition:: Tuple , objs, i= nothing )
58+ map (condition, ntuple (identity, length (condition))) do c, i
59+ _test (name, c, objs, i)
60+ end
61+ end
62+ function _test (name:: Symbol , condition:: Tuple , objs:: TestObjectWrapper , i= nothing )
63+ map (condition, ntuple (identity, length (condition))) do c, i
64+ _test (name, c, objs, i)
65+ end
66+ end
67+ function _test (name:: Symbol , condition, objs:: TestObjectWrapper , i= nothing )
68+ map (o -> _test (name, condition, o, i), objs. objects)
69+ end
70+ function _test (name:: Symbol , condition, obj, i= nothing )
71+ try
72+ res = condition isa Pair ? condition[2 ](obj) : condition (obj)
73+ # Allow returning a function or tuple of functions that are tested again
74+ if res isa Union{Pair,Tuple,Base. Callable}
75+ return _test (name, res, obj, i)
76+ else
77+ return condition isa Pair ? condition[1 ] => res : res
78+ end
79+ catch e
80+ num = isnothing (i) ? " " : " , condition $i "
81+ desc = condition isa Pair ? string (" \" " , condition[1 ], " \" " ) : " "
82+ @warn " interface test :$name$num$desc failed for test object $obj "
83+ rethrow (e)
84+ end
85+ end
5086
5187function _showresults (results:: NamedTuple , title:: String )
5288 printstyled (title; color= :light_black )
@@ -59,6 +95,11 @@ function _showresults(results::NamedTuple, title::String)
5995end
6096
6197_showresult (key, res) = show (res)
98+ function _showresult (key, pair:: Pair )
99+ desc, res = pair
100+ print (desc, " : " )
101+ printstyled (res; color= (res ? :green : :red ))
102+ end
62103_showresult (key, res:: Bool ) = printstyled (res; color= (res ? :green : :red ))
63104function _showresult (key, res:: NTuple{<:Any,Bool} )
64105 _showresult (key, first (res))
@@ -71,6 +112,7 @@ function _showresult(key, res::NTuple{<:Any})
71112end
72113
73114_bool (xs:: Union{Tuple,NamedTuple,AbstractArray} ) = all (map (_bool, xs))
115+ _bool (x:: Pair ) = x[2 ]
74116_bool (x:: Bool ) = x
75117_bool (x) = convert (Bool, x)
76118
0 commit comments