Skip to content

Commit

Permalink
Merge pull request #200 from infertux/on_ruby
Browse files Browse the repository at this point in the history
Refactor on_ruby helper to handle Ruby 2.0
  • Loading branch information
colszowka committed Feb 12, 2013
2 parents 1def2a5 + 16b2848 commit ae49b30
Show file tree
Hide file tree
Showing 13 changed files with 469 additions and 503 deletions.
24 changes: 12 additions & 12 deletions lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
# Code coverage for ruby 1.9. Please check out README for a full introduction.
#
module SimpleCov
# Indicates invalid coverage data
class CoverageDataError < StandardError; end;

class << self
attr_accessor :running#, :result # TODO: Remove result?
attr_accessor :running

#
# Sets up SimpleCov to run against your project.
Expand All @@ -27,11 +24,6 @@ class << self
#
def start(adapter=nil, &block)
if SimpleCov.usable?
unless defined? Coverage
require 'coverage'
require 'simplecov/jruby16_fix'
end

load_adapter(adapter) if adapter
configure(&block) if block_given?
@result = nil
Expand Down Expand Up @@ -104,11 +96,19 @@ def load_adapter(name)
end

#
# Checks whether we're on a proper version of ruby (1.9+) and returns false if this is not the case,
# also printing an appropriate warning
# Checks whether we're on a proper version of Ruby (likely 1.9+) which
# provides coverage support
#
def usable?
"1.9".respond_to?(:encoding)
return @usable unless @usable.nil?

@usable = begin
require 'coverage'
require 'simplecov/jruby16_fix'
true
rescue LoadError
false
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def source_fixture(filename)
# Keep 1.8-rubies from complaining about missing tests in each file that covers only 1.9 functionality
def default_test
end

end

