File: module_function_handler_spec.rb

package info (click to toggle)
yard 0.9.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,736 kB
  • sloc: ruby: 31,680; javascript: 7,658; makefile: 21
file content (121 lines) | stat: -rw-r--r-- 3,250 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
# frozen_string_literal: true
require File.dirname(__FILE__) + '/spec_helper'

RSpec.describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}VisibilityHandler" do
  after { Registry.clear }

  def assert_module_function(namespace, name)
    klass = Registry.at("#{namespace}.#{name}")
    instance = Registry.at("#{namespace}##{name}")
    expect(klass).not_to be nil
    expect(instance).not_to be nil
    expect(klass).to be_module_function
    expect(instance).not_to be_module_function
    expect(klass.visibility).to eq :public
    expect(instance.visibility).to eq :private
  end

  it "is able to create a module function with parameters" do
    YARD.parse_string <<-eof
      module Foo
        def bar; end
        def baz; end

        module_function :bar, :baz
      end
    eof
    assert_module_function('Foo', 'bar')
    assert_module_function('Foo', 'baz')
  end

  it "is able to set scope for duration of block without params" do
    YARD.parse_string <<-eof
      module Foo
        def qux; end

        module_function

        def bar; end
        def baz; end
      end
    eof
    expect(Registry.at('Foo.qux')).to be nil
    assert_module_function('Foo', 'bar')
    assert_module_function('Foo', 'baz')
  end

  it "can decorate a method definition" do
    YARD.parse_string <<-eof
      module Foo
        def qux; end

        module_function def bar; end

        def baz; end
      end
    eof
    expect(Registry.at('Foo.qux')).to be nil
    assert_module_function('Foo', 'bar')
    expect(Registry.at('Foo.baz')).to be nil
  end

  # @bug gh-563
  it "copies tags to module function properly" do
    YARD.parse_string <<-eof
      module Foo
        # @param [String] foo bar
        # @option foo [String] bar (nil) baz
        # @return [void]
        def bar(foo); end
        module_function :bar
      end
    eof
    assert_module_function('Foo', 'bar')
    o = Registry.at('Foo.bar')
    expect(o.tag(:param).types).to eq ['String']
    expect(o.tag(:param).name).to eq 'foo'
    expect(o.tag(:param).text).to eq 'bar'
    expect(o.tag(:option).name).to eq 'foo'
    expect(o.tag(:option).pair.types).to eq ['String']
    expect(o.tag(:option).pair.defaults).to eq ['nil']
    expect(o.tag(:option).pair.text).to eq 'baz'
    expect(o.tag(:return).types).to eq ['void']
  end

  it "handles all method names in parameters" do
    YARD.parse_string <<-eof
      module Foo
        def -(t); end
        def ==(other); end
        def a?; end
        module_function :-, '==', :a?
      end
    eof
    assert_module_function('Foo', '-')
    assert_module_function('Foo', '==')
    assert_module_function('Foo', 'a?')
  end

  it "only accepts strings and symbols" do
    YARD.parse_string <<-eof
      module Foo
        module_function name
        module_function *argument
        module_function *(method_call)
      end
    eof
    expect(Registry.at('Foo#name')).to be nil
    expect(Registry.at('Foo#argument')).to be nil
    expect(Registry.at('Foo#method_call')).to be nil
  end

  it "handles constants passed in as symbols" do
    YARD.parse_string <<-eof
      module Foo
        def Foo; end
        module_function :Foo
      end
    eof
    assert_module_function('Foo', 'Foo')
  end
end