|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "helper" |
| 4 | +require "support/coverage_fixtures" |
4 | 5 |
|
5 | 6 | COVERAGE_FOR_SAMPLE_RB = { |
6 | 7 | "lines" => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil, nil, 1, 0, nil, nil, nil], |
|
16 | 17 | "branches" => nil |
17 | 18 | }.freeze |
18 | 19 |
|
19 | | -COVERAGE_FOR_BRANCHES_RB = { |
20 | | - "lines" => [1, 1, 1, nil, 1, nil, 1, 0, nil, 1, nil, nil, nil], |
21 | | - "branches" => { |
22 | | - [:if, 0, 3, 4, 3, 21] => |
23 | | - {[:then, 1, 3, 4, 3, 10] => 0, [:else, 2, 3, 4, 3, 21] => 1}, |
24 | | - [:if, 3, 5, 4, 5, 26] => |
25 | | - {[:then, 4, 5, 16, 5, 20] => 1, [:else, 5, 5, 23, 5, 26] => 0}, |
26 | | - [:if, 6, 7, 4, 11, 7] => |
27 | | - {[:then, 7, 8, 6, 8, 10] => 0, [:else, 8, 10, 6, 10, 9] => 1} |
28 | | - } |
29 | | -}.freeze |
30 | | - |
31 | | -COVERAGE_FOR_INLINE = { |
32 | | - "lines" => [1, 1, 1, nil, 1, 1, 0, nil, 1, nil, nil, nil, nil], |
33 | | - "branches" => { |
34 | | - [:if, 0, 3, 11, 3, 33] => |
35 | | - {[:then, 1, 3, 23, 3, 27] => 1, [:else, 2, 3, 30, 3, 33] => 0}, |
36 | | - [:if, 3, 6, 6, 10, 9] => |
37 | | - {[:then, 4, 7, 8, 7, 12] => 0, [:else, 5, 9, 8, 9, 11] => 1} |
38 | | - } |
39 | | -}.freeze |
40 | | - |
41 | | -COVERAGE_FOR_NEVER_RB = {"lines" => [nil, nil], "branches" => {}}.freeze |
42 | | - |
43 | 20 | COVERAGE_FOR_SKIPPED_RB = {"lines" => [nil, nil, nil, nil]}.freeze |
44 | 21 |
|
45 | 22 | COVERAGE_FOR_SKIPPED_RB_2 = {"lines" => [nil, nil, 0, nil]}.freeze |
|
52 | 29 | } |
53 | 30 | }.freeze |
54 | 31 |
|
55 | | -COVERAGE_FOR_NOCOV_COMPLEX_RB = { |
56 | | - "lines" => [nil, nil, 1, 1, nil, 1, nil, nil, nil, 1, nil, nil, 1, nil, nil, 0, nil, 1, nil, 0, nil, nil, 1, nil, nil, nil, nil], |
57 | | - "branches" => { |
58 | | - [:if, 0, 6, 4, 11, 7] => |
59 | | - {[:then, 1, 7, 6, 7, 7] => 0, [:else, 2, 10, 6, 10, 7] => 1}, |
60 | | - [:if, 3, 13, 4, 13, 24] => |
61 | | - {[:then, 4, 13, 4, 13, 12] => 1, [:else, 5, 13, 4, 13, 24] => 0}, |
62 | | - [:while, 6, 16, 4, 16, 27] => |
63 | | - {[:body, 7, 16, 4, 16, 12] => 2}, |
64 | | - [:case, 8, 18, 4, 24, 7] => { |
65 | | - [:when, 9, 20, 6, 20, 11] => 0, |
66 | | - [:when, 10, 23, 6, 23, 10] => 1, |
67 | | - [:else, 11, 18, 4, 24, 7] => 0 |
68 | | - } |
69 | | - } |
70 | | -}.freeze |
71 | | - |
72 | | -COVERAGE_FOR_NESTED_BRANCHES_RB = { |
73 | | - "lines" => [nil, nil, 1, 1, 1, 1, 1, 1, nil, nil, 0, nil, nil, nil, nil], |
74 | | - "branches" => { |
75 | | - [:while, 0, 7, 8, 7, 31] => |
76 | | - {[:body, 1, 7, 8, 7, 16] => 2}, |
77 | | - [:if, 2, 6, 6, 9, 9] => |
78 | | - {[:then, 3, 7, 8, 8, 11] => 1, [:else, 4, 6, 6, 9, 9] => 0}, |
79 | | - [:if, 5, 5, 4, 12, 7] => |
80 | | - {[:then, 6, 6, 6, 9, 9] => 1, [:else, 7, 11, 6, 11, 11] => 0} |
81 | | - } |
82 | | -}.freeze |
83 | | - |
84 | | -COVERAGE_FOR_CASE_STATEMENT_RB = { |
85 | | - "lines" => [1, 1, 1, nil, 0, nil, 1, nil, 0, nil, 0, nil, nil, nil], |
86 | | - "branches" => { |
87 | | - [:case, 0, 3, 4, 12, 7] => { |
88 | | - [:when, 1, 5, 6, 5, 10] => 0, |
89 | | - [:when, 2, 7, 6, 7, 10] => 1, |
90 | | - [:when, 3, 9, 6, 9, 10] => 0, |
91 | | - [:else, 4, 11, 6, 11, 11] => 0 |
92 | | - } |
93 | | - } |
94 | | -}.freeze |
95 | | - |
96 | | -COVERAGE_FOR_CASE_WITHOUT_ELSE_STATEMENT_RB = { |
97 | | - "lines" => [1, 1, 1, nil, 0, nil, 1, nil, 0, nil, nil, nil], |
98 | | - "branches" => { |
99 | | - [:case, 0, 3, 4, 10, 7] => { |
100 | | - [:when, 1, 5, 6, 5, 10] => 0, |
101 | | - [:when, 2, 7, 6, 7, 10] => 1, |
102 | | - [:when, 3, 9, 6, 9, 10] => 0, |
103 | | - [:else, 4, 3, 4, 10, 7] => 0 |
104 | | - } |
105 | | - } |
106 | | -}.freeze |
107 | | - |
108 | | -COVERAGE_FOR_ELSIF_RB = { |
109 | | - "lines" => [1, 1, 1, 0, 1, 0, 1, 1, nil, 0, nil, nil, nil], |
110 | | - "branches" => { |
111 | | - [:if, 0, 7, 4, 10, 10] => |
112 | | - {[:then, 1, 8, 6, 8, 10] => 1, [:else, 2, 10, 6, 10, 10] => 0}, |
113 | | - [:if, 3, 5, 4, 10, 10] => |
114 | | - {[:then, 4, 6, 6, 6, 10] => 0, [:else, 5, 7, 4, 10, 10] => 1}, |
115 | | - [:if, 6, 3, 4, 11, 7] => |
116 | | - {[:then, 7, 4, 6, 4, 10] => 0, [:else, 8, 5, 4, 10, 10] => 1} |
117 | | - } |
118 | | -}.freeze |
119 | | - |
120 | | -COVERAGE_FOR_BRANCH_TESTER_RB = { |
121 | | - "lines" => [nil, nil, 1, 1, nil, 1, nil, 1, 1, nil, nil, 1, 0, nil, nil, 1, 0, nil, 1, nil, nil, 1, 1, 1, nil, nil, 1, 0, nil, nil, 1, 1, nil, 0, nil, 1, 1, 0, 0, 1, 5, 0, 0, nil, 0, nil, 0, nil, nil, nil], |
122 | | - "branches" => { |
123 | | - [:if, 0, 4, 0, 4, 19] => |
124 | | - {[:then, 1, 4, 12, 4, 15] => 0, [:else, 2, 4, 18, 4, 19] => 1}, |
125 | | - [:unless, 3, 6, 0, 6, 23] => |
126 | | - {[:else, 4, 6, 0, 6, 23] => 0, [:then, 5, 6, 0, 6, 6] => 1}, |
127 | | - [:unless, 6, 8, 0, 10, 3] => |
128 | | - {[:else, 7, 8, 0, 10, 3] => 0, [:then, 8, 9, 2, 9, 14] => 1}, |
129 | | - [:unless, 9, 12, 0, 14, 3] => |
130 | | - {[:else, 10, 12, 0, 14, 3] => 1, [:then, 11, 13, 2, 13, 14] => 0}, |
131 | | - [:unless, 12, 16, 0, 20, 3] => |
132 | | - {[:else, 13, 19, 2, 19, 13] => 1, [:then, 14, 17, 2, 17, 14] => 0}, |
133 | | - [:if, 15, 22, 0, 22, 19] => |
134 | | - {[:then, 16, 22, 0, 22, 6] => 0, [:else, 17, 22, 0, 22, 19] => 1}, |
135 | | - [:if, 18, 23, 0, 25, 3] => |
136 | | - {[:then, 19, 24, 2, 24, 14] => 1, [:else, 20, 23, 0, 25, 3] => 0}, |
137 | | - [:if, 21, 27, 0, 29, 3] => |
138 | | - {[:then, 22, 28, 2, 28, 14] => 0, [:else, 23, 27, 0, 29, 3] => 1}, |
139 | | - [:if, 24, 31, 0, 35, 3] => |
140 | | - {[:then, 25, 32, 2, 32, 14] => 1, [:else, 26, 34, 2, 34, 13] => 0}, |
141 | | - [:if, 27, 42, 0, 47, 8] => |
142 | | - {[:then, 28, 43, 2, 45, 13] => 0, [:else, 29, 47, 2, 47, 8] => 0}, |
143 | | - [:if, 30, 40, 0, 47, 8] => |
144 | | - {[:then, 31, 41, 2, 41, 25] => 1, [:else, 32, 42, 0, 47, 8] => 0}, |
145 | | - [:if, 33, 37, 0, 48, 3] => |
146 | | - {[:then, 34, 38, 2, 39, 21] => 0, [:else, 35, 40, 0, 47, 8] => 1} |
147 | | - } |
148 | | -}.freeze |
149 | | - |
150 | | -COVERAGE_FOR_SINGLE_NOCOV_RB = { |
151 | | - "lines" => [nil, 1, 1, 1, 0, 1, 0, 1, 1, nil, 0, nil, nil, nil], |
152 | | - "branches" => { |
153 | | - [:if, 0, 8, 4, 11, 10] => |
154 | | - {[:then, 1, 9, 6, 9, 10] => 1, [:else, 2, 11, 6, 11, 10] => 0}, |
155 | | - [:if, 3, 6, 4, 11, 10] => |
156 | | - {[:then, 4, 7, 6, 7, 10] => 0, [:else, 5, 8, 4, 11, 10] => 1}, |
157 | | - [:if, 6, 4, 4, 12, 7] => |
158 | | - {[:then, 7, 5, 6, 5, 10] => 0, [:else, 8, 6, 4, 11, 10] => 1} |
159 | | - } |
160 | | -}.freeze |
161 | | - |
162 | | -COVERAGE_FOR_UNEVEN_NOCOV_RB = { |
163 | | - "lines" => [1, 1, nil, 1, 0, 1, 0, nil, 1, 1, nil, nil, 0, nil, nil, nil], |
164 | | - "branches" => { |
165 | | - [:if, 0, 9, 4, 13, 10] => |
166 | | - {[:then, 1, 10, 6, 10, 10] => 1, [:else, 2, 13, 6, 13, 10] => 0}, |
167 | | - [:if, 3, 6, 4, 13, 10] => |
168 | | - {[:then, 4, 7, 6, 7, 10] => 0, [:else, 5, 9, 4, 13, 10] => 1}, |
169 | | - [:if, 6, 4, 4, 14, 7] => |
170 | | - {[:then, 7, 5, 6, 5, 10] => 0, [:else, 8, 6, 4, 13, 10] => 1} |
171 | | - } |
172 | | -}.freeze |
173 | | - |
174 | 32 | COVERAGE_FOR_SINGLE_LINE = {"lines" => [nil]}.freeze |
175 | 33 |
|
176 | 34 | COVERAGE_FOR_DOUBLE_LINES = {"lines" => [nil, 1]}.freeze |
|
448 | 306 |
|
449 | 307 | context "when file with branches" do |
450 | 308 | subject(:source_file) do |
451 | | - described_class.new(source_fixture("branches.rb"), COVERAGE_FOR_BRANCHES_RB) |
| 309 | + described_class.new(source_fixture("branches.rb"), CoverageFixtures::BRANCHES_RB) |
452 | 310 | end |
453 | 311 |
|
454 | 312 | describe "branch coverage" do |
|
525 | 383 |
|
526 | 384 | context "when A file that has inline branches" do |
527 | 385 | subject(:source_file) do |
528 | | - described_class.new(source_fixture("inline.rb"), COVERAGE_FOR_INLINE) |
| 386 | + described_class.new(source_fixture("inline.rb"), CoverageFixtures::INLINE_RB) |
529 | 387 | end |
530 | 388 |
|
531 | 389 | it "has branches report on 3 lines" do |
|
548 | 406 |
|
549 | 407 | context "when a file that is never relevant" do |
550 | 408 | subject(:source_file) do |
551 | | - described_class.new(source_fixture("never.rb"), COVERAGE_FOR_NEVER_RB) |
| 409 | + described_class.new(source_fixture("never.rb"), CoverageFixtures::NEVER_RB) |
552 | 410 | end |
553 | 411 |
|
554 | 412 | it "has 0.0 covered_strength" do |
|
647 | 505 |
|
648 | 506 | context "when a file with more complex skipping" do |
649 | 507 | subject(:source_file) do |
650 | | - described_class.new(source_fixture("nocov_complex.rb"), COVERAGE_FOR_NOCOV_COMPLEX_RB) |
| 508 | + described_class.new(source_fixture("nocov_complex.rb"), CoverageFixtures::NOCOV_COMPLEX_RB) |
651 | 509 | end |
652 | 510 |
|
653 | 511 | describe "line coverage" do |
|
691 | 549 |
|
692 | 550 | context "when a file with nested branches" do |
693 | 551 | subject(:source_file) do |
694 | | - described_class.new(source_fixture("nested_branches.rb"), COVERAGE_FOR_NESTED_BRANCHES_RB) |
| 552 | + described_class.new(source_fixture("nested_branches.rb"), CoverageFixtures::NESTED_BRANCHES_RB) |
695 | 553 | end |
696 | 554 |
|
697 | 555 | describe "line coverage" do |
|
715 | 573 |
|
716 | 574 | context "when a file with case" do |
717 | 575 | subject(:source_file) do |
718 | | - described_class.new(source_fixture("case.rb"), COVERAGE_FOR_CASE_STATEMENT_RB) |
| 576 | + described_class.new(source_fixture("case.rb"), CoverageFixtures::CASE_RB) |
719 | 577 | end |
720 | 578 |
|
721 | 579 | describe "line coverage" do |
|
746 | 604 |
|
747 | 605 | context "when a file with case without else" do |
748 | 606 | subject(:source_file) do |
749 | | - described_class.new(source_fixture("case_without_else.rb"), COVERAGE_FOR_CASE_WITHOUT_ELSE_STATEMENT_RB) |
| 607 | + described_class.new(source_fixture("case_without_else.rb"), CoverageFixtures::CASE_WITHOUT_ELSE_RB) |
750 | 608 | end |
751 | 609 |
|
752 | 610 | describe "line coverage" do |
|
781 | 639 |
|
782 | 640 | context "when a file with if/elsif" do |
783 | 641 | subject(:source_file) do |
784 | | - described_class.new(source_fixture("elsif.rb"), COVERAGE_FOR_ELSIF_RB) |
| 642 | + described_class.new(source_fixture("elsif.rb"), CoverageFixtures::ELSIF_RB) |
785 | 643 | end |
786 | 644 |
|
787 | 645 | describe "line coverage" do |
|
807 | 665 |
|
808 | 666 | context "when the branch tester script" do |
809 | 667 | subject(:source_file) do |
810 | | - described_class.new(source_fixture("branch_tester_script.rb"), COVERAGE_FOR_BRANCH_TESTER_RB) |
| 668 | + described_class.new(source_fixture("branch_tester_script.rb"), CoverageFixtures::BRANCH_TESTER_RB) |
811 | 669 | end |
812 | 670 |
|
813 | 671 | describe "line coverage" do |
|
831 | 689 |
|
832 | 690 | context "when a file using the deprecated # :nocov: directive" do |
833 | 691 | subject(:source_file) do |
834 | | - described_class.new(source_fixture("single_nocov.rb"), COVERAGE_FOR_SINGLE_NOCOV_RB) |
| 692 | + described_class.new(source_fixture("single_nocov.rb"), CoverageFixtures::SINGLE_NOCOV_RB) |
835 | 693 | end |
836 | 694 |
|
837 | 695 | before { described_class.nocov_warned.clear } |
|
848 | 706 |
|
849 | 707 | it "deduplicates the warning for the same file across SourceFile instances" do |
850 | 708 | capture_stderr { source_file.lines } |
851 | | - another = described_class.new(source_fixture("single_nocov.rb"), COVERAGE_FOR_SINGLE_NOCOV_RB) |
| 709 | + another = described_class.new(source_fixture("single_nocov.rb"), CoverageFixtures::SINGLE_NOCOV_RB) |
852 | 710 | stderr = capture_stderr { another.lines } |
853 | 711 |
|
854 | 712 | expect(stderr).to be_empty |
|
857 | 715 |
|
858 | 716 | context "when a file entirely ignored with a single # :nocov:" do |
859 | 717 | subject(:source_file) do |
860 | | - described_class.new(source_fixture("single_nocov.rb"), COVERAGE_FOR_SINGLE_NOCOV_RB) |
| 718 | + described_class.new(source_fixture("single_nocov.rb"), CoverageFixtures::SINGLE_NOCOV_RB) |
861 | 719 | end |
862 | 720 |
|
863 | 721 | describe "line coverage" do |
|
890 | 748 |
|
891 | 749 | context "when a file with an uneven usage of # :nocov:s" do |
892 | 750 | subject(:source_file) do |
893 | | - described_class.new(source_fixture("uneven_nocovs.rb"), COVERAGE_FOR_UNEVEN_NOCOV_RB) |
| 751 | + described_class.new(source_fixture("uneven_nocovs.rb"), CoverageFixtures::UNEVEN_NOCOVS_RB) |
894 | 752 | end |
895 | 753 |
|
896 | 754 | describe "line coverage" do |
|
0 commit comments