summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/coverage/coverage.c29
-rw-r--r--test/coverage/test_coverage.rb8
2 files changed, 37 insertions, 0 deletions
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
@@ -24,6 +24,32 @@ 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
* Coverage.setup(:all) => 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;