Skip to content

Commit

Permalink
Fix time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-victor-nascimento committed Mar 30, 2016
1 parent 13620c5 commit df31c23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/formatter.ex
Expand Up @@ -118,6 +118,8 @@ defmodule JUnitFormatter do
{:ok, config}
end

def format_time(time), do: time |> us_to_ms |> format_ms

# PRIVATE ------------

defp adjust_case_stats(%ExUnit.Test{} = test, config) do
Expand All @@ -138,19 +140,28 @@ defmodule JUnitFormatter do
failures: stats.failures,
name: name,
tests: stats.tests,
time: normalize_us(stats.time)],
time: stats.time |> format_time],
for test <- stats.test_cases do
generate_testcases(test)
end
}
end

defp normalize_us(us), do: div(us, 1000000)
defp us_to_ms(us), do: div(us, 10000)

defp format_ms(ms) do
if ms < 10 do
"0.0#{ms}"
else
ms = div ms, 10
"#{div(ms, 10)}.#{rem(ms, 10)}"
end
end

defp generate_testcases(test) do
{:testcase, [classname: Atom.to_char_list(test.case),
name: Atom.to_char_list(test.name),
time: test.time],
time: test.time |> us_to_ms |> format_ms],
generate_test_body(test)
}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
@@ -1,7 +1,7 @@
defmodule Formatter.Mixfile do
use Mix.Project

@version "1.0.0"
@version "1.0.1"

def project do
[app: :junit_formatter,
Expand Down
12 changes: 11 additions & 1 deletion test/formatter_test.exs
Expand Up @@ -3,6 +3,8 @@ defmodule FormatterTest do

test "that a valid test generates a proper report" do

:timer.sleep 1223

defmodule ValidTest do
use ExUnit.Case

Expand Down Expand Up @@ -82,6 +84,14 @@ defmodule FormatterTest do
assert output =~ "<testcase classname=\"Elixir.FormatterTest.RaiseWithNoReason\" name=\"test it raises without reason\" ><failure message=\"throw: nil\"> test/formatter_test.exs FormatterTest.RaiseWithNoReason.\"test it raises without reason\"/1\n</failure></testcase>"
end

test "it can format time" do
assert JUnitFormatter.format_time(1000000) == "1.0"
assert JUnitFormatter.format_time(10000) == "0.01"
assert JUnitFormatter.format_time(20000) == "0.02"
assert JUnitFormatter.format_time(110000) == "0.1"
assert JUnitFormatter.format_time(1100000) == "1.1"
end

# Utilities --------------------

defp read_fixture(extra) do
Expand All @@ -97,7 +107,7 @@ defmodule FormatterTest do
end

defp strip_time_and_line_number(output) do
output = String.replace output, ~r/time=\"[0-9]+\"/, ""
output = String.replace output, ~r/time=\"[0-9]+\.[0-9]+\"/, ""
file = List.last String.split __ENV__.file, ~r/\//
String.replace output, ~r/#{file}:[0-9]+:/, file
end
Expand Down

0 comments on commit df31c23

Please sign in to comment.