require 'assert/view/base' require 'assert/result' require 'ansi/code' module Assert::View class Terminal < Base options do default_styled false default_passed_styles :green default_failed_styles :red, :bold default_errored_styles :yellow, :bold default_skipped_styles :cyan default_ignored_styles :magenta end def render(*args, &runner) self.io_puts(:load_stmt) if count(:tests) > 0 self.io_puts(:run_stmt) runner.call if runner self.io_puts(:detailed_results) end self.io_puts(:results_stmt) end def handle_runtime_result(result) sym = result.to_sym self.io_print(result_io_msg(self.options.send("#{sym}_abbrev"), sym)) end protected def load_stmt tplur = (tcount = count(:tests)) == 1 ? "test": "tests" "\nLoaded suite (#{tcount} #{tplur})" end def run_stmt "Running tests in random order, seed: '#{self.runner_seed}'" end def detailed_results details = self.suite.ordered_tests.reverse.collect do |test| test.results.collect do |result| if show_result_details?(result) [ result_io_msg(result.to_s, result.to_sym), output_io_msg(test.output.to_s) ].join("\n") end end end.flatten.compact "\n\n" + details.join("\n\n") if !details.empty? end def results_stmt rplur = (rcount = count(:results)) == 1 ? "result" : "results" [ "\n", "#{rcount} test #{rplur}: ", results_breakdown, "\n\n", "(#{self.run_time} seconds)" ].join('') end def results_breakdown if count(:passed) == count(:results) stmnt = if count(:results) < 1 "uhh..." elsif count(:results) == 1 "it passed" else "all passed" end result_io_msg(stmnt, :passed) else breakdowns = [:passed, :failed, :ignored, :skipped, :errored] breakdowns = breakdowns.inject([]) do |results, result_sym| results << (if count(result_sym) > 0 result_io_msg("#{count(result_sym)} #{result_sym}", result_sym) end) end.compact if breakdowns.size < 2 breakdowns.join('') elsif breakdowns.size == 2 breakdowns.join(" and ") else [breakdowns[0..-2].join(", "), breakdowns.last].join(", and ") end end end def result_io_msg(msg, result_sym) term_styles = if self.options.styled self.options.send("#{result_sym}_styles") end io_msg(msg, :term_styles => term_styles) end def output_io_msg(output) if output && !output.empty? [ "--- stdout ---", io_msg(output), "--------------" ].collect{|i| i.strip}.join("\n") end end def io_msg(msg, opts={}) val = super if !(style = term_style(*opts[:term_styles])).empty? val = style + val + ANSI.send(:reset) else val end end def term_style(*ansi_codes) ansi_codes.collect{|code| ANSI.send(code) rescue nil}.compact.join('') end def show_result_details?(result) ([:failed, :errored].include?(result.to_sym)) || ([:skipped, :ignored].include?(result.to_sym) && result.message) end end end