Class: Whitestone::Output
- Defined in:
- lib/whitestone/output.rb,
lib/whitestone/output.rb
Overview
Defined Under Namespace
Classes: BacktraceProcessor
Class Method Summary collapse
Instance Method Summary collapse
- #display_details_of_failures_and_errors ⇒ Object
-
#display_results_npass_nfail_nerror_etc(stats) ⇒ Object
Prepares and displays a colourful summary message saying how many tests have passed, failed and errored.
-
#display_test_by_test_result(top_level) ⇒ Object
Print the name and result of each test, using indentation.
-
#initialize ⇒ Output
constructor
A new instance of Output.
- #report_failure(description, message, backtrace) ⇒ Object
- #report_specification_error(e) ⇒ Object
- #report_uncaught_exception(description, exception, _calls, force_filter_bt = false) ⇒ Object
- #set_full_backtrace ⇒ Object
Constructor Details
#initialize ⇒ Output
Returns a new instance of Output.
17 18 19 20 21 22 23 24 25 |
# File 'lib/whitestone/output.rb', line 17 def initialize # @buf is the buffer into which we write details of errors and failures so # that they can be emitted to the console all together. @buf = StringIO.new # @@files is a means of printing lines of code in failure and error # details. @@files ||= Hash.new { |h,k| h[k] = File.readlines(k) rescue nil } @filter_backtrace_yn = true end |
Class Method Details
Instance Method Details
#display_details_of_failures_and_errors ⇒ Object
102 103 104 105 106 107 |
# File 'lib/whitestone/output.rb', line 102 def display_details_of_failures_and_errors unless @buf.string.strip.empty? puts puts @buf.string end end |
#display_results_npass_nfail_nerror_etc(stats) ⇒ Object
Prepares and displays a colourful summary message saying how many tests have passed, failed and errored.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/whitestone/output.rb', line 113 def display_results_npass_nfail_nerror_etc(stats) npass = stats[:pass] || 0 nfail = stats[:fail] || 0 nerror = stats[:error] || 0 overall = (nfail + nerror > 0) ? :FAIL : :PASS time = stats[:time] assertions = stats[:assertions] overall_str = overall.to_s.ljust(9) npass_str = sprintf "#pass: %-6d", npass nfail_str = sprintf "#fail: %-6d", nfail nerror_str = sprintf "#error: %-6d", nerror assertions_str = sprintf "assertions: %-6d", assertions time_str = sprintf "time: %3.3f", time overall_col = (overall == :PASS) ? :green : :red npass_col = :green nfail_col = (nfail > 0) ? :red : :green nerror_col = (nerror > 0) ? :magenta : :green assertions_col = :white time_col = :white coloured_info = Col.inline( overall_str, [overall_col, :bold], npass_str, [npass_col, :bold], nfail_str, [nfail_col, :bold], nerror_str, [nerror_col, :bold], assertions_str, [assertions_col, :bold], time_str, [time_col, :bold] ) equals = Col["=" * 80].fmt [overall_col, :bold] nl = "\n" output = String.new.tap { |str| str << equals << nl str << " " << coloured_info << nl str << equals << nl } puts puts output end |
#display_test_by_test_result(top_level) ⇒ Object
Print the name and result of each test, using indentation. This must be done after execution is finished in order to get the tree structure right.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/whitestone/output.rb', line 42 def display_test_by_test_result(top_level) pipe = "|" space = " " #empty_line = space + pipe + (space * 76) + pipe header = Col[" +----- Report " + "-" * (77-14) + "+"].cb empty_line = Col[space, pipe, space * 76, pipe].fmt(:_, :cb, :_, :cb) line = lambda { |desc,c1,s1,result,c2,s2| padding = space * ( 77 - (1 + desc.size + result.size ) ) Col.inline( space, :_, pipe, :cb, desc, [c1,s1], \ result, [c2,s2], padding, :_, pipe, :cb) } = Col[" +#{'-'*76}+"].cb puts puts header tree_walk(top_level.tests) do |test, level| description = (space + space + " " * level + test.description).ljust(67) description = description[0...67] colour1, style1 = case test.result when :pass then [:_, :_] when :fail then [:red, :bold] when :error then [:magenta, :bold] when :blank then [:_, :_] end result, colour2, style2 = case test.result when :pass then ['PASS', :green, :bold] when :fail then ['FAIL', :red, :bold] when :error then ['ERROR', :magenta, :bold] when :blank then ['-', :green, :bold] end result = " " + result if level == 0 puts empty_line colour1 = (test.passed? or test.blank?) ? :yellow : colour2 style1 = :bold end puts line[description, colour1, style1, result, colour2, style2] end puts empty_line puts end |
#report_failure(description, message, backtrace) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/whitestone/output.rb', line 159 def report_failure(description, , backtrace) ||= "No message! #{__FILE__}:#{__LINE__}" bp = BacktraceProcessor.new(backtrace, @filter_backtrace_yn) # Determine the file and line number of the failed assertion, and extract # the code surrounding that line. file, line = bp.determine_file_and_lineno() code = extract_code(file, line) # Emit the failure report. @buf.puts @buf.puts Col["FAIL: #{description}"].rb @buf.puts code.___indent(4) if code @buf.puts .___indent(2) @buf.puts " Backtrace\n" + bp.backtrace.join("\n").___indent(4) end |
#report_specification_error(e) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/whitestone/output.rb', line 198 def report_specification_error(e) puts puts "You have made an error in specifying one of your assertions." puts "Details below; can't continue; exiting." puts puts Col.inline("Message: ", :_, e., :yb) puts puts "Filtered backtrace:" filtered = BacktraceProcessor.new(e.backtrace, true).backtrace puts filtered.join("\n").___indent(2) puts puts "Full backtrace:" puts e.backtrace.join("\n").___indent(2) puts end |
#report_uncaught_exception(description, exception, _calls, force_filter_bt = false) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/whitestone/output.rb', line 178 def report_uncaught_exception(description, exception, _calls, force_filter_bt=false) filter_yn = @filter_backtrace_yn || force_filter_bt bp = BacktraceProcessor.new(exception.backtrace, filter_yn) # Determine the current test file, the line number that triggered the # error, and extract the code surrounding that line. file, line = bp.determine_file_and_lineno(_calls) code = extract_code(file, line) # Emit the error report. @buf.puts @buf.puts Col("ERROR: #{description}").fmt(:mb) @buf.puts code.___indent(4) if code @buf.puts Col.inline(" Class: ", :mb, exception.class, :yb) @buf.puts Col.inline(" Message: ", :mb, exception., :yb) @buf.puts " Backtrace\n" + bp.backtrace.join("\n").___indent(4) end |
#set_full_backtrace ⇒ Object
27 28 29 |
# File 'lib/whitestone/output.rb', line 27 def set_full_backtrace @filter_backtrace_yn = false end |