ripper-tags 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,120 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require 'ostruct'
4
+ require 'ripper-tags'
5
+
6
+ class FormattersTest < Test::Unit::TestCase
7
+ def build_tag(attrs = {})
8
+ { :kind => 'class',
9
+ :line => 1,
10
+ :path => './script.rb',
11
+ :access => 'public',
12
+ }.merge(attrs)
13
+ end
14
+
15
+ def formatter_for(opts)
16
+ options = OpenStruct.new(opts)
17
+ RipperTags.formatter_for(options)
18
+ end
19
+
20
+ def test_custom
21
+ default = formatter_for(:format => 'custom', :tag_file_name => '-')
22
+
23
+ tags = []
24
+ tags << build_tag(:line => 1, :kind => 'class', :full_name => 'A::B', :inherits => 'C')
25
+ tags << build_tag(:line => 2, :kind => 'method', :full_name => 'A::B#imethod')
26
+ tags << build_tag(:line => 3, :kind => 'singleton method', :full_name => 'A::B.smethod')
27
+
28
+ output = capture_stdout do
29
+ default.with_output do |out|
30
+ tags.each { |tag| default.write(tag, out) }
31
+ end
32
+ end
33
+
34
+ assert_equal <<-OUT, output
35
+ 1 class A::B < C
36
+ 2 def A::B#imethod
37
+ 3 def A::B.smethod
38
+ OUT
39
+ end
40
+
41
+ def test_vim
42
+ vim = formatter_for(:format => 'vim')
43
+ assert_equal %{C\t./script.rb\t/^class C < D$/;"\tc\tclass:A.B\tinherits:D}, vim.format(build_tag(
44
+ :kind => 'class', :name => 'C',
45
+ :pattern => "class C < D",
46
+ :class => 'A::B', :inherits => 'D'
47
+ ))
48
+ assert_equal %{M\t./script.rb\t/^module M$/;"\tm\tclass:A.B}, vim.format(build_tag(
49
+ :kind => 'module', :name => 'M',
50
+ :pattern => "module M",
51
+ :class => 'A::B'
52
+ ))
53
+ assert_equal %{imethod\t./script.rb\t/^ def imethod(*args)$/;"\tf\tclass:A.B}, vim.format(build_tag(
54
+ :kind => 'method', :name => 'imethod',
55
+ :pattern => " def imethod(*args)",
56
+ :class => 'A::B'
57
+ ))
58
+ assert_equal %{smethod\t./script.rb\t/^ def self.smethod(*args)$/;"\tF\tclass:A.B}, vim.format(build_tag(
59
+ :kind => 'singleton method', :name => 'smethod',
60
+ :pattern => " def self.smethod(*args)",
61
+ :class => 'A::B'
62
+ ))
63
+ end
64
+
65
+ def test_emacs
66
+ emacs = formatter_for(:format => 'emacs')
67
+ assert_equal %{ class C < D\x7FC\x015,0}, emacs.format(build_tag(
68
+ :kind => 'class', :name => 'C',
69
+ :pattern => " class C < D", :line => 5,
70
+ :class => 'A::B', :inherits => 'D'
71
+ ))
72
+ end
73
+
74
+ def test_emacs_file_section_headers
75
+ emacs = formatter_for(:format => 'emacs', :tag_file_name => '-')
76
+
77
+ tags = []
78
+ tags << build_tag(:line => 1, :path => 'path/to/source.rb', :name => 'imethod', :pattern => 'def imethod')
79
+ tags << build_tag(:line => 2, :path => 'path/to/source.rb', :name => 'smethod', :pattern => 'def self.smethod')
80
+ tags << build_tag(:line => 3, :path => 'path/to/another.rb', :name => 'imethod', :pattern => 'def imethod')
81
+
82
+ output = capture_stdout do
83
+ emacs.with_output do |out|
84
+ tags.each { |tag| emacs.write(tag, out) }
85
+ end
86
+ end
87
+
88
+ assert_equal <<-OUT, output
89
+ \x0C
90
+ path/to/source.rb,53
91
+ def imethod\x7Fimethod\x011,0
92
+ def self.smethod\x7Fsmethod\x012,0
93
+ \x0C
94
+ path/to/another.rb,24
95
+ def imethod\x7Fimethod\x013,0
96
+ OUT
97
+ end
98
+
99
+ def test_relative
100
+ formatter = formatter_for(:format => 'custom', :tag_file_name => '.git/tags', :tag_relative => true)
101
+ tag = build_tag(:path => 'path/to/script.rb')
102
+ assert_equal '../path/to/script.rb', formatter.relative_path(tag)
103
+ end
104
+
105
+ def test_no_relative
106
+ formatter = formatter_for(:format => 'custom', :tag_file_name => '.git/tags')
107
+ tag = build_tag(:path => 'path/to/script.rb')
108
+ assert_equal 'path/to/script.rb', formatter.relative_path(tag)
109
+ end
110
+
111
+ def capture_stdout
112
+ old_stdout, $stdout = $stdout, StringIO.new
113
+ begin
114
+ yield
115
+ $stdout.string
116
+ ensure
117
+ $stdout = old_stdout
118
+ end
119
+ end
120
+ end
@@ -1,9 +1,23 @@
1
- require File.expand_path('../../lib/tag_ripper', __FILE__)
2
1
  require 'test/unit'
