cognitive_distance 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.autotest +19 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE.md +221 -0
  6. data/README.md +50 -0
  7. data/Rakefile +14 -0
  8. data/cognitive_distance.gemspec +23 -0
  9. data/lib/cognitive_distance.rb +23 -0
  10. data/lib/cognitive_distance/measurements.rb +16 -0
  11. data/lib/cognitive_distance/measurements/distinct_module_hops.rb +19 -0
  12. data/lib/cognitive_distance/measurements/measurement.rb +7 -0
  13. data/lib/cognitive_distance/measurements/module_hops.rb +17 -0
  14. data/lib/cognitive_distance/presenters.rb +2 -0
  15. data/lib/cognitive_distance/presenters/graph_to_dot.rb +11 -0
  16. data/lib/cognitive_distance/structures.rb +9 -0
  17. data/lib/cognitive_distance/structures/call_node.rb +74 -0
  18. data/lib/cognitive_distance/structures/call_node_root.rb +29 -0
  19. data/lib/cognitive_distance/structures/call_tree.rb +34 -0
  20. data/lib/cognitive_distance/structures/graph.rb +77 -0
  21. data/lib/cognitive_distance/structures/missing_call_context.rb +10 -0
  22. data/lib/cognitive_distance/tracer.rb +32 -0
  23. data/lib/cognitive_distance/transforms.rb +5 -0
  24. data/lib/cognitive_distance/transforms/call_tree_to_module_boundary_graph.rb +30 -0
  25. data/lib/cognitive_distance/version.rb +3 -0
  26. data/spec/cognitive_distance/measurements/distinct_module_hops_spec.rb +52 -0
  27. data/spec/cognitive_distance/measurements/measurement_spec.rb +44 -0
  28. data/spec/cognitive_distance/measurements/module_hops_spec.rb +54 -0
  29. data/spec/cognitive_distance/measurements_spec.rb +19 -0
  30. data/spec/cognitive_distance/presenters/graph_to_dot_spec.rb +26 -0
  31. data/spec/cognitive_distance/structures/call_node_root_spec.rb +31 -0
  32. data/spec/cognitive_distance/structures/call_node_spec.rb +91 -0
  33. data/spec/cognitive_distance/structures/call_tree_spec.rb +55 -0
  34. data/spec/cognitive_distance/structures/graph_spec.rb +142 -0
  35. data/spec/cognitive_distance/structures/missing_call_context_spec.rb +21 -0
  36. data/spec/cognitive_distance/tracer_spec.rb +57 -0
  37. data/spec/cognitive_distance/transforms/call_tree_to_module_boundary_graph_spec.rb +57 -0
  38. data/spec/spec_dummies.rb +60 -0
  39. data/spec/spec_helper.rb +12 -0
  40. metadata +111 -0
