Skip to content

Commit 2019b2e

Browse files
author
Brett Hazen
committed
Add unit tests for results table
1 parent fe374ec commit 2019b2e

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

src/rt_reporter.erl

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
-behaviour(gen_server).
3131

3232
-define(HEADER, [<<"Test">>, <<"Result">>, <<"Reason">>, <<"Test Duration">>]).
33+
-ifdef(TEST).
34+
-include_lib("eunit/include/eunit.hrl").
35+
-endif.
36+
3337

3438
%% API
3539
-export([start_link/3,
@@ -263,16 +267,19 @@ print_summary(TestResults, _CoverResult, Verbose) ->
263267
%% end,
264268
ok.
265269

266-
%% @doc Convert Milliseconds into human-readable string
270+
%% @doc Convert Microseconds into human-readable string
267271
-spec(test_summary_format_time(integer()) -> string()).
268-
test_summary_format_time(Milliseconds) ->
269-
Mills = trunc(((Milliseconds / 1000000) - (Milliseconds div 1000000)) * 1000000),
270-
TotalSecs = (Milliseconds - Mills) div 1000000,
272+
test_summary_format_time(Microseconds) ->
273+
Micros = trunc(((Microseconds / 1000000) - (Microseconds div 1000000)) * 1000000),
274+
TotalSecs = (Microseconds - Micros) div 1000000,
271275
TotalMins = TotalSecs div 60,
272276
Hours = TotalSecs div 3600,
273277
Secs = TotalSecs - (TotalMins * 60),
274278
Mins = TotalMins - (Hours * 60),
275-
list_to_binary(io_lib:format("~ph ~pm ~p.~ps", [Hours, Mins, Secs, Mills])).
279+
Decimal = lists:flatten(io_lib:format("~6..0B", [Micros])),
280+
FirstDigit = string:left(Decimal, 1),
281+
Fractional = string:strip(tl(Decimal), right, $0),
282+
list_to_binary(io_lib:format("~ph ~pm ~p.~s~ss", [Hours, Mins, Secs, FirstDigit, Fractional])).
276283

277284
%% @doc Count the number of passed, failed and skipped tests
278285
test_summary_fun(Result = {_, pass, _}, {{Pass, _Fail, _Skipped}, Rows}) ->
@@ -294,7 +301,7 @@ format_test_row({TestPlan, Result, Duration}) ->
294301
{FailOrSkip, Failure} ->
295302
{FailOrSkip, lists:flatten(io_lib:format("~p", [Failure]))};
296303
pass ->
297-
{"pass", "N/A"}
304+
{pass, "N/A"}
298305
end,
299306
[TestName, Status, Reason, test_summary_format_time(Duration)].
300307

@@ -332,3 +339,45 @@ report_and_gather_logs(UploadToGiddyUp, LogDir, TestResult = {TestPlan, _, _}) -
332339
%% fail -> RetList ++ [{reason, iolist_to_binary(io_lib:format("~p", [Reason]))}];
333340
%% _ -> RetList
334341
%% end.
342+
343+
-ifdef(TEST).
344+
345+
format_result_row_pass_test() ->
346+
%% Need to prime the config with any old default version
347+
rt_config:set(versions, [{default, {riak_ee, "1.3.4"}}]),
348+
Plan = rt_test_plan:new([{module,test},{backend,bitcask}]),
349+
?assertEqual(["test-bitcask", pass, "N/A", <<"0h 0m 0.012345s">>], format_test_row({Plan, pass, 12345})).
350+
351+
format_result_row_fail_atom_test() ->
352+
%% Need to prime the config with any old default version
353+
rt_config:set(versions, [{default, {riak_ee, "1.3.4"}}]),
354+
Plan = rt_test_plan:new([{module,test},{backend,bitcask}]),
355+
?assertEqual(["test-bitcask", fail, "timeout", <<"0h 0m 0.012345s">>], format_test_row({Plan, {fail,timeout}, 12345})).
356+
357+
format_result_row_fail_string_test() ->
358+
%% Need to prime the config with any old default version
359+
rt_config:set(versions, [{default, {riak_ee, "1.3.4"}}]),
360+
Plan = rt_test_plan:new([{module,test},{backend,bitcask}]),
361+
?assertEqual(["test-bitcask", fail, "some reason", <<"0h 0m 0.012345s">>], format_test_row({Plan, {fail,"some reason"}, 12345})).
362+
363+
format_result_row_fail_list_test() ->
364+
%% Need to prime the config with any old default version
365+
rt_config:set(versions, [{default, {riak_ee, "1.3.4"}}]),
366+
Plan = rt_test_plan:new([{module,test},{backend,bitcask}]),
367+
?assertEqual(["test-bitcask", fail, "nested", <<"0h 0m 0.012345s">>], format_test_row({Plan, {fail,[[$n],[$e],[[$s]],[$t],$e,$d]}, 12345})).
368+
369+
format_time_microsecond_test() ->
370+
?assertEqual(<<"0h 0m 0.000001s">>, test_summary_format_time(1)).
371+
372+
format_time_millisecond_test() ->
373+
?assertEqual(<<"0h 0m 0.001s">>, test_summary_format_time(1000)).
374+
375+
format_time_second_test() ->
376+
?assertEqual(<<"0h 0m 1.0s">>, test_summary_format_time(1000000)).
377+
378+
format_time_minute_test() ->
379+
?assertEqual(<<"0h 1m 0.0s">>, test_summary_format_time(60000000)).
380+
381+
format_time_hour_test() ->
382+
?assertEqual(<<"1h 0m 0.0s">>, test_summary_format_time(3600000000)).
383+
-endif.

0 commit comments

Comments
 (0)