2
+ require 'ripper-tags/parser'
3
3
 
4
4
  class TagRipperTest < Test::Unit::TestCase
5
+ def extract(code)
6
+ RipperTags::Parser.extract(code)
7
+ end
8
+
9
+ def inspect(tag)
10
+ raise ArgumentError, "expected tag, got %p" % tag unless tag
11
+ "%d: %s %s%s" % [
12
+ tag[:line],
13
+ tag[:kind],
14
+ tag[:full_name],
15
+ tag[:inherits] ? " < #{tag[:inherits]}" : "",
16
+ ]
17
+ end
18
+
5
19
  def test_extract_basics
6
- tags = TagRipper.extract(<<-EOC)
20
+ tags = extract(<<-EOC)
7
21
  Const1 = 123
8
22
  def gmethod
9
23
  end
@@ -63,7 +77,7 @@ class TagRipperTest < Test::Unit::TestCase
63
77
  end
64
78
 
65
79
  def test_extract_access
66
- tags = TagRipper.extract(<<-EOC)
80
+ tags = extract(<<-EOC)
67
81
  class Test
68
82
  def abc() end
69
83
  private
@@ -80,4 +94,205 @@ class TagRipperTest < Test::Unit::TestCase
80
94
  assert_equal 'protected', tags.find{ |t| t[:name] == 'ghi' }[:access]
81
95
  assert_equal 'public', tags.find{ |t| t[:name] == 'jkl' }[:access]
82
96
  end
