Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit test for result, moved filters to own file

  • Loading branch information...
commit 37f9696e783bdcf2980552933c52b0f2e4b765a4 1 parent 5d11038
@colszowka authored
View
59 lib/simple_cov.rb
@@ -2,40 +2,49 @@
module SimpleCov
class CoverageDataError < StandardError; end;
- class Filter
- attr_reader :filter_argument
- def initialize(filter_argument)
- @filter_argument = filter_argument
- end
-
- def passes?(source_file)
- raise "The base filter class is not intended for direct use"
- end
- end
-
- class StringFilter < SimpleCov::Filter
- def passes?(source_file)
- !(source_file.filename =~ /#{filter_argument}/)
- end
- end
-
- class BlockFilter < SimpleCov::Filter
- def passes?(source_file)
- !filter_argument.call(source_file)
- end
- end
-
class << self
attr_writer :filters
def filters
@filters ||= []
end
+ #
+ # Add a filter to the processing chain.
+ # There are three ways to define a filter:
+ #
+ # * as a String that will then be matched against all source files' file paths,
+ # SimpleCov.add_filter 'app/models' # will reject all your models
+ # * as a block which will be passed the source file in question and should either
+ # return a true or false value, depending on whether the file should be removed
+ # SimpleCov.add_filter do |src_file|
+ # File.basename(src_file.filename) == 'environment.rb'
+ # end # Will exclude environment.rb files from the results
+ # * as an instance of a subclass of SimpleCov::Filter. See the documentation there
+ # on how to define your own filter classes
+ #
def add_filter(filter_argument=nil, &filter_proc)
- filters << filter
+ if filter_argument.kind_of?(SimpleCov::Filter)
+ filters << filter_argument
+ elsif filter_argument.kind_of?(String)
+ filters << StringFilter.new(filter_argument)
+ elsif filter_proc
+ filters << BlockFilter.new(filter_proc)
+ else
+ raise ArgumentError, "Please specify either a string or a block to filter with"
+ end
+ end
+
+ # Applies the configured filters on the given array of SimpleCov::SourceFile items
+ def apply_filters(files)
+ result = files.clone
+ filters.each do |filter|
+ result = result.select {|source_file| filter.passes?(source_file) }
+ end
+ result
end
end
end
require 'simple_cov/source_file'
-require 'simple_cov/result'
+require 'simple_cov/result'
+require 'simple_cov/filter'
View
35 lib/simple_cov/filter.rb
@@ -0,0 +1,35 @@
+module SimpleCov
+ #
+ # Base filter class. Inherit from this to create custom filters,
+ # and overwrite the passes?(source_file) instance method
+ #
+ # # A sample class that rejects all source files.
+ # class StupidFilter < SimpleCov::Filter
+ # def passes?(source_file)
+ # false
+ # end
+ # end
+ #
+ class Filter
+ attr_reader :filter_argument
+ def initialize(filter_argument)
+ @filter_argument = filter_argument
+ end
+
+ def passes?(source_file)
+ raise "The base filter class is not intended for direct use"
+ end
+ end
+
+ class StringFilter < SimpleCov::Filter
+ def passes?(source_file)
+ !(source_file.filename =~ /#{filter_argument}/)
+ end
+ end
+
+ class BlockFilter < SimpleCov::Filter
+ def passes?(source_file)
+ !filter_argument.call(source_file)
+ end
+ end
+end
View
8 lib/simple_cov/result.rb
@@ -12,11 +12,13 @@ def initialize(original_result)
def filenames
files.map(&:filename)
end
+
+ def covered_percent
+ files.map(&:covered_percent).inject(:+) / files.count.to_f
+ end
def filter!
- SimpleCov.filters.each do |filter|
- @files = files.reject {|source_file| filter.call(source_file) }
- end
+ @files = SimpleCov.apply_filters(files)
end
end
end
View
10 test/fixtures/app/controllers/sample_controller.rb
@@ -0,0 +1,10 @@
+# Foo class
+class Foo
+ def initialize
+ @foo = 'baz'
+ end
+
+ def bar
+ @foo
+ end
+end
View
10 test/fixtures/app/models/user.rb
@@ -0,0 +1,10 @@
+# Foo class
+class Foo
+ def initialize
+ @foo = 'baz'
+ end
+
+ def bar
+ @foo
+ end
+end
View
36 test/test_filters.rb
@@ -34,4 +34,40 @@ class TestFilters < Test::Unit::TestCase
assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).passes?(@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
+
+ should "return 0 items after executing SimpleCov.apply_filters on files when using a 'sample' string filter" do
+ SimpleCov.add_filter "sample"
+ assert_equal 0, SimpleCov.apply_filters(@files).count
+ end
+
+ should "return 0 items after executing SimpleCov.apply_filters on files when using a 'test/fixtures/' string filter" do
+ SimpleCov.add_filter "test/fixtures"
+ assert_equal 0, SimpleCov.apply_filters(@files).count
+ end
+
+ should "return 1 item after executing SimpleCov.apply_filters on files when using a 'fooo' string filter" do
+ SimpleCov.add_filter "fooo"
+ assert_equal 1, SimpleCov.apply_filters(@files).count
+ end
+
+ should "return 0 items after executing SimpleCov.apply_filters on files when using a block filter that returns true" do
+ SimpleCov.add_filter do |src_file|
+ true
+ end
+ assert_equal 0, SimpleCov.apply_filters(@files).count
+ end
+
+ should "return 1 item after executing SimpleCov.apply_filters on files when using an always-false block filter" do
+ SimpleCov.add_filter do |src_file|
+ false
+ end
+ assert_equal 1, SimpleCov.apply_filters(@files).count
+ end
+ end
end
View
45 test/test_result.rb
@@ -0,0 +1,45 @@
+class TestFilters < Test::Unit::TestCase
+ context "With a (mocked) Coverage.result" do
+ setup do
+ SimpleCov.filters = []
+ @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]}
+ end
+
+ context "a simple cov result initialized from that" do
+ setup { @result = SimpleCov::Result.new(@original_result) }
+
+ should "have 3 filenames" do
+ assert_equal 3, @result.filenames.count
+ end
+
+ should "have 3 source files" do
+ assert_equal 3, @result.source_files.count
+ assert @result.source_files.all? {|s| s.instance_of?(SimpleCov::SourceFile)}, "Not alL instances are of SimpleCov::SourceFile type"
+ end
+
+ should "have files equal to source_files" do
+ assert_equal @result.files, @result.source_files
+ end
+
+ should "have 93.3 covered percent" do
+ assert_equal 93.3, @result.covered_percent.round(1)
+ end
+ end
+
+ context "with some filters set up" do
+ setup do
+ SimpleCov.add_filter 'sample.rb'
+ end
+
+ should "have 2 files in a new simple cov result" do
+ assert_equal 2, SimpleCov::Result.new(@original_result).source_files.length
+ end
+
+ should "have 90 covered percent" do
+ assert_equal 90, SimpleCov::Result.new(@original_result).covered_percent
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.