   end
 
   def handle_cast({:test_started, %ExUnit.Test{} = test}, config) do
-    if config.trace, do: IO.write "  * #{test.name}"
+    if config.trace, do: IO.write("  * #{test.name}")
     {:noreply, config}
   end
 
   def handle_cast({:test_finished, %ExUnit.Test{state: nil} = test}, config) do
-    if config.trace do
-      IO.puts success(trace_test_result(test), config)
-    else
-      IO.write success(".", config)
-    end
-    {:noreply, %{config | test_counter: update_test_counter(config.test_counter, test),
-                          test_timings: update_test_timings(config.test_timings, test)}}
+    if config.trace() do
+      IO.puts(success(trace_test_result(test), config))
+    else
+      IO.write(success(".", config))
+    end
+
+    test_counter = update_test_counter(config.test_counter, test)
+    test_timings = update_test_timings(config.test_timings, test)
+    config = %{config | test_counter: test_counter, test_timings: test_timings}
+
+    {:noreply, config}
   end
 
   def handle_cast({:test_finished, %ExUnit.Test{state: {:skip, _}} = test}, config) do
-    if config.trace, do: IO.puts trace_test_skip(test)
-    {:noreply, %{config | test_counter: update_test_counter(config.test_counter, test),
-                          skipped_counter: config.skipped_counter + 1}}
+    if config.trace, do: IO.puts(trace_test_skip(test))
+
+    test_counter = update_test_counter(config.test_counter, test)
+    config = %{config | test_counter: test_counter, skipped_counter: config.skipped_counter + 1}
+
+    {:noreply, config}
   end
 
   def handle_cast({:test_finished, %ExUnit.Test{state: {:invalid, _}} = test}, config) do
-    if config.trace do
-      IO.puts invalid(trace_test_result(test), config)
-    else
-      IO.write invalid("?", config)
-    end
-
-    {:noreply, %{config | test_counter: update_test_counter(config.test_counter, test),
-                          invalid_counter: config.invalid_counter + 1}}
+    if config.trace() do
+      IO.puts(invalid(trace_test_result(test), config))
+    else
+      IO.write(invalid("?", config))
+    end
+
+    test_counter = update_test_counter(config.test_counter, test)
+    config = %{config | test_counter: test_counter, invalid_counter: config.invalid_counter + 1}
+
+    {:noreply, config}
   end
 
   def handle_cast({:test_finished, %ExUnit.Test{state: {:failed, failures}} = test}, config) do
-    if config.trace do
-      IO.puts failure(trace_test_result(test), config)
-    end
-
-    formatted = format_test_failure(test, failures, config.failure_counter + 1,
-                                    config.width, &formatter(&1, &2, config))
+    if config.trace() do
+      IO.puts(failure(trace_test_result(test), config))
+    end
+
+    formatted =
+      format_test_failure(
+        test,
+        failures,
+        config.failure_counter + 1,
+        config.width,
+        &formatter(&1, &2, config)
+      )
+
     print_failure(formatted, config)
     print_logs(test.logs)
 
-    {:noreply, %{config | test_counter: update_test_counter(config.test_counter, test),
-                          test_timings: update_test_timings(config.test_timings, test),
-                          failure_counter: config.failure_counter + 1}}
+    test_counter = update_test_counter(config.test_counter, test)
+    test_timings = update_test_timings(config.test_timings, test)
+    failure_counter = config.failure_counter + 1
+
+    config = %{
+      config
+      | test_counter: test_counter,
+        test_timings: test_timings,
+        failure_counter: failure_counter
+    }
+
+    {:noreply, config}
   end
 
   def handle_cast({:module_started, %ExUnit.TestModule{name: name}}, config) do
-    if config.trace do
-      IO.puts("\n#{inspect name}")
+    if config.trace() do
+      IO.puts("\n#{inspect(name)}")
     end
 
     {:noreply, config}


    message =
      "#{test_type_counts}#{config.failure_counter} #{failure_pl}"
      |> if_true(config.skipped_counter > 0, & &1 <> ", #{config.skipped_counter} skipped")
      |> if_true(config.invalid_counter > 0, & &1 <> ", #{config.invalid_counter} invalid")

    cond do
      config.failure_counter > 0 -> IO.puts failure(message, config)
      config.invalid_counter > 0 -> IO.puts invalid(message, config)
      true -> IO.puts success(message, config)
    end

    IO.puts "\nRandomized with seed #{config.seed}"
  end

  defp if_true(value, false, _fun), do: value
  defp if_true(value, true, fun), do: fun.(value)

  defp print_filters([include: [], exclude: []]) do
    :ok
  end

  defp print_filters([include: include, exclude: exclude]) do
    if include != [], do: IO.puts format_filters(include, :include)
    if exclude != [], do: IO.puts format_filters(exclude, :exclude)
    IO.puts("")
    :ok
  end

  defp print_failure(formatted, config) do
    cond do
      config.trace -> IO.puts ""
      true -> IO.puts "\n"
    end
    IO.puts formatted
  end

  defp format_test_type_counts(%{test_counter: test_counter} = _config) do
    Enum.map test_counter, fn {type, count} ->
      type_pluralized = pluralize(count, type, ExUnit.plural_rule(type |> to_string()))
      "#{count} #{type_pluralized}, "
    end
  end

  # Color styles

    colorize(:red, msg, config)
  end

  defp formatter(:diff_enabled?, _, %{colors: colors}), do: colors[:enabled]

  defp formatter(:error_info, msg, config), do: colorize(:red, msg, config)

  defp formatter(:extra_info, msg, config), do: colorize(:cyan, msg, config)

  defp formatter(:location_info, msg, config), do: colorize([:bright, :black], msg, config)

  defp formatter(:diff_delete, msg, config), do: colorize(:red, msg, config)

  defp formatter(:diff_delete_whitespace, msg, config),
    do: colorize(IO.ANSI.color_background(2, 0, 0), msg, config)

  defp formatter(:diff_insert, msg, config), do: colorize(:green, msg, config)

  defp formatter(:diff_insert_whitespace, msg, config),
    do: colorize(IO.ANSI.color_background(0, 2, 0), msg, config)
