called_from 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ ###
2
+ ### $Release: 0.1.0 $
3
+ ### copyright(c) kuwata-lab.com all rights reserved.
4
+ ### Ruby's License
5
+ ###
6
+
7
+ require 'test/unit'
8
+ begin
9
+ require 'called_from.so'
10
+ rescue LoadError => x
11
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
12
+ require 'called_from.rb'
13
+ end
14
+
15
+
16
+ class CalledFromTest < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @fname = File.basename(__FILE__)
20
+ @fname = File.join('test', @fname) if File.basename(Dir.pwd) != 'test'
21
+ end
22
+
23
+ def f1
24
+ f2()
25
+ end
26
+
27
+ def f2
28
+ f3()
29
+ end
30
+
31
+ def f3
32
+ f4()
33
+ end
34
+
35
+ def f4
36
+ called_from(@level)
37
+ end
38
+
39
+ def test_level_nil
40
+ if :'called then returns array of filename, line number, and function name'
41
+ @level = nil
42
+ arr = f1()
43
+ assert_equal("[\"#{@fname}\", #{32}, \"f3\"]", arr.inspect)
44
+ end
45
+ end
46
+
47
+ def test_level_1
48
+ if :'called without args then returns the same as arg 1 specified'
49
+ @level = 1
50
+ arr1 = f1()
51
+ @level = nil
52
+ arr0 = f1()
53
+ assert_equal(arr1.inspect, arr0.inspect)
54
+ end
55
+ end
56
+
57
+ def test_level_n
58
+ if :'called with level then returns array according to level'
59
+ @level = 2
60
+ assert_equal("[\"#{@fname}\", #{28}, \"f2\"]", f1().inspect)
61
+ @level = 3
62
+ assert_equal("[\"#{@fname}\", #{24}, \"f1\"]", f1().inspect)
63
+ end
64
+ end
65
+
66
+ def test_level_too_deep
67
+ if :'called with too deep level then returns nil'
68
+ @level = 100
69
+ assert_nil(f1())
70
+ end
71
+ end
72
+
73
+ def test_level_top
74
+ if :'caller is not found then function name is nil'
75
+ @level = 100
76
+ while (ret = f1()).nil?
77
+ @level -= 1
78
+ end
79
+ #$stderr.puts "*** debug: ret=#{ret.inspect}"
80
+ assert_kind_of(String, ret[0])
81
+ assert_kind_of(Fixnum, ret[1])
82
+ assert_nil(ret[2])
83
+ end
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: called_from
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - makoto kuata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-01 15:52:20.692214 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |
17
+ Extention Module 'called_from' provides called_from() global function
18
+ which gets filename and line number of caller.
19
+
20
+ In short:
21
+
22
+ require 'called_from'
23
+ filename, linenum, function = called_from(1)
24
+
25
+ is equivarent to:
26
+
27
+ caller(1)[0] =~ /:(d+)( in `(.*)')?/
28
+ filename, linenum, function = $`, $1, $2
29
+
30
+ But called_from() is much faster than caller()[0].
31
+
32
+ email: kwa.at.kuwata-lab.com
33
+ executables: []
34
+
35
+ extensions:
36
+ - ext/called_from/extconf.rb
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - README.txt
41
+ - Rakefile
42
+ - setup.rb
43
+ - called_from.gemspec
44
+ - ext/called_from/called_from.c
45
+ - ext/called_from/extconf.rb
46
+ - lib/called_from.rb
47
+ - test/test_called_from.rb
48
+ - benchapp.zip
49
+ has_rdoc: true
50
+ homepage: http://github.com/kwatch/called_from/
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: called_from
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: much faster of caller()[0]
77
+ test_files: []
78
+