Project

General

Profile

Feature #10816 ยป snapshot.diff

tenderlovemaking (Aaron Patterson), 02/01/2015 11:57 PM

View differences:

ext/coverage/coverage.c
33 33
}
34 34

  
35 35
static int
36
coverage_result_i(st_data_t key, st_data_t val, st_data_t h)
36
coverage_clear_result_i(st_data_t key, st_data_t val, st_data_t h)
37
{
38
    VALUE coverage = (VALUE)val;
39
    rb_ary_clear((VALUE)val);
40
    return ST_CONTINUE;
41
}
42

  
43
static int
44
coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
37 45
{
38 46
    VALUE path = (VALUE)key;
39 47
    VALUE coverage = (VALUE)val;
40 48
    VALUE coverages = (VALUE)h;
41 49
    coverage = rb_ary_dup(coverage);
42
    rb_ary_clear((VALUE)val);
43 50
    rb_ary_freeze(coverage);
44 51
    rb_hash_aset(coverages, path, coverage);
45 52
    return ST_CONTINUE;
......
47 54

  
48 55
/*
49 56
 *  call-seq:
50
 *     Coverage.result  => hash
57
 *     Coverage.peek_result  => hash
51 58
 *
52
 * Returns a hash that contains filename as key and coverage array as value
53
 * and disables coverage measurement.
59
 * Returns a hash that contains filename as key and coverage array as value.
54 60
 */
55 61
static VALUE
56
rb_coverage_result(VALUE klass)
62
rb_coverage_peek_result(VALUE klass)
57 63
{
58 64
    VALUE coverages = rb_get_coverages();
59 65
    VALUE ncoverages = rb_hash_new();
60 66
    if (!RTEST(coverages)) {
61 67
	rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
62 68
    }
63
    st_foreach(RHASH_TBL(coverages), coverage_result_i, ncoverages);
69
    st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
64 70
    rb_hash_freeze(ncoverages);
71
    return ncoverages;
72
}
73

  
74
/*
75
 *  call-seq:
76
 *     Coverage.result  => hash
77
 *
78
 * Returns a hash that contains filename as key and coverage array as value
79
 * and disables coverage measurement.
80
 */
81
static VALUE
82
rb_coverage_result(VALUE klass)
83
{
84
    VALUE ncoverages = rb_coverage_peek_result(klass);
85
    VALUE coverages = rb_get_coverages();
86
    st_foreach(RHASH_TBL(coverages), coverage_clear_result_i, ncoverages);
65 87
    rb_reset_coverages();
66 88
    return ncoverages;
67 89
}
......
105 127
    VALUE rb_mCoverage = rb_define_module("Coverage");
106 128
    rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
107 129
    rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
130
    rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
108 131
    rb_gc_register_address(&rb_coverages);
109 132
}
test/coverage/test_coverage.rb
16 16
    end
17 17
  end
18 18

  
19
  def test_coverage_snapshot
20
    loaded_features = $".dup
21

  
22
    Dir.mktmpdir {|tmp|
23
      Dir.chdir(tmp) {
24
        File.open("test.rb", "w") do |f|
25
          f.puts <<-EOS
26
            def coverage_test_method
27
              :ok
28
            end
29
          EOS
30
        end
31

  
32
        Coverage.start
33
        require tmp + '/test.rb'
34
        cov = Coverage.peek_result[tmp + '/test.rb']
35
        coverage_test_method
36
        cov2 = Coverage.peek_result[tmp + '/test.rb']
37
        assert_equal cov[1] + 1, cov2[1]
38
        assert_equal cov2, Coverage.result[tmp + '/test.rb']
39
      }
40
    }
41
  ensure
42
    $".replace loaded_features
43
  end
44

  
19 45
  def test_restarting_coverage
20 46
    loaded_features = $".dup
21 47