require 'shoulda_macros'
Expand Down
10 changes: 0 additions & 10 deletions test/shoulda_macros.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
module ShouldaMacros
#
# Simple block helper for running certain tests only on specific ruby versions.
# The given strings will be regexp-matched against RUBY_VERSION
#
def on_ruby(*ruby_versions)
context "On Ruby #{RUBY_VERSION}" do
yield
end if ruby_versions.any? {|v| RUBY_VERSION =~ /#{v}/ }
end

def should_be(boolean_flag)
should "be #{boolean_flag}" do
assert_equal true, subject.send(boolean_flag)
Expand Down
34 changes: 16 additions & 18 deletions test/test_1_8_fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@
# be called in a test/spec-helper simplecov config block
#
class Test18FallBacks < Test::Unit::TestCase
on_ruby '1.8' do
should "return false when calling SimpleCov.start" do
assert_equal false, SimpleCov.start
end
should "return false when calling SimpleCov.start" do
assert_equal false, SimpleCov.start
end

should "return false when calling SimpleCov.start with a block" do
assert_equal false, SimpleCov.start { raise "Shouldn't reach this!?" }
end
should "return false when calling SimpleCov.start with a block" do
assert_equal false, SimpleCov.start { raise "Shouldn't reach this!?" }
end

should "return false when calling SimpleCov.configure with a block" do
assert_equal false, SimpleCov.configure { raise "Shouldn't reach this!?" }
end
should "return false when calling SimpleCov.configure with a block" do
assert_equal false, SimpleCov.configure { raise "Shouldn't reach this!?" }
end

should "allow to define an adapter" do
begin
SimpleCov.adapters.define 'testadapter' do
add_filter '/config/'
end
rescue => err
assert false, "Adapter definition should have been fine, but raised #{err}"
should "allow to define an adapter" do
begin
SimpleCov.adapters.define 'testadapter' do
add_filter '/config/'
end
rescue => err
assert false, "Adapter definition should have been fine, but raised #{err}"
end
end
end
end if RUBY_VERSION.start_with? '1.8'
28 changes: 13 additions & 15 deletions test/test_command_guesser.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
require 'helper'

class TestCommandGuesser < Test::Unit::TestCase
on_ruby '1.9' do
def self.should_guess_command_name(expectation, *argv)
argv.each do |args|
should "return '#{expectation}' for '#{args}'" do
SimpleCov::CommandGuesser.original_run_command = args
assert_equal expectation, SimpleCov::CommandGuesser.guess
end
def self.should_guess_command_name(expectation, *argv)
argv.each do |args|
should "return '#{expectation}' for '#{args}'" do
SimpleCov::CommandGuesser.original_run_command = args
assert_equal expectation, SimpleCov::CommandGuesser.guess
end
end

should_guess_command_name "Unit Tests", '/some/path/test/units/foo_bar_test.rb', 'test/units/foo.rb', 'test/foo.rb', 'test/{models,helpers,unit}/**/*_test.rb'
should_guess_command_name "Functional Tests", '/some/path/test/functional/foo_bar_controller_test.rb', 'test/{controllers,mailers,functional}/**/*_test.rb'
should_guess_command_name "Integration Tests", '/some/path/test/integration/foo_bar_controller_test.rb', 'test/integration/**/*_test.rb'
should_guess_command_name "Cucumber Features", 'features', 'cucumber', 'cucumber features'
should_guess_command_name "RSpec", '/some/path/spec/foo.rb'
should_guess_command_name "Unit Tests", 'some_arbitrary_command with arguments' # Because Test::Unit const is defined!
end
end

should_guess_command_name "Unit Tests", '/some/path/test/units/foo_bar_test.rb', 'test/units/foo.rb', 'test/foo.rb', 'test/{models,helpers,unit}/**/*_test.rb'
should_guess_command_name "Functional Tests", '/some/path/test/functional/foo_bar_controller_test.rb', 'test/{controllers,mailers,functional}/**/*_test.rb'
should_guess_command_name "Integration Tests", '/some/path/test/integration/foo_bar_controller_test.rb', 'test/integration/**/*_test.rb'
should_guess_command_name "Cucumber Features", 'features', 'cucumber', 'cucumber features'
should_guess_command_name "RSpec", '/some/path/spec/foo.rb'
should_guess_command_name "Unit Tests", 'some_arbitrary_command with arguments' # Because Test::Unit const is defined!
end if SimpleCov.usable?
12 changes: 5 additions & 7 deletions test/test_deleted_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
# Test to verify correct handling of deleted files,
# see issue #9 on github
class TestDeletedSource < Test::Unit::TestCase
on_ruby '1.8', '1.9' do
context "A source file which is subsequently deleted" do
should "not cause an error" do
Dir.chdir(File.join(File.dirname(__FILE__), 'fixtures')) do
`ruby deleted_source_sample.rb`
assert_equal 0, $?.exitstatus
end
context "A source file which is subsequently deleted" do
should "not cause an error" do
Dir.chdir(File.join(File.dirname(__FILE__), 'fixtures')) do
`ruby deleted_source_sample.rb`
assert_equal 0, $?.exitstatus
end
end
end
Expand Down
32 changes: 15 additions & 17 deletions test/test_file_list.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
require 'helper'

class TestFileList < Test::Unit::TestCase
on_ruby "1.9" do
context "With a file list from a result" do
setup do
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, 1, 0, nil, nil]}
@file_list = SimpleCov::Result.new(original_result).files
end
context "With a file list from a result" do
setup do
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, 1, 0, nil, nil]}
@file_list = SimpleCov::Result.new(original_result).files
end

should("have 13 covered_lines") { assert_equal 13, @file_list.covered_lines }
should("have 2 missed_lines") { assert_equal 2, @file_list.missed_lines }
should("have 18 never_lines") { assert_equal 18, @file_list.never_lines }
should("have 15 lines_of_code") { assert_equal 15, @file_list.lines_of_code }
should("have 3 skipped_lines") { assert_equal 3, @file_list.skipped_lines }
should("have 13 covered_lines") { assert_equal 13, @file_list.covered_lines }
should("have 2 missed_lines") { assert_equal 2, @file_list.missed_lines }
should("have 18 never_lines") { assert_equal 18, @file_list.never_lines }
should("have 15 lines_of_code") { assert_equal 15, @file_list.lines_of_code }
should("have 3 skipped_lines") { assert_equal 3, @file_list.skipped_lines }

should "have correct covered_percent" do
assert_equal 100.0*13/15, @file_list.covered_percent
end
should "have correct covered_percent" do
assert_equal 100.0*13/15, @file_list.covered_percent
end
end
end
end if SimpleCov.usable?
114 changes: 56 additions & 58 deletions test/test_filters.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,78 @@
require 'helper'

class TestFilters < Test::Unit::TestCase
on_ruby '1.9' do
context "A source file initialized with some coverage data" do
setup do
@source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
end
context "A source file initialized with some coverage data" do
setup do
@source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
end