97
+
98
+ def test_extract_module_eval
99
+ tags = extract(<<-EOC)
100
+ M.module_eval do
101
+ class C; end
102
+ def imethod; end
103
+ end
104
+ EOC
105
+ assert_equal '2: class M::C', inspect(tags[0])
106
+ assert_equal '3: method M#imethod', inspect(tags[1])
107
+ end
108
+
109
+ def test_extract_manual_subclass
110
+ tags = extract(<<-EOC)
111
+ module M
112
+ C = Class.new(Sup::Klass)
113
+ C = Class.new Sup::Klass
114
+ C = Class.new
115
+ C = Class.new(klass)
116
+ end
117
+ EOC
118
+ assert_equal '2: class M::C < Sup::Klass', inspect(tags[1])
119
+ assert_equal '3: class M::C < Sup::Klass', inspect(tags[2])
120
+ assert_equal '4: class M::C', inspect(tags[3])
121
+ assert_equal '5: class M::C', inspect(tags[4])
122
+ end
123
+
124
+ def test_extract_assign_from_struct
125
+ tags = extract(<<-EOC)
126
+ module M
127
+ C = Struct.new(:name)
128
+ C = Struct.new :name
129
+ end
130
+ EOC
131
+ assert_equal '2: class M::C', inspect(tags[1])
132
+ assert_equal '3: class M::C', inspect(tags[2])
133
+ end
134
+
135
+ def test_extract_class_struct_scope
136
+ tags = extract(<<-EOC)
137
+ module M
138
+ S = Struct.new(:name) do
139
+ def imethod; end
140
+ end
141
+ C = Class.new(SuperClass) do
142
+ def imethod; end
143
+ end
144
+ end
145
+ EOC
146
+ assert_equal '3: method M::S#imethod', inspect(tags[2])
147
+ assert_equal '5: class M::C < SuperClass', inspect(tags[3])
148
+ assert_equal '6: method M::C#imethod', inspect(tags[4])
149
+ end
150
+
151
+ def test_extract_manual_module
152
+ tags = extract(<<-EOC)
153
+ class C
154
+ M = Module.new
155
+ M = Module.new do
156
+ def imethod; end
157
+ end
158
+ end
159
+ EOC
160
+ assert_equal '2: module C::M', inspect(tags[1])
161
+ assert_equal '3: module C::M', inspect(tags[2])
162
+ assert_equal '4: method C::M#imethod', inspect(tags[3])
163
+ end
164
+
165
+ def test_extract_define_method
166
+ tags = extract(<<-EOC)
167
+ module M
168
+ define_method(:imethod) do |arg|
169
+ end
170
+ define_method :imethod do |arg|
171
+ end
172
+ define_method(:imethod) { |arg| }
173
+ end
174
+ EOC
175
+ assert_equal '2: method M#imethod', inspect(tags[1])
176
+ assert_equal '4: method M#imethod', inspect(tags[2])
177
+ assert_equal '6: method M#imethod', inspect(tags[3])
178
+ end
179
+
180
+ def test_ignore_dynamic_define_method
181
+ tags = extract(<<-EOC)
182
+ module M
183
+ define_method(:"imethod_\#{i}") { |arg| }
184
+ define_method("imethod_\#{i}") { |arg| }
185
+ end
186
+ EOC
187
+ assert_equal 1, tags.length
188
+ end
189
+
190
+ def test_extract_alias
191
+ tags = extract(<<-EOC)
192
+ module M
193
+ alias :"[]" :get
194
+ alias :"[]=" :set
195
+ alias :set :"[]="
196
+ end
197
+ EOC
198
+ assert_equal '2: alias M#[] < get', inspect(tags[1])
199
+ assert_equal '3: alias M#[]= < set', inspect(tags[2])
200
+ assert_equal '4: alias M#set < []=', inspect(tags[3])
201
+ end
202
+
203
+ def test_ignore_dynamic_alias
204
+ tags = extract(<<-EOC)
205
+ module M
206
+ alias :"imethod_\#{i}" :foo
207
+ alias "imethod_\#{i}" :foo
208
+ end
209
+ EOC
210
+ assert_equal 1, tags.length
211
+ end
212
+
213
+ def test_extract_alias_method
214
+ tags = extract(<<-EOC)
215
+ module M
216
+ alias_method(:imethod, :foo)
217
+ alias_method :imethod, :foo
218
+ end
219
+ EOC
220
+ assert_equal '2: alias M#imethod < foo', inspect(tags[1])
221
+ assert_equal '3: alias M#imethod < foo', inspect(tags[2])
222
+ end
223
+
224
+ def test_ignore_dynamic_alias_method
225
+ tags = extract(<<-EOC)
226
+ module M
227
+ alias_method :"imethod_\#{i}", :foo
228
+ alias_method "imethod_\#{i}", :foo
229
+ end
230
+ EOC
231
+ assert_equal 1, tags.length
232
+ end
233
+
234
+ def test_extract_attr_accessor
235
+ tags = extract(<<-EOC)
236
+ module M
237
+ attr_accessor :a, :b
238
+ attr_reader(:a, :b)
239
+ attr_writer(:a, :b)
240
+ end
241
+ EOC
242
+ assert_equal '2: method M#a', inspect(tags[1])
243
+ assert_equal '2: method M#a=', inspect(tags[2])
244
+ assert_equal '2: method M#b', inspect(tags[3])
245
+ assert_equal '2: method M#b=', inspect(tags[4])
246
+ assert_equal '3: method M#a', inspect(tags[5])
247
+ assert_equal '3: method M#b', inspect(tags[6])
248
+ assert_equal '4: method M#a=', inspect(tags[7])
249
+ assert_equal '4: method M#b=', inspect(tags[8])
250
+ end
251
+
252
+ def test_extract_rails_associations
253
+ tags = extract(<<-EOC)
254
+ class C
255
+ belongs_to :org, :touch => true
256
+ has_one :author, :dependent => :destroy
257
+ has_many :posts
258
+ has_and_belongs_to_many :tags, :join_table => 'c_tags'
259
+ end
260
+ EOC
261
+ assert_equal 'Rails', tags[1][:language]
262
+
263
+ assert_equal '2: belongs_to C.org', inspect(tags[1])
264
+ assert_equal '2: belongs_to C.org=', inspect(tags[2])
265
+ assert_equal '2: belongs_to C.build_org', inspect(tags[3])
266
+ assert_equal '2: belongs_to C.create_org', inspect(tags[4])
267
+ assert_equal '2: belongs_to C.create_org!', inspect(tags[5])
268
+
269
+ assert_equal '3: has_one C.author', inspect(tags[6])
270
+ assert_equal '3: has_one C.author=', inspect(tags[7])
271
+ assert_equal '3: has_one C.build_author', inspect(tags[8])
272
+ assert_equal '3: has_one C.create_author', inspect(tags[9])
273
+ assert_equal '3: has_one C.create_author!', inspect(tags[10])
274
+
275
+ assert_equal '4: has_many C.posts', inspect(tags[11])
276
+ assert_equal '4: has_many C.posts=', inspect(tags[12])
277
+ assert_equal '4: has_many C.post_ids', inspect(tags[13])
278
+ assert_equal '4: has_many C.post_ids=', inspect(tags[14])
279
+
280
+ assert_equal '5: has_and_belongs_to_many C.tags', inspect(tags[15])
281
+ assert_equal '5: has_and_belongs_to_many C.tags=', inspect(tags[16])
282
+ assert_equal '5: has_and_belongs_to_many C.tag_ids', inspect(tags[17])
283
+ assert_equal '5: has_and_belongs_to_many C.tag_ids=', inspect(tags[18])
284
+ end
285
+
286
+ def test_extract_rails_scopes
287
+ tags = extract(<<-EOC)
288
+ class C
289
+ named_scope(:red) { {:conditions=>{:color => 'red'}} }
290
+ scope :red, where(:color => 'red')
291
+ end
292
+ EOC
293
+ assert_equal 'Rails', tags[1][:language]
294
+
295
+ assert_equal '2: scope C.red', inspect(tags[1])
296
+ assert_equal '3: scope C.red', inspect(tags[2])
297
+ end
83
298
  end
