File: result_spec.rb

package info (click to toggle)
ruby-simplecov 0.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: ruby: 5,550; makefile: 10
file content (239 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# frozen_string_literal: true

require "helper"

describe SimpleCov::Result do
  context "with a (mocked) Coverage.result" do
    before do
      @prev_filters = SimpleCov.filters
      SimpleCov.filters = []
      @prev_groups = SimpleCov.groups
      SimpleCov.groups = {}
      @prev_formatter = SimpleCov.formatter
      SimpleCov.formatter = nil
    end

    after do
      SimpleCov.filters   = @prev_filters
      SimpleCov.groups    = @prev_groups
      SimpleCov.formatter = @prev_formatter
    end

    let(:original_result) do
      {
        source_fixture("sample.rb") => {"lines" => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil]},
        source_fixture("app/models/user.rb") => {"lines" => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil]},
        source_fixture("app/controllers/sample_controller.rb") => {"lines" => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil]}
      }
    end

    context "a simple cov result initialized from that" do
      subject { SimpleCov::Result.new(original_result) }

      it "has 3 filenames" do
        expect(subject.filenames.count).to eq(3)
      end

      it "has 3 source files" do
        expect(subject.source_files.count).to eq(3)
        subject.source_files.each do |source_file|
          expect(source_file).to be_a SimpleCov::SourceFile
        end
      end

      it "returns an instance of SimpleCov::FileList for source_files and files" do
        expect(subject.files).to be_a SimpleCov::FileList
        expect(subject.source_files).to be_a SimpleCov::FileList
      end

      it "has files equal to source_files" do
        expect(subject.files).to eq(subject.source_files)
      end

      it "has accurate covered percent" do
        # in our fixture, there are 13 covered line (result in 1) in all 15 relevant line (result in non-nil)
        expect(subject.covered_percent).to eq(86.66666666666667)
      end

      it "has accurate covered percentages" do
        expect(subject.covered_percentages).to eq([80.0, 80.0, 100.0])
      end

      it "has accurate least covered file" do
        expect(subject.least_covered_file).to match(/sample_controller.rb/)
      end

      %i[covered_percent covered_percentages least_covered_file covered_strength covered_lines missed_lines total_lines].each do |msg|
        it "responds to #{msg}" do
          expect(subject).to respond_to(msg)
        end
      end

      context "dumped with to_hash" do
        it "is a hash" do
          expect(subject.to_hash).to be_a Hash
        end

        context "loaded back with from_hash" do
          let(:dumped_result) do
            SimpleCov::Result.from_hash(subject.to_hash).first
          end

          it "has 3 source files" do
            expect(dumped_result.source_files.count).to eq(subject.source_files.count)
          end

          it "has the same covered_percent" do
            expect(dumped_result.covered_percent).to eq(subject.covered_percent)
          end

          it "has the same covered_percentages" do
            expect(dumped_result.covered_percentages).to eq(subject.covered_percentages)
          end

          it "has the same timestamp" do
            expect(dumped_result.created_at.to_i).to eq(subject.created_at.to_i)
          end

          it "has the same command_name" do
            expect(dumped_result.command_name).to eq(subject.command_name)
          end

          it "has the same original_result" do
            expect(dumped_result.original_result).to eq(subject.original_result)
          end
        end
      end
    end

    context "with some filters set up" do
      before do
        SimpleCov.add_filter "sample.rb"
      end

      it "has 2 files in a new simple cov result" do
        expect(SimpleCov::Result.new(original_result).source_files.length).to eq(2)
      end

      it "has 80 covered percent" do
        expect(SimpleCov::Result.new(original_result).covered_percent).to eq(80)
      end

      it "has [80.0, 80.0] covered percentages" do
        expect(SimpleCov::Result.new(original_result).covered_percentages).to eq([80.0, 80.0])
      end
    end

    context "with groups set up for all files" do
      before do
        SimpleCov.add_group "Models", "app/models"
        SimpleCov.add_group "Controllers", ["app/controllers"]
        SimpleCov.add_group "Other" do |src_file|
          File.basename(src_file.filename) == "sample.rb"
        end
      end

      subject do
        SimpleCov::Result.new(original_result)
      end

      it "has 3 groups" do
        expect(subject.groups.length).to eq(3)
      end

      it "has user.rb in 'Models' group" do
        expect(File.basename(subject.groups["Models"].first.filename)).to eq("user.rb")
      end

      it "has sample_controller.rb in 'Controllers' group" do
        expect(File.basename(subject.groups["Controllers"].first.filename)).to eq("sample_controller.rb")
      end

      context "and simple formatter being used" do
        before do
          SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter
        end

        it "returns a formatted string with result.format!" do
          expect(subject.format!).to be_a String
        end
      end

      context "and multi formatter being used" do
        before do
          SimpleCov.formatters = [
            SimpleCov::Formatter::SimpleFormatter,
            SimpleCov::Formatter::SimpleFormatter
          ]
        end

        it "returns an array containing formatted string with result.format!" do
          formatted = subject.format!
          expect(formatted.count).to eq(2)
          expect(formatted.first).to be_a String
        end
      end
    end

    context "with groups set up that do not match all files" do
      before do
        SimpleCov.configure do
          add_group "Models", "app/models"
          add_group "Controllers", "app/controllers"
        end
      end

      subject { SimpleCov::Result.new(original_result) }

      it "has 3 groups" do
        expect(subject.groups.length).to eq(3)
      end

      it "has 1 item per group" do
        subject.groups.each_value do |files|
          expect(files.length).to eq(1)
        end
      end

      it 'has sample.rb in "Ungrouped" group' do
        expect(File.basename(subject.groups["Ungrouped"].first.filename)).to eq("sample.rb")
      end

      it "returns all groups as instances of SimpleCov::FileList" do
        subject.groups.each_value do |files|
          expect(files).to be_a SimpleCov::FileList
        end
      end
    end

    describe ".from_hash" do
      let(:other_result) do
        {
          source_fixture("sample.rb") => {"lines" => [nil, 1, 1, 1, nil, nil, 0, 0, nil, nil]}
        }
      end
      let(:created_at) { Time.now.to_i }

      it "can consume multiple commands" do
        input = {
          "rspec" => {
            "coverage" => original_result,
            "timestamp" => created_at
          },
          "cucumber" => {
            "coverage" => other_result,
            "timestamp" => created_at
          }
        }

        result = described_class.from_hash(input)

        expect(result.size).to eq 2
        sorted = result.sort_by(&:command_name)
        expect(sorted.map(&:command_name)).to eq %w[cucumber rspec]
        expect(sorted.map(&:created_at).map(&:to_i)).to eq [created_at, created_at]
        expect(sorted.map(&:original_result)).to eq [other_result, original_result]
      end
    end
  end
end