relevance-rcov 0.9.3-java
Sign up to get free protection for your applications and to get access to all the features.
- data/BLURB +111 -0
- data/LICENSE +53 -0
- data/Rakefile +94 -0
- data/THANKS +110 -0
- data/bin/rcov +514 -0
- data/doc/readme_for_api.markdown +22 -0
- data/doc/readme_for_emacs.markdown +52 -0
- data/doc/readme_for_rake.markdown +51 -0
- data/doc/readme_for_vim.markdown +34 -0
- data/editor-extensions/rcov.el +131 -0
- data/editor-extensions/rcov.vim +38 -0
- data/ext/java/src/CallsiteHook.java +137 -0
- data/ext/java/src/CoverageHook.java +117 -0
- data/ext/java/src/RcovHook.java +9 -0
- data/ext/java/src/RcovrtService.java +130 -0
- data/lib/rcov.rb +33 -0
- data/lib/rcov/call_site_analyzer.rb +225 -0
- data/lib/rcov/code_coverage_analyzer.rb +268 -0
- data/lib/rcov/coverage_info.rb +36 -0
- data/lib/rcov/differential_analyzer.rb +116 -0
- data/lib/rcov/file_statistics.rb +334 -0
- data/lib/rcov/formatters.rb +13 -0
- data/lib/rcov/formatters/base_formatter.rb +173 -0
- data/lib/rcov/formatters/failure_report.rb +15 -0
- data/lib/rcov/formatters/full_text_report.rb +48 -0
- data/lib/rcov/formatters/html_coverage.rb +274 -0
- data/lib/rcov/formatters/html_erb_template.rb +62 -0
- data/lib/rcov/formatters/text_coverage_diff.rb +193 -0
- data/lib/rcov/formatters/text_report.rb +32 -0
- data/lib/rcov/formatters/text_summary.rb +11 -0
- data/lib/rcov/lowlevel.rb +146 -0
- data/lib/rcov/rcovtask.rb +155 -0
- data/lib/rcov/templates/detail.html.erb +64 -0
- data/lib/rcov/templates/index.html.erb +93 -0
- data/lib/rcov/templates/jquery-1.3.2.min.js +19 -0
- data/lib/rcov/templates/jquery.tablesorter.min.js +15 -0
- data/lib/rcov/templates/print.css +12 -0
- data/lib/rcov/templates/rcov.js +42 -0
- data/lib/rcov/templates/screen.css +270 -0
- data/lib/rcov/version.rb +10 -0
- data/lib/rcovrt.jar +0 -0
- data/setup.rb +1588 -0
- data/test/assets/sample_01.rb +7 -0
- data/test/assets/sample_02.rb +5 -0
- data/test/assets/sample_03.rb +20 -0
- data/test/assets/sample_04.rb +10 -0
- data/test/assets/sample_05-new.rb +17 -0
- data/test/assets/sample_05-old.rb +13 -0
- data/test/assets/sample_05.rb +17 -0
- data/test/assets/sample_06.rb +8 -0
- data/test/call_site_analyzer_test.rb +171 -0
- data/test/code_coverage_analyzer_test.rb +219 -0
- data/test/file_statistics_test.rb +471 -0
- data/test/functional_test.rb +91 -0
- data/test/turn_off_rcovrt.rb +4 -0
- metadata +115 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
# RCov
|
2
|
+
|
3
|
+
RCov is a:
|
4
|
+
|
5
|
+
1. tool for code coverage analysis for Ruby
|
6
|
+
2. library for collecting code coverage and execution count information introspectively
|
7
|
+
|
8
|
+
If you want to use the command line tool, the output from `rcov -h` is self explanatory. If you want to automate the execution of RCov via Rake take a look at [readme for rake]("http://github.com/relevance/rcov/blob/master/doc/readme_for_rake.markdown"). If you want to use the associated library, read on.
|
9
|
+
|
10
|
+
## Usage of the RCov runtime/library
|
11
|
+
|
12
|
+
RCov is primarily a tool for code coverage analysis, but since 0.4.0 it exposes some of its code so that you can build on top of its heuristics for code analysis and its capabilities for coverage information and execution count gathering. The main classes of interest are `Rcov::FileStatistics`, `Rcov::CodeCoverageAnalyzer` and `Rcov::CallSiteAnalyzer`.
|
13
|
+
|
14
|
+
* `Rcov::FileStatistics` can use some heuristics to determine which parts of the file are executable and which are mere comments.
|
15
|
+
|
16
|
+
* `Rcov::CodeCoverageAnalyzer` is used to gather code coverage and execution count information inside a running Ruby program.
|
17
|
+
|
18
|
+
* `Rcov::CallSiteAnalyzer` is used to obtain information about where methods are defined and who calls them.
|
19
|
+
|
20
|
+
The parts of RCov's runtime meant to be reused (i.e. the external API) are documented with RDoc. Those not meant to be used are clearly marked as so or were deliberately removed from the present documentation.
|
21
|
+
|
22
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# rcov.el
|
2
|
+
|
3
|
+
`rcov.el` allows you to use rcov from Emacs conveniently.
|
4
|
+
|
5
|
+
* Run unit tests and jump to uncovered code by <code>C-x `</code>
|
6
|
+
* Run unit tests and save the current coverage status.
|
7
|
+
* Run unit tests and jump to uncovered code introduced since the last run.
|
8
|
+
* View cross-reference annotated code.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Copy <tt>rcov.el</tt> to the appropriate directory, which is in load-path then require it.
|
13
|
+
|
14
|
+
`(require 'rcov)`
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
There are some commands to run RCov in Emacs. All of them will display RCov window, whose `major-mode` is `compilation-mode`. This allow you to jump to uncovered code using C-x `. rcov-command-line, rcovsave-command-line, and rcovdiff-command-line define command line to run rcov. If you do not use RCov from Rake, you must modify them.
|
19
|
+
|
20
|
+
### Finding uncovered code
|
21
|
+
|
22
|
+
Type the following while editing your program:
|
23
|
+
|
24
|
+
`M-x rcov`
|
25
|
+
|
26
|
+
### Setting the reference point
|
27
|
+
|
28
|
+
RCov's `--text-coverage-diff` mode compares the current coverage status against the saved one. It therefore needs that information to be recorded before you write new code (typically right after you perform a commit) in order to have something to compare against. You can save the current status with the `--save` option. Type the following to save the current status in Emacs:
|
29
|
+
|
30
|
+
`M-x rcovsave`
|
31
|
+
|
32
|
+
If you do not use RCov from Rake, you must modify `rcovsave-command-line` variable.
|
33
|
+
|
34
|
+
### Finding new uncovered code
|
35
|
+
|
36
|
+
Type the following to save the current status in Emacs:
|
37
|
+
|
38
|
+
`M-x rcovdiff`
|
39
|
+
|
40
|
+
### Viewing cross-reference annotated code
|
41
|
+
|
42
|
+
If you read cross-reference annotated code, issue
|
43
|
+
|
44
|
+
`rake rcov RCOVOPTS='-a'`
|
45
|
+
|
46
|
+
at the beginning. This command creates a `coverage` directory and many `*.rb` files in it. Filenames of these Ruby scripts are converted from original path. You can browse them by normally `C-x C-f`. You can think of `-a` option as `--xrefs` option and output format is Ruby script. After `find-file-ed` annotated script, the `major-mode` is `rcov-xref-mode`,
|
47
|
+
which is derived from `ruby-mode` and specializes navigation.
|
48
|
+
|
49
|
+
* `Tab` and `M-Tab` goes forward/backward links.
|
50
|
+
* `Ret` follows selected link.
|
51
|
+
|
52
|
+
This feature is useful to read third-party code or to follow control flow.
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Code coverage analysis automation with Rake
|
2
|
+
|
3
|
+
Since 0.4.0, RCov features a `Rcov::RcovTask` task for rake
|
4
|
+
which can be used to automate test coverage analysis. Basic usage is as
|
5
|
+
follows:
|
6
|
+
<pre><code>
|
7
|
+
require 'rcov/rcovtask'
|
8
|
+
Rcov::RcovTask.new do |t|
|
9
|
+
t.test_files = FileList['test/test*.rb']
|
10
|
+
# t.verbose = true # uncomment to see the executed command
|
11
|
+
end
|
12
|
+
</pre></code>
|
13
|
+
|
14
|
+
This will create by default a task named `rcov`, and also a task to remove the output directory where the XHTML report is generated. The latter will be named `clobber_rcov`, and will be added to the main `clobber` target.
|
15
|
+
|
16
|
+
## Passing command line options to RCov
|
17
|
+
|
18
|
+
You can provide a description, change the name of the generated tasks (the one used to generate the report(s) and the `clobber_` one) and pass options to RCov:
|
19
|
+
<pre><code>
|
20
|
+
desc "Analyze code coverage of the unit tests."
|
21
|
+
Rcov::RcovTask.new(:coverage) do |t|
|
22
|
+
t.test_files = FileList['test/test*.rb']
|
23
|
+
t.verbose = true
|
24
|
+
## get a text report on stdout when rake is run:
|
25
|
+
t.rcov_opts << "--text-report"
|
26
|
+
## only report files under 80% coverage
|
27
|
+
t.rcov_opts << "--threshold 80"
|
28
|
+
end
|
29
|
+
</pre></code>
|
30
|
+
|
31
|
+
This will generate a `coverage` task and the associated `clobber_coverage` task to remove the directory the report is dumped to (`coverage` by default). You can specify a different destination directory, which comes handy if you have several `RcovTask`s; the `clobber_*` will take care of removing that directory:
|
32
|
+
<pre><code>
|
33
|
+
desc "Analyze code coverage for the FileStatistics class."
|
34
|
+
Rcov::RcovTask.new(:rcov_sourcefile) do |t|
|
35
|
+
t.test_files = FileList['test/test_FileStatistics.rb']
|
36
|
+
t.verbose = true
|
37
|
+
t.rcov_opts << "--test-unit-only"
|
38
|
+
t.output_dir = "coverage.sourcefile"
|
39
|
+
end
|
40
|
+
|
41
|
+
Rcov::RcovTask.new(:rcov_ccanalyzer) do |t|
|
42
|
+
t.test_files = FileList['test/test_CodeCoverageAnalyzer.rb']
|
43
|
+
t.verbose = true
|
44
|
+
t.rcov_opts << "--test-unit-only"
|
45
|
+
t.output_dir = "coverage.ccanalyzer"
|
46
|
+
end
|
47
|
+
</pre></code>
|
48
|
+
|
49
|
+
## Options passed through the `rake` command line
|
50
|
+
|
51
|
+
You can override the options defined in the RcovTask by passing the new options at the time you invoke rake. The documentation for the `Rcov::RcovTask` explains how this can be done.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# rcov.vim
|
2
|
+
|
3
|
+
`rcov.vim` allows you to run unit tests from vim and enter quickfix mode in order to jump to uncovered code introduced since the last run.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Copy `rcov.vim` to the appropriate `compiler` directory (typically `$HOME/.vim/compiler`).
|
7
|
+
|
8
|
+
### Usage
|
9
|
+
|
10
|
+
#### Setting the reference point
|
11
|
+
|
12
|
+
RCov's `--text-coverage-diff` mode compares the current coverage status against the saved one. It therefore needs that information to be recorded before you write new code (typically right after you perform a commit) in order to have something to compare against. You can save the current status with the `--save` option. If you're running RCov from Rake, you can do something like
|
13
|
+
|
14
|
+
`rake rcov_units RCOVOPTS="-T --save --rails"`
|
15
|
+
|
16
|
+
in order to take the current status as the reference point.
|
17
|
+
|
18
|
+
#### Finding new uncovered code
|
19
|
+
|
20
|
+
Type the following in command mode while editing your program:
|
21
|
+
|
22
|
+
`:compiler rcov`
|
23
|
+
|
24
|
+
`rcov.vim` assumes RCov can be invoked with a rake task (see [readme for rake]("http://github.com/relevance/rcov/blob/master/doc/readme_for_rake.markdown") for information on how to create it).
|
25
|
+
|
26
|
+
You can then execute +rcov+ and enter quickfix mode by typing
|
27
|
+
|
28
|
+
`:make <taskname>`
|
29
|
+
|
30
|
+
where taskname is the +rcov+ task you want to use; if you didn't override the default name in the Rakefile, just
|
31
|
+
|
32
|
+
`:make rcov`
|
33
|
+
|
34
|
+
will do. Vim will then enter quickfix mode, allowing you to jump to the areas that were not covered since the last time you saved the coverage data.
|
@@ -0,0 +1,131 @@
|
|
1
|
+
;;; rcov.el -- Ruby Coverage Analysis Tool
|
2
|
+
|
3
|
+
;;; Copyright (c) 2006 rubikitch <rubikitch@ruby-lang.org>
|
4
|
+
;;;
|
5
|
+
;;; Use and distribution subject to the terms of the rcov license.
|
6
|
+
|
7
|
+
(defvar rcov-xref-before-visit-source-hook nil
|
8
|
+
"Hook executed before jump.")
|
9
|
+
(defvar rcov-xref-after-visit-source-hook nil
|
10
|
+
"Hook executed after jump.")
|
11
|
+
(defvar rcov-command-line "rake rcov RCOVOPTS='--gcc --no-html'"
|
12
|
+
"Rcov command line to find uncovered code.
|
13
|
+
It is good to use rcov with Rake because it `cd's appropriate directory.
|
14
|
+
`--gcc' option is strongly recommended because `rcov' uses compilation-mode.")
|
15
|
+
(defvar rcovsave-command-line "rake rcov RCOVOPTS='--gcc --no-html --save=coverage.info'"
|
16
|
+
"Rcov command line to save coverage status. See also `rcov-command-line'.")
|
17
|
+
(defvar rcovdiff-command-line "rake rcov RCOVOPTS='-D --gcc --no-html'"
|
18
|
+
"Rcov command line to find new uncovered code. See also `rcov-command-line'.")
|
19
|
+
|
20
|
+
;;;; rcov-xref-mode
|
21
|
+
(define-derived-mode rcov-xref-mode ruby-mode "Rxref"
|
22
|
+
"Major mode for annotated Ruby scripts (coverage/*.rb) by rcov."
|
23
|
+
(setq truncate-lines t)
|
24
|
+
;; ruby-electric-mode / pabbrev-mode hijacks TAB binding.
|
25
|
+
(and ruby-electric-mode (ruby-electric-mode -1))
|
26
|
+
(and (boundp 'pabbrev-mode) pabbrev-mode (pabbrev-mode -1))
|
27
|
+
(suppress-keymap rcov-xref-mode-map)
|
28
|
+
(define-key rcov-xref-mode-map "\C-i" 'rcov-xref-next-tag)
|
29
|
+
(define-key rcov-xref-mode-map "\M-\C-i" 'rcov-xref-previous-tag)
|
30
|
+
(define-key rcov-xref-mode-map "\C-m" 'rcov-xref-visit-source)
|
31
|
+
(set (make-local-variable 'automatic-hscrolling) nil)
|
32
|
+
)
|
33
|
+
|
34
|
+
(defvar rcov-xref-tag-regexp "\\[\\[\\(.*?\\)\\]\\]")
|
35
|
+
|
36
|
+
(defun rcov-xref-next-tag (n)
|
37
|
+
"Go to next LINK."
|
38
|
+
(interactive "p")
|
39
|
+
(when (looking-at rcov-xref-tag-regexp)
|
40
|
+
(goto-char (match-end 0)))
|
41
|
+
(when (re-search-forward rcov-xref-tag-regexp nil t n)
|
42
|
+
(goto-char (match-beginning 0)))
|
43
|
+
(rcov-xref-show-link))
|
44
|
+
|
45
|
+
(defun rcov-xref-previous-tag (n)
|
46
|
+
"Go to previous LINK."
|
47
|
+
(interactive "p")
|
48
|
+
(re-search-backward rcov-xref-tag-regexp nil t n)
|
49
|
+
(rcov-xref-show-link))
|
50
|
+
|
51
|
+
(defvar rcov-xref-link-tempbuffer " *rcov-link*")
|
52
|
+
(defun rcov-xref-show-link ()
|
53
|
+
"Follow current LINK."
|
54
|
+
(let ((link (match-string 1))
|
55
|
+
(eol (point-at-eol)))
|
56
|
+
(save-excursion
|
57
|
+
(when (and link
|
58
|
+
(re-search-backward "# \\(>>\\|<<\\) " (point-at-bol) t))
|
59
|
+
(while (re-search-forward rcov-xref-tag-regexp eol t)
|
60
|
+
(let ((matched (match-string 1)))
|
61
|
+
(when (string= link matched)
|
62
|
+
(add-text-properties 0 (length matched) '(face highlight) matched))
|
63
|
+
(with-current-buffer (get-buffer-create rcov-xref-link-tempbuffer)
|
64
|
+
(insert matched "\n"))))
|
65
|
+
(let (message-log-max) ; inhibit *Messages*
|
66
|
+
(message "%s" (with-current-buffer rcov-xref-link-tempbuffer
|
67
|
+
(substring (buffer-string) 0 -1)))) ; chomp
|
68
|
+
(kill-buffer rcov-xref-link-tempbuffer)))))
|
69
|
+
|
70
|
+
|
71
|
+
;; copied from jw-visit-source
|
72
|
+
(defun rcov-xref-extract-file-lines (line)
|
73
|
+
"Extract a list of file/line pairs from the given line of text."
|
74
|
+
(let*
|
75
|
+
((unix_fn "[^ \t\n\r\"'([<{]+")
|
76
|
+
(dos_fn "[a-zA-Z]:[^ \t\n\r\"'([<{]+")
|
77
|
+
(flre (concat "\\(" unix_fn "\\|" dos_fn "\\):\\([0-9]+\\)"))
|
78
|
+
(start nil)
|
79
|
+
(result nil))
|
80
|
+
(while (string-match flre line start)
|
81
|
+
(setq start (match-end 0))
|
82
|
+
(setq result
|
83
|
+
(cons (list
|
84
|
+
(substring line (match-beginning 1) (match-end 1))
|
85
|
+
(string-to-int (substring line (match-beginning 2) (match-end 2))))
|
86
|
+
result)))
|
87
|
+
result))
|
88
|
+
|
89
|
+
(defun rcov-xref-select-file-line (candidates)
|
90
|
+
"Select a file/line candidate that references an existing file."
|
91
|
+
(cond ((null candidates) nil)
|
92
|
+
((file-readable-p (caar candidates)) (car candidates))
|
93
|
+
(t (rcov-xref-select-file-line (cdr candidates))) ))
|
94
|
+
|
95
|
+
(defun rcov-xref-visit-source ()
|
96
|
+
"If the current line contains text like '../src/program.rb:34', visit
|
97
|
+
that file in the other window and position point on that line."
|
98
|
+
(interactive)
|
99
|
+
(let* ((line (progn (looking-at rcov-xref-tag-regexp) (match-string 1)))
|
100
|
+
(candidates (rcov-xref-extract-file-lines line))
|
101
|
+
(file-line (rcov-xref-select-file-line candidates)))
|
102
|
+
(cond (file-line
|
103
|
+
(run-hooks 'rcov-xref-before-visit-source-hook)
|
104
|
+
(find-file (car file-line))
|
105
|
+
(goto-line (cadr file-line))
|
106
|
+
(run-hooks 'rcov-xref-after-visit-source-hook))
|
107
|
+
(t
|
108
|
+
(error "No source location on line.")) )))
|
109
|
+
|
110
|
+
;;;; Running rcov with various options.
|
111
|
+
(defun rcov-internal (cmdline)
|
112
|
+
"Run rcov with various options."
|
113
|
+
(compile-internal cmdline ""
|
114
|
+
nil nil nil (lambda (x) "*rcov*")))
|
115
|
+
|
116
|
+
(defun rcov ()
|
117
|
+
"Run rcov to find uncovered code."
|
118
|
+
(interactive)
|
119
|
+
(rcov-internal rcov-command-line))
|
120
|
+
|
121
|
+
(defun rcovsave ()
|
122
|
+
"Run rcov to save coverage status."
|
123
|
+
(interactive)
|
124
|
+
(rcov-internal rcovsave-command-line))
|
125
|
+
|
126
|
+
(defun rcovdiff ()
|
127
|
+
"Run rcov to find new uncovered code."
|
128
|
+
(interactive)
|
129
|
+
(rcov-internal rcovdiff-command-line))
|
130
|
+
|
131
|
+
(provide 'rcov)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
" Vim compiler file
|
2
|
+
" Language: Ruby
|
3
|
+
" Function: Code coverage information with rcov
|
4
|
+
" Maintainer: Mauricio Fernandez <mfp at acm dot org>
|
5
|
+
" Info:
|
6
|
+
" URL: http://eigenclass.org/hiki.rb?rcov
|
7
|
+
" ----------------------------------------------------------------------------
|
8
|
+
"
|
9
|
+
" Changelog:
|
10
|
+
" 0.1: initial version, shipped with rcov 0.6.0
|
11
|
+
"
|
12
|
+
" Comments:
|
13
|
+
" Initial attempt.
|
14
|
+
" ----------------------------------------------------------------------------
|
15
|
+
|
16
|
+
if exists("current_compiler")
|
17
|
+
finish
|
18
|
+
endif
|
19
|
+
let current_compiler = "rcov"
|
20
|
+
|
21
|
+
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
|
22
|
+
command -nargs=* CompilerSet setlocal <args>
|
23
|
+
endif
|
24
|
+
|
25
|
+
let s:cpo_save = &cpo
|
26
|
+
set cpo-=C
|
27
|
+
|
28
|
+
CompilerSet makeprg=rake\ $*\ RCOVOPTS=\"-D\ --no-html\ --no-color\"\ $*
|
29
|
+
|
30
|
+
CompilerSet errorformat=
|
31
|
+
\%+W\#\#\#\ %f:%l\,
|
32
|
+
\%-C\ \ \ ,
|
33
|
+
\%-C!!\
|
34
|
+
|
35
|
+
let &cpo = s:cpo_save
|
36
|
+
unlet s:cpo_save
|
37
|
+
|
38
|
+
" vim: nowrap sw=2 sts=2 ts=8 ff=unix :
|
@@ -0,0 +1,137 @@
|
|
1
|
+
import java.util.regex.Matcher;
|
2
|
+
import java.util.regex.Pattern;
|
3
|
+
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyArray;
|
6
|
+
import org.jruby.RubyHash;
|
7
|
+
import org.jruby.runtime.Frame;
|
8
|
+
import org.jruby.runtime.ThreadContext;
|
9
|
+
import org.jruby.runtime.RubyEvent;
|
10
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
11
|
+
|
12
|
+
public class CallsiteHook extends RcovHook {
|
13
|
+
|
14
|
+
private static CallsiteHook callsiteHook;
|
15
|
+
|
16
|
+
public static CallsiteHook getCallsiteHook() {
|
17
|
+
if (callsiteHook == null) {
|
18
|
+
callsiteHook = new CallsiteHook();
|
19
|
+
}
|
20
|
+
return callsiteHook;
|
21
|
+
}
|
22
|
+
|
23
|
+
private boolean active;
|
24
|
+
private RubyHash defsites;
|
25
|
+
private RubyHash callsites;
|
26
|
+
|
27
|
+
private CallsiteHook() {
|
28
|
+
super();
|
29
|
+
}
|
30
|
+
|
31
|
+
public boolean isActive() {
|
32
|
+
return active;
|
33
|
+
}
|
34
|
+
|
35
|
+
public boolean isInterestedInEvent(RubyEvent event) {
|
36
|
+
return event == RubyEvent.CALL || event == RubyEvent.C_CALL;
|
37
|
+
}
|
38
|
+
|
39
|
+
public RubyArray getCallsiteInfo(Ruby runtime) {
|
40
|
+
RubyArray info = runtime.newArray();
|
41
|
+
info.add(getCallsites(runtime));
|
42
|
+
info.add(getDefsites(runtime));
|
43
|
+
return info;
|
44
|
+
}
|
45
|
+
|
46
|
+
public void setActive(boolean active) {
|
47
|
+
this.active = active;
|
48
|
+
}
|
49
|
+
|
50
|
+
public RubyHash resetDefsites() {
|
51
|
+
defsites.clear();
|
52
|
+
return defsites;
|
53
|
+
}
|
54
|
+
|
55
|
+
public void eventHandler(ThreadContext context, String event, String file, int line, String name, IRubyObject type) {
|
56
|
+
RubyArray currentMethod = context.getRuntime().newArray();
|
57
|
+
currentMethod.add(context.getFrameKlazz());
|
58
|
+
currentMethod.add(context.getRuntime().newSymbol(name));
|
59
|
+
|
60
|
+
RubyArray fileLoc = context.getRuntime().newArray();
|
61
|
+
fileLoc.add(file);
|
62
|
+
fileLoc.add(Long.valueOf(line));
|
63
|
+
defsites = getDefsites(context.getRuntime());
|
64
|
+
|
65
|
+
if (!context.isWithinTrace()) {
|
66
|
+
context.setWithinTrace(true);
|
67
|
+
defsites.put(currentMethod, fileLoc);
|
68
|
+
callsites = getCallsites(context.getRuntime());
|
69
|
+
|
70
|
+
if (!callsites.containsKey(currentMethod)) {
|
71
|
+
callsites.put(currentMethod, RubyHash.newHash(context.getRuntime()));
|
72
|
+
}
|
73
|
+
|
74
|
+
RubyHash hash = (RubyHash) callsites.get(currentMethod);
|
75
|
+
RubyArray callerArray = customBacktrace(context);
|
76
|
+
|
77
|
+
if (!hash.containsKey(callerArray)) {
|
78
|
+
hash.put(callerArray, Long.valueOf(0));
|
79
|
+
}
|
80
|
+
|
81
|
+
Long count = (Long) hash.get(callerArray);
|
82
|
+
long itCount = count.longValue() + 1L;
|
83
|
+
hash.put(callerArray, Long.valueOf(itCount));
|
84
|
+
context.setWithinTrace(false);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
private RubyArray customBacktrace(ThreadContext context) {
|
89
|
+
Frame[] frames = context.createBacktrace(1, false);
|
90
|
+
RubyArray ary = context.getRuntime().newArray();
|
91
|
+
ary.addAll(formatBacktrace(context.getRuntime(), frames[frames.length - 1]));
|
92
|
+
return context.getRuntime().newArray((IRubyObject) ary);
|
93
|
+
}
|
94
|
+
|
95
|
+
/*
|
96
|
+
* TODO: The logic in this method really needs to be wrapped in a backtrace
|
97
|
+
* object or something. Then I could fix the file path issues that cause
|
98
|
+
* test failures.
|
99
|
+
* @param runtime
|
100
|
+
* @param backtrace
|
101
|
+
* @return
|
102
|
+
*/
|
103
|
+
private RubyArray formatBacktrace(Ruby runtime, Frame backtrace) {
|
104
|
+
RubyArray ary = runtime.newArray();
|
105
|
+
|
106
|
+
if (backtrace == null) {
|
107
|
+
ary.add(runtime.getNil());
|
108
|
+
ary.add(runtime.getNil());
|
109
|
+
ary.add("");
|
110
|
+
ary.add(Long.valueOf(0));
|
111
|
+
} else {
|
112
|
+
ary.add(backtrace.getKlazz());
|
113
|
+
ary.add((backtrace.getName() == null ? runtime.getNil() : runtime.newSymbol( backtrace.getName())));
|
114
|
+
ary.add(backtrace.getFile());
|
115
|
+
//Add 1 to compensate for the zero offset in the Frame elements.
|
116
|
+
ary.add(backtrace.getLine() + 1);
|
117
|
+
}
|
118
|
+
|
119
|
+
return ary;
|
120
|
+
}
|
121
|
+
|
122
|
+
private RubyHash getCallsites(Ruby runtime) {
|
123
|
+
if (this.callsites == null) {
|
124
|
+
this.callsites = RubyHash.newHash(runtime);
|
125
|
+
}
|
126
|
+
|
127
|
+
return this.callsites;
|
128
|
+
}
|
129
|
+
|
130
|
+
private RubyHash getDefsites(Ruby runtime) {
|
131
|
+
if (this.defsites == null) {
|
132
|
+
this.defsites = RubyHash.newHash(runtime);
|
133
|
+
}
|
134
|
+
|
135
|
+
return this.defsites;
|
136
|
+
}
|
137
|
+
}
|