2008-07-03 10:27:43 -04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
coverage.c -
|
|
|
|
|
|
|
|
$Author: $
|
|
|
|
|
|
|
|
Copyright (c) 2008 Yusuke Endoh
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
2008-07-03 08:55:12 -04:00
|
|
|
#include "ruby.h"
|
2010-08-14 06:12:13 -04:00
|
|
|
#include "vm_core.h"
|
2008-07-08 11:13:22 -04:00
|
|
|
|
2011-07-04 11:55:06 -04:00
|
|
|
static VALUE rb_coverages = Qundef;
|
|
|
|
|
2008-07-08 11:13:22 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.start => nil
|
|
|
|
*
|
|
|
|
* Enables coverage measurement.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_start(VALUE klass)
|
|
|
|
{
|
|
|
|
if (!RTEST(rb_get_coverages())) {
|
2011-07-04 11:55:06 -04:00
|
|
|
if (rb_coverages == Qundef) {
|
|
|
|
rb_coverages = rb_hash_new();
|
|
|
|
RBASIC(rb_coverages)->klass = 0;
|
|
|
|
}
|
|
|
|
rb_set_coverages(rb_coverages);
|
2008-07-08 11:13:22 -04:00
|
|
|
}
|
2008-07-10 01:09:08 -04:00
|
|
|
return Qnil;
|
2008-07-08 11:13:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-07-04 11:55:06 -04:00
|
|
|
coverage_result_i(st_data_t key, st_data_t val, st_data_t h)
|
2008-07-08 11:13:22 -04:00
|
|
|
{
|
2011-07-04 11:55:06 -04:00
|
|
|
VALUE path = (VALUE)key;
|
2008-07-08 11:13:22 -04:00
|
|
|
VALUE coverage = (VALUE)val;
|
2011-07-04 11:55:06 -04:00
|
|
|
VALUE coverages = (VALUE)h;
|
|
|
|
coverage = rb_ary_dup(coverage);
|
|
|
|
rb_ary_clear((VALUE)val);
|
2008-07-08 11:13:22 -04:00
|
|
|
rb_ary_freeze(coverage);
|
2011-07-04 11:55:06 -04:00
|
|
|
rb_hash_aset(coverages, path, coverage);
|
2008-07-08 11:13:22 -04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.result => hash
|
|
|
|
*
|
|
|
|
* Returns a hash that contains filename as key and coverage array as value
|
|
|
|
* and disables coverage measurement.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_result(VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE coverages = rb_get_coverages();
|
2011-07-04 11:55:06 -04:00
|
|
|
VALUE ncoverages = rb_hash_new();
|
2008-07-08 11:13:22 -04:00
|
|
|
if (!RTEST(coverages)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
|
|
|
|
}
|
2011-07-04 11:55:06 -04:00
|
|
|
st_foreach(RHASH_TBL(coverages), coverage_result_i, ncoverages);
|
|
|
|
rb_hash_freeze(ncoverages);
|
2008-07-08 11:13:22 -04:00
|
|
|
rb_reset_coverages();
|
2011-07-04 11:55:06 -04:00
|
|
|
return ncoverages;
|
2008-07-08 11:13:22 -04:00
|
|
|
}
|
2008-07-03 08:55:12 -04:00
|
|
|
|
2008-07-03 10:27:43 -04:00
|
|
|
/* Coverage provides coverage measurement feature for Ruby.
|
2008-07-08 11:13:22 -04:00
|
|
|
* This feature is experimental, so these APIs may be changed in future.
|
2008-07-03 10:27:43 -04:00
|
|
|
*
|
|
|
|
* = Usage
|
|
|
|
*
|
|
|
|
* (1) require "coverage.so"
|
2008-07-08 11:13:22 -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
|
2008-07-03 10:27:43 -04:00
|
|
|
* coverage array as value.
|
|
|
|
*
|
|
|
|
* = Example
|
|
|
|
*
|
|
|
|
* [foo.rb]
|
|
|
|
* s = 0
|
|
|
|
* 10.times do |x|
|
|
|
|
* s += x
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* if s == 45
|
|
|
|
* p :ok
|
|
|
|
* else
|
|
|
|
* p :ng
|
|
|
|
* end
|
|
|
|
* [EOF]
|
|
|
|
*
|
|
|
|
* require "coverage.so"
|
2008-07-08 11:13:22 -04:00
|
|
|
* Coverage.start
|
2008-07-03 10:27:43 -04:00
|
|
|
* require "foo.rb"
|
2008-07-08 11:13:22 -04:00
|
|
|
* p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
|
2008-07-03 10:27:43 -04:00
|
|
|
*/
|
2008-07-03 08:55:12 -04:00
|
|
|
void
|
|
|
|
Init_coverage(void)
|
|
|
|
{
|
2008-08-31 00:48:02 -04:00
|
|
|
VALUE rb_mCoverage = rb_define_module("Coverage");
|
2008-07-08 11:13:22 -04:00
|
|
|
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
|
|
|
|
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
|
2011-07-04 11:55:06 -04:00
|
|
|
rb_gc_register_address(&rb_coverages);
|
2008-07-03 08:55:12 -04:00
|
|
|
}
|