metadata CHANGED
@@ -1,85 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ripper-tags
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Aman Gupta
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2013-01-03 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2013-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: yajl-ruby
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
32
20
  type: :runtime
33
- version_requirements: *id001
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
34
27
  description: fast, accurate ctags generator for ruby source code using Ripper
35
- email:
28
+ email:
36
29
  - aman@tmm1.net
37
- executables:
30
+ executables:
38
31
  - ripper-tags
39
32
  extensions: []
40
-
41
33
  extra_rdoc_files: []
42
-
43
- files:
34
+ files:
35
+ - .gitignore
44
36
  - Gemfile
37
+ - LICENSE
45
38
  - README.md
46
39
  - Rakefile
47
40
  - bin/ripper-tags
48
- - lib/tag_ripper.rb
41
+ - lib/ripper-tags.rb
42
+ - lib/ripper-tags/data_reader.rb
43
+ - lib/ripper-tags/default_formatter.rb
44
+ - lib/ripper-tags/emacs_formatter.rb
45
+ - lib/ripper-tags/json_formatter.rb
46
+ - lib/ripper-tags/parser.rb
47
+ - lib/ripper-tags/vim_formatter.rb
49
48
  - ripper-tags.gemspec
49
+ - test/fixtures/_git/hooks/hook.rb
50
+ - test/fixtures/encoding.rb
51
+ - test/fixtures/non-script.txt
52
+ - test/fixtures/very/deep/script.rb
53
+ - test/fixtures/very/inter.rb
54
+ - test/test_cli.rb
55
+ - test/test_data_reader.rb
56
+ - test/test_formatters.rb
50
57
  - test/test_ripper_tags.rb
51
58
  homepage: http://github.com/tmm1/ripper-tags
52
- licenses: []
53
-
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
54
62
  post_install_message:
55
63
  rdoc_options: []
56
-
57
- require_paths:
64
+ require_paths:
58
65
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
60
- none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
- version: "0"
68
- required_rubygems_version: !ruby/object:Gem::Requirement
69
- none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
- version: "0"
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
77
76
  requirements: []
78
-
79
77
  rubyforge_project:
80
- rubygems_version: 1.8.24
78
+ rubygems_version: 2.1.11
81
79
  signing_key:
82
- specification_version: 3
80
+ specification_version: 4
83
81
  summary: ctags generator for ruby code
84
82
  test_files: []
85
-
83
+ has_rdoc: false