should "not match a new SimpleCov::StringFilter 'foobar'" do
assert !SimpleCov::StringFilter.new('foobar').matches?(@source_file)
end
should "not match a new SimpleCov::StringFilter 'foobar'" do
assert !SimpleCov::StringFilter.new('foobar').matches?(@source_file)
end

should "not match a new SimpleCov::StringFilter 'some/path'" do
assert !SimpleCov::StringFilter.new('some/path').matches?(@source_file)
end
should "not match a new SimpleCov::StringFilter 'some/path'" do
assert !SimpleCov::StringFilter.new('some/path').matches?(@source_file)
end

should "match a new SimpleCov::StringFilter 'test/fixtures'" do
assert SimpleCov::StringFilter.new('test/fixtures').matches?(@source_file)
end
should "match a new SimpleCov::StringFilter 'test/fixtures'" do
assert SimpleCov::StringFilter.new('test/fixtures').matches?(@source_file)
end

should "match a new SimpleCov::StringFilter 'test/fixtures/sample.rb'" do
assert SimpleCov::StringFilter.new('test/fixtures/sample.rb').matches?(@source_file)
end
should "match a new SimpleCov::StringFilter 'test/fixtures/sample.rb'" do
assert SimpleCov::StringFilter.new('test/fixtures/sample.rb').matches?(@source_file)
end

should "match a new SimpleCov::StringFilter 'sample.rb'" do
assert SimpleCov::StringFilter.new('sample.rb').matches?(@source_file)
end
should "match a new SimpleCov::StringFilter 'sample.rb'" do
assert SimpleCov::StringFilter.new('sample.rb').matches?(@source_file)
end

should "not match a new SimpleCov::BlockFilter that is not applicable" do
assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'foo.rb'}).matches?(@source_file)
end
should "not match a new SimpleCov::BlockFilter that is not applicable" do
assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'foo.rb'}).matches?(@source_file)
end

should "match a new SimpleCov::BlockFilter that is applicable" do
assert SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).matches?(@source_file)
end
should "match a new SimpleCov::BlockFilter that is applicable" do
assert SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).matches?(@source_file)
end
end

context "with no filters set up and a basic source file in an array" do
setup do
SimpleCov.filters = []
@files = [SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
end
context "with no filters set up and a basic source file in an array" do
setup do
SimpleCov.filters = []
@files = [SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
end

should "return 0 items after executing SimpleCov.filtered on files when using a 'sample' string filter" do
SimpleCov.add_filter "sample"
assert_equal 0, SimpleCov.filtered(@files).count
end
should "return 0 items after executing SimpleCov.filtered on files when using a 'sample' string filter" do
SimpleCov.add_filter "sample"
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 0 items after executing SimpleCov.filtered on files when using a 'test/fixtures/' string filter" do
SimpleCov.add_filter "test/fixtures"
assert_equal 0, SimpleCov.filtered(@files).count
end
should "return 0 items after executing SimpleCov.filtered on files when using a 'test/fixtures/' string filter" do
SimpleCov.add_filter "test/fixtures"
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 1 item after executing SimpleCov.filtered on files when using a 'fooo' string filter" do
SimpleCov.add_filter "fooo"
assert_equal 1, SimpleCov.filtered(@files).count
end
should "return 1 item after executing SimpleCov.filtered on files when using a 'fooo' string filter" do
SimpleCov.add_filter "fooo"
assert_equal 1, SimpleCov.filtered(@files).count
end

should "return 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
SimpleCov.add_filter do |src_file|
true
end
assert_equal 0, SimpleCov.filtered(@files).count
should "return 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
SimpleCov.add_filter do |src_file|
true
end
assert_equal 0, SimpleCov.filtered(@files).count
end

should "return 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
SimpleCov.add_filter do |src_file|
false
end
assert_equal 1, SimpleCov.filtered(@files).count
should "return 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
SimpleCov.add_filter do |src_file|
false
end
assert_equal 1, SimpleCov.filtered(@files).count
end

should "return a FileList after filtering" do
SimpleCov.add_filter "fooo"
assert_equal SimpleCov::FileList, SimpleCov.filtered(@files).class
end
should "return a FileList after filtering" do
SimpleCov.add_filter "fooo"
assert_equal SimpleCov::FileList, SimpleCov.filtered(@files).class
end
end
end
end if SimpleCov.usable?
Loading

0 comments on commit ae49b30

Please sign in to comment.