simplecov 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ # Foo class
2
+ class Foo
3
+ def initialize
4
+ @foo = 'baz'
5
+ end
6
+
7
+ def bar
8
+ @foo
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # Foo class
2
+ class Foo
3
+ def initialize
4
+ @foo = 'baz'
5
+ end
6
+
7
+ def bar
8
+ @foo
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ puts "foo"
2
+ puts "foo"
3
+ puts "foo"
4
+ puts "foo"
@@ -0,0 +1,5 @@
1
+
2
+ class Resultset
3
+ VERSION = 2
4
+ end
5
+
@@ -0,0 +1,10 @@
1
+ # Foo class
2
+ class Foo
3
+ def initialize
4
+ @foo = 'baz'
5
+ end
6
+
7
+ def bar
8
+ @foo
9
+ end
10
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'simplecov'
8
+ SimpleCov.coverage_dir('tmp/coverage')
9
+
10
+ class Test::Unit::TestCase
11
+ def source_fixture(filename)
12
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', filename))
13
+ end
14
+ end
@@ -0,0 +1,73 @@
1
+ require 'helper'
2
+
3
+ class TestFilters < Test::Unit::TestCase
4
+ context "A source file initialized with some coverage data" do
5
+ setup do
6
+ @source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
7
+ end
8
+
9
+ should "pass a new SimpleCov::StringFilter 'foobar'" do
10
+ assert SimpleCov::StringFilter.new('foobar').passes?(@source_file)
11
+ end
12
+
13
+ should "pass a new SimpleCov::StringFilter 'some/path'" do
14
+ assert SimpleCov::StringFilter.new('some/path').passes?(@source_file)
15
+ end
16
+
17
+ should "not pass a new SimpleCov::StringFilter 'test/fixtures'" do
18
+ assert !SimpleCov::StringFilter.new('test/fixtures').passes?(@source_file)
19
+ end
20
+
21
+ should "not pass a new SimpleCov::StringFilter 'test/fixtures/sample.rb'" do
22
+ assert !SimpleCov::StringFilter.new('test/fixtures/sample.rb').passes?(@source_file)
23
+ end
24
+
25
+ should "not pass a new SimpleCov::StringFilter 'sample.rb'" do
26
+ assert !SimpleCov::StringFilter.new('sample.rb').passes?(@source_file)
27
+ end
28
+
29
+ should "pass a new SimpleCov::BlockFilter that is not applicable" do
30
+ assert SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'foo.rb'}).passes?(@source_file)
31
+ end
32
+
33
+ should "not pass a new SimpleCov::BlockFilter that is applicable" do
34
+ assert !SimpleCov::BlockFilter.new(Proc.new {|s| File.basename(s.filename) == 'sample.rb'}).passes?(@source_file)
35
+ end
36
+ end
37
+
38
+ context "with no filters set up and a basic source file in an array" do
39
+ setup do
40
+ SimpleCov.filters = []
41
+ @files = [SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])]
42
+ end
43
+
44
+ should "return 0 items after executing SimpleCov.filtered on files when using a 'sample' string filter" do
45
+ SimpleCov.add_filter "sample"
46
+ assert_equal 0, SimpleCov.filtered(@files).count
47
+ end
48
+
49
+ should "return 0 items after executing SimpleCov.filtered on files when using a 'test/fixtures/' string filter" do
50
+ SimpleCov.add_filter "test/fixtures"
51
+ assert_equal 0, SimpleCov.filtered(@files).count
52
+ end
53
+
54
+ should "return 1 item after executing SimpleCov.filtered on files when using a 'fooo' string filter" do
55
+ SimpleCov.add_filter "fooo"
56
+ assert_equal 1, SimpleCov.filtered(@files).count
57
+ end
58
+
59
+ should "return 0 items after executing SimpleCov.filtered on files when using a block filter that returns true" do
60
+ SimpleCov.add_filter do |src_file|
61
+ true
62
+ end
63
+ assert_equal 0, SimpleCov.filtered(@files).count
64
+ end
65
+
66
+ should "return 1 item after executing SimpleCov.filtered on files when using an always-false block filter" do
67
+ SimpleCov.add_filter do |src_file|
68
+ false
69
+ end
70
+ assert_equal 1, SimpleCov.filtered(@files).count
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,92 @@
1
+ require 'helper'
2
+
3
+ class TestMergeHelpers < Test::Unit::TestCase
4
+ context "With two faked coverage resultsets" do
5
+ setup do
6
+ SimpleCov.use_merging true
7
+ @resultset1 = {source_fixture('sample.rb') => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
8
+ source_fixture('app/models/user.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
9
+ source_fixture('app/controllers/sample_controller.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
10
+ source_fixture('resultset1.rb') => [1, 1, 1, 1]}
11
+
12
+ @resultset2 = {source_fixture('sample.rb') => [1, nil, 1, 1, nil, nil, 1, 1, nil, nil],
13
+ source_fixture('app/models/user.rb') => [nil, 1, 5, 1, nil, nil, 1, 0, nil, nil],
14
+ source_fixture('app/controllers/sample_controller.rb') => [nil, 3, 1, nil, nil, nil, 1, 0, nil, nil],
15
+ source_fixture('resultset2.rb') => [nil, 1, 1, nil]}
16
+ end
17
+
18
+ context "a merge" do
19
+ setup do
20
+ assert @merged = @resultset1.merge_resultset(@resultset2)
21
+ end
22
+
23
+ should "have proper results for sample.rb" do
24
+ assert_equal [1, 1, 2, 2, nil, nil, 2, 2, nil, nil], @merged[source_fixture('sample.rb')]
25
+ end
26
+
27
+ should "have proper results for user.rb" do
28
+ assert_equal [nil, 2, 6, 2, nil, nil, 2, 0, nil, nil], @merged[source_fixture('app/models/user.rb')]
29
+ end
30
+
31
+ should "have proper results for sample_controller.rb" do
32
+ assert_equal [nil, 4, 2, 1, nil, nil, 2, 0, nil, nil], @merged[source_fixture('app/controllers/sample_controller.rb')]
33
+ end
34
+
35
+ should "have proper results for resultset1.rb" do
36
+ assert_equal [1, 1, 1, 1], @merged[source_fixture('resultset1.rb')]
37
+ end
38
+
39
+ should "have proper results for resultset2.rb" do
40
+ assert_equal [nil, 1, 1, nil], @merged[source_fixture('resultset2.rb')]
41
+ end
42
+ end
43
+
44
+ context "and results generated from those" do
45
+ setup do
46
+ system "rm #{SimpleCov::ResultMerger.resultset_path}" if File.exist?(SimpleCov::ResultMerger.resultset_path)
47
+ @result1 = SimpleCov::Result.new(@resultset1)
48
+ @result1.command_name = "result1"
49
+ @result2 = SimpleCov::Result.new(@resultset2)
50
+ @result2.command_name = "result2"
51
+ end
52
+
53
+ context "with stored results" do
54
+ setup do
55
+ assert SimpleCov::ResultMerger.store_result(@result1)
56
+ assert SimpleCov::ResultMerger.store_result(@result2)
57
+ end
58
+
59
+ should "have stored data in resultset_path yaml file" do
60
+ assert File.readlines(SimpleCov::ResultMerger.resultset_path).length > 50
61
+ end
62
+
63
+ should "return a hash containing keys ['result1' and 'result2'] for resultset" do
64
+ assert_equal ['result1', 'result2'], SimpleCov::ResultMerger.resultset.keys.sort
65
+ end
66
+
67
+ should "return proper values for merged_result" do
68
+ assert_equal [nil, 2, 6, 2, nil, nil, 2, 0, nil, nil], SimpleCov::ResultMerger.merged_result.source_files.find {|s| s.filename =~ /user/}.lines.map(&:coverage)
69
+ end
70
+
71
+ context "with second result way above the merge_timeout" do
72
+ setup do
73
+ @result2.created_at = Time.mktime(Time.now.year, Time.now.month, Time.now.day-2)
74
+ assert SimpleCov::ResultMerger.store_result(@result2)
75
+ end
76
+
77
+ should "have only one result in SimpleCov::ResultMerger.results" do
78
+ assert_equal 1, SimpleCov::ResultMerger.results.length
79
+ end
80
+ end
81
+
82
+ context "with merging disabled" do
83
+ setup { SimpleCov.use_merging false }
84
+
85
+ should "return nil for SimpleCov.result" do
86
+ assert_nil SimpleCov.result
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,133 @@
1
+ require 'helper'
2
+
3
+ class TestResult < Test::Unit::TestCase
4
+ context "With a (mocked) Coverage.result" do
5
+ setup do
6
+ SimpleCov.filters = []
7
+ SimpleCov.groups = {}
8
+ SimpleCov.formatter = nil
9
+ @original_result = {source_fixture('sample.rb') => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
10
+ source_fixture('app/models/user.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
11
+ source_fixture('app/controllers/sample_controller.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil]}
12
+ end
13
+
14
+ context "a simple cov result initialized from that" do
15
+ setup { @result = SimpleCov::Result.new(@original_result) }
16
+
17
+ should "have 3 filenames" do
18
+ assert_equal 3, @result.filenames.count
19
+ end
20
+
21
+ should "have 3 source files" do
22
+ assert_equal 3, @result.source_files.count
23
+ assert @result.source_files.all? {|s| s.instance_of?(SimpleCov::SourceFile)}, "Not alL instances are of SimpleCov::SourceFile type"
24
+ end
25
+
26
+ should "have files equal to source_files" do
27
+ assert_equal @result.files, @result.source_files
28
+ end
29
+
30
+ should "have 86.7 covered percent" do
31
+ assert_equal 86.7, @result.covered_percent.round(1)
32
+ end
33
+
34
+ context "dumped with to_yaml" do
35
+ setup { @yaml = @result.to_yaml }
36
+ should("be a string") { assert_equal String, @yaml.class }
37
+
38
+ context "loaded back with from_yaml" do
39
+ setup { @dumped_result = SimpleCov::Result.from_yaml(@yaml) }
40
+
41
+ should "have 3 source files" do
42
+ assert_equal @result.source_files.count, @dumped_result.source_files.count
43
+ end
44
+
45
+ should "have the same covered_percent" do
46
+ assert_equal @result.covered_percent, @dumped_result.covered_percent
47
+ end
48
+
49
+ should "have the same created_at" do
50
+ assert_equal @result.created_at, @dumped_result.created_at
51
+ end
52
+
53
+ should "have the same command_name" do
54
+ assert_equal @result.command_name, @dumped_result.command_name
55
+ end
56
+
57
+ should "have the same original_result" do
58
+ assert_equal @result.original_result, @dumped_result.original_result
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ context "with some filters set up" do
65
+ setup do
66
+ SimpleCov.add_filter 'sample.rb'
67
+ end
68
+
69
+ should "have 2 files in a new simple cov result" do
70
+ assert_equal 2, SimpleCov::Result.new(@original_result).source_files.length
71
+ end
72
+
73
+ should "have 80 covered percent" do
74
+ assert_equal 80, SimpleCov::Result.new(@original_result).covered_percent
75
+ end
76
+ end
77
+
78
+ context "with groups set up for all files" do
79
+ setup do
80
+ SimpleCov.add_group 'Models', 'app/models'
81
+ SimpleCov.add_group 'Controllers', 'app/controllers'
82
+ SimpleCov.add_group 'Other' do |src_file|
83
+ File.basename(src_file.filename) == 'sample.rb'
84
+ end
85
+ @result = SimpleCov::Result.new(@original_result)
86
+ end
87
+
88
+ should "have 3 groups" do
89
+ assert_equal 3, @result.groups.length
90
+ end
91
+
92
+ should "have user.rb in 'Models' group" do
93
+ assert_equal 'user.rb', File.basename(@result.groups['Models'].first.filename)
94
+ end
95
+
96
+ should "have sample_controller.rb in 'Controllers' group" do
97
+ assert_equal 'sample_controller.rb', File.basename(@result.groups['Controllers'].first.filename)
98
+ end
99
+
100
+ context "and simple formatter being used" do
101
+ setup {SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter}
102
+
103
+ should "return a formatted string with result.format!" do
104
+ assert_equal String, @result.format!.class
105
+ end
106
+ end
107
+ end
108
+
109
+ context "with groups set up that do not match all files" do
110
+ setup do
111
+ SimpleCov.configure do
112
+ add_group 'Models', 'app/models'
113
+ add_group 'Controllers', 'app/controllers'
114
+ end
115
+ @result = SimpleCov::Result.new(@original_result)
116
+ end
117
+
118
+ should "have 3 groups" do
119
+ assert_equal 3, @result.groups.length
120
+ end
121
+
122
+ should "have 1 item per group" do
123
+ @result.groups.each do |name, files|
124
+ assert_equal 1, files.length, "Group #{name} should have 1 file"
125
+ end
126
+ end
127
+
128
+ should "have sample.rb in 'Ungrouped' group" do
129
+ assert_equal 'sample.rb', File.basename(@result.groups['Ungrouped'].first.filename)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class TestSourceFile < Test::Unit::TestCase
4
+ context "A source file initialized with some coverage data" do
5
+ setup do
6
+ @source_file = SimpleCov::SourceFile.new(source_fixture('sample.rb'), [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil])
7
+ end
8
+
9
+ should "have a filename" do
10
+ assert @source_file.filename
11
+ end
12
+
13
+ should "have source equal to src" do
14
+ assert_equal @source_file.source, @source_file.src
15
+ end
16
+
17
+ should "have source_lines equal to lines" do
18
+ assert_equal @source_file.source_lines, @source_file.lines
19
+ end
20
+
21
+ should "have 10 source lines" do
22
+ assert_equal 10, @source_file.lines.count
23
+ end
24
+
25
+ should "have all source lines of type SimpleCov::SourceFile::Line" do
26
+ assert @source_file.lines.all? {|l| l.instance_of?(SimpleCov::SourceFile::Line)}
27
+ end
28
+
29
+ should "have 'class Foo' as line(2).source" do
30
+ assert_equal "class Foo\n", @source_file.line(2).source
31
+ end
32
+
33
+ should "return lines number 2, 3, 4, 7 for covered_lines" do
34
+ assert_equal [2, 3, 4, 7], @source_file.covered_lines.map(&:line)
35
+ end
36
+
37
+ should "return lines number 8 for missed_lines" do
38
+ assert_equal [8], @source_file.missed_lines.map(&:line)
39
+ end
40
+
41
+ should "return lines number 1, 5, 6, 9, 10 for never_lines" do
42
+ assert_equal [1, 5, 6, 9, 10], @source_file.never_lines.map(&:line)
43
+ end
44
+
45
+ should "have 80% covered_percent" do
46
+ assert_equal 80.0, @source_file.covered_percent
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,110 @@
1
+ require 'helper'
2
+
3
+ class TestSourceFileLine < Test::Unit::TestCase
4
+ context "A source line" do
5
+ setup do
6
+ @line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 3)
7
+ end
8
+
9
+ should 'return "# the ruby source" as src' do
10
+ assert_equal '# the ruby source', @line.src
11
+ end
12
+
13
+ should 'return the same for source as for src' do
14
+ assert_equal @line.src, @line.source
15
+ end
16
+
17
+ should 'have line number 5' do
18
+ assert_equal 5, @line.line_number
19
+ end
20
+
21
+ should 'have equal line_number, line and number' do
22
+ assert_equal @line.line_number, @line.line
23
+ assert_equal @line.line_number, @line.number
24
+ end
25
+ end
26
+
27
+ context "A source line with coverage" do
28
+ setup do
29
+ @line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 3)
30
+ end
31
+
32
+ should "have coverage of 3" do
33
+ assert_equal 3, @line.coverage
34
+ end
35
+
36
+ should "be covered?" do
37
+ assert @line.covered?
38
+ end
39
+
40
+ should "not be never?" do
41
+ assert !@line.never?
42
+ end
43
+
44
+ should "not be missed?" do
45
+ assert !@line.missed?
46
+ end
47
+ end
48
+
49
+ context "A source line without coverage" do
50
+ setup do
51
+ @line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, 0)
52
+ end
53
+
54
+ should "have coverage of 0" do
55
+ assert_equal 0, @line.coverage
56
+ end
57
+
58
+ should "not be covered?" do
59
+ assert !@line.covered?
60
+ end
61
+
62
+ should "not be never?" do
63
+ assert !@line.never?
64
+ end
65
+
66
+ should "be missed?" do
67
+ assert @line.missed?
68
+ end
69
+ end
70
+
71
+ context "A source line with no code" do
72
+ setup do
73
+ @line = SimpleCov::SourceFile::Line.new('# the ruby source', 5, nil)
74
+ end
75
+
76
+ should "have nil coverage" do
77
+ assert_nil @line.coverage
78
+ end
79
+
80
+ should "not be covered?" do
81
+ assert !@line.covered?
82
+ end
83
+
84
+ should "be never?" do
85
+ assert @line.never?
86
+ end
87
+
88
+ should "not be missed?" do
89
+ assert !@line.missed?
90
+ end
91
+ end
92
+
93
+ should "raise ArgumentError when initialized with invalid src" do
94
+ assert_raise ArgumentError do
95
+ SimpleCov::SourceFile::Line.new(:symbol, 5, 3)
96
+ end
97
+ end
98
+
99
+ should "raise ArgumentError when initialized with invalid line_number" do
100
+ assert_raise ArgumentError do
101
+ SimpleCov::SourceFile::Line.new("some source", "five", 3)
102
+ end
103
+ end
104
+
105
+ should "raise ArgumentError when initialized with invalid coverage" do
106
+ assert_raise ArgumentError do
107
+ SimpleCov::SourceFile::Line.new("some source", 5, "three")
108
+ end
109
+ end
110
+ end