1
%% Copyright (c) 2008-2009 Nick Gerakines <nick@gerakines.net>
3
%% Permission is hereby granted, free of charge, to any person
4
%% obtaining a copy of this software and associated documentation
5
%% files (the "Software"), to deal in the Software without
6
%% restriction, including without limitation the rights to use,
7
%% copy, modify, merge, publish, distribute, sublicense, and/or sell
8
%% copies of the Software, and to permit persons to whom the
9
%% Software is furnished to do so, subject to the following
12
%% The above copyright notice and this permission notice shall be
13
%% included in all copies or substantial portions of the Software.
15
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
%% OTHER DEALINGS IN THE SOFTWARE.
24
%% @reference http://testanything.org/wiki/index.php/Main_Page
25
%% @reference http://en.wikipedia.org/wiki/Test_Anything_Protocol
26
%% @doc Provide test functionality modules
30
loaded_ok/2, can_ok/2, can_ok/3,
31
has_attrib/2, is_attrib/3, is_behaviour/2
34
%% @spec loaded_ok(atom(), string()) -> true | false
35
%% @doc Assert that a module has been loaded successfully.
36
loaded_ok(M, Desc) when is_atom(M) ->
37
etap:fun_is(fun({module, _}) -> true; (_) -> false end, code:load_file(M), Desc).
39
%% @spec can_ok(atom(), atom()) -> true | false
40
%% @doc Assert that a module exports a given function.
41
can_ok(M, F) when is_atom(M), is_atom(F) ->
42
Matches = [X || {X, _} <- M:module_info(exports), X == F],
43
etap:ok(Matches > 0, lists:concat([M, " can ", F])).
45
%% @spec can_ok(atom(), atom(), integer()) -> true | false
46
%% @doc Assert that a module exports a given function with a given arity.
47
can_ok(M, F, A) when is_atom(M); is_atom(F), is_number(A) ->
48
Matches = [X || X <- M:module_info(exports), X == {F, A}],
49
etap:ok(Matches > 0, lists:concat([M, " can ", F, "/", A])).
51
%% @spec has_attrib(M, A) -> true | false
54
%% @doc Asserts that a module has a given attribute.
55
has_attrib(M, A) when is_atom(M), is_atom(A) ->
57
proplists:get_value(A, M:module_info(attributes), 'asdlkjasdlkads'),
59
lists:concat([M, " has attribute ", A])
62
%% @spec has_attrib(M, A. V) -> true | false
66
%% @doc Asserts that a module has a given attribute with a given value.
67
is_attrib(M, A, V) when is_atom(M) andalso is_atom(A) ->
69
proplists:get_value(A, M:module_info(attributes)),
71
lists:concat([M, "'s ", A, " is ", V])
74
%% @spec is_behavior(M, B) -> true | false
77
%% @doc Asserts that a given module has a specific behavior.
78
is_behaviour(M, B) when is_atom(M) andalso is_atom(B) ->
79
is_attrib(M, behaviour, B).