summaryrefslogtreecommitdiff
path: root/lib/wx/test/wxt.erl
blob: 2b380606d5e24e85ee11057f1a149cb70e79f361 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2008-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%% Description : Shortcuts for running tests with wx internal test_server
%%-------------------------------------------------------------------
-module(wxt).
-export([t/0, t/1, t/2, user/0, user/1,user/2]).

%%  Modules or suites can be shortcuts i.e. basic expands to wx_basic_SUITE.
%%  
%%  t(Tests) run wx testcases.
%%    Tests can be module, {module, test_case} or [module|{module,test_case}]

t() ->
    t(read_test_case()).
t(Test) ->
    t(Test, []).

t(Mod, TC) when is_atom(Mod), is_atom(TC) ->
    t({Mod,TC}, []);
t(all, Config) when is_list(Config) ->
    Fs = filelib:wildcard("wx_*_SUITE.erl"),
    t([list_to_atom(filename:rootname(File)) || File <- Fs, File =/= "wx_app_SUITE.erl"], Config);
t(Test,Config) when is_list(Config) ->
    Tests = resolve(Test),
    write_test_case(Test),
    Res = wx_test_lib:run_test(Tests, Config),    
    append_test_case_info(Test, Res).


user() ->
    user(read_test_case()). 
user(Mod) ->
    t(Mod, [{user,step}]).
user(Mod,Tc) when is_atom(Tc) ->
    t({Mod,Tc}, [{user,step}]).
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Resolves the name of test suites and test cases
%% according to the alias definitions. Single atoms
%% are assumed to be the name of a test suite. 
resolve(Suite0) when is_atom(Suite0) ->
    case alias(Suite0) of
	Suite when is_atom(Suite) ->
	    {Suite, all};
	{Suite, Case} ->
	    {Suite, Case}
    end;
resolve({Suite0, Case}) when is_atom(Suite0), is_atom(Case) ->
    case alias(Suite0) of
	Suite when is_atom(Suite) ->
	    {Suite, Case};
	{Suite, Case2} ->
	    {Suite, Case2}
    end;
resolve(List) when is_list(List) ->
    [resolve(Case) || Case <- List].

alias(Suite) when is_atom(Suite) ->
    Str = atom_to_list(Suite),
    case {Str, lists:reverse(Str)} of
	{"wx" ++ _, "ETIUS" ++ _} ->
	    Suite;
	_ -> 
	    list_to_atom("wx_" ++ Str ++ "_SUITE")
    end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

test_case_fname() ->
    "wx_test_case_info".

%% Read name of test case
read_test_case() ->
    Fname = test_case_fname(),
    case file:open(Fname, [read]) of
	{ok, Fd} ->
	    Res = io:read(Fd, []),
	    file:close(Fd),
	    case Res of
		{ok, TestCase} ->
		    wx_test_lib:log("Using test case ~w from file ~s~n",
					[TestCase, Fname]),
		    TestCase;
		{error, _} ->
		    default_test_case(Fname)
	    end;
	{error, _} ->
	    default_test_case(Fname)
    end.

default_test_case(Fname) ->
    TestCase = all, 
    wx_test_lib:log("<>WARNING<> Cannot read file ~s, "
		    "using default test case: ~w~n",
			[Fname, TestCase]),
    TestCase.

write_test_case(TestCase) ->
    Fname = test_case_fname(),
    {ok, Fd} = file:open(Fname, write),
    ok = io:format(Fd, "~p.~n",[TestCase]),
    file:close(Fd).

append_test_case_info(TestCase, TestCaseInfo) ->
    Fname = test_case_fname(),
    {ok, Fd} = file:open(Fname, [read, write]),
    ok = io:format(Fd, "~p.~n",[TestCase]),
    ok = io:format(Fd, "~p.~n",[TestCaseInfo]),
    file:close(Fd),
    TestCaseInfo.