Polished up the formatting and code for the output and separated out the colorizing...
[maraby.git] / bacon / output.rb
blob770b20e9ed6fa336473d9e6fedc37643c2560119
1 # Colorising Bacon's output.
3 module OutputColorizer
4   # colorize
5   def color(text, color_val)
6     "#{color_val}#{text}\e[0m"
7   end
8   # colors
9   def green(text); color(text, "\e[32m"); end
10   def red(text); color(text, "\e[31m"); end
11   def magenta(text); color(text, "\e[35m"); end
12   def yellow(text); color(text, "\e[33m"); end
13   def blue(text); color(text, "\e[34m"); end
14   # colors but default to black on false
15   def green!(text,test); return green(text) if test; text; end
16   def red!(text,test); return red(text) if test; text; end
17   def magenta!(text,test); return magenta(text) if test; text; end
18   def yellow!(text,test); return yellow(text) if test; text; end
19   def blue!(text,test); return blue(text) if test; text; end
20 end
22 module Bacon
23   
24   # Colorizes the SpecDox output
25   module CSpecDoxOutput
26     include OutputColorizer
27     def handle_specification(name)
28       puts name
29       yield
30       puts
31     end
32     def handle_requirement(description)
33       error = yield
34       if error.empty?
35         puts green("- #{description}")
36       else
37         case label = error[0..0]
38         when 'F'
39           puts red("- #{description} [#{error}]")
40         when 'E'
41           puts magenta("- #{description} [#{error}]")
42         end
43       end
44     end
45     def handle_summary
46       specs, reqs, failed, errors = Counter.values_at(:specifications, :requirements, :failed, :errors)
47       puts "", ErrorLog
48       msg = green("%d tests, %d assertions, %d failures, %d errors") unless failed > 0 || errors > 0
49       msg = "%d tests, %d assertions, #{red!("%d failures",failed>0)}, #{magenta!("%d errors",errors>0)}"
50       puts msg % Counter.values_at(:specifications, :requirements, :failed, :errors)
51     end
52   end
53   
54   # Colorizes the TestUnit output
55   module CTestUnitOutput
56     include OutputColorizer
57     def handle_specification(name)  yield  end
58     def handle_requirement(description)
59       error = yield
60       if error.empty?
61         print green(".")
62       else
63         case label = error[0..0]
64         when 'F'
65           print red(label)
66         when 'E'
67           print magenta(label)
68         end
69       end
70     end
71     def handle_summary
72       specs, reqs, failed, errors = Counter.values_at(:specifications, :requirements, :failed, :errors)
73       puts "", ErrorLog
74       msg = green("%d tests, %d assertions, %d failures, %d errors") unless failed > 0 || errors > 0
75       msg = "%d tests, %d assertions, #{red!("%d failures",failed>0)}, #{magenta!("%d errors",errors>0)}"
76       puts msg % Counter.values_at(:specifications, :requirements, :failed, :errors)
77     end
78   end
79   
80 end