Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.jruby.ext.coverage;
 
 import java.util.Map;
 import org.jruby.Ruby;
Implementation of Ruby 1.9.2's "Coverage" module
public class CoverageModule {
    @JRubyMethod(module = true)
    public static IRubyObject start(ThreadContext contextIRubyObject self) {
        Ruby runtime = context.runtime;
        
        if (!runtime.getCoverageData().isCoverageEnabled()) {
            runtime.getCoverageData().setCoverageEnabled(runtimetrue);
        }
        
        return context.nil;
    }
    @JRubyMethod(module = true)
    public static IRubyObject result(ThreadContext contextIRubyObject self) {
        Ruby runtime = context.runtime;
        
        if (!runtime.getCoverageData().isCoverageEnabled()) {
            throw runtime.newRuntimeError("coverage measurement is not enabled");
        }
        
        Map<StringInteger[]> coverage = runtime.getCoverageData().resetCoverage(runtime);
        
        // populate a Ruby Hash with coverage data
        RubyHash covHash = RubyHash.newHash(runtime);
        for (Map.Entry<StringInteger[]> entry : coverage.entrySet()) {
            RubyArray ary = RubyArray.newArray(runtimeentry.getValue().length);
            for (int i = 0; i < entry.getValue().lengthi++) {
                Integer integer = entry.getValue()[i];
                ary.store(iinteger == null ? runtime.getNil() : runtime.newFixnum(integer));
                covHash.fastASetCheckString(runtime, RubyString.newString(runtimeentry.getKey()), ary);
            }
        }
        
        return covHash;
    }
    
New to GrepCode? Check out our FAQ X