Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/coverage/coverage.c

Go to the documentation of this file.

    00001 /************************************************
    00002
    00003   coverage.c -
    00004
    00005   $Author: $
    00006
    00007   Copyright (c) 2008 Yusuke Endoh
    00008
    00009 ************************************************/
    00010
    00011 #include "ruby.h"
    00012
    00013 extern VALUE rb_get_coverages(void);
    00014 extern void rb_set_coverages(VALUE);
    00015 extern void rb_reset_coverages(void);
    00016
    00017 /*
    00018  * call-seq:
    00019  *    Coverage.start  => nil
    00020  *
    00021  * Enables coverage measurement.
    00022  */
    00023 static VALUE
    00024 rb_coverage_start(VALUE klass)
    00025 {
    00026     if (!RTEST(rb_get_coverages())) {
    00027         VALUE coverages = rb_hash_new();
    00028         RBASIC(coverages)->klass = 0;
    00029         rb_set_coverages(coverages);
    00030     }
    00031     return Qnil;
    00032 }
    00033
    00034 static int
    00035 coverage_result_i(st_data_t key, st_data_t val, st_data_t dummy)
    00036 {
    00037     VALUE coverage = (VALUE)val;
    00038     RBASIC(coverage)->klass = rb_cArray;
    00039     rb_ary_freeze(coverage);
    00040     return ST_CONTINUE;
    00041 }
    00042
    00043 /*
    00044  *  call-seq:
    00045  *     Coverage.result  => hash
    00046  *
    00047  * Returns a hash that contains filename as key and coverage array as value
    00048  * and disables coverage measurement.
    00049  */
    00050 static VALUE
    00051 rb_coverage_result(VALUE klass)
    00052 {
    00053     VALUE coverages = rb_get_coverages();
    00054     if (!RTEST(coverages)) {
    00055         rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
    00056     }
    00057     RBASIC(coverages)->klass = rb_cHash;
    00058     st_foreach(RHASH_TBL(coverages), coverage_result_i, 0);
    00059     rb_hash_freeze(coverages);
    00060     rb_reset_coverages();
    00061     return coverages;
    00062 }
    00063
    00064 /* Coverage provides coverage measurement feature for Ruby.
    00065  * This feature is experimental, so these APIs may be changed in future.
    00066  *
    00067  * = Usage
    00068  *
    00069  * (1) require "coverage.so"
    00070  * (2) do Coverage.start
    00071  * (3) require or load Ruby source file
    00072  * (4) Coverage.result will return a hash that contains filename as key and
    00073  *     coverage array as value.
    00074  *
    00075  * = Example
    00076  *
    00077  *   [foo.rb]
    00078  *   s = 0
    00079  *   10.times do |x|
    00080  *     s += x
    00081  *   end
    00082  *
    00083  *   if s == 45
    00084  *     p :ok
    00085  *   else
    00086  *     p :ng
    00087  *   end
    00088  *   [EOF]
    00089  *
    00090  *   require "coverage.so"
    00091  *   Coverage.start
    00092  *   require "foo.rb"
    00093  *   p Coverage.result  #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
    00094  */
    00095 void
    00096 Init_coverage(void)
    00097 {
    00098     VALUE rb_mCoverage = rb_define_module("Coverage");
    00099     rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
    00100     rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
    00101 }
    00102