diff options
| author | Peter Zhu <peter@peterzhu.ca> | 2026-01-20 19:39:14 -0500 |
|---|---|---|
| committer | Peter Zhu <peter@peterzhu.ca> | 2026-01-21 17:43:44 -0500 |
| commit | 965b16d766df66e296a8d8254263da3c1cf45717 (patch) | |
| tree | 41881b9911d6c905c8801eef8c21afb162b8f139 | |
| parent | 0cc4e212c47f15f9f5384fb9871a2da8a6276ed4 (diff) | |
[DOC] Add doc about eval coverage
| -rw-r--r-- | ext/coverage/coverage.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c index 1fda8191cc..6b7e96f622 100644 --- a/ext/coverage/coverage.c +++ b/ext/coverage/coverage.c @@ -600,6 +600,62 @@ rb_coverage_running(VALUE klass) * 5. The ending line number the method appears on in the file. * 6. The ending column number the method appears on in the file. * + * == Eval \Coverage + * + * Eval coverage can be combined with the coverage types above to track + * coverage for eval. + * + * require "coverage" + * Coverage.start(eval: true, lines: true) + * + * eval(<<~RUBY, nil, "eval 1") + * ary = [] + * 10.times do |i| + * ary << "hello" * i + * end + * RUBY + * + * Coverage.result # => {"eval 1" => {lines: [1, 1, 10, nil]}} + * + * Note that the eval must have a filename assigned, otherwise coverage + * will not be measured. + * + * require "coverage" + * Coverage.start(eval: true, lines: true) + * + * eval(<<~RUBY) + * ary = [] + * 10.times do |i| + * ary << "hello" * i + * end + * RUBY + * + * Coverage.result # => {"(eval)" => {lines: [nil, nil, nil, nil]}} + * + * Also note that if a line number is assigned to the eval and it is not 1, + * then the resulting coverage will be padded with +nil+ if the line number is + * greater than 1, and truncated if the line number is less than 1. + * + * require "coverage" + * Coverage.start(eval: true, lines: true) + * + * eval(<<~RUBY, nil, "eval 1", 3) + * ary = [] + * 10.times do |i| + * ary << "hello" * i + * end + * RUBY + * + * eval(<<~RUBY, nil, "eval 2", -1) + * ary = [] + * 10.times do |i| + * ary << "hello" * i + * end + * RUBY + * + * Coverage.result + * # => {"eval 1" => {lines: [nil, nil, 1, 1, 10, nil]}, "eval 2" => {lines: [10, nil]}} + * * == All \Coverage Modes * * You can also run all modes of coverage simultaneously with this shortcut. |
