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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
-module(showtime_ffi).
-export([run_test/4, functions/0, capture_output/1, gleam_error/1]).
gleam_error(Value) ->
erlang:error(#{
gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => Value,
module => <<"this/is/not/used"/utf8>>,
function => <<"gleam_error"/utf8>>,
% Not used
line => 0
}).
start_output_capture(Capture) ->
OldGroupLeader = group_leader(),
CapturePid = spawn(showtime_ffi, capture_output, [{[], {OldGroupLeader, Capture}}]),
group_leader(CapturePid, self()),
{CapturePid, OldGroupLeader}.
stop_output_capture({CapturePid, OldGroupLeader}) ->
group_leader(OldGroupLeader, self()),
CapturePid ! {capture_done, self()},
receive
Buffer ->
Buffer
end.
capture_output({Buffer, {OldGroupLeader, Capture}}) ->
receive
{io_request, From, ReplyAs, {put_chars, unicode, BitString}} ->
case Capture of
yes ->
From ! {io_reply, ReplyAs, ok},
capture_output({[BitString | Buffer], {OldGroupLeader, Capture}});
mixed ->
OldGroupLeader ! {io_request, From, ReplyAs, {put_chars, unicode, BitString}},
capture_output({[BitString | Buffer], {OldGroupLeader, Capture}});
no ->
OldGroupLeader ! {io_request, From, ReplyAs, {put_chars, unicode, BitString}},
capture_output({Buffer, {OldGroupLeader, Capture}})
end;
{capture_done, SenderPid} ->
SenderPid ! Buffer;
OtherMessage ->
OldGroupLeader ! OtherMessage,
capture_output({Buffer, {OldGroupLeader, Capture}})
end.
run_test(Module, Function, IgnoreTags, Capture) ->
OutputCapture = start_output_capture(Capture),
try
Result = apply(Module, Function, []),
{ResultType, FinalResult} =
case Result of
{test, {meta, _Description, Tags}, TestFun} ->
case
lists:any(
fun(Tag) ->
lists:any(fun(IgnoreTag) -> IgnoreTag == Tag end, IgnoreTags)
end,
Tags
)
of
true ->
{ignored, ignore};
false ->
{test_function_return, TestFun()}
end;
DirectResult ->
{test_function_return, DirectResult}
end,
OutputCaptureBuffer = stop_output_capture(OutputCapture),
case ResultType of
ignored -> {ok, {ResultType, FinalResult}};
_ -> {ok, {ResultType, FinalResult, OutputCaptureBuffer}}
end
catch
Class:Reason:Stacktrace ->
GleamReason =
case Reason of
{Assertion, ReasonList} ->
ErlangReasonList =
lists:map(
fun(ReasonDetail) ->
case ReasonDetail of
{line, LineNo} ->
{reason_line, LineNo};
{expression, List} ->
{expression, list_to_binary(List)};
{module, ModuleAtom} ->
{module, atom_to_binary(ModuleAtom)};
{pattern, Pattern} ->
{pattern, list_to_binary(Pattern)};
Other ->
Other
end
end,
ReasonList
),
GleamAssertionType =
case Assertion of
assertEqual ->
assert_equal;
assertNotEqual ->
assert_not_equal;
assertMatch ->
assert_match;
OtherAssertionType ->
OtherAssertionType
end,
{GleamAssertionType, ErlangReasonList};
#{
function := GleamFunction,
gleam_error := GleamError,
line := Line,
message := Message,
module := GleamModule,
value := Value
} ->
case Value of
{error, {OkValue, _, _, _}} when OkValue == not_eq; OkValue == eq ->
{gleam_error,
{GleamError, GleamModule, GleamFunction, Line, Message, Value}};
{error, {OkValue, _, _}} when OkValue == is_ok; OkValue == is_error ->
{gleam_error,
{GleamError, GleamModule, GleamFunction, Line, Message, Value}};
{error, {OkValue, _}} when OkValue == fail ->
{gleam_error,
{GleamError, GleamModule, GleamFunction, Line, Message, Value}};
_ ->
{gleam_assert, Value, Line}
end;
OtherReason ->
{generic_exception, OtherReason}
end,
GleamClass =
case Class of
error ->
erlang_error;
Other ->
Other
end,
GleamTraceList =
lists:map(
fun(Trace) ->
case Trace of
{ModuleName, FunctionName, Arity, ExtraInfoList} ->
{trace_module, atom_to_binary(ModuleName),
atom_to_binary(FunctionName), map_arity(Arity),
map_extra_info_list(ExtraInfoList)};
{FunctionName, Arity, ExtraInfoList} ->
{trace, atom_to_binary(FunctionName), map_arity(Arity),
map_extra_info_list(ExtraInfoList)}
end
end,
Stacktrace
),
OutputCaptureBufferCatch = stop_output_capture(OutputCapture),
{error,
{erlang_exception, GleamClass, GleamReason, {trace_list, GleamTraceList},
OutputCaptureBufferCatch}}
end.
map_extra_info_list(ExtraInfoList) ->
lists:map(
fun(ExtraInfo) ->
case ExtraInfo of
{file, FileNameList} -> {file, list_to_binary(FileNameList)};
Other -> Other
end
end,
ExtraInfoList
).
map_arity(Arity) ->
if
is_list(Arity) ->
{arg_list, Arity};
is_integer(Arity) ->
{num, Arity}
end.
functions() ->
Funs = module_info(exports),
Funs.
|