2008-07-03 18:27:43 +04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
coverage.c -
|
|
|
|
|
|
|
|
$Author: $
|
|
|
|
|
|
|
|
Copyright (c) 2008 Yusuke Endoh
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
2023-02-08 14:56:53 +03:00
|
|
|
#include "internal/gc.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/hash.h"
|
|
|
|
#include "internal/thread.h"
|
2020-09-26 00:55:12 +03:00
|
|
|
#include "internal/sanitizers.h"
|
2008-07-03 16:55:12 +04:00
|
|
|
#include "ruby.h"
|
2010-08-14 14:12:13 +04:00
|
|
|
#include "vm_core.h"
|
2008-07-08 19:13:22 +04:00
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
static enum {
|
|
|
|
IDLE,
|
|
|
|
SUSPENDED,
|
|
|
|
RUNNING
|
|
|
|
} current_state = IDLE;
|
2017-09-14 05:36:26 +03:00
|
|
|
static int current_mode;
|
2017-12-05 10:16:42 +03:00
|
|
|
static VALUE me2counter = Qnil;
|
2017-09-14 05:36:26 +03:00
|
|
|
|
2022-09-28 14:08:03 +03:00
|
|
|
/*
|
|
|
|
* 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:
|
2022-12-29 11:23:25 +03:00
|
|
|
* +:lines+, +:oneshot_lines+, +:branches+, +:methods+, +:eval+.
|
2022-09-28 14:08:03 +03:00
|
|
|
*
|
|
|
|
* 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") ||
|
2022-12-29 11:23:25 +03:00
|
|
|
mode == rb_intern("oneshot_lines") ||
|
2022-09-28 14:08:03 +03:00
|
|
|
mode == rb_intern("branches") ||
|
|
|
|
mode == rb_intern("methods") ||
|
|
|
|
mode == rb_intern("eval")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-07-08 19:13:22 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2022-09-28 13:35:42 +03:00
|
|
|
* Coverage.setup => nil
|
|
|
|
* Coverage.setup(:all) => nil
|
|
|
|
* Coverage.setup(lines: bool, branches: bool, methods: bool, eval: bool) => nil
|
|
|
|
* Coverage.setup(oneshot_lines: true) => nil
|
2008-07-08 19:13:22 +04:00
|
|
|
*
|
2021-10-25 14:00:51 +03:00
|
|
|
* Set up the coverage measurement.
|
|
|
|
*
|
|
|
|
* Note that this method does not start the measurement itself.
|
|
|
|
* Use Coverage.resume to start the measurement.
|
|
|
|
*
|
|
|
|
* You may want to use Coverage.start to setup and then start the measurement.
|
2008-07-08 19:13:22 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2021-10-25 14:00:51 +03:00
|
|
|
rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
|
2008-07-08 19:13:22 +04:00
|
|
|
{
|
2017-09-14 05:36:26 +03:00
|
|
|
VALUE coverages, opt;
|
2017-12-12 03:20:58 +03:00
|
|
|
int mode;
|
2017-09-14 05:36:26 +03:00
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
if (current_state != IDLE) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is already setup");
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:36:26 +03:00
|
|
|
rb_scan_args(argc, argv, "01", &opt);
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
mode = 0; /* compatible mode */
|
|
|
|
}
|
|
|
|
else if (opt == ID2SYM(rb_intern("all"))) {
|
2022-09-28 13:35:42 +03:00
|
|
|
mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS | COVERAGE_TARGET_EVAL;
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = 0;
|
2017-09-15 03:40:38 +03:00
|
|
|
opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2017-09-14 05:36:26 +03:00
|
|
|
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("lines")))))
|
|
|
|
mode |= COVERAGE_TARGET_LINES;
|
|
|
|
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("branches")))))
|
|
|
|
mode |= COVERAGE_TARGET_BRANCHES;
|
|
|
|
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("methods")))))
|
|
|
|
mode |= COVERAGE_TARGET_METHODS;
|
2018-10-20 08:33:13 +03:00
|
|
|
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("oneshot_lines"))))) {
|
2018-10-20 08:33:04 +03:00
|
|
|
if (mode & COVERAGE_TARGET_LINES)
|
|
|
|
rb_raise(rb_eRuntimeError, "cannot enable lines and oneshot_lines simultaneously");
|
|
|
|
mode |= COVERAGE_TARGET_LINES;
|
|
|
|
mode |= COVERAGE_TARGET_ONESHOT_LINES;
|
|
|
|
}
|
2022-09-28 13:35:42 +03:00
|
|
|
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("eval")))))
|
|
|
|
mode |= COVERAGE_TARGET_EVAL;
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (mode & COVERAGE_TARGET_METHODS) {
|
2019-07-02 20:13:25 +03:00
|
|
|
me2counter = rb_ident_hash_new();
|
2017-12-05 10:16:42 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
me2counter = Qnil;
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:36:26 +03:00
|
|
|
coverages = rb_get_coverages();
|
2016-04-06 17:29:17 +03:00
|
|
|
if (!RTEST(coverages)) {
|
|
|
|
coverages = rb_hash_new();
|
|
|
|
rb_obj_hide(coverages);
|
2017-09-14 05:36:26 +03:00
|
|
|
current_mode = mode;
|
|
|
|
if (mode == 0) mode = COVERAGE_TARGET_LINES;
|
2017-12-05 10:16:42 +03:00
|
|
|
rb_set_coverages(coverages, mode, me2counter);
|
2021-10-25 14:00:51 +03:00
|
|
|
current_state = SUSPENDED;
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
else if (current_mode != mode) {
|
|
|
|
rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
2021-10-25 14:00:51 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.resume => nil
|
|
|
|
*
|
|
|
|
* Start/resume the coverage measurement.
|
|
|
|
*
|
|
|
|
* Caveat: Currently, only process-global coverage measurement is supported.
|
2022-11-21 08:07:18 +03:00
|
|
|
* You cannot measure per-thread coverage. If your process has multiple thread,
|
2021-10-25 14:00:51 +03:00
|
|
|
* using Coverage.resume/suspend to capture code coverage executed from only
|
|
|
|
* a limited code block, may yield misleading results.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_coverage_resume(VALUE klass)
|
|
|
|
{
|
|
|
|
if (current_state == IDLE) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not set up yet");
|
|
|
|
}
|
|
|
|
if (current_state == RUNNING) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is already running");
|
|
|
|
}
|
|
|
|
rb_resume_coverages();
|
|
|
|
current_state = RUNNING;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2022-09-28 13:35:42 +03:00
|
|
|
* Coverage.start => nil
|
|
|
|
* Coverage.start(:all) => nil
|
|
|
|
* Coverage.start(lines: bool, branches: bool, methods: bool, eval: bool) => nil
|
|
|
|
* Coverage.start(oneshot_lines: true) => nil
|
2021-10-25 14:00:51 +03:00
|
|
|
*
|
|
|
|
* Enables the coverage measurement.
|
|
|
|
* See the documentation of Coverage class in detail.
|
|
|
|
* This is equivalent to Coverage.setup and Coverage.resume.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_start(int argc, VALUE *argv, VALUE klass)
|
|
|
|
{
|
|
|
|
rb_coverage_setup(argc, argv, klass);
|
|
|
|
rb_coverage_resume(klass);
|
2008-07-10 09:09:08 +04:00
|
|
|
return Qnil;
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
|
|
|
|
2020-06-19 08:52:35 +03:00
|
|
|
struct branch_coverage_result_builder
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
VALUE result;
|
|
|
|
VALUE children;
|
|
|
|
VALUE counters;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
branch_coverage_ii(VALUE _key, VALUE branch, VALUE v)
|
|
|
|
{
|
|
|
|
struct branch_coverage_result_builder *b = (struct branch_coverage_result_builder *) v;
|
|
|
|
|
|
|
|
VALUE target_label = RARRAY_AREF(branch, 0);
|
|
|
|
VALUE target_first_lineno = RARRAY_AREF(branch, 1);
|
|
|
|
VALUE target_first_column = RARRAY_AREF(branch, 2);
|
|
|
|
VALUE target_last_lineno = RARRAY_AREF(branch, 3);
|
|
|
|
VALUE target_last_column = RARRAY_AREF(branch, 4);
|
|
|
|
long counter_idx = FIX2LONG(RARRAY_AREF(branch, 5));
|
|
|
|
rb_hash_aset(b->children, rb_ary_new_from_args(6, target_label, LONG2FIX(b->id++), target_first_lineno, target_first_column, target_last_lineno, target_last_column), RARRAY_AREF(b->counters, counter_idx));
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
branch_coverage_i(VALUE _key, VALUE branch_base, VALUE v)
|
|
|
|
{
|
|
|
|
struct branch_coverage_result_builder *b = (struct branch_coverage_result_builder *) v;
|
|
|
|
|
|
|
|
VALUE base_type = RARRAY_AREF(branch_base, 0);
|
|
|
|
VALUE base_first_lineno = RARRAY_AREF(branch_base, 1);
|
|
|
|
VALUE base_first_column = RARRAY_AREF(branch_base, 2);
|
|
|
|
VALUE base_last_lineno = RARRAY_AREF(branch_base, 3);
|
|
|
|
VALUE base_last_column = RARRAY_AREF(branch_base, 4);
|
|
|
|
VALUE branches = RARRAY_AREF(branch_base, 5);
|
|
|
|
VALUE children = rb_hash_new();
|
|
|
|
rb_hash_aset(b->result, rb_ary_new_from_args(6, base_type, LONG2FIX(b->id++), base_first_lineno, base_first_column, base_last_lineno, base_last_column), children);
|
|
|
|
b->children = children;
|
|
|
|
rb_hash_foreach(branches, branch_coverage_ii, v);
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-09-14 06:25:36 +03:00
|
|
|
static VALUE
|
|
|
|
branch_coverage(VALUE branches)
|
|
|
|
{
|
2020-06-19 08:52:35 +03:00
|
|
|
VALUE structure = RARRAY_AREF(branches, 0);
|
|
|
|
|
|
|
|
struct branch_coverage_result_builder b;
|
|
|
|
b.id = 0;
|
|
|
|
b.result = rb_hash_new();
|
|
|
|
b.counters = RARRAY_AREF(branches, 1);
|
|
|
|
|
|
|
|
rb_hash_foreach(structure, branch_coverage_i, (VALUE)&b);
|
2017-09-14 06:25:36 +03:00
|
|
|
|
2020-06-19 08:52:35 +03:00
|
|
|
return b.result;
|
2017-09-14 06:25:36 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
static int
|
|
|
|
method_coverage_i(void *vstart, void *vend, size_t stride, void *data)
|
2017-09-14 08:12:34 +03:00
|
|
|
{
|
2017-12-05 10:16:42 +03:00
|
|
|
/*
|
|
|
|
* ObjectSpace.each_object(Module){|mod|
|
|
|
|
* mod.instance_methods.each{|mid|
|
|
|
|
* m = mod.instance_method(mid)
|
|
|
|
* if loc = m.source_location
|
|
|
|
* p [m.name, loc, $g_method_cov_counts[m]]
|
|
|
|
* end
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
VALUE ncoverages = *(VALUE*)data, v;
|
2017-09-14 08:12:34 +03:00
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
|
2020-09-26 00:55:12 +03:00
|
|
|
void *poisoned = asan_poisoned_object_p(v);
|
|
|
|
asan_unpoison_object(v, false);
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (RB_TYPE_P(v, T_IMEMO) && imemo_type(v) == imemo_ment) {
|
2019-10-03 06:26:41 +03:00
|
|
|
const rb_method_entry_t *me = (rb_method_entry_t *) v;
|
2017-12-05 11:56:51 +03:00
|
|
|
VALUE path, first_lineno, first_column, last_lineno, last_column;
|
2017-12-05 11:56:50 +03:00
|
|
|
VALUE data[5], ncoverage, methods;
|
2017-12-05 10:16:42 +03:00
|
|
|
VALUE methods_id = ID2SYM(rb_intern("methods"));
|
|
|
|
VALUE klass;
|
|
|
|
const rb_method_entry_t *me2 = rb_resolve_me_location(me, data);
|
|
|
|
if (me != me2) continue;
|
|
|
|
klass = me->owner;
|
|
|
|
if (RB_TYPE_P(klass, T_ICLASS)) {
|
|
|
|
rb_bug("T_ICLASS");
|
|
|
|
}
|
|
|
|
path = data[0];
|
|
|
|
first_lineno = data[1];
|
2017-12-05 11:56:51 +03:00
|
|
|
first_column = data[2];
|
|
|
|
last_lineno = data[3];
|
|
|
|
last_column = data[4];
|
2017-12-05 10:16:42 +03:00
|
|
|
if (FIX2LONG(first_lineno) <= 0) continue;
|
|
|
|
ncoverage = rb_hash_aref(ncoverages, path);
|
|
|
|
if (NIL_P(ncoverage)) continue;
|
|
|
|
methods = rb_hash_aref(ncoverage, methods_id);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
{
|
|
|
|
VALUE method_id = ID2SYM(me->def->original_id);
|
|
|
|
VALUE rcount = rb_hash_aref(me2counter, (VALUE) me);
|
2017-12-05 11:56:51 +03:00
|
|
|
VALUE key = rb_ary_new_from_args(6, klass, method_id, first_lineno, first_column, last_lineno, last_column);
|
2017-12-05 10:16:42 +03:00
|
|
|
VALUE rcount2 = rb_hash_aref(methods, key);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (NIL_P(rcount)) rcount = LONG2FIX(0);
|
|
|
|
if (NIL_P(rcount2)) rcount2 = LONG2FIX(0);
|
|
|
|
if (!POSFIXABLE(FIX2LONG(rcount) + FIX2LONG(rcount2))) {
|
|
|
|
rcount = LONG2FIX(FIXNUM_MAX);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rcount = LONG2FIX(FIX2LONG(rcount) + FIX2LONG(rcount2));
|
|
|
|
}
|
|
|
|
rb_hash_aset(methods, key, rcount);
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 00:55:12 +03:00
|
|
|
|
|
|
|
if (poisoned) {
|
|
|
|
asan_poison_object(v);
|
|
|
|
}
|
2017-12-05 10:16:42 +03:00
|
|
|
}
|
|
|
|
return 0;
|
2017-09-14 08:12:34 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 18:53:47 +03:00
|
|
|
static int
|
|
|
|
coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
|
2008-07-08 19:13:22 +04:00
|
|
|
{
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE path = (VALUE)key;
|
2008-07-08 19:13:22 +04:00
|
|
|
VALUE coverage = (VALUE)val;
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE coverages = (VALUE)h;
|
2017-09-14 05:36:26 +03:00
|
|
|
if (current_mode == 0) {
|
|
|
|
/* compatible mode */
|
|
|
|
VALUE lines = rb_ary_dup(RARRAY_AREF(coverage, COVERAGE_INDEX_LINES));
|
2017-09-03 17:26:06 +03:00
|
|
|
rb_ary_freeze(lines);
|
2017-09-14 05:36:26 +03:00
|
|
|
coverage = lines;
|
2017-09-03 17:26:06 +03:00
|
|
|
}
|
2017-09-14 05:36:26 +03:00
|
|
|
else {
|
|
|
|
VALUE h = rb_hash_new();
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (current_mode & COVERAGE_TARGET_LINES) {
|
|
|
|
VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
|
2018-10-20 08:33:04 +03:00
|
|
|
const char *kw = (current_mode & COVERAGE_TARGET_ONESHOT_LINES) ? "oneshot_lines" : "lines";
|
2017-09-14 05:36:26 +03:00
|
|
|
lines = rb_ary_dup(lines);
|
|
|
|
rb_ary_freeze(lines);
|
2018-10-20 08:33:13 +03:00
|
|
|
rb_hash_aset(h, ID2SYM(rb_intern(kw)), lines);
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (current_mode & COVERAGE_TARGET_BRANCHES) {
|
|
|
|
VALUE branches = RARRAY_AREF(coverage, COVERAGE_INDEX_BRANCHES);
|
2017-09-14 06:57:22 +03:00
|
|
|
rb_hash_aset(h, ID2SYM(rb_intern("branches")), branch_coverage(branches));
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 10:16:42 +03:00
|
|
|
if (current_mode & COVERAGE_TARGET_METHODS) {
|
|
|
|
rb_hash_aset(h, ID2SYM(rb_intern("methods")), rb_hash_new());
|
2017-09-14 05:36:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
coverage = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_hash_aset(coverages, path, coverage);
|
2008-07-08 19:13:22 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2015-02-13 18:53:47 +03:00
|
|
|
* Coverage.peek_result => hash
|
2008-07-08 19:13:22 +04:00
|
|
|
*
|
2015-02-13 18:53:47 +03:00
|
|
|
* Returns a hash that contains filename as key and coverage array as value.
|
2018-10-20 08:33:04 +03:00
|
|
|
* This is the same as `Coverage.result(stop: false, clear: false)`.
|
2017-09-14 05:36:26 +03:00
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "file.rb" => [1, 2, nil],
|
|
|
|
* ...
|
|
|
|
* }
|
2008-07-08 19:13:22 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2015-02-13 18:53:47 +03:00
|
|
|
rb_coverage_peek_result(VALUE klass)
|
2008-07-08 19:13:22 +04:00
|
|
|
{
|
|
|
|
VALUE coverages = rb_get_coverages();
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE ncoverages = rb_hash_new();
|
2008-07-08 19:13:22 +04:00
|
|
|
if (!RTEST(coverages)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
|
|
|
|
}
|
2020-08-17 05:12:23 +03:00
|
|
|
OBJ_WB_UNPROTECT(coverages);
|
2024-02-23 01:02:36 +03:00
|
|
|
|
|
|
|
rb_hash_foreach(coverages, coverage_peek_result_i, ncoverages);
|
2017-12-05 10:16:42 +03:00
|
|
|
|
|
|
|
if (current_mode & COVERAGE_TARGET_METHODS) {
|
|
|
|
rb_objspace_each_objects(method_coverage_i, &ncoverages);
|
|
|
|
}
|
|
|
|
|
2011-07-04 19:55:06 +04:00
|
|
|
rb_hash_freeze(ncoverages);
|
2015-02-13 18:53:47 +03:00
|
|
|
return ncoverages;
|
|
|
|
}
|
|
|
|
|
2018-10-20 08:33:04 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
clear_me2counter_i(VALUE key, VALUE value, VALUE unused)
|
|
|
|
{
|
|
|
|
rb_hash_aset(me2counter, key, INT2FIX(0));
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.suspend => nil
|
|
|
|
*
|
|
|
|
* Suspend the coverage measurement.
|
2021-11-02 12:29:53 +03:00
|
|
|
* You can use Coverage.resume to restart the measurement.
|
2021-10-25 14:00:51 +03:00
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_coverage_suspend(VALUE klass)
|
|
|
|
{
|
|
|
|
if (current_state != RUNNING) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not running");
|
|
|
|
}
|
|
|
|
rb_suspend_coverages();
|
|
|
|
current_state = SUSPENDED;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:53:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2018-10-20 08:33:04 +03:00
|
|
|
* Coverage.result(stop: true, clear: true) => hash
|
2015-02-13 18:53:47 +03:00
|
|
|
*
|
2018-10-20 08:33:04 +03:00
|
|
|
* Returns a hash that contains filename as key and coverage array as value.
|
|
|
|
* If +clear+ is true, it clears the counters to zero.
|
|
|
|
* If +stop+ is true, it disables coverage measurement.
|
2015-02-13 18:53:47 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2018-10-20 08:33:04 +03:00
|
|
|
rb_coverage_result(int argc, VALUE *argv, VALUE klass)
|
2015-02-13 18:53:47 +03:00
|
|
|
{
|
2018-10-20 08:33:04 +03:00
|
|
|
VALUE ncoverages;
|
|
|
|
VALUE opt;
|
|
|
|
int stop = 1, clear = 1;
|
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
if (current_state == IDLE) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
|
|
|
|
}
|
|
|
|
|
2018-10-20 08:33:04 +03:00
|
|
|
rb_scan_args(argc, argv, "01", &opt);
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
|
|
|
|
stop = RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("stop"))));
|
|
|
|
clear = RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("clear"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
ncoverages = rb_coverage_peek_result(klass);
|
|
|
|
if (stop && !clear) {
|
|
|
|
rb_warn("stop implies clear");
|
|
|
|
clear = 1;
|
|
|
|
}
|
|
|
|
if (clear) {
|
|
|
|
rb_clear_coverages();
|
|
|
|
if (!NIL_P(me2counter)) rb_hash_foreach(me2counter, clear_me2counter_i, Qnil);
|
|
|
|
}
|
|
|
|
if (stop) {
|
2021-10-25 14:00:51 +03:00
|
|
|
if (current_state == RUNNING) {
|
|
|
|
rb_coverage_suspend(klass);
|
|
|
|
}
|
2018-10-20 08:33:04 +03:00
|
|
|
rb_reset_coverages();
|
|
|
|
me2counter = Qnil;
|
2021-10-25 14:00:51 +03:00
|
|
|
current_state = IDLE;
|
2018-10-20 08:33:04 +03:00
|
|
|
}
|
2011-07-04 19:55:06 +04:00
|
|
|
return ncoverages;
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
2008-07-03 16:55:12 +04:00
|
|
|
|
2018-10-20 08:33:04 +03:00
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.state => :idle, :suspended, :running
|
|
|
|
*
|
|
|
|
* Returns the state of the coverage measurement.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_state(VALUE klass)
|
|
|
|
{
|
|
|
|
switch (current_state) {
|
|
|
|
case IDLE: return ID2SYM(rb_intern("idle"));
|
|
|
|
case SUSPENDED: return ID2SYM(rb_intern("suspended"));
|
|
|
|
case RUNNING: return ID2SYM(rb_intern("running"));
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2017-09-01 17:58:52 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.running? => bool
|
|
|
|
*
|
|
|
|
* Returns true if coverage stats are currently being collected (after
|
|
|
|
* Coverage.start call, but before Coverage.result call)
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_running(VALUE klass)
|
|
|
|
{
|
2021-10-25 14:00:51 +03:00
|
|
|
return current_state == RUNNING ? Qtrue : Qfalse;
|
2017-09-01 17:58:52 +03:00
|
|
|
}
|
|
|
|
|
2008-07-03 18:27:43 +04:00
|
|
|
/* Coverage provides coverage measurement feature for Ruby.
|
2008-07-08 19:13:22 +04:00
|
|
|
* This feature is experimental, so these APIs may be changed in future.
|
2008-07-03 18:27:43 +04:00
|
|
|
*
|
2021-10-25 14:00:51 +03:00
|
|
|
* Caveat: Currently, only process-global coverage measurement is supported.
|
2022-11-21 08:07:18 +03:00
|
|
|
* You cannot measure per-thread coverage.
|
2021-10-25 14:00:51 +03:00
|
|
|
*
|
2008-07-03 18:27:43 +04:00
|
|
|
* = Usage
|
|
|
|
*
|
2015-04-12 07:52:23 +03:00
|
|
|
* 1. require "coverage"
|
2011-10-11 01:06:39 +04:00
|
|
|
* 2. do Coverage.start
|
|
|
|
* 3. require or load Ruby source file
|
|
|
|
* 4. Coverage.result will return a hash that contains filename as key and
|
|
|
|
* coverage array as value. A coverage array gives, for each line, the
|
|
|
|
* number of line execution by the interpreter. A +nil+ value means
|
|
|
|
* coverage is disabled for this line (lines like +else+ and +end+).
|
2008-07-03 18:27:43 +04:00
|
|
|
*
|
2020-11-20 00:38:43 +03:00
|
|
|
* = Examples
|
2008-07-03 18:27:43 +04:00
|
|
|
*
|
|
|
|
* [foo.rb]
|
|
|
|
* s = 0
|
|
|
|
* 10.times do |x|
|
|
|
|
* s += x
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* if s == 45
|
|
|
|
* p :ok
|
|
|
|
* else
|
|
|
|
* p :ng
|
|
|
|
* end
|
|
|
|
* [EOF]
|
|
|
|
*
|
2015-04-12 07:52:23 +03:00
|
|
|
* require "coverage"
|
2008-07-08 19:13:22 +04:00
|
|
|
* Coverage.start
|
2008-07-03 18:27:43 +04:00
|
|
|
* require "foo.rb"
|
2008-07-08 19:13:22 +04:00
|
|
|
* p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
|
2020-11-20 00:38:43 +03:00
|
|
|
*
|
|
|
|
* == Lines Coverage
|
|
|
|
*
|
|
|
|
* If a coverage mode is not explicitly specified when starting coverage, lines
|
|
|
|
* coverage is what will run. It reports the number of line executions for each
|
|
|
|
* line.
|
|
|
|
*
|
|
|
|
* require "coverage"
|
|
|
|
* Coverage.start(lines: true)
|
|
|
|
* require "foo.rb"
|
|
|
|
* p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}}
|
|
|
|
*
|
|
|
|
* The value of the lines coverage result is an array containing how many times
|
|
|
|
* each line was executed. Order in this array is important. For example, the
|
|
|
|
* first item in this array, at index 0, reports how many times line 1 of this
|
|
|
|
* file was executed while coverage was run (which, in this example, is one
|
|
|
|
* time).
|
|
|
|
*
|
|
|
|
* A +nil+ value means coverage is disabled for this line (lines like +else+
|
|
|
|
* and +end+).
|
|
|
|
*
|
|
|
|
* == Oneshot Lines Coverage
|
|
|
|
*
|
|
|
|
* Oneshot lines coverage tracks and reports on the executed lines while
|
|
|
|
* coverage is running. It will not report how many times a line was executed,
|
|
|
|
* only that it was executed.
|
|
|
|
*
|
|
|
|
* require "coverage"
|
|
|
|
* Coverage.start(oneshot_lines: true)
|
|
|
|
* require "foo.rb"
|
|
|
|
* p Coverage.result #=> {"foo.rb"=>{:oneshot_lines=>[1, 2, 3, 6, 7]}}
|
|
|
|
*
|
|
|
|
* The value of the oneshot lines coverage result is an array containing the
|
|
|
|
* line numbers that were executed.
|
|
|
|
*
|
|
|
|
* == Branches Coverage
|
|
|
|
*
|
|
|
|
* Branches coverage reports how many times each branch within each conditional
|
|
|
|
* was executed.
|
|
|
|
*
|
|
|
|
* require "coverage"
|
|
|
|
* Coverage.start(branches: true)
|
|
|
|
* require "foo.rb"
|
|
|
|
* p Coverage.result #=> {"foo.rb"=>{:branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}}}
|
|
|
|
*
|
|
|
|
* Each entry within the branches hash is a conditional, the value of which is
|
|
|
|
* another hash where each entry is a branch in that conditional. The values
|
|
|
|
* are the number of times the method was executed, and the keys are identifying
|
|
|
|
* information about the branch.
|
|
|
|
*
|
|
|
|
* The information that makes up each key identifying branches or conditionals
|
|
|
|
* is the following, from left to right:
|
|
|
|
*
|
|
|
|
* 1. A label for the type of branch or conditional.
|
|
|
|
* 2. A unique identifier.
|
|
|
|
* 3. The starting line number it appears on in the file.
|
|
|
|
* 4. The starting column number it appears on in the file.
|
|
|
|
* 5. The ending line number it appears on in the file.
|
|
|
|
* 6. The ending column number it appears on in the file.
|
|
|
|
*
|
|
|
|
* == Methods Coverage
|
|
|
|
*
|
|
|
|
* Methods coverage reports how many times each method was executed.
|
|
|
|
*
|
|
|
|
* [foo_method.rb]
|
|
|
|
* class Greeter
|
|
|
|
* def greet
|
|
|
|
* "welcome!"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def hello
|
|
|
|
* "Hi"
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* hello()
|
|
|
|
* Greeter.new.greet()
|
|
|
|
* [EOF]
|
|
|
|
*
|
|
|
|
* require "coverage"
|
|
|
|
* Coverage.start(methods: true)
|
|
|
|
* require "foo_method.rb"
|
|
|
|
* p Coverage.result #=> {"foo_method.rb"=>{:methods=>{[Object, :hello, 7, 0, 9, 3]=>1, [Greeter, :greet, 2, 2, 4, 5]=>1}}}
|
|
|
|
*
|
|
|
|
* Each entry within the methods hash represents a method. The values in this
|
|
|
|
* hash are the number of times the method was executed, and the keys are
|
|
|
|
* identifying information about the method.
|
|
|
|
*
|
|
|
|
* The information that makes up each key identifying a method is the following,
|
|
|
|
* from left to right:
|
|
|
|
*
|
|
|
|
* 1. The class.
|
|
|
|
* 2. The method name.
|
|
|
|
* 3. The starting line number the method appears on in the file.
|
|
|
|
* 4. The starting column number the method appears on in the file.
|
|
|
|
* 5. The ending line number the method appears on in the file.
|
|
|
|
* 6. The ending column number the method appears on in the file.
|
|
|
|
*
|
|
|
|
* == All Coverage Modes
|
|
|
|
*
|
|
|
|
* You can also run all modes of coverage simultaneously with this shortcut.
|
|
|
|
* Note that running all coverage modes does not run both lines and oneshot
|
|
|
|
* lines. Those modes cannot be run simultaneously. Lines coverage is run in
|
|
|
|
* this case, because you can still use it to determine whether or not a line
|
|
|
|
* was executed.
|
|
|
|
*
|
|
|
|
* require "coverage"
|
|
|
|
* Coverage.start(:all)
|
|
|
|
* require "foo.rb"
|
|
|
|
* p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil], :branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}, :methods=>{}}}
|
2008-07-03 18:27:43 +04:00
|
|
|
*/
|
2008-07-03 16:55:12 +04:00
|
|
|
void
|
|
|
|
Init_coverage(void)
|
|
|
|
{
|
2008-08-31 08:48:02 +04:00
|
|
|
VALUE rb_mCoverage = rb_define_module("Coverage");
|
2022-09-28 14:08:03 +03:00
|
|
|
|
|
|
|
rb_define_singleton_method(rb_mCoverage, "supported?", rb_coverage_supported, 1);
|
|
|
|
|
2021-10-25 14:00:51 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "setup", rb_coverage_setup, -1);
|
2017-09-14 05:36:26 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, -1);
|
2021-10-25 14:00:51 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "resume", rb_coverage_resume, 0);
|
|
|
|
rb_define_module_function(rb_mCoverage, "suspend", rb_coverage_suspend, 0);
|
2018-10-20 08:33:04 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, -1);
|
2015-02-13 18:53:47 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
|
2021-10-25 14:00:51 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "state", rb_coverage_state, 0);
|
2017-09-01 17:58:52 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "running?", rb_coverage_running, 0);
|
2017-12-05 10:16:42 +03:00
|
|
|
rb_global_variable(&me2counter);
|
2008-07-03 16:55:12 +04:00
|
|
|
}
|