Skip to content

Commit dbeeb8f

Browse files
committed
Validate Coverage.start parameters
1 parent 1dacb04 commit dbeeb8f

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

lib/truffle/coverage.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,27 @@
1010
# GNU Lesser General Public License version 2.1.
1111

1212
module Coverage
13+
UNDEFINED = Object.new
1314

1415
def self.supported?(mode)
1516
mode == :lines
1617
end
1718

18-
def self.start(*arguments, **options)
19-
# We have to track if the :lines option was provided, as that calls for a
20-
# different result format
21-
@lines = !!options[:lines]
19+
def self.start(modes = UNDEFINED)
20+
if modes == :all || modes == UNDEFINED
21+
options = {}
22+
else
23+
options = Truffle::Type.rb_convert_type(modes, Hash, :to_hash)
24+
end
25+
26+
if options[:lines] && options[:oneshot_lines]
27+
raise 'cannot enable lines and oneshot_lines simultaneously'
28+
end
29+
30+
@lines = Primitive.as_boolean(options[:lines]) # presence of :lines affects `result`'s report format
2231
Truffle::Coverage.enable
32+
33+
nil
2334
end
2435

2536
def self.result(stop: true, clear: true)

spec/ruby/library/coverage/start_spec.rb

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,77 @@
22
require 'coverage'
33

44
describe 'Coverage.start' do
5+
before :each do
6+
Coverage.should_not.running?
7+
end
8+
9+
after :each do
10+
Coverage.result(stop: true, clear: true) if Coverage.running?
11+
end
12+
13+
it "enables the coverage measurement" do
14+
Coverage.start
15+
Coverage.should.running?
16+
end
17+
18+
it "returns nil" do
19+
Coverage.start.should == nil
20+
end
21+
522
ruby_version_is '3.2' do
6-
it "can measure coverage within eval" do
23+
it "accepts :all optional argument" do
24+
Coverage.start(:all)
25+
Coverage.should.running?
26+
end
27+
28+
it "accepts lines: optional keyword argument" do
29+
Coverage.start(lines: true)
30+
Coverage.should.running?
31+
end
32+
33+
it "accepts branches: optional keyword argument" do
34+
Coverage.start(branches: true)
35+
Coverage.should.running?
36+
end
37+
38+
it "accepts methods: optional keyword argument" do
39+
Coverage.start(methods: true)
40+
Coverage.should.running?
41+
end
42+
43+
it "accepts eval: optional keyword argument" do
44+
Coverage.start(eval: true)
45+
Coverage.should.running?
46+
end
47+
48+
it "accepts oneshot_lines: optional keyword argument" do
49+
Coverage.start(oneshot_lines: true)
50+
Coverage.should.running?
51+
end
52+
53+
it "ignores unknown keyword arguments" do
54+
Coverage.start(foo: true)
55+
Coverage.should.running?
56+
end
57+
58+
it "expects a Hash if not passed :all" do
59+
-> {
60+
Coverage.start(42)
61+
}.should raise_error(TypeError, "no implicit conversion of Integer into Hash")
62+
end
63+
64+
it "does not accept both lines: and oneshot_lines: keyword arguments" do
65+
-> {
66+
Coverage.start(lines: true, oneshot_lines: true)
67+
}.should raise_error(RuntimeError, "cannot enable lines and oneshot_lines simultaneously")
68+
end
69+
70+
it "enables the coverage measurement if passed options with `false` value" do
71+
Coverage.start(lines: false, branches: false, methods: false, eval: false, oneshot_lines: false)
72+
Coverage.should.running?
73+
end
74+
75+
it "measures coverage within eval" do
776
Coverage.start(lines: true, eval: true)
877
eval("Object.new\n"*3, binding, "test.rb", 1)
978
Coverage.result["test.rb"].should == {lines: [1, 1, 1]}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fails:Coverage.start can measure coverage within eval
1+
fails:Coverage.start measures coverage within eval

0 commit comments

Comments
 (0)