From bd2fbd35888413217337dd327ad90015c0322d12 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 29 Sep 2022 00:08:03 +1300 Subject: [PATCH] Add `Coverage.supported?` to detect what modes are supported. --- ext/coverage/coverage.c | 29 +++++++++++++++++++++++++++++ test/coverage/test_coverage.rb | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c index 6978bb24cb..c8229ae8fe 100644 --- a/ext/coverage/coverage.c +++ b/ext/coverage/coverage.c @@ -23,6 +23,32 @@ static enum { static int current_mode; static VALUE me2counter = Qnil; +/* + * call-seq: Coverage.supported?(mode) -> true or false + * + * Returns true if coverage measurement is supported for the given mode. + * + * The mode should be one of the following symbols: + * +:lines+, +:branches+, +:methods+, +:eval+. + * + * Example: + * + * Coverage.supported?(:lines) #=> true + * Coverage.supported?(:all) #=> false + */ +static VALUE +rb_coverage_supported(VALUE self, VALUE _mode) +{ + ID mode = RB_SYM2ID(_mode); + + return RBOOL( + mode == rb_intern("lines") || + mode == rb_intern("branches") || + mode == rb_intern("methods") || + mode == rb_intern("eval") + ); +} + /* * call-seq: * Coverage.setup => nil @@ -590,6 +616,9 @@ void Init_coverage(void) { VALUE rb_mCoverage = rb_define_module("Coverage"); + + rb_define_singleton_method(rb_mCoverage, "supported?", rb_coverage_supported, 1); + rb_define_module_function(rb_mCoverage, "setup", rb_coverage_setup, -1); rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, -1); rb_define_module_function(rb_mCoverage, "resume", rb_coverage_resume, 0); diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb index f9f9b2c04d..211faa0cbc 100644 --- a/test/coverage/test_coverage.rb +++ b/test/coverage/test_coverage.rb @@ -172,6 +172,14 @@ class TestCoverage < Test::Unit::TestCase end; end + def test_coverage_supported + assert Coverage.supported?(:lines) + assert Coverage.supported?(:branches) + assert Coverage.supported?(:methods) + assert Coverage.supported?(:eval) + refute Coverage.supported?(:all) + end + def test_nocoverage_optimized_line assert_ruby_status(%w[], "#{<<-"begin;"}\n#{<<-'end;'}") begin;