lib/xcpretty/printer.rb in xcpretty-0.0.3 vs lib/xcpretty/printer.rb in xcpretty-0.0.4

- old
+ new

@@ -1,53 +1,69 @@ -require "paint" +require "xcpretty/ansi" module XCPretty module Printer - attr_accessor :colorize + module Matchers + # @regex Captured groups + # $1 = suite + # $2 = time + TESTS_RUN_START_MATCHER = /Test Suite '(?:.*\/)?(.*[ox]ctest.*)' started at(.*)/ - # @regex Captured groups - # $1 = file - # $2 = test_case - # $3 = failure_message - FAILING_TEST_MATCHER = /(.+:\d+):\serror:\s[\+\-]\[(.*)\]\s:(?:\s'.*'\s\[FAILED\],)?\s(.*)/ + # @regex Captured groups + # $1 = suite + # $2 = time + TESTS_RUN_COMPLETION_MATCHER = /Test Suite '(?:.*\/)?(.*[ox]ctest.*)' finished at(.*)/ - # @regex Captured groups - # $1 = test_case - # $2 = time - PASSING_TEST_MATCHER = /Test Case\s'-\[(.*)\]'\spassed\s\((\d*\.\d{3})\sseconds\)/ + # @regex Captured groups + # $1 = test_case + # $2 = time + PASSING_TEST_MATCHER = /Test Case\s'-\[.*\s(.*)\]'\spassed\s\((\d*\.\d{3})\sseconds\)/ - TESTS_DONE_MATCHER = /Test Suite ('.*\.(o|x)ctest(.*)') finished at/ - # @regex Captured groups - # $1 test suite name - TESTS_START_MATCHER = /Test Suite ('.*(\.(o|x)ctest(.*))?') started at/ - EXECUTED_MATCHER = /^Executed/ + # @regex Captured groups + # $1 = file + # $2 = test_suite + # $3 = test_case + # $4 = reason + FAILING_TEST_MATCHER = /(.+:\d+):\serror:\s[\+\-]\[(.*)\s(.*)\]\s:(?:\s'.*'\s\[FAILED\],)?\s(.*)/ - Paint::SHORTCUTS[:printer] = { - :white => Paint.color(:bold), - :red => Paint.color(:red), - :green => Paint.color(:green, :bright), - :link => Paint.color(:cyan), - } + # @regex Captured groups + # $1 test suite name + TEST_SUITE_START_MATCHER = /Test Suite '(.*)' started at/ + EXECUTED_MATCHER = /^Executed/ + end - include Paint::Printer + include ANSI + include Matchers + def use_unicode=(bool) + @use_unicode = !!bool + end + + def use_unicode? + !!@use_unicode + end + def pretty_print(text) update_test_state(text) formatted_text = pretty_format(text) formatted_text = format_test_summary(text) if formatted_text.empty? STDOUT.print(formatted_text + optional_newline) unless formatted_text.empty? end def update_test_state(text) case text - when FAILING_TEST_MATCHER - store_failure($1, $2, $3) - when TESTS_DONE_MATCHER + when TESTS_RUN_START_MATCHER + @tests_done = false + @printed_summary = false + @failures = {} + when TESTS_RUN_COMPLETION_MATCHER @tests_done = true + when FAILING_TEST_MATCHER + store_failure($1, $2, $3, $4) end end def format_test_summary(text) if text =~ EXECUTED_MATCHER && @tests_done && !@printed_summary @@ -73,36 +89,36 @@ :configuration => configuration } end def test_summary(executed_message) - formatted_failures = failures.map do |f| - reason = colorize? ? red(f[:failure_message]) : f[:failure_message] - path = colorize? ? link(f[:file]) : f[:file] - "#{f[:test_case]}, #{reason}\n#{path}" - end.join("\n\n") + formatted_suites = failures_per_suite.map do |suite, failures| + formatted_failures = failures.map do |f| + " #{f[:test_case]}, #{f[:reason]}\n #{f[:file]}" + end.join("\n\n") + + "#{suite}\n#{formatted_failures}" + end + final_message = if colorize? - failures.any? ? red(executed_message) : green(executed_message) + formatted_suites.any? ? red(executed_message) : green(executed_message) else executed_message end - text = [formatted_failures, final_message].join("\n\n\n").strip + text = [formatted_suites, final_message].join("\n\n\n").strip "\n\n#{text}" end - def failures - @failures ||= [] + def failures_per_suite + @failures ||= {} end - def store_failure(file, test_case, failure_message) - failures << { - :file => file, + def store_failure(file, test_suite, test_case, reason) + failures_per_suite[test_suite] ||= [] + failures_per_suite[test_suite] << { + :file => cyan(file), + :reason => red(reason), :test_case => test_case, - :failure_message => failure_message } - end - - def colorize? - !!@colorize end end end