Permalink
Browse files

made test suite generate lots of code & coverage

  • Loading branch information...
1 parent 26f3c64 commit 85edee123baa4a635aec9c95b934ca899265515f @cap10morgan cap10morgan committed Feb 8, 2011
Showing with 58 additions and 22 deletions.
  1. +6 −6 test/fixtures/file_tr.html
  2. +2 −2 test/fixtures/sample.rb
  3. +5 −5 test/fixtures/totals_tr.html
  4. +45 −9 test/simplecov-rcov_test.rb
View
@@ -1,7 +1,7 @@
<tr class="all_files">
- <td class="left_align"><a href="#./test/fixtures/app/controllers/sample_controller.rb">./test/fixtures/app/controllers/sample_controller.rb</a></td>
- <td class='right_align'><tt>10</tt></td>
- <td class='right_align'><tt>5</tt></td>
- <td class="left_align"><tt class=''>60.00%</tt></td>
- <td class="left_align"><tt class=''>60.00%</tt></td>
- </tr>
+ <td class="left_align"><a href="#./app/controllers/sample_controller.rb">./app/controllers/sample_controller.rb</a></td>
+ <td class='right_align'><tt>900</tt></td>
+ <td class='right_align'><tt>600</tt></td>
+ <td class="left_align"><tt class=''>50.00%</tt></td>
+ <td class="left_align"><tt class=''>50.00%</tt></td>
+ </tr>
View
@@ -3,8 +3,8 @@ class Foo
def initialize
@foo = 'baz'
end
-
+
def bar
@foo
end
-end
+end
@@ -1,7 +1,7 @@
<tr>
<td class="left_align">TOTAL</td>
- <td class='right_align'><tt>30</tt></td>
- <td class='right_align'><tt>15</tt></td>
- <td class="left_align"><tt class=''>80.00%</tt></td>
- <td class="left_align"><tt class='coverage_total'>80.00%</tt></td>
- </tr>
+ <td class='right_align'><tt>2700</tt></td>
+ <td class='right_align'><tt>1800</tt></td>
+ <td class="left_align"><tt class=''>50.00%</tt></td>
+ <td class="left_align"><tt class='coverage_total'>50.00%</tt></td>
+ </tr>
@@ -1,26 +1,62 @@
-require "#{File.dirname(__FILE__)}/helper"
+require "#{File.expand_path(File.dirname(__FILE__))}/helper"
class SimplecovRcovFormatterTest < Test::Unit::TestCase
def test_format
SimpleCov::Formatter::RcovFormatter.stubs( :path_result ).returns( '/tmp/rcov/coverage_test_index.html' )
if File.exists?( SimpleCov::Formatter::RcovFormatter.path_result )
File.delete( SimpleCov::Formatter::RcovFormatter.path_result )
end
-
- @original_result = {
- source_fixture( 'sample.rb' ) => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
- source_fixture( 'app/models/user.rb' ) => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
- source_fixture( 'app/controllers/sample_controller.rb' ) => [nil, 1, 1, 1, nil, nil, 0, 0, nil, nil]
- }
+
+ # Sets the number of lines of source we generate.
+ # If you change it from 900, you'll need to edit the fixtures appropriately.
+ # The number should always be evenly divisible by 3 and the coverage percentage
+ # will always be 50%.
+ source_lines_count = 900
+
+ fixtures = ['sample.rb', 'app/models/user.rb', 'app/controllers/sample_controller.rb']
+ @original_result = {}
+ fakeroot = '/fake'
+ fixtures.each do |fixture|
+ @original_result[File.join(fakeroot, fixture)] = generate_coverage_results(source_lines_count)
+ fixture_source_lines = generate_source_lines(fixture, source_lines_count)
+ File.stubs(:readlines).with(File.join(fakeroot, fixture)).returns(fixture_source_lines)
+ SimpleCov.stubs(:root).returns(fakeroot)
+ end
@result = SimpleCov::Result.new( @original_result )
rcov_result = SimpleCov::Formatter::RcovFormatter.new().format( @result )
-
+
assert_match( File.read( "#{File.dirname(__FILE__)}/fixtures/totals_tr.html"), rcov_result )
assert_match( File.read( "#{File.dirname(__FILE__)}/fixtures/file_tr.html"), rcov_result )
assert( File.exists?( SimpleCov::Formatter::RcovFormatter.path_result ) )
end
-
+
+ def generate_coverage_results(num_lines)
+ result_types = [nil, 0, 1]
+ each_result_count = num_lines / 3
+ results = []
+ each_result_count.times do
+ results += result_types
+ end
+ rand_results = []
+ results.count.times do
+ idx = rand_between(0, results.count-1)
+ rand_results << results.delete_at(idx)
+ end
+ rand_results
+ end
+
+ def rand_between(min, max)
+ min + rand(max-min)
+ end
+
+ def generate_source_lines(template, num_lines)
+ template_lines = File.read(source_fixture(template)).split("\n")
+ source_lines = []
+ num_lines.times { source_lines << template_lines.sample }
+ source_lines
+ end
+
def source_fixture( filename )
File.expand_path( File.join( File.dirname( __FILE__ ), 'fixtures', filename ) )
end

0 comments on commit 85edee1

Please sign in to comment.