who_called_me 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Alain Ravet
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = who_called_me
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Alain Ravet. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "who_called_me"
8
+ gem.summary = %Q{track and list the code lines that exercise a line}
9
+ gem.description = %Q{track and list the code lines that exercise a line}
10
+ gem.email = "alain.ravet+git@gmail.com"
11
+ gem.homepage = "http://github.com/alainravet/who_called_me"
12
+ gem.authors = ["Alain Ravet"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "who_called_me #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,25 @@
1
+ module WhoCalledMe
2
+ module ArrayUtils
3
+
4
+ def self.first_elements_with_common_prefix(arr, pre_len_to_ignore=0)
5
+ arr.each_index do |i|
6
+ # exclude the n first characters from the comparison <- remove them
7
+ arr[i] = arr[i][pre_len_to_ignore..-1]
8
+ end
9
+
10
+ [].tap do |results|
11
+ first_line = arr.first
12
+ results << first_line
13
+ (0..arr.length-2).each do |i|
14
+ line, next_line = arr[i], arr[i+1]
15
+
16
+ next_line_not_in_same_code_tree = !StringUtils.common_substring?(line, next_line)
17
+ next_line_not_in_same_code_tree ?
18
+ break :
19
+ (results << next_line)
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module WhoCalledMe
2
+ module StringUtils
3
+
4
+ def self.common_substring?(a, b)
5
+ 1 <= common_substring(a, b).length
6
+ end
7
+
8
+ def self.common_substring(a, b)
9
+ difference = a.to_str.each_char.with_index.find { |ch, idx|
10
+ b[idx].nil? or ch != b[idx].chr
11
+ }
12
+ difference ? b[0, difference.last] : b
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module WhoCalledMe
2
+ module TraceUtils
3
+
4
+ def self.fulltrace
5
+ raise
6
+ rescue
7
+ $!.backtrace
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + '/string_utils'
2
+ require File.dirname(__FILE__) + '/array_utils'
3
+ require File.dirname(__FILE__) + '/trace_utils'
4
+
5
+ module WhoCalledMe
6
+
7
+ @@__who_called_me_accumul_h = {}
8
+ @@__disable_puts = false
9
+
10
+ def self.disable_puts ; @@__disable_puts = true end
11
+ def self.enable_puts ; @@__disable_puts = false end
12
+
13
+
14
+ def self._who_called_me(msg=nil, nof_internal_methods_to_skim=1+1) # this + _fulltrace
15
+ fulltrace = TraceUtils.fulltrace
16
+ trace = ArrayUtils.first_elements_with_common_prefix(fulltrace, 1)
17
+ trace = trace[nof_internal_methods_to_skim..-1]
18
+ trace.reverse!
19
+
20
+ record(msg || :all, trace)
21
+
22
+ trace
23
+ end
24
+
25
+ def self.record(key, trace)
26
+ @@__who_called_me_accumul_h[key] ||= []
27
+ @@__who_called_me_accumul_h[key] << trace
28
+ end
29
+
30
+
31
+
32
+ def self._who_called_me_data(options={})
33
+ if options[:only_top]
34
+ {}.tap do |result|
35
+ @@__who_called_me_accumul_h.each do |key, traces|
36
+ result[key] = traces.collect{|trace| trace.first }.uniq
37
+ end
38
+ end
39
+ else
40
+ @@__who_called_me_accumul_h
41
+ end
42
+ end
43
+
44
+
45
+ def self.puts_formatted_traces
46
+ return if _who_called_me_data.empty?
47
+ return if @@__disable_puts
48
+ l0 = '-'*99
49
+ puts l0
50
+ puts 'who_called_me report :'
51
+ puts '======================'
52
+ _who_called_me_data.keys.each do |key|
53
+ puts
54
+ puts "+----------------------------"
55
+ puts (key == :all) ? '| who_called_me' : "| who_called_me(#{key})"
56
+ puts "+----------------------------"
57
+ _who_called_me_data[key].each do |trace|
58
+ puts
59
+ puts trace.shift
60
+ puts ' was called by :'
61
+ padding = ''
62
+ trace.each do |line|
63
+ padding += ' '
64
+ puts " .#{padding}#{line}"
65
+ end
66
+ end
67
+ end
68
+ puts
69
+ puts l0
70
+ end
71
+
72
+ # used by tests only
73
+ def self.reset
74
+ @@__who_called_me_accumul_h = {}
75
+ @@__disable_puts = false
76
+ end
77
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/who_called_me/who_called_me.rb'
2
+
3
+ module Kernel
4
+ def who_called_me(msg=nil)
5
+ nof_internal_methods_to_skim = 3 #this + WhoCalledMe._who_called_me + _fulltrace
6
+ WhoCalledMe._who_called_me(msg, nof_internal_methods_to_skim)
7
+ end
8
+ def who_called_me_data(options={})
9
+ WhoCalledMe._who_called_me_data(options)
10
+ end
11
+ end
12
+
13
+ at_exit do
14
+ if !who_called_me_data.empty?
15
+ WhoCalledMe.puts_formatted_traces
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../lib/who_called_me')
2
+
3
+ class Foo
4
+ def foo
5
+ bar
6
+ end
7
+ def bar
8
+ who_called_me
9
+ end
10
+
11
+ def self.point_1
12
+ FooFoo.point_1
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/foo')
2
+
3
+ class FooFoo
4
+ def foofoo
5
+ Foo.new.bar
6
+ Foo.new.foo
7
+ end
8
+
9
+ def self.point_1
10
+ who_called_me('point 1')
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/foo_foo')
2
+ FooFoo.new.foofoo
3
+ FooFoo.point_1
4
+ Foo.point_1
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'who_called_me'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.before(:each) do
9
+ WhoCalledMe.reset
10
+ end
11
+
12
+ end
13
+
14
+ def capture_stdout
15
+ @@original_stdout = STDOUT
16
+ $stdout = StringIO.new
17
+ end
18
+
19
+ def restore_stdout
20
+ $stdout = @@original_stdout
21
+ end
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/foo')
3
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/foo_foo')
4
+
5
+ # Simplest call : no parameter
6
+ #
7
+ describe "who_called_me_data()" do
8
+ before { WhoCalledMe.disable_puts}
9
+ #don't restore in :after, because the puts happens in at_exit
10
+
11
+ before do
12
+ @dir = File.dirname(__FILE__)[1..-1]
13
+ FooFoo.new.foofoo ; @line = __LINE__
14
+ end
15
+
16
+ it "who_called_me_data() returns 2 fulltraces" do
17
+ who_called_me_data[:all].should == [
18
+ ["#{@dir}/who_called_me_spec.rb:#{@line}" ,
19
+ "#{@dir}/fixtures/foo_foo.rb:5:in `foofoo'",
20
+ "#{@dir}/fixtures/foo.rb:8:in `bar'"
21
+ ],
22
+ ["#{@dir}/who_called_me_spec.rb:#{@line}" ,
23
+ "#{@dir}/fixtures/foo_foo.rb:6:in `foofoo'",
24
+ "#{@dir}/fixtures/foo.rb:5:in `foo'" ,
25
+ "#{@dir}/fixtures/foo.rb:8:in `bar'"
26
+ ]
27
+ ]
28
+ end
29
+
30
+ it "who_called_me_data(:only_top=>true}) returns only the unique 1st lines" do
31
+ who_called_me_data(:only_top=>true).should ==
32
+ {
33
+ :all => ["#{@dir}/who_called_me_spec.rb:#{@line}"]
34
+ }
35
+ end
36
+ end
37
+
38
+ # call with parameter
39
+ # who_called_me_data('point 1')
40
+ #
41
+ describe "who_called_me_data('point 1')" do
42
+ before { WhoCalledMe.disable_puts}
43
+ #don't restore in :after, because the puts happens in at_exit
44
+
45
+ before do
46
+ @dir = File.dirname(__FILE__)[1..-1]
47
+ FooFoo.point_1; @line = __LINE__
48
+ Foo .point_1; @line2= __LINE__
49
+ end
50
+
51
+ it "who_called_me_data() returns 2 fulltraces" do
52
+
53
+ who_called_me_data.should ==
54
+ {
55
+ 'point 1' => [
56
+ ["#{@dir}/who_called_me_spec.rb:#{@line}" ,
57
+ "#{@dir}/fixtures/foo_foo.rb:10:in `point_1'"
58
+ ],
59
+ ["#{@dir}/who_called_me_spec.rb:#{@line2}" ,
60
+ "#{@dir}/fixtures/foo.rb:12:in `point_1'" ,
61
+ "#{@dir}/fixtures/foo_foo.rb:10:in `point_1'"
62
+ ]
63
+ ]
64
+ }
65
+ end
66
+
67
+ it "who_called_me_data(:only_top=>true}) returns only the unique 1st lines" do
68
+ who_called_me_data(:only_top=>true).should ==
69
+ {
70
+ 'point 1' => ["#{@dir}/who_called_me_spec.rb:#{@line }",
71
+ "#{@dir}/who_called_me_spec.rb:#{@line2}"]
72
+
73
+ }
74
+ end
75
+ end
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{who_called_me}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alain Ravet"]
12
+ s.date = %q{2010-08-23}
13
+ s.description = %q{track and list the code lines that exercise a line}
14
+ s.email = %q{alain.ravet+git@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/who_called_me.rb",
27
+ "lib/who_called_me/array_utils.rb",
28
+ "lib/who_called_me/string_utils.rb",
29
+ "lib/who_called_me/trace_utils.rb",
30
+ "lib/who_called_me/who_called_me.rb",
31
+ "spec/fixtures/foo.rb",
32
+ "spec/fixtures/foo_foo.rb",
33
+ "spec/fixtures/foo_foo_caller.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb",
36
+ "spec/who_called_me_spec.rb",
37
+ "who_called_me.gemspec"
38
+ ]
39
+ s.homepage = %q{http://github.com/alainravet/who_called_me}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{track and list the code lines that exercise a line}
44
+ s.test_files = [
45
+ "spec/fixtures/foo.rb",
46
+ "spec/fixtures/foo_foo.rb",
47
+ "spec/fixtures/foo_foo_caller.rb",
48
+ "spec/spec_helper.rb",
49
+ "spec/who_called_me_spec.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
58
+ else
59
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
63
+ end
64
+ end
65
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: who_called_me
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Alain Ravet
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-23 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: track and list the code lines that exercise a line
38
+ email: alain.ravet+git@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/who_called_me.rb
54
+ - lib/who_called_me/array_utils.rb
55
+ - lib/who_called_me/string_utils.rb
56
+ - lib/who_called_me/trace_utils.rb
57
+ - lib/who_called_me/who_called_me.rb
58
+ - spec/fixtures/foo.rb
59
+ - spec/fixtures/foo_foo.rb
60
+ - spec/fixtures/foo_foo_caller.rb
61
+ - spec/spec.opts
62
+ - spec/spec_helper.rb
63
+ - spec/who_called_me_spec.rb
64
+ - who_called_me.gemspec
65
+ has_rdoc: true
66
+ homepage: http://github.com/alainravet/who_called_me
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: track and list the code lines that exercise a line
99
+ test_files:
100
+ - spec/fixtures/foo.rb
101
+ - spec/fixtures/foo_foo.rb
102
+ - spec/fixtures/foo_foo_caller.rb
103
+ - spec/spec_helper.rb
104
+ - spec/who_called_me_spec.rb