Module: Monkey

Defined in:
lib/monkeytest.rb

Constant Summary collapse

PATTERN =
/.+_test.rb/
DIRECTORIES =
['./test/unit/','./test/functional/','./test/integration/']

Class Method Summary collapse

Class Method Details

.run_tests(mode, html = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
156
157
158
159
160
161
162
163
# File 'lib/monkeytest.rb', line 9

def self.run_tests(mode, html=false)
  
  if File.new("./lib/tasks/monkey.rake").read.match(/created with monkeytest 0.2/).nil?
    puts "It looks like you've updated your gem.\nPlease run monkeytest again on your project."
    exit 1
  end
  
  quiet = ((mode.to_s == "quiet") or (mode.to_s == "q"))
  silent = ((mode.to_s == "silent") or (mode.to_s == "s"))
  directories = DIRECTORIES.collect {|dir| File.expand_path(dir) }
  failures = Array.new
  totalTests = totalAsserts = totalErrors = totalFailures = 0

  if html
    puts "<div id='monkeytest'>"
  end
  
  directories.each do |dir|
    if html
      puts "<div style='margin-bottom: 1em;'><div style='font-weight: bold;'>#{dir.match(/\/([^\/]+)\/?$/)[1].upcase} TESTS:</div><table style='width: 550px;'>"
    else
      puts "\n#{dir.match(/\/([^\/]+)\/?$/)[1].upcase} TESTS:".bold
    end
    Dir.foreach(dir) do |file|
      if file.match(PATTERN)
        if html
          print "<tr><td style='width: 50%; padding-left: 8px;'>#{file}</td>"
        else
          print "   #{file.ljust(35)}"
        end
    
        fullResult = `ruby -C \"#{dir}\" \"#{dir}/#{file}\"`
    
        result = fullResult.match(/([\d]+) tests, ([\d]+) assertions, ([\d]+) failures, ([\d]+) errors/) or break
        numTests = result[1].to_i
        numAsserts = result[2].to_i
        numFailures = result[3].to_i
        numErrors = result[4].to_i

        totalTests += numTests
        totalAsserts += numAsserts
        totalErrors += numErrors
        totalFailures += numFailures
    
        if html
          print "<td style='width: 20%; text-align:right;'>#{numTests.to_s} Tests</td>"
          print "<td style='width: 20%; text-align:right;'>#{numAsserts.to_s} Assertions</td>"
        else
          print "#{numTests.to_s.rjust(4)} Tests  "
          print "#{numAsserts.to_s.rjust(4)} Assertions     "
        end
        
        if numErrors > 0 then
          if html
            puts "<td style='color: red; width: 10%; text-align:right;'>ERROR</td></tr>"
          else
            puts "ERROR".red
          end
          failures.push fullResult
        elsif numFailures > 0 then
          if html
            puts "<td style='color: yellow; width: 10%; text-align:right;'>FAIL</td></tr>"
          else
            puts "FAIL".yellow
          end
          failures.push fullResult
        else
          if html
            puts "<td style='color: green; width: 10%; text-align:right;'>PASS</td></tr>"
          else
            puts "PASS".green
          end
        end
      end
    end
    if html
      print "</table></div>"
    end
  end
  if html
    puts "<hr style=\"width: 500px; position: absolute; left: 25px;\"/>"
    puts "<div id='totals'>"
    print "<table style='width: 550px;'><tr><td style='font-weight: bold; width: 40%;'>TOTALS</td>"
    print "<td style='width: 20%; text-align:right;'>#{totalTests.to_s} Tests</td>"
    print "<td style='width: 20%; text-align:right;'>#{totalAsserts.to_s} Assertions</td>"
    print "<td style='width: 20%; text-align:right;'>#{(totalFailures+totalErrors).to_s} Problem(s)</td></tr></table>"
  else
    puts '-'*80
    print "TOTALS:".bold.ljust(38)
    print "#{totalTests.to_s.rjust(4)} Tests  "
    print "#{totalAsserts.to_s.rjust(4)} Assertions  "
    print "#{(totalFailures+totalErrors).to_s.rjust(4)} Problem(s)\n"
  end
  if silent
    if html
      puts "</div>"
    else
      puts "\n"
    end
  elsif quiet
    
    if html
      puts "<div id='errors-failures' style=\"margin-top: 20px\"><div style='font-weight: bold;'>Errors & Failures</div>"
    else
      puts "\n\nErrors & Failures:".bold 
    end unless failures.empty?
    
    puts "<table>" if html
    failures.each do |badthing|
      while matches = badthing.match(/(Error|Failure):[\s]+(test_[^\()]+)\(([^\)]+)\)([\s]\[[^\]]+\/(.+?):([\d]+)\])?/m) do
        unless html
          msg = "   #{matches[1].ljust(10).red}" if matches[1] == "Error"
          msg = "   #{matches[1].ljust(10).yellow}" if matches[1] == "Failure"
          msg << " #{matches[3].underline} : #{matches[2]}"
          msg << " (#{matches[5]}:#{matches[6]})" unless matches[6].nil?
        else
          msg = "<tr>"
          msg << "<td style=\"color:#FF0000; width: 10%\">#{matches[1]}</td>" if matches[1] == "Error"
          msg << "<td style=\"color:#FFFF00; width: 10%\">#{matches[1]}</td>" if matches [1] == "Failure"
          msg << "<td><u>#{matches[3]}</u> : #{matches[2]}"
          msg << " (#{matches[5]}:#{matches[6]})" unless matches[6].nil?
          msg << "</td></tr>"
        end
        puts msg
        badthing = matches.post_match
      end
    end
    if html
      puts "</table></div>"
    else
      puts "\n"
    end
  else
    if html
      puts "<div id='errors-failures'><div style='font-weight: bold;'>Errors & Failures</div>" unless failures.empty?
    else
      puts "\n\nErrors & Failures:".bold unless failures.empty?
    end
    failures.each do |badthing|
      if html
        puts badthing
        puts "</div>"
      else
        puts badthing
        puts "\n"
      end
      if html 
        puts "</div>"
      end
    end
  end
  if html
    puts "</div>"
  end
end