callgrapher 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +2 -2
  2. data/lib/callgrapher.rb +9 -20
  3. data/test/test.rb +22 -11
  4. metadata +2 -2
data/README.md CHANGED
@@ -10,9 +10,9 @@ Ruby 1.9 or later & Graphviz on your $PATH
10
10
 
11
11
  Usage
12
12
  -----
13
- Require 'callgrapher' and call trace_class_dependencies, passing in a block
13
+ Require `callgrapher` and call `trace_class_dependencies`, passing in a block
14
14
  containing the code you want to graph. A Hash of sets will be returned, ready
15
- for proccesing with make_class_graph. Calling make_class_graph uses the `dot`
15
+ for procesing with make_class_graph. Calling `make_class_graph` uses the `dot`
16
16
  command to generate a graph & save it to /tmp/graph.png.
17
17
 
18
18
  Known Limitations
@@ -13,24 +13,20 @@
13
13
  # You should have received a copy of the GNU General Public License
14
14
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- # This is the main executable for the whysynth-controller project. It also
17
- # contains most of the code that relies on external libraries.
18
-
19
16
  require 'set'
20
17
 
21
18
  module CallGrapher
22
19
 
23
20
  # @param [Array<String>] file_whitelist
24
21
  # Only include classes defined in these files.
25
- # @param [Integer] namespace_depth
26
- # The number of namespaces to report. For example, a class name of
27
- # Foo::Bar::Baz, when processed with namespace_depth == 1, would be reported
28
- # as just Foo. With namespace_depth == 2, as Foo::Bar, and so on. A
29
- # namespace_depth of 0 indicates that the entire class name should be
30
- # reported.
31
- def self.trace_class_dependencies(namespace_depth = 0,
32
- file_whitelist = nil,
33
- class_blacklist = nil)
22
+ # @param [Array<String>] class_blacklist
23
+ # Don't include these classes.
24
+ # @param [Proc] class_name_processor
25
+ # A Proc that takes a single argument (a fully qualified classname) and
26
+ # returns the class as it should be shown in the graph.
27
+ def self.trace_class_dependencies(file_whitelist = nil,
28
+ class_blacklist = nil,
29
+ class_name_processor = lambda{|x| x})
34
30
  callstack = []
35
31
  classgraph = Hash.new{ |hash, key| hash[key] = Set.new }
36
32
 
@@ -40,14 +36,7 @@ module CallGrapher
40
36
  case event
41
37
  when 'call'
42
38
  caller = callstack[-1]
43
-
44
- klass =
45
- if namespace_depth > 0
46
- klass.name.split('::').first(namespace_depth).join('::')
47
- else
48
- klass.name
49
- end
50
-
39
+ klass = class_name_processor.call klass.name
51
40
  classgraph[caller].add klass if caller && caller != klass
52
41
  callstack.push klass
53
42
  when 'return'
@@ -115,31 +115,42 @@ class Test < MiniTest::Unit::TestCase
115
115
  EmptyHash = {}
116
116
 
117
117
  def test_class_dependency_tracing
118
- assert_equal ExpectedTestGraph, CallGrapher.trace_class_dependencies{ Class1.new.test }
118
+ assert_equal ExpectedTestGraph,
119
+ CallGrapher.trace_class_dependencies{ Class1.new.test }
119
120
  end
120
121
 
121
122
  def test_graphviz_output
122
- assert_equal ExpectedGraphvizGraph, CallGrapher.make_graphviz_graph(ExpectedTestGraph)
123
+ assert_equal ExpectedGraphvizGraph,
124
+ CallGrapher.make_graphviz_graph(ExpectedTestGraph)
123
125
  end
124
126
 
125
127
  def test_file_whitelist
126
128
  assert_equal EmptyHash,
127
- CallGrapher.trace_class_dependencies(0, []){ Class1.new.test }
129
+ CallGrapher.trace_class_dependencies([]){ Class1.new.test }
128
130
 
129
131
  assert_equal ExpectedTestGraph,
130
- CallGrapher.trace_class_dependencies(0, [__FILE__]) { Class1.new.test}
132
+ CallGrapher.trace_class_dependencies([__FILE__]) { Class1.new.test}
131
133
  end
132
134
 
133
- def test_namespace_depth
134
- assert_equal ExpectedTestGraph_NamespaceDepth1,
135
- CallGrapher.trace_class_dependencies(1) { Class1.new.test }
136
-
137
- assert_equal ExpectedTestGraph_NamespaceDepth2,
138
- CallGrapher.trace_class_dependencies(2) { Class1.new.test }
135
+ def test_class_name_processing
136
+ first_namespace = lambda { |name| name.split('::').first }
137
+ graph = CallGrapher.trace_class_dependencies(nil,nil,first_namespace) {
138
+ Class1.new.test
139
+ }
140
+ assert_equal ExpectedTestGraph_NamespaceDepth1, graph
141
+
142
+ first_two_namespaces = lambda { |name| name.split('::').first(2).join('::') }
143
+ graph2 = CallGrapher.trace_class_dependencies(nil, nil, first_two_namespaces) {
144
+ Class1.new.test
145
+ }
146
+ assert_equal ExpectedTestGraph_NamespaceDepth2, graph2
139
147
  end
140
148
 
141
149
  def test_class_blacklist
142
- assert_equal ExpectedTestGraph_WithoutClass4, CallGrapher.trace_class_dependencies(0, [__FILE__], ['Class4']) { Class1.new.test }
150
+ assert_equal ExpectedTestGraph_WithoutClass4,
151
+ CallGrapher.trace_class_dependencies([__FILE__], ['Class4']) {
152
+ Class1.new.test
153
+ }
143
154
  end
144
155
 
145
156
  # This test doesn't assert anything, it's just convenient to have the tests
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callgrapher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-02 00:00:00.000000000 Z
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Produce a call graph graph by tracing the method calls of running code.
15
15
  The output is a .dot file for use with GraphViz