This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
espec_console_listener.erl
99 lines (78 loc) · 3.68 KB
/
espec_console_listener.erl
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
-module(espec_console_listener).
-behaviour(espec_listener).
-export([start_spec/2, end_spec/2, start_example/2, end_example/3, pending_example/2, start_group/2, end_group/2, new/0]).
-record(state, {color = false, indentation = 0}).
new() ->
Color = not is_running_in_shell(),
#state{color = Color}.
start_spec(Name, #state{indentation = Indentation} = State) ->
io:format(user, "~s~s~n", [indentation(Indentation), Name]),
State#state{indentation = Indentation + 1}.
end_spec(_Name, #state{indentation = Indentation} = State) ->
State#state{indentation = Indentation - 1}.
start_example(_Description, State) ->
State.
end_example(Description, Result, #state{indentation = Indentation} = State) ->
case Result of
ok ->
io:format(user, "~s~s~s~s\n", [success_color(State), indentation(Indentation + 1), Description, no_color(State)]);
{error, {Class, Reason, Stacktrace}} ->
io:format(user, "~s~s~s (FAILED):\n", [failure_color(State), indentation(Indentation + 1), Description]),
print_error(Class, Reason, Stacktrace, Indentation),
io:format(user, "~s~n", [no_color(State)])
end,
State.
print_error(error, {assertEqual_failed, Options}, _, Indentation) ->
io:format(user, "~sassertEqual (line ~p) failed\n", [indentation(Indentation + 2), proplists:get_value(line, Options)]),
io:format(user, "~sExpression:~n~s~s~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(expression, Options)]),
io:format(user, "~sExpected:~n~s~p~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(expected, Options)]),
io:format(user, "~sGot:~n~s~p~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(got, Options)]);
print_error(error, {assertMatch_failed, Options}, _, Indentation) ->
io:format(user, "~sassertMatch (line ~p) failed\n", [indentation(Indentation + 2), proplists:get_value(line, Options)]),
io:format(user, "~sExpression:~n~s~s~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(expression, Options)]),
io:format(user, "~sExpected To Match:~n~s~s~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(expected, Options)]),
io:format(user, "~sGot:~n~s~p~n", [indentation(Indentation + 2), indentation(Indentation + 4), proplists:get_value(got, Options)]);
print_error(Class, Reason, Stacktrace, Indentation) ->
io:format(user, "~s~p ~p\n", [indentation(Indentation + 2), Class, Reason]),
io:format(user, "~s~p", [indentation(Indentation + 2), Stacktrace]).
pending_example(Description, #state{indentation = Indentation} = State) ->
io:format(user, "~s~s~s (PENDING)~s\n", [pending_color(State), indentation(Indentation + 1), Description, no_color(State)]),
State.
start_group(GroupDescription, #state{indentation = Indentation} = State) ->
io:format(user, "~s~s~n", [indentation(Indentation), GroupDescription]),
State#state{indentation = Indentation + 1}.
end_group(_Description, #state{indentation = Indentation} = State) ->
State#state{indentation = Indentation - 1}.
indentation(Depth) ->
lists:duplicate(Depth * 2, " ").
success_color(#state{color = false}) ->
"";
success_color(_) ->
green().
pending_color(#state{color = false}) ->
"";
pending_color(_) ->
yellow().
failure_color(#state{color = false}) ->
"";
failure_color(_) ->
red().
no_color(#state{color = false}) ->
"";
no_color(_) ->
"\e[m".
green() ->
"\e[0;32m".
red() ->
"\e[0;31m".
yellow() ->
"\e[0;33m".
is_running_in_shell() ->
ProcessInfo = process_info(self()),
RootPid = list_to_pid("<0.0.0>"),
case proplists:get_value(links, ProcessInfo) of
[RootPid] ->
false;
_ ->
true
end.