summaryrefslogtreecommitdiff
path: root/src/etap/etap_can.erl
blob: 552b7174b47e311dc9b8d3e9769ccda9e475858f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%% Copyright (c) 2008-2009 Nick Gerakines <nick@gerakines.net>
%% 
%% Permission is hereby granted, free of charge, to any person
%% obtaining a copy of this software and associated documentation
%% files (the "Software"), to deal in the Software without
%% restriction, including without limitation the rights to use,
%% copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following
%% conditions:
%% 
%% The above copyright notice and this permission notice shall be
%% included in all copies or substantial portions of the Software.
%% 
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
%% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
%% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
%% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
%%
%% @reference http://testanything.org/wiki/index.php/Main_Page
%% @reference http://en.wikipedia.org/wiki/Test_Anything_Protocol
%% @doc Provide test functionality modules
-module(etap_can).

-export([
    loaded_ok/2, can_ok/2, can_ok/3,
    has_attrib/2, is_attrib/3, is_behaviour/2
]).

%% @spec loaded_ok(atom(), string()) -> true | false
%% @doc Assert that a module has been loaded successfully.
loaded_ok(M, Desc) when is_atom(M) ->
    etap:fun_is(fun({module, _}) -> true; (_) -> false end, code:load_file(M), Desc).

%% @spec can_ok(atom(), atom()) -> true | false
%% @doc Assert that a module exports a given function.
can_ok(M, F) when is_atom(M), is_atom(F) ->
    Matches = [X || {X, _} <- M:module_info(exports), X == F],
    etap:ok(Matches > 0, lists:concat([M, " can ", F])).

%% @spec can_ok(atom(), atom(), integer()) -> true | false
%% @doc Assert that a module exports a given function with a given arity.
can_ok(M, F, A) when is_atom(M); is_atom(F), is_number(A) ->
    Matches = [X || X <- M:module_info(exports), X == {F, A}],
    etap:ok(Matches > 0, lists:concat([M, " can ", F, "/", A])).

%% @spec has_attrib(M, A) -> true | false
%%       M = atom()
%%       A = atom()
%% @doc Asserts that a module has a given attribute.
has_attrib(M, A) when is_atom(M), is_atom(A) ->
    etap:isnt(
        proplists:get_value(A, M:module_info(attributes), 'asdlkjasdlkads'),
        'asdlkjasdlkads',
        lists:concat([M, " has attribute ", A])
    ).

%% @spec has_attrib(M, A. V) -> true | false
%%       M = atom()
%%       A = atom()
%%       V = any()
%% @doc Asserts that a module has a given attribute with a given value.
is_attrib(M, A, V) when is_atom(M) andalso is_atom(A) ->
    etap:is(
        proplists:get_value(A, M:module_info(attributes)),
        [V],
        lists:concat([M, "'s ", A, " is ", V])
    ).

%% @spec is_behavior(M, B) -> true | false
%%       M = atom()
%%       B = atom()
%% @doc Asserts that a given module has a specific behavior.
is_behaviour(M, B) when is_atom(M) andalso is_atom(B) ->
    is_attrib(M, behaviour, B).