@@ -0,0 +1,19 @@
1
+ # vim: set filetype=ruby :
2
+
3
+ require 'autotest/restart'
4
+ require 'autotest/growl'
5
+
6
+ Autotest.add_hook :initialize do |at|
7
+ at.testlib = 'minitest/spec'
8
+
9
+ at.clear_mappings
10
+ at.add_mapping(/^lib\/(.*)\.rb$/) { |f, m| "spec/#{m[1]}_spec.rb" }
11
+ at.add_mapping(/^spec\/.*_spec\.rb$/) { |f,_| f }
12
+ at.add_mapping(/^spec\/spec_helper\.rb$/) { |f,_|
13
+ at.files_matching(/^spec\/.*_spec\.rb$/)
14
+ }
15
+
16
+ at.add_exception '.git'
17
+ at.add_exception 'coverage'
18
+ end
19
+
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *~
6
+ *.swp
7
+ .DS_Store
8
+ coverage/*
9
+ exs/*
10
+
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ script: bundle exec rake test
5
+
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cognitive_distance.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'minitest'
8
+ gem 'minitest-emoji'
9
+ gem 'autotest'
10
+ gem 'autotest-growl'
11
+ gem 'simplecov'
12
+ end
@@ -0,0 +1,221 @@
1
+ # Apache License
2
+
3
+ Version 2.0, January 2004
4
+
5
+ [http://www.apache.org/licenses/](http://www.apache.org/licenses/)
6
+
7
+ ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ ### 1. Definitions
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ ### 2. Grant of Copyright License
69
+
70
+ Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ copyright license to reproduce, prepare Derivative Works of,
74
+ publicly display, publicly perform, sublicense, and distribute the
75
+ Work and such Derivative Works in Source or Object form.
76
+
77
+ ### 3. Grant of Patent License
78
+ Subject to the terms and conditions of
79
+ this License, each Contributor hereby grants to You a perpetual,
80
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
81
+ (except as stated in this section) patent license to make, have made,
82
+ use, offer to sell, sell, import, and otherwise transfer the Work,
83
+ where such license applies only to those patent claims licensable
84
+ by such Contributor that are necessarily infringed by their
85
+ Contribution(s) alone or by combination of their Contribution(s)
86
+ with the Work to which such Contribution(s) was submitted. If You
87
+ institute patent litigation against any entity (including a
88
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
89
+ or a Contribution incorporated within the Work constitutes direct
90
+ or contributory patent infringement, then any patent licenses
91
+ granted to You under this License for that Work shall terminate
92
+ as of the date such litigation is filed.
93
+
94
+ ### 4. Redistribution
95
+
96
+ You may reproduce and distribute copies of the
97
+ Work or Derivative Works thereof in any medium, with or without
98
+ modifications, and in Source or Object form, provided that You
99
+ meet the following conditions:
100
+
101
+ <ol style="list-style-type: lower-alpha;">
102
+ <li>
103
+ You must give any other recipients of the Work or
104
+ Derivative Works a copy of this License; and
105
+ </li>
106
+ <li>
107
+ You must cause any modified files to carry prominent notices
108
+ stating that You changed the files; and
109
+ </li>
110
+ <li>
111
+ You must retain, in the Source form of any Derivative Works
112
+ that You distribute, all copyright, patent, trademark, and
113
+ attribution notices from the Source form of the Work,
114
+ excluding those notices that do not pertain to any part of
115
+ the Derivative Works; and
116
+ </li>
117
+ <li>
118
+ If the Work includes a "NOTICE" text file as part of its
119
+ distribution, then any Derivative Works that You distribute must
120
+ include a readable copy of the attribution notices contained
121
+ within such NOTICE file, excluding those notices that do not
122
+ pertain to any part of the Derivative Works, in at least one
123
+ of the following places: within a NOTICE text file distributed
124
+ as part of the Derivative Works; within the Source form or
125
+ documentation, if provided along with the Derivative Works; or,
126
+ within a display generated by the Derivative Works, if and
127
+ wherever such third-party notices normally appear. The contents
128
+ of the NOTICE file are for informational purposes only and
129
+ do not modify the License. You may add Your own attribution
130
+ notices within Derivative Works that You distribute, alongside
131
+ or as an addendum to the NOTICE text from the Work, provided
132
+ that such additional attribution notices cannot be construed
133
+ as modifying the License.
134
+ </li>
135
+ </ol>
136
+
137
+ You may add Your own copyright statement to Your modifications and
138
+ may provide additional or different license terms and conditions
139
+ for use, reproduction, or distribution of Your modifications, or
140
+ for any such Derivative Works as a whole, provided Your use,
141
+ reproduction, and distribution of the Work otherwise complies with
142
+ the conditions stated in this License.
143
+
144
+ ### 5. Submission of Contributions
145
+ Unless You explicitly state otherwise,
146
+ any Contribution intentionally submitted for inclusion in the Work
147
+ by You to the Licensor shall be under the terms and conditions of
148
+ this License, without any additional terms or conditions.
149
+ Notwithstanding the above, nothing herein shall supersede or modify
150
+ the terms of any separate license agreement you may have executed
151
+ with Licensor regarding such Contributions.
152
+
153
+ ### 6. Trademarks
154
+ This License does not grant permission to use the trade
155
+ names, trademarks, service marks, or product names of the Licensor,
156
+ except as required for reasonable and customary use in describing the
157
+ origin of the Work and reproducing the content of the NOTICE file.
158
+
159
+ ### 7. Disclaimer of Warranty
160
+ Unless required by applicable law or
161
+ agreed to in writing, Licensor provides the Work (and each
162
+ Contributor provides its Contributions) on an "AS IS" BASIS,
163
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
164
+ implied, including, without limitation, any warranties or conditions
165
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
166
+ PARTICULAR PURPOSE. You are solely responsible for determining the
167
+ appropriateness of using or redistributing the Work and assume any
168
+ risks associated with Your exercise of permissions under this License.
169
+
170
+ ### 8. Limitation of Liability
171
+ In no event and under no legal theory,
172
+ whether in tort (including negligence), contract, or otherwise,
173
+ unless required by applicable law (such as deliberate and grossly
174
+ negligent acts) or agreed to in writing, shall any Contributor be
175
+ liable to You for damages, including any direct, indirect, special,
176
+ incidental, or consequential damages of any character arising as a
177
+ result of this License or out of the use or inability to use the
178
+ Work (including but not limited to damages for loss of goodwill,
179
+ work stoppage, computer failure or malfunction, or any and all
180
+ other commercial damages or losses), even if such Contributor
181
+ has been advised of the possibility of such damages.
182
+
183
+ ### 9. Accepting Warranty or Additional Liability
184
+ While redistributing
185
+ the Work or Derivative Works thereof, You may choose to offer,
186
+ and charge a fee for, acceptance of support, warranty, indemnity,
187
+ or other liability obligations and/or rights consistent with this
188
+ License. However, in accepting such obligations, You may act only
189
+ on Your own behalf and on Your sole responsibility, not on behalf
190
+ of any other Contributor, and only if You agree to indemnify,
191
+ defend, and hold each Contributor harmless for any liability
192
+ incurred by, or claims asserted against, such Contributor by reason
193
+ of your accepting any such warranty or additional liability.
194
+
195
+ ## END OF TERMS AND CONDITIONS
196
+
197
+ ## APPENDIX: How to apply the Apache License to your work.
198
+
199
+ To apply the Apache License to your work, attach the following
200
+ boilerplate notice, with the fields enclosed by brackets "[]"
201
+ replaced with your own identifying information. (Don't include
202
+ the brackets!) The text should be enclosed in the appropriate
203
+ comment syntax for the file format. We also recommend that a
204
+ file or class name and description of purpose be included on the
205
+ same "printed page" as the copyright notice for easier
206
+ identification within third-party archives.
207
+
208
+ Copyright [yyyy] [name of copyright owner]
209
+
210
+ Licensed under the Apache License, Version 2.0 (the "License");
211
+ you may not use this file except in compliance with the License.
212
+ You may obtain a copy of the License at
213
+
214
+ http://www.apache.org/licenses/LICENSE-2.0
215
+
216
+ Unless required by applicable law or agreed to in writing, software
217
+ distributed under the License is distributed on an "AS IS" BASIS,
218
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
219
+ See the License for the specific language governing permissions and
220
+ limitations under the License.
221
+
@@ -0,0 +1,50 @@
1
+ # Cognitive Distance
2
+
3
+ [![Build Status](https://secure.travis-ci.org/iande/cognitive_distance.png)](http://travis-ci.org/iande/cognitive_distance)
4
+
5
+ ## Installing
6
+
7
+ Not yet.
8
+
9
+ ## Usage
10
+
11
+ ### Measure the number of modules "hopped" by a method call
12
+
13
+ class Mine
14
+ def my_method
15
+ Yours.new.your_method
16
+ end
17
+ end
18
+
19
+ class Yours
20
+ def initialize
21
+ end
22
+
23
+ def your_method
24
+ end
25
+ end
26
+
27
+ CognitiveDistance::Measurements.measure_module_hops Mine.new, :my_method
28
+ # => 2
29
+ CognitiveDistance::Measurements.measure_distinct_module_hopes Mine.new, :my_method
30
+ # => 1
31
+
32
+ The module hops are:
33
+
34
+ 1. Initializing the newly instantiated `Yours` object (`Yours#initialize`)
35
+ 2. Calling `your_method` on the new instance
36
+
37
+ There is only 1 distinct module hop because `my_method => Yours#initialize`
38
+ and `my_method => Yours#your_method` cross the same boundary.
39
+
40
+ At this time, only Ruby code is traced, so if `Yours` did not define an
41
+ `initialize` method, both hop counts would be 1.
42
+
43
+ *Brief Aside*: tracing `c-call` events changes nothing in this case, as no
44
+ instance methods of the `Yours` instance are invoked until `#your_method` is
45
+ called.
46
+
47
+ ## License
48
+
49
+ Let's go with Apache 2.0, I've been using it pretty frequently.
50
+
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Generate a coverage report from tests with simplecov"
11
+ task :coverage do
12
+ ENV['SCOV'] = '1'
13
+ Rake::Task['test'].invoke
14
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cognitive_distance/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cognitive_distance"
7
+ s.version = CognitiveDistance::VERSION
8
+ s.authors = ["Ian D. Eccles"]
9
+ s.email = ["ian.eccles@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A (currently incomplete) measure of locality}
12
+ s.description = %q{A tool for measuring locality of Ruby source. No real features yet, wait for the non-pre version}
13
+
14
+ s.rubyforge_project = "cognitive_distance"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'rake'
22
+ end
23
+
@@ -0,0 +1,23 @@
1
+ require 'cognitive_distance/version'
2
+ require 'cognitive_distance/structures'
3
+ require 'cognitive_distance/tracer'
4
+ require 'cognitive_distance/measurements'
5
+ require 'cognitive_distance/transforms'
6
+ require 'cognitive_distance/presenters'
7
+
8
+ module CognitiveDistance
9
+ end
10
+ # Copyright 2012 Ian D. Eccles
11
+
12
+ # Licensed under the Apache License, Version 2.0 (the "License");
13
+ # you may not use this file except in compliance with the License.
14
+ # You may obtain a copy of the License at
15
+
16
+ # http://www.apache.org/licenses/LICENSE-2.0
17
+
18
+ # Unless required by applicable law or agreed to in writing, software
19
+ # distributed under the License is distributed on an "AS IS" BASIS,
20
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ # See the License for the specific language governing permissions and
22
+ # limitations under the License.
23
+
@@ -0,0 +1,16 @@
1
+ module CognitiveDistance::Measurements
2
+ class << self
3
+ def register_measurement mod, method, measurement
4
+ instance_eval <<-EODEF, __FILE__, __LINE__
5
+ def measure_#{measurement}(*args)
6
+ #{mod.name}.#{method} *args
7
+ end
8
+ EODEF
9
+ end
10
+ end
11
+ end
12
+
13
+ require 'cognitive_distance/measurements/measurement'
14
+ require 'cognitive_distance/measurements/module_hops'
15
+ require 'cognitive_distance/measurements/distinct_module_hops'
16
+
@@ -0,0 +1,19 @@
1
+ module CognitiveDistance::Measurements
2
+ class DistinctModuleHops
3
+ extend Measurement
4
+
5
+ register :distinct_module_hops
6
+
7
+ def measure tree
8
+ CognitiveDistance::Transforms::CallTreeToModuleBoundaryGraph.
9
+ transform(tree).map { |(v1,v2)|
10
+ [v1.context, v2.context]
11
+ }.uniq.size
12
+ end
13
+
14
+ def self.measure obj, meth
15
+ new.measure(CognitiveDistance::Tracer.new(obj).trace(meth))
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,7 @@
1
+ module CognitiveDistance::Measurements
2
+ module Measurement
3
+ def register name, measure=:measure
4
+ CognitiveDistance::Measurements.register_measurement self, measure, name
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module CognitiveDistance::Measurements
2
+ class ModuleHops
3
+ extend Measurement
4
+
5
+ register :module_hops
6
+
7
+ def measure tree
8
+ CognitiveDistance::Transforms::CallTreeToModuleBoundaryGraph.
9
+ transform(tree).edges.size
10
+ end
11
+
12
+ def self.measure obj, meth
13
+ new.measure(CognitiveDistance::Tracer.new(obj).trace(meth))
14
+ end
15
+ end
16
+ end
17
+