Class: JEMC::Reporter
- Inherits:
-
Object
- Object
- JEMC::Reporter
- Includes:
- Minitest::Reporter
- Defined in:
- lib/jemc/reporter.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#max_tests ⇒ Object
Returns the value of attribute max_tests.
Instance Method Summary collapse
- #after_suite(suite) ⇒ Object
- #after_suites(suites, type) ⇒ Object
- #backtrace(exception) ⇒ Object
- #before_suite(suite) ⇒ Object
- #before_suites(suites, type) ⇒ Object
- #error(suite, test, test_runner) ⇒ Object
- #error_show(test, exc) ⇒ Object
- #failure(suite, test, test_runner) ⇒ Object
- #failure_show(test, exc) ⇒ Object
-
#initialize(options = {}) ⇒ Reporter
constructor
A new instance of Reporter.
- #pass(suite, test, test_runner) ⇒ Object
- #skip(suite, test, test_runner) ⇒ Object
- #skipped_show(test, exc) ⇒ Object
-
#str_fit(str, length, keep: :both, align: :left, pad: ' ', mid: '...') ⇒ Object
Pad or truncate a string intelligently.
Constructor Details
#initialize(options = {}) ⇒ Reporter
Returns a new instance of Reporter.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jemc/reporter.rb', line 12 def initialize( = {}) # @detailed_skip = options.fetch(:detailed_skip, true) # @slow_count = options.fetch(:slow_count, 0) # @slow_suite_count = options.fetch(:slow_suite_count, 0) # @fast_fail = options.fetch(:fast_fail, false) # @test_times = [] # @suite_times = [] @color = .fetch(:color) do output.tty? && ( ENV["TERM"] =~ /^screen|color/ || ENV["EMACS"] == "t" ) end @columns = ENV['COLUMNS'] || `tput cols`.to_i || 80 end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
10 11 12 |
# File 'lib/jemc/reporter.rb', line 10 def color @color end |
#columns ⇒ Object
Returns the value of attribute columns.
10 11 12 |
# File 'lib/jemc/reporter.rb', line 10 def columns @columns end |
#max_tests ⇒ Object
Returns the value of attribute max_tests.
10 11 12 |
# File 'lib/jemc/reporter.rb', line 10 def max_tests @max_tests end |
Instance Method Details
#after_suite(suite) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/jemc/reporter.rb', line 78 def after_suite(suite) return if suite.test_methods.size <= 0 puts suite.suffix suite.tests[:skip].each { |x| skipped_show(*x) } suite.tests[:fail].each { |x| failure_show(*x) } suite.tests[:error].each { |x| error_show(*x) } end |
#after_suites(suites, type) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/jemc/reporter.rb', line 86 def after_suites(suites, type) puts puts '%d tests, %d assertions, %d failures, %d errors, %d skips' % [runner.test_count, runner.assertion_count, \ runner.failures, runner.errors, runner.skips] end |
#backtrace(exception) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/jemc/reporter.rb', line 144 def backtrace(exception) index = exception.backtrace \ .find_index{|x| x=~/\/minitest\/(?=[^\/]*$).*['`]run['`]$/} index ||= -1 exception.backtrace.map do |line| m = line.match /(.*)(?<=\/)([^\:\/]*):(\d+)(?=:in):in ['`](.*)['`]/ m ||= line.match /()([^\:\/]*):(\d+)(?=:in):in ['`](.*)['`]/ str_fit(m[1], columns/3, :keep=>:tail, :align=>:right) \ + white(str_fit(m[2], columns/3-m[3].size-1, :keep=>:head))+':'+white(m[3]) \ + bold(cyan(str_fit(' '+m[4], (columns-2*(columns/3)), \ :keep=>:head, :align=>:right))) end[0...index].reverse end |
#before_suite(suite) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/jemc/reporter.rb', line 71 def before_suite(suite) return if suite.test_methods.size <= 0 length = @columns-suite.test_methods.size-suite.suffix_size print str_fit(suite.name, length-1)+' ' end |
#before_suites(suites, type) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 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 |
# File 'lib/jemc/reporter.rb', line 30 def before_suites(suites, type) puts self.max_tests = suites.map{|x| x.test_methods.size}.max for suite in suites do class << suite attr_accessor :reporter attr_accessor :tests end suite.reporter = self suite.tests = Hash.new suite.tests[:pass] = [] suite.tests[:fail] = [] suite.tests[:error] = [] suite.tests[:skip] = [] def suite.suffix dig = reporter.max_tests.to_s.size groups = [:pass, :fail, :error] colors = [:green,:red,:magenta] (' ' + groups .map { |x| self.tests[x].size } .map { |x,e| (x > 0) ? ("%0#{dig}d" % x) : dig.times.map{' '}.join } .each_with_index .map { |x,i| reporter.send(colors[i], x) } .join('/')) end def suite.suffix_size ANSI::Code.uncolor(self.suffix).size end end end |
#error(suite, test, test_runner) ⇒ Object
108 109 110 111 |
# File 'lib/jemc/reporter.rb', line 108 def error(suite, test, test_runner) print magenta('E') suite.tests[:error] << [test, test_runner.exception] end |
#error_show(test, exc) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jemc/reporter.rb', line 132 def error_show(test, exc) pre = black('>')+magenta('>')+bold(magenta('E'))+' ' pre_size = ANSI::Code.uncolor(pre).size puts pre+magenta(str_fit(test, @columns-pre_size)) puts backtrace(exc) puts yellow("#{exc.class}: #{exc.}") puts end |
#failure(suite, test, test_runner) ⇒ Object
103 104 105 106 |
# File 'lib/jemc/reporter.rb', line 103 def failure(suite, test, test_runner) print red('F') suite.tests[:fail] << [test, test_runner.exception] end |
#failure_show(test, exc) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jemc/reporter.rb', line 119 def failure_show(test, exc) pre = black('>')+red('>')+bold(red('F'))+' ' pre_size = ANSI::Code.uncolor(pre).size puts pre+red(str_fit(test, @columns-pre_size)) exc.backtrace.unshift runner.location(exc)+":in `'" puts backtrace(exc) puts yellow("#{exc.class}: #{exc.}") puts end |
#pass(suite, test, test_runner) ⇒ Object
93 94 95 96 |
# File 'lib/jemc/reporter.rb', line 93 def pass(suite, test, test_runner) print green('.') suite.tests[:pass] << [test, test_runner.exception] end |
#skip(suite, test, test_runner) ⇒ Object
98 99 100 101 |
# File 'lib/jemc/reporter.rb', line 98 def skip(suite, test, test_runner) print blue('s') suite.tests[:skip] << [test, test_runner.exception] end |
#skipped_show(test, exc) ⇒ Object
113 114 115 116 117 |
# File 'lib/jemc/reporter.rb', line 113 def skipped_show(test, exc) pre = black('>')+blue('>')+bold(blue('s'))+' ' pre_size = ANSI::Code.uncolor(pre).size puts pre+blue(str_fit(test, @columns-pre_size)) end |
#str_fit(str, length, keep: :both, align: :left, pad: ' ', mid: '...') ⇒ Object
Pad or truncate a string intelligently
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/jemc/reporter.rb', line 172 def str_fit(str, length, keep: :both, align: :left, pad:' ', mid:'...') str = str.to_s raise ArgumentError, "Keyword argument :keep must be :head, :tail, or :both."\ unless [:head, :tail, :both].include? keep raise ArgumentError, "Keyword argument :align must be :left, :right, or :center."\ unless [:left, :right, :center].include? align length = [0,length].max diff = length - str.size # Return padded string if length is the same or to be increased if diff>=0 padstr = diff.times.map{pad}.join[0..diff] case align when :left return str + padstr when :right return padstr + str when :center return padstr[0...(padstr.size/2)] + str + padstr[0...(padstr.size-(padstr.size/2))] end end # Return truncated midstring if length is the same or smaller than mid's size return mid[0...length] if length<=mid.size boundary_re = /((?<=[a-zA-Z])(?![a-zA-Z]))|((?<![a-zA-Z])(?=[a-zA-Z]))/ indices = str.to_enum(:scan, boundary_re).map{$`.size}-[0,str.size] # p indices # Collect pairs of indices closest to desired diff nearest = [[str.size,0,str.size]] for x in indices x = 0 if keep==:tail for y in indices y = str.size if keep==:head if (y>x) candidate = [(y-x+diff-mid.size).abs,(y-x+diff-mid.size),x,y] if nearest[0][0]> candidate[0] nearest = [candidate] elsif nearest[0][0]==candidate[0] nearest << candidate end end end end nearest.uniq! # Choose the most appropriate pair of indices from nearest case keep when :both nearest.sort!{|a,b| (str.size-(a[2]+a[3])).abs <=> (str.size-(b[2]+b[3])).abs} when :tail nearest.reverse! end nearest = nearest.first if nearest[0]!=0 case keep when :both nearest[2]+=(nearest[1]/2) nearest[3]-=(nearest[1]-(nearest[1]/2)) when :head nearest[2]+=nearest[1] when :tail nearest[3]-=nearest[1] end if nearest[2]<0 nearest[2]+=(0-nearest[2]) nearest[3]+=(0-nearest[2]) elsif nearest[3]>str.size nearest[3]+=(str.size-nearest[2]) nearest[3]+=(str.size-nearest[2]) end nearest[1]=(y-x+diff-mid.size) nearest[0]=nearest[1].abs end return str[0...nearest[2]]+mid+str[nearest[3]..str.size] end |