Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Coverage.setup
static VALUE
rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
{
VALUE coverages, opt;
int mode;
if (current_state != IDLE) {
rb_raise(rb_eRuntimeError, "coverage measurement is already setup");
}
rb_scan_args(argc, argv, "01", &opt);
if (argc == 0) {
mode = 0; /* compatible mode */
}
else if (opt == ID2SYM(rb_intern("all"))) {
mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS | COVERAGE_TARGET_EVAL;
}
else {
mode = 0;
opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
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;
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("oneshot_lines"))))) {
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;
}
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("eval")))))
mode |= COVERAGE_TARGET_EVAL;
}
if (mode & COVERAGE_TARGET_METHODS) {
cme2counter = rb_ident_hash_new();
me_set = rb_ident_hash_new();
}
else {
cme2counter = Qnil;
me_set = Qnil;
}
method_template_reset();
coverages = rb_get_coverages();
if (!RTEST(coverages)) {
coverages = rb_hash_new();
rb_obj_hide(coverages);
current_mode = mode;
if (mode == 0) mode = COVERAGE_TARGET_LINES;
rb_set_coverages(coverages, mode, cme2counter, me_set);
current_state = SUSPENDED;
}
else if (current_mode != mode) {
rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
}
return Qnil;
}
Performs setup for coverage measurement, but does not start coverage measurement. To start coverage measurement, use Coverage.resume.
To perform both setup and start coverage measurement, Coverage.start can be used.
With argument type given and type is symbol :all, enables all types of coverage (lines, branches, methods, and eval).
Keyword arguments or hash type can be given with each of the following keys:
-
lines: Enables line coverage that records the number of times each line was executed. Iflinesis enabled,oneshot_linescannot be enabled. See Lines Coverage. -
branches: Enables branch coverage that records the number of times each branch in each conditional was executed. See Branches Coverage. -
methods: Enables method coverage that records the number of times each method was exectued. See Methods Coverage. -
eval: Enables coverage for evaluations (e.g.Kernel#eval,Module#class_eval). See Eval Coverage.