File: docstring_spec.rb

package info (click to toggle)
yard 0.9.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,720 kB
  • sloc: ruby: 31,354; javascript: 7,608; makefile: 21
file content (373 lines) | stat: -rw-r--r-- 12,596 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# frozen_string_literal: true

RSpec.describe YARD::Docstring do
  before { YARD::Registry.clear }

  describe "#initialize" do
    it "handles docstrings with empty newlines" do
      expect(Docstring.new("\n\n")).to eq ""
    end
  end

  describe "#+" do
    it "adds another Docstring" do
      d = Docstring.new("FOO") + Docstring.new("BAR")
      expect(d).to eq "FOO\nBAR"
    end

    it "copies over tags" do
      d1 = Docstring.new("FOO\n@api private\n")
      d2 = Docstring.new("BAR\n@param foo descr")
      d = (d1 + d2)
      expect(d).to have_tag(:api)
      expect(d).to have_tag(:param)
    end

    it "adds a String" do
      d = Docstring.new("FOO") + "BAR"
      expect(d).to eq "FOOBAR"
    end
  end

  describe "#line" do
    it "returns nil if #line_range is not set" do
      expect(Docstring.new('foo').line).to be nil
    end

    it "returns line_range.first if #line_range is set" do
      doc = Docstring.new('foo')
      doc.line_range = (1..10)
      expect(doc.line).to eq doc.line_range.first
    end
  end

  describe "#summary" do
    it "handles empty docstrings" do
      o1 = Docstring.new
      expect(o1.summary).to eq ""
    end

    it "handles multiple calls" do
      o1 = Docstring.new("Hello. world")
      5.times { expect(o1.summary).to eq "Hello." }
    end

    it "strips newlines in first paragraph before summarizing" do
      doc = Docstring.new("Foo\n<code>==</code> bar.")
      expect(doc.summary).to eq 'Foo <code>==</code> bar.'
    end

    it "returns the first sentence" do
      o = Docstring.new("DOCSTRING. Another sentence")
      expect(o.summary).to eq "DOCSTRING."
    end

    it "returns the first paragraph" do
      o = Docstring.new("DOCSTRING, and other stuff\n\nAnother sentence.")
      expect(o.summary).to eq "DOCSTRING, and other stuff."
    end

    it "returns proper summary when docstring is changed" do
      o = Docstring.new "DOCSTRING, and other stuff\n\nAnother sentence."
      expect(o.summary).to eq "DOCSTRING, and other stuff."
      o = Docstring.new "DOCSTRING."
      expect(o.summary).to eq "DOCSTRING."
    end

    it "does not double the ending period" do
      o = Docstring.new("Returns a list of tags specified by +name+ or all tags if +name+ is not specified.\n\nTest")
      expect(o.summary).to eq "Returns a list of tags specified by +name+ or all tags if +name+ is not specified."

      doc = Docstring.new(<<-eof)

        Returns a list of tags specified by +name+ or all tags if +name+ is not specified.

        @param name the tag name to return data for, or nil for all tags
        @return [Array<Tags::Tag>] the list of tags by the specified tag name
      eof
      expect(doc.summary).to eq "Returns a list of tags specified by +name+ or all tags if +name+ is not specified."
    end

    it "does not attach period if entire summary is include" do
      YARD.parse_string "# docstring\ndef foo; end"
      expect(Docstring.new("{include:#foo}").summary).to eq '{include:#foo}'
      Registry.clear
    end

    it "handles references embedded in summary" do
      expect(Docstring.new("Aliasing {Test.test}. Done.").summary).to eq "Aliasing {Test.test}."
    end

    it "only ends first sentence when outside parentheses" do
      expect(Docstring.new("Hello (the best.) world. Foo bar.").summary).to eq "Hello (the best.) world."
      expect(Docstring.new("A[b.]c.").summary).to eq "A[b.]c."
    end

    it "only sees '.' as period if whitespace (or eof) follows" do
      expect(Docstring.new("hello 1.5 times.").summary).to eq "hello 1.5 times."
      expect(Docstring.new("hello... me").summary).to eq "hello..."
      expect(Docstring.new("hello.").summary).to eq "hello."
    end

    it "returns summary if there is a newline and parentheses count doesn't match" do
      expect(Docstring.new("Happy method call :-)\n\nCall any time.").summary).to eq "Happy method call :-)."
      expect(Docstring.new("Sad method call :-(\n\nCall any time.").summary).to eq "Sad method call :-(."
      expect(Docstring.new("Hello (World. Forget to close.\n\nNew text").summary).to eq "Hello (World. Forget to close."
      expect(Docstring.new("Hello (World. Forget to close\n\nNew text").summary).to eq "Hello (World. Forget to close."
    end
  end

  describe "#ref_tags" do
    it "parses reference tag into ref_tags" do
      doc = Docstring.new("@return (see Foo#bar)")
      expect(doc.ref_tags.size).to eq 1
      expect(doc.ref_tags.first.owner).to eq P("Foo#bar")
      expect(doc.ref_tags.first.tag_name).to eq "return"
      expect(doc.ref_tags.first.name).to be nil
    end

    it "parses named reference tag into ref_tags" do
      doc = Docstring.new("@param blah \n   (see Foo#bar )")
      expect(doc.ref_tags.size).to eq 1
      expect(doc.ref_tags.first.owner).to eq P("Foo#bar")
      expect(doc.ref_tags.first.tag_name).to eq "param"
      expect(doc.ref_tags.first.name).to eq "blah"
    end

    it "fails to parse named reference tag into ref_tags" do
      doc = Docstring.new("@param blah THIS_BREAKS_REFTAG (see Foo#bar)")
      expect(doc.ref_tags.size).to eq 0
    end

    it "returns all valid reference tags along with #tags" do
      o = CodeObjects::MethodObject.new(:root, 'Foo#bar')
      o.docstring.add_tag Tags::Tag.new('return', 'testing')
      doc = Docstring.new("@return (see Foo#bar)")
      tags = doc.tags
      expect(tags.size).to eq 1
      expect(tags.first.text).to eq 'testing'
      expect(tags.first).to be_kind_of(Tags::RefTag)
      expect(tags.first.owner).to eq o
    end

    it "returns all valid named reference tags along with #tags(name)" do
      o = CodeObjects::MethodObject.new(:root, 'Foo#bar')
      o.docstring.add_tag Tags::Tag.new('param', 'testing', nil, '*args')
      o.docstring.add_tag Tags::Tag.new('param', 'NOTtesting', nil, 'notargs')
      doc = Docstring.new("@param *args (see Foo#bar)")
      tags = doc.tags('param')
      expect(tags.size).to eq 1
      expect(tags.first.text).to eq 'testing'
      expect(tags.first).to be_kind_of(Tags::RefTag)
      expect(tags.first.owner).to eq o
    end

    it "ignores invalid reference tags" do
      doc = Docstring.new("@param *args (see INVALID::TAG#tag)")
      tags = doc.tags('param')
      expect(tags.size).to eq 0
    end

    it "resolves references to methods in the same class with #methname" do
      klass = CodeObjects::ClassObject.new(:root, "Foo")
      o = CodeObjects::MethodObject.new(klass, "bar")
      ref = CodeObjects::MethodObject.new(klass, "baz")
      o.docstring.add_tag Tags::Tag.new('param', 'testing', nil, 'arg1')
      ref.docstring = "@param (see #bar)"

      tags = ref.docstring.tags("param")
      expect(tags.size).to eq 1
      expect(tags.first.text).to eq "testing"
      expect(tags.first).to be_kind_of(Tags::RefTag)
      expect(tags.first.owner).to eq o
    end

    it "returns an empty list (and warning) if circular reftags are found" do
      YARD.parse_string <<-eof
        class Foo
          # @param (see #b)
          def a; end
          # @param (see #a)
          def b; end
        end
      eof

      expect(log.io.string).to match(/error.*circular reference tag in `Foo#b'/)
      expect(Registry.at('Foo#a').tags).to be_empty
      expect(Registry.at('Foo#b').tags).to be_empty
    end

    it "returns an empty list (and warning) if self-circular reftags are found" do
      YARD.parse_string <<-eof
        class Foo
          # @param (see #bar)
          def bar; end
        end
      eof

      expect(log.io.string).to match(/error.*circular reference tag in `Foo#bar'/)
      expect(Registry.at('Foo#bar').tags).to be_empty
    end
  end

  describe "#empty?/#blank?" do
    before(:all) do
      Tags::Library.define_tag "Invisible", :invisible_tag
    end

    it "is blank and empty if it has no content and no tags" do
      expect(Docstring.new).to be_blank
      expect(Docstring.new).to be_empty
    end

    it "isn't empty or blank if it has content" do
      d = Docstring.new("foo bar")
      expect(d).not_to be_empty
      expect(d).not_to be_blank
    end

    it "is empty but not blank if it has tags" do
      d = Docstring.new("@param foo")
      expect(d).to be_empty
      expect(d).not_to be_blank
    end

    it "is empty but not blank if it has ref tags" do
      o = CodeObjects::MethodObject.new(:root, 'Foo#bar')
      o.docstring.add_tag Tags::Tag.new('return', 'testing')
      d = Docstring.new("@return (see Foo#bar)")
      expect(d).to be_empty
      expect(d).not_to be_blank
    end

    it "is blank if it has no visible tags" do
      d = Docstring.new("@invisible_tag value")
      expect(d).to be_blank
    end

    it "is not blank if it has invisible tags and only_visible_tags = false" do
      d = Docstring.new("@invisible_tag value")
      d.add_tag Tags::Tag.new('invisible_tag', nil, nil)
      expect(d.blank?(false)).to be false
    end
  end

  describe "#delete_tags" do
    it "deletes tags by a given tag name" do
      doc = Docstring.new("@param name x\n@param name2 y\n@return foo")
      doc.delete_tags(:param)
      expect(doc.tags.size).to eq 1
    end
  end

  describe "#delete_tag_if" do
    it "deletes tags for a given block" do
      doc = Docstring.new("@param name x\n@param name2 y\n@return foo")
      doc.delete_tag_if {|t| t.name == 'name2' }
      expect(doc.tags.size).to eq 2
    end
  end

  describe "#to_raw" do
    it "returns a clean representation of tags" do
      doc = Docstring.new("Hello world\n@return [String, X] foobar\n@param name<Array> the name\nBYE!")
      expect(doc.to_raw).to eq "Hello world\nBYE!\n@param [Array] name\n  the name\n@return [String, X] foobar"
    end

    it "handles tags with newlines and indentation" do
      doc = Docstring.new("@example TITLE\n  the \n  example\n  @foo\n@param [X] name\n  the name")
      expect(doc.to_raw).to eq "@example TITLE\n  the \n  example\n  @foo\n@param [X] name\n  the name"
    end

    it "handles deleted tags" do
      doc = Docstring.new("@example TITLE\n  the \n  example\n  @foo\n@param [X] name\n  the name")
      doc.delete_tags(:param)
      expect(doc.to_raw).to eq "@example TITLE\n  the \n  example\n  @foo"
    end

    it "handles added tags" do
      doc = Docstring.new("@example TITLE\n  the \n  example\n  @foo")
      doc.add_tag(Tags::Tag.new('foo', 'foo'))
      expect(doc.to_raw).to eq "@example TITLE\n  the \n  example\n  @foo\n@foo foo"
    end

    it "is equal to .all if not modified" do
      doc = Docstring.new("123\n@param")
      expect(doc.to_raw).to eq doc.all
    end

    it "is stable sorting tags" do
      expected = Docstring.new("123\n@param x\n@param y\n@version A")
      doc = Docstring.new("123")
      doc.add_tag(Tags::Tag.new('version', 'A'))
      doc.add_tag(Tags::Tag.new('param', 'x'))
      doc.add_tag(Tags::Tag.new('param', 'y'))
      expect(doc.to_raw).to eq expected.all
    end

    # @bug gh-563
    it "handles full @option tags" do
      doc = Docstring.new("@option foo [String] bar (nil) baz")
      expect(doc.to_raw).to eq "@option foo [String] bar (nil) baz"
    end

    # @bug gh-563
    it "handles simple @option tags" do
      doc = Docstring.new("@option foo :key bar")
      expect(doc.to_raw).to eq "@option foo :key bar"
    end
  end

  describe "#dup" do
    it "duplicates docstring text" do
      doc = Docstring.new("foo")
      expect(doc.dup).to eq doc
      expect(doc.dup.all).to eq doc
    end

    it "duplicates tags to new list" do
      doc = Docstring.new("@param x\n@return y")
      doc2 = doc.dup
      doc2.delete_tags(:param)
      expect(doc.tags.size).to eq 2
      expect(doc2.tags.size).to eq 1
    end

    it "preserves summary" do
      doc = Docstring.new("foo. bar")
      expect(doc.dup.summary).to eq doc.summary
    end

    it "preserves hash_flag" do
      doc = Docstring.new
      doc.hash_flag = 'foo'
      expect(doc.dup.hash_flag).to eq doc.hash_flag
    end

    it "preserves line_range" do
      doc = Docstring.new
      doc.line_range = (1..2)
      expect(doc.dup.line_range).to eq doc.line_range
    end
  end

  describe "reference docstrings" do
    it "allows for construction of docstring with ref object" do
      YARD.parse_string <<-eof
        class A
          # Docstring
          # @return [Boolean]
          def a; end
          # (see #a)
          def b; end
        end
      eof

      object = YARD::Registry.at('A#b')
      expect(object.docstring).to eq 'Docstring'
      expect(object.tags.map(&:tag_name)).to eq ['return']

      YARD::Registry.clear
    end
  end
end