summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-08 15:13:22 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-08 15:13:22 +0000
commit91c07a5ac67bc7dad114a84697f3abc32ed78318 (patch)
tree3a4428340bf02ec6500a8c79866b744ad0a31f9c /ext
parent485754369101514e6230f4cd0a68922016f29b0e (diff)
downloadruby-91c07a5ac67bc7dad114a84697f3abc32ed78318.tar.gz
* thread.c (rb_set_coverages, rb_reset_coverages): enable and disable
coverage measurement. * thread.c (rb_get_coverages): rename and move from vm.c. * vm.c (rb_vm_get_coverages): ditto. * iseq.c (prepare_iseq_build): ditto. * thread.c (clear_coverage): ditto. * parse.y (coverage): ditto. * ext/coverage/coverage.c: use above functions, add new method Coverage.start and fix rdoc . git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/coverage/coverage.c65
1 files changed, 60 insertions, 5 deletions
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c
index b40b28bc21..3c6746f7aa 100644
--- a/ext/coverage/coverage.c
+++ b/ext/coverage/coverage.c
@@ -10,15 +10,67 @@
#include "ruby.h"
-extern void rb_enable_coverages(void);
+static VALUE rb_mCoverage;
+
+extern VALUE rb_get_coverages(void);
+extern void rb_set_coverages(VALUE);
+extern void rb_reset_coverages(void);
+
+/*
+ * call-seq:
+ * Coverage.start => nil
+ *
+ * Enables coverage measurement.
+ */
+static VALUE
+rb_coverage_start(VALUE klass)
+{
+ if (!RTEST(rb_get_coverages())) {
+ VALUE coverages = rb_hash_new();
+ RBASIC(coverages)->klass = 0;
+ rb_set_coverages(coverages);
+ }
+}
+
+static int
+coverage_result_i(st_data_t key, st_data_t val, st_data_t dummy)
+{
+ VALUE coverage = (VALUE)val;
+ RBASIC(coverage)->klass = rb_cArray;
+ rb_ary_freeze(coverage);
+ 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();
+ if (!RTEST(coverages)) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
+ }
+ RBASIC(coverages)->klass = rb_cHash;
+ st_foreach(RHASH_TBL(coverages), coverage_result_i, 0);
+ rb_hash_freeze(coverages);
+ rb_reset_coverages();
+ return coverages;
+}
/* Coverage provides coverage measurement feature for Ruby.
+ * This feature is experimental, so these APIs may be changed in future.
*
* = Usage
*
* (1) require "coverage.so"
- * (2) require or load Ruby source file
- * (3) Coverage.result will return a hash that contains filename as key and
+ * (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.
*
* = Example
@@ -37,11 +89,14 @@ extern void rb_enable_coverages(void);
* [EOF]
*
* require "coverage.so"
+ * Coverage.start
* require "foo.rb"
- * p COVERAGE__ #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
+ * p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
*/
void
Init_coverage(void)
{
- rb_enable_coverages();
+ rb_mCoverage = rb_define_module("Coverage");
+ rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
+